* configure.tgt (mips64*el-*-linux-gnu*): Define targ_extra_libpath
[binutils.git] / bfd / pef.c
blob7f266c5cff9f5664e2d4b3a95094ce00742d7fab
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"
23 #include "pef.h"
24 #include "pef-traceback.h"
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "libbfd.h"
30 #include "libiberty.h"
32 #ifndef BFD_IO_FUNCS
33 #define BFD_IO_FUNCS 0
34 #endif
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_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
41 #define bfd_pef_get_lineno _bfd_nosymbols_get_lineno
42 #define bfd_pef_find_nearest_line _bfd_nosymbols_find_nearest_line
43 #define bfd_pef_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
44 #define bfd_pef_read_minisymbols _bfd_generic_read_minisymbols
45 #define bfd_pef_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
46 #define bfd_pef_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
47 #define bfd_pef_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
48 #define bfd_pef_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
49 #define bfd_pef_set_arch_mach _bfd_generic_set_arch_mach
50 #define bfd_pef_get_section_contents _bfd_generic_get_section_contents
51 #define bfd_pef_set_section_contents _bfd_generic_set_section_contents
52 #define bfd_pef_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
53 #define bfd_pef_bfd_relax_section bfd_generic_relax_section
54 #define bfd_pef_bfd_gc_sections bfd_generic_gc_sections
55 #define bfd_pef_bfd_merge_sections bfd_generic_merge_sections
56 #define bfd_pef_bfd_is_group_section bfd_generic_is_group_section
57 #define bfd_pef_bfd_discard_group bfd_generic_discard_group
58 #define bfd_pef_section_already_linked _bfd_generic_section_already_linked
59 #define bfd_pef_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
60 #define bfd_pef_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
61 #define bfd_pef_bfd_link_add_symbols _bfd_generic_link_add_symbols
62 #define bfd_pef_bfd_link_just_syms _bfd_generic_link_just_syms
63 #define bfd_pef_bfd_final_link _bfd_generic_final_link
64 #define bfd_pef_bfd_link_split_section _bfd_generic_link_split_section
65 #define bfd_pef_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
67 static void bfd_pef_print_symbol PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
68 static void bfd_pef_convert_architecture PARAMS ((unsigned long, enum bfd_architecture *, unsigned long *));
69 static bfd_boolean bfd_pef_mkobject PARAMS ((bfd *));
70 static int bfd_pef_parse_traceback_table PARAMS ((bfd *, asection *, unsigned char *, size_t, size_t, asymbol *, FILE *));
71 static const char *bfd_pef_section_name PARAMS ((bfd_pef_section *));
72 static unsigned long bfd_pef_section_flags PARAMS ((bfd_pef_section *));
73 static asection *bfd_pef_make_bfd_section PARAMS ((bfd *, bfd_pef_section *));
74 static int bfd_pef_read_header PARAMS ((bfd *, bfd_pef_header *));
75 static const bfd_target *bfd_pef_object_p PARAMS ((bfd *));
76 static int bfd_pef_parse_traceback_tables PARAMS ((bfd *, asection *, unsigned char *, size_t, long *, asymbol **));
77 static int bfd_pef_parse_function_stub PARAMS ((bfd *, unsigned char *, size_t, unsigned long *));
78 static int bfd_pef_parse_function_stubs PARAMS ((bfd *, asection *, unsigned char *, size_t, unsigned char *, size_t, unsigned long *, asymbol **));
79 static long bfd_pef_parse_symbols PARAMS ((bfd *, asymbol **));
80 static long bfd_pef_count_symbols PARAMS ((bfd *));
81 static long bfd_pef_get_symtab_upper_bound PARAMS ((bfd *));
82 static long bfd_pef_canonicalize_symtab PARAMS ((bfd *, asymbol **));
83 static asymbol *bfd_pef_make_empty_symbol PARAMS ((bfd *));
84 static void bfd_pef_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
85 static int bfd_pef_sizeof_headers PARAMS ((bfd *, bfd_boolean));
86 static int bfd_pef_xlib_read_header PARAMS ((bfd *, bfd_pef_xlib_header *));
87 static int bfd_pef_xlib_scan PARAMS ((bfd *, bfd_pef_xlib_header *));
88 static const bfd_target *bfd_pef_xlib_object_p PARAMS ((bfd *));
90 static void
91 bfd_pef_print_symbol (abfd, afile, symbol, how)
92 bfd *abfd;
93 PTR afile;
94 asymbol *symbol;
95 bfd_print_symbol_type how;
97 FILE *file = (FILE *) afile;
99 switch (how)
101 case bfd_print_symbol_name:
102 fprintf (file, "%s", symbol->name);
103 break;
104 default:
105 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
106 fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
107 if (strncmp (symbol->name, "__traceback_", strlen ("__traceback_")) == 0)
109 char *buf = alloca (symbol->udata.i);
110 size_t offset = symbol->value + 4;
111 size_t len = symbol->udata.i;
112 int ret;
114 bfd_get_section_contents (abfd, symbol->section, buf, offset, len);
115 ret = bfd_pef_parse_traceback_table (abfd, symbol->section, buf,
116 len, 0, NULL, file);
117 if (ret < 0)
118 fprintf (file, " [ERROR]");
123 static void
124 bfd_pef_convert_architecture (architecture, type, subtype)
125 unsigned long architecture;
126 enum bfd_architecture *type;
127 unsigned long *subtype;
129 const unsigned long ARCH_POWERPC = 0x70777063; /* 'pwpc' */
130 const unsigned long ARCH_M68K = 0x6d36386b; /* 'm68k' */
132 *subtype = bfd_arch_unknown;
133 *type = bfd_arch_unknown;
135 if (architecture == ARCH_POWERPC)
136 *type = bfd_arch_powerpc;
137 else if (architecture == ARCH_M68K)
138 *type = bfd_arch_m68k;
141 static bfd_boolean
142 bfd_pef_mkobject (abfd)
143 bfd *abfd ATTRIBUTE_UNUSED;
145 return TRUE;
148 static int
149 bfd_pef_parse_traceback_table (abfd, section, buf, len, pos, sym, file)
150 bfd *abfd;
151 asection *section;
152 unsigned char *buf;
153 size_t len;
154 size_t pos;
155 asymbol *sym;
156 FILE *file;
158 struct traceback_table table;
159 size_t offset;
160 const char *s;
161 asymbol tmpsymbol;
163 if (sym == NULL)
164 sym = &tmpsymbol;
166 sym->name = NULL;
167 sym->value = 0;
168 sym->the_bfd = abfd;
169 sym->section = section;
170 sym->flags = 0;
171 sym->udata.i = 0;
173 /* memcpy is fine since all fields are unsigned char. */
175 if ((pos + 8) > len)
176 return -1;
177 memcpy (&table, buf + pos, 8);
179 /* Calling code relies on returned symbols having a name and
180 correct offset. */
182 if ((table.lang != TB_C) && (table.lang != TB_CPLUSPLUS))
183 return -1;
185 if (! (table.flags2 & TB_NAME_PRESENT))
186 return -1;
188 if (! table.flags1 & TB_HAS_TBOFF)
189 return -1;
191 offset = 8;
193 if ((table.flags5 & TB_FLOATPARAMS) || (table.fixedparams))
194 offset += 4;
196 if (table.flags1 & TB_HAS_TBOFF)
198 struct traceback_table_tboff off;
200 if ((pos + offset + 4) > len)
201 return -1;
202 off.tb_offset = bfd_getb32 (buf + pos + offset);
203 offset += 4;
205 /* Need to subtract 4 because the offset includes the 0x0L
206 preceding the table. */
208 if (file != NULL)
209 fprintf (file, " [offset = 0x%lx]", off.tb_offset);
211 if ((file == NULL) && ((off.tb_offset + 4) > (pos + offset)))
212 return -1;
214 sym->value = pos - off.tb_offset - 4;
217 if (table.flags2 & TB_INT_HNDL)
218 offset += 4;
220 if (table.flags1 & TB_HAS_CTL)
222 struct traceback_table_anchors anchors;
224 if ((pos + offset + 4) > len)
225 return -1;
226 anchors.ctl_info = bfd_getb32 (buf + pos + offset);
227 offset += 4;
229 if (anchors.ctl_info > 1024)
230 return -1;
232 offset += anchors.ctl_info * 4;
235 if (table.flags2 & TB_NAME_PRESENT)
237 struct traceback_table_routine name;
238 char *namebuf;
240 if ((pos + offset + 2) > len)
241 return -1;
242 name.name_len = bfd_getb16 (buf + pos + offset);
243 offset += 2;
245 if (name.name_len > 4096)
246 return -1;
248 if ((pos + offset + name.name_len) > len)
249 return -1;
251 namebuf = (char *) bfd_alloc (abfd, name.name_len + 1);
252 if (namebuf == NULL)
253 return -1;
255 memcpy (namebuf, buf + pos + offset, name.name_len);
256 namebuf[name.name_len] = '\0';
258 /* Strip leading period inserted by compiler. */
259 if (namebuf[0] == '.')
260 memmove (namebuf, namebuf + 1, name.name_len + 1);
262 sym->name = namebuf;
264 for (s = sym->name; (*s != '\0'); s++)
265 if (! ISPRINT (*s))
266 return -1;
268 offset += name.name_len;
271 if (table.flags2 & TB_USES_ALLOCA)
272 offset += 4;
274 if (table.flags4 & TB_HAS_VEC_INFO)
275 offset += 4;
277 if (file != NULL)
278 fprintf (file, " [length = 0x%lx]", (long) offset);
280 return offset;
283 static const char *bfd_pef_section_name (section)
284 bfd_pef_section *section;
286 switch (section->section_kind)
288 case BFD_PEF_SECTION_CODE: return "code";
289 case BFD_PEF_SECTION_UNPACKED_DATA: return "unpacked-data";
290 case BFD_PEF_SECTION_PACKED_DATA: return "packed-data";
291 case BFD_PEF_SECTION_CONSTANT: return "constant";
292 case BFD_PEF_SECTION_LOADER: return "loader";
293 case BFD_PEF_SECTION_DEBUG: return "debug";
294 case BFD_PEF_SECTION_EXEC_DATA: return "exec-data";
295 case BFD_PEF_SECTION_EXCEPTION: return "exception";
296 case BFD_PEF_SECTION_TRACEBACK: return "traceback";
297 default: return "unknown";
301 static unsigned long bfd_pef_section_flags (section)
302 bfd_pef_section *section;
304 switch (section->section_kind)
306 case BFD_PEF_SECTION_CODE:
307 return SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
308 case BFD_PEF_SECTION_UNPACKED_DATA:
309 case BFD_PEF_SECTION_PACKED_DATA:
310 case BFD_PEF_SECTION_CONSTANT:
311 case BFD_PEF_SECTION_LOADER:
312 case BFD_PEF_SECTION_DEBUG:
313 case BFD_PEF_SECTION_EXEC_DATA:
314 case BFD_PEF_SECTION_EXCEPTION:
315 case BFD_PEF_SECTION_TRACEBACK:
316 default:
317 return SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
321 static asection *
322 bfd_pef_make_bfd_section (abfd, section)
323 bfd *abfd;
324 bfd_pef_section *section;
326 asection *bfdsec;
327 const char *name = bfd_pef_section_name (section);
329 bfdsec = bfd_make_section_anyway (abfd, name);
330 if (bfdsec == NULL)
331 return NULL;
333 bfdsec->vma = section->default_address + section->container_offset;
334 bfdsec->lma = section->default_address + section->container_offset;
335 bfdsec->size = section->container_length;
336 bfdsec->filepos = section->container_offset;
337 bfdsec->alignment_power = section->alignment;
339 bfdsec->flags = bfd_pef_section_flags (section);
341 return bfdsec;
344 int bfd_pef_parse_loader_header (abfd, buf, len, header)
345 bfd *abfd ATTRIBUTE_UNUSED;
346 unsigned char *buf;
347 size_t len;
348 bfd_pef_loader_header *header;
350 BFD_ASSERT (len == 56);
352 header->main_section = bfd_getb32 (buf);
353 header->main_offset = bfd_getb32 (buf + 4);
354 header->init_section = bfd_getb32 (buf + 8);
355 header->init_offset = bfd_getb32 (buf + 12);
356 header->term_section = bfd_getb32 (buf + 16);
357 header->term_offset = bfd_getb32 (buf + 20);
358 header->imported_library_count = bfd_getb32 (buf + 24);
359 header->total_imported_symbol_count = bfd_getb32 (buf + 28);
360 header->reloc_section_count = bfd_getb32 (buf + 32);
361 header->reloc_instr_offset = bfd_getb32 (buf + 36);
362 header->loader_strings_offset = bfd_getb32 (buf + 40);
363 header->export_hash_offset = bfd_getb32 (buf + 44);
364 header->export_hash_table_power = bfd_getb32 (buf + 48);
365 header->exported_symbol_count = bfd_getb32 (buf + 52);
367 return 0;
370 int bfd_pef_parse_imported_library (abfd, buf, len, header)
371 bfd *abfd ATTRIBUTE_UNUSED;
372 unsigned char *buf;
373 size_t len;
374 bfd_pef_imported_library *header;
376 BFD_ASSERT (len == 24);
378 header->name_offset = bfd_getb32 (buf);
379 header->old_implementation_version = bfd_getb32 (buf + 4);
380 header->current_version = bfd_getb32 (buf + 8);
381 header->imported_symbol_count = bfd_getb32 (buf + 12);
382 header->first_imported_symbol = bfd_getb32 (buf + 16);
383 header->options = buf[20];
384 header->reserved_a = buf[21];
385 header->reserved_b = bfd_getb16 (buf + 22);
387 return 0;
390 int bfd_pef_parse_imported_symbol (abfd, buf, len, symbol)
391 bfd *abfd ATTRIBUTE_UNUSED;
392 unsigned char *buf;
393 size_t len;
394 bfd_pef_imported_symbol *symbol;
396 unsigned long value;
398 BFD_ASSERT (len == 4);
400 value = bfd_getb32 (buf);
401 symbol->class = value >> 24;
402 symbol->name = value & 0x00ffffff;
404 return 0;
407 int bfd_pef_scan_section (abfd, section)
408 bfd *abfd;
409 bfd_pef_section *section;
411 unsigned char buf[28];
413 bfd_seek (abfd, section->header_offset, SEEK_SET);
414 if (bfd_bread ((PTR) buf, 28, abfd) != 28)
415 return -1;
417 section->name_offset = bfd_h_get_32 (abfd, buf);
418 section->default_address = bfd_h_get_32 (abfd, buf + 4);
419 section->total_length = bfd_h_get_32 (abfd, buf + 8);
420 section->unpacked_length = bfd_h_get_32 (abfd, buf + 12);
421 section->container_length = bfd_h_get_32 (abfd, buf + 16);
422 section->container_offset = bfd_h_get_32 (abfd, buf + 20);
423 section->section_kind = buf[24];
424 section->share_kind = buf[25];
425 section->alignment = buf[26];
426 section->reserved = buf[27];
428 section->bfd_section = bfd_pef_make_bfd_section (abfd, section);
429 if (section->bfd_section == NULL)
430 return -1;
432 return 0;
435 void
436 bfd_pef_print_loader_header (abfd, header, file)
437 bfd *abfd ATTRIBUTE_UNUSED;
438 bfd_pef_loader_header *header;
439 FILE *file;
441 fprintf (file, "main_section: %ld\n", header->main_section);
442 fprintf (file, "main_offset: %lu\n", header->main_offset);
443 fprintf (file, "init_section: %ld\n", header->init_section);
444 fprintf (file, "init_offset: %lu\n", header->init_offset);
445 fprintf (file, "term_section: %ld\n", header->term_section);
446 fprintf (file, "term_offset: %lu\n", header->term_offset);
447 fprintf (file, "imported_library_count: %lu\n",
448 header->imported_library_count);
449 fprintf (file, "total_imported_symbol_count: %lu\n",
450 header->total_imported_symbol_count);
451 fprintf (file, "reloc_section_count: %lu\n", header->reloc_section_count);
452 fprintf (file, "reloc_instr_offset: %lu\n", header->reloc_instr_offset);
453 fprintf (file, "loader_strings_offset: %lu\n",
454 header->loader_strings_offset);
455 fprintf (file, "export_hash_offset: %lu\n", header->export_hash_offset);
456 fprintf (file, "export_hash_table_power: %lu\n",
457 header->export_hash_table_power);
458 fprintf (file, "exported_symbol_count: %lu\n",
459 header->exported_symbol_count);
463 bfd_pef_print_loader_section (abfd, file)
464 bfd *abfd;
465 FILE *file;
467 bfd_pef_loader_header header;
468 asection *loadersec = NULL;
469 unsigned char *loaderbuf = NULL;
470 size_t loaderlen = 0;
471 int ret;
473 loadersec = bfd_get_section_by_name (abfd, "loader");
474 if (loadersec == NULL)
475 return -1;
477 loaderlen = loadersec->size;
478 loaderbuf = (unsigned char *) bfd_malloc (loaderlen);
479 if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
481 free (loaderbuf);
482 return -1;
484 if (bfd_bread ((PTR) loaderbuf, loaderlen, abfd) != loaderlen)
486 free (loaderbuf);
487 return -1;
490 if (loaderlen < 56)
492 free (loaderbuf);
493 return -1;
495 ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
496 if (ret < 0)
498 free (loaderbuf);
499 return -1;
502 bfd_pef_print_loader_header (abfd, &header, file);
503 return 0;
507 bfd_pef_scan_start_address (abfd)
508 bfd *abfd;
510 bfd_pef_loader_header header;
511 asection *section;
513 asection *loadersec = NULL;
514 unsigned char *loaderbuf = NULL;
515 size_t loaderlen = 0;
516 int ret;
518 loadersec = bfd_get_section_by_name (abfd, "loader");
519 if (loadersec == NULL)
520 goto end;
522 loaderlen = loadersec->size;
523 loaderbuf = (unsigned char *) bfd_malloc (loaderlen);
524 if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
525 goto error;
526 if (bfd_bread ((PTR) loaderbuf, loaderlen, abfd) != loaderlen)
527 goto error;
529 if (loaderlen < 56)
530 goto error;
531 ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
532 if (ret < 0)
533 goto error;
535 if (header.main_section < 0)
536 goto end;
538 for (section = abfd->sections; section != NULL; section = section->next)
539 if ((section->index + 1) == header.main_section)
540 break;
542 if (section == NULL)
543 goto error;
545 abfd->start_address = section->vma + header.main_offset;
547 end:
548 if (loaderbuf != NULL)
549 free (loaderbuf);
550 return 0;
552 error:
553 if (loaderbuf != NULL)
554 free (loaderbuf);
555 return -1;
559 bfd_pef_scan (abfd, header, mdata)
560 bfd *abfd;
561 bfd_pef_header *header;
562 bfd_pef_data_struct *mdata;
564 unsigned int i;
565 enum bfd_architecture cputype;
566 unsigned long cpusubtype;
568 mdata->header = *header;
570 bfd_pef_convert_architecture (header->architecture, &cputype, &cpusubtype);
571 if (cputype == bfd_arch_unknown)
573 fprintf (stderr, "bfd_pef_scan: unknown architecture 0x%lx\n",
574 header->architecture);
575 return -1;
577 bfd_set_arch_mach (abfd, cputype, cpusubtype);
579 mdata->header = *header;
581 abfd->flags = (abfd->xvec->object_flags
582 | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
584 if (header->section_count != 0)
586 mdata->sections =
587 ((bfd_pef_section *)
588 bfd_alloc (abfd, header->section_count * sizeof (bfd_pef_section)));
590 if (mdata->sections == NULL)
591 return -1;
593 for (i = 0; i < header->section_count; i++)
595 bfd_pef_section *cur = &mdata->sections[i];
596 cur->header_offset = 40 + (i * 28);
597 if (bfd_pef_scan_section (abfd, cur) < 0)
598 return -1;
602 if (bfd_pef_scan_start_address (abfd) < 0)
605 abfd->tdata.pef_data = mdata;
607 return 0;
610 static int
611 bfd_pef_read_header (abfd, header)
612 bfd *abfd;
613 bfd_pef_header *header;
615 unsigned char buf[40];
617 bfd_seek (abfd, 0, SEEK_SET);
619 if (bfd_bread ((PTR) buf, 40, abfd) != 40)
620 return -1;
622 header->tag1 = bfd_getb32 (buf);
623 header->tag2 = bfd_getb32 (buf + 4);
624 header->architecture = bfd_getb32 (buf + 8);
625 header->format_version = bfd_getb32 (buf + 12);
626 header->timestamp = bfd_getb32 (buf + 16);
627 header->old_definition_version = bfd_getb32 (buf + 20);
628 header->old_implementation_version = bfd_getb32 (buf + 24);
629 header->current_version = bfd_getb32 (buf + 28);
630 header->section_count = bfd_getb32 (buf + 32) + 1;
631 header->instantiated_section_count = bfd_getb32 (buf + 34);
632 header->reserved = bfd_getb32 (buf + 36);
634 return 0;
637 static const bfd_target *
638 bfd_pef_object_p (abfd)
639 bfd *abfd;
641 struct bfd_preserve preserve;
642 bfd_pef_header header;
644 preserve.marker = NULL;
645 if (bfd_pef_read_header (abfd, &header) != 0)
646 goto wrong;
648 if (header.tag1 != BFD_PEF_TAG1 || header.tag2 != BFD_PEF_TAG2)
649 goto wrong;
651 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_pef_data_struct));
652 if (preserve.marker == NULL
653 || !bfd_preserve_save (abfd, &preserve))
654 goto fail;
656 if (bfd_pef_scan (abfd, &header,
657 (bfd_pef_data_struct *) preserve.marker) != 0)
658 goto wrong;
660 bfd_preserve_finish (abfd, &preserve);
661 return abfd->xvec;
663 wrong:
664 bfd_set_error (bfd_error_wrong_format);
666 fail:
667 if (preserve.marker != NULL)
668 bfd_preserve_restore (abfd, &preserve);
669 return NULL;
672 static int bfd_pef_parse_traceback_tables (abfd, sec, buf, len, nsym, csym)
673 bfd *abfd;
674 asection *sec;
675 unsigned char *buf;
676 size_t len;
677 long *nsym;
678 asymbol **csym;
680 char *name;
682 asymbol function;
683 asymbol traceback;
685 const char *const tbprefix = "__traceback_";
686 size_t tbnamelen;
688 size_t pos = 0;
689 unsigned long count = 0;
690 int ret;
692 for (;;)
694 /* We're reading symbols two at a time. */
695 if (csym && ((csym[count] == NULL) || (csym[count + 1] == NULL)))
696 break;
698 pos += 3;
699 pos -= (pos % 4);
701 while ((pos + 4) <= len)
703 if (bfd_getb32 (buf + pos) == 0)
704 break;
705 pos += 4;
708 if ((pos + 4) > len)
709 break;
711 ret = bfd_pef_parse_traceback_table (abfd, sec, buf, len, pos + 4,
712 &function, 0);
713 if (ret < 0)
715 /* Skip over 0x0L to advance to next possible traceback table. */
716 pos += 4;
717 continue;
720 BFD_ASSERT (function.name != NULL);
722 /* Don't bother to compute the name if we are just
723 counting symbols. */
725 if (csym)
727 tbnamelen = strlen (tbprefix) + strlen (function.name);
728 name = bfd_alloc (abfd, tbnamelen + 1);
729 if (name == NULL)
731 bfd_release (abfd, (PTR) function.name);
732 function.name = NULL;
733 break;
735 snprintf (name, tbnamelen + 1, "%s%s", tbprefix, function.name);
736 traceback.name = name;
737 traceback.value = pos;
738 traceback.the_bfd = abfd;
739 traceback.section = sec;
740 traceback.flags = 0;
741 traceback.udata.i = ret;
743 *(csym[count]) = function;
744 *(csym[count + 1]) = traceback;
747 pos += ret;
748 count += 2;
751 *nsym = count;
752 return 0;
755 static int bfd_pef_parse_function_stub (abfd, buf, len, offset)
756 bfd *abfd ATTRIBUTE_UNUSED;
757 unsigned char *buf;
758 size_t len;
759 unsigned long *offset;
761 BFD_ASSERT (len == 24);
763 if ((bfd_getb32 (buf) & 0xffff0000) != 0x81820000)
764 return -1;
765 if (bfd_getb32 (buf + 4) != 0x90410014)
766 return -1;
767 if (bfd_getb32 (buf + 8) != 0x800c0000)
768 return -1;
769 if (bfd_getb32 (buf + 12) != 0x804c0004)
770 return -1;
771 if (bfd_getb32 (buf + 16) != 0x7c0903a6)
772 return -1;
773 if (bfd_getb32 (buf + 20) != 0x4e800420)
774 return -1;
776 if (offset != NULL)
777 *offset = (bfd_getb32 (buf) & 0x0000ffff) / 4;
779 return 0;
782 static int bfd_pef_parse_function_stubs (abfd, codesec, codebuf, codelen,
783 loaderbuf, loaderlen, nsym, csym)
784 bfd *abfd;
785 asection *codesec;
786 unsigned char *codebuf;
787 size_t codelen;
788 unsigned char *loaderbuf;
789 size_t loaderlen;
790 unsigned long *nsym;
791 asymbol **csym;
793 const char *const sprefix = "__stub_";
795 size_t codepos = 0;
796 unsigned long count = 0;
798 bfd_pef_loader_header header;
799 bfd_pef_imported_library *libraries = NULL;
800 bfd_pef_imported_symbol *imports = NULL;
802 unsigned long i;
803 int ret;
805 if (loaderlen < 56)
806 goto error;
808 ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
809 if (ret < 0)
810 goto error;
812 libraries = (bfd_pef_imported_library *) bfd_malloc
813 (header.imported_library_count * sizeof (bfd_pef_imported_library));
814 imports = (bfd_pef_imported_symbol *) bfd_malloc
815 (header.total_imported_symbol_count * sizeof (bfd_pef_imported_symbol));
817 if (loaderlen < (56 + (header.imported_library_count * 24)))
818 goto error;
819 for (i = 0; i < header.imported_library_count; i++)
821 ret = bfd_pef_parse_imported_library
822 (abfd, loaderbuf + 56 + (i * 24), 24, &libraries[i]);
823 if (ret < 0)
824 goto error;
827 if (loaderlen < (56 + (header.imported_library_count * 24)
828 + (header.total_imported_symbol_count * 4)))
829 goto error;
830 for (i = 0; i < header.total_imported_symbol_count; i++)
832 ret = (bfd_pef_parse_imported_symbol
833 (abfd,
834 loaderbuf + 56 + (header.imported_library_count * 24) + (i * 4),
835 4, &imports[i]));
836 if (ret < 0)
837 goto error;
840 codepos = 0;
842 for (;;)
844 asymbol sym;
845 const char *symname;
846 char *name;
847 unsigned long index;
848 int ret;
850 if (csym && (csym[count] == NULL))
851 break;
853 codepos += 3;
854 codepos -= (codepos % 4);
856 while ((codepos + 4) <= codelen)
858 if ((bfd_getb32 (codebuf + codepos) & 0xffff0000) == 0x81820000)
859 break;
860 codepos += 4;
863 if ((codepos + 4) > codelen)
864 break;
866 ret = bfd_pef_parse_function_stub (abfd, codebuf + codepos, 24, &index);
867 if (ret < 0)
869 codepos += 24;
870 continue;
873 if (index >= header.total_imported_symbol_count)
875 codepos += 24;
876 continue;
880 size_t max, namelen;
881 const char *s;
883 if (loaderlen < (header.loader_strings_offset + imports[index].name))
884 goto error;
886 max = loaderlen - (header.loader_strings_offset + imports[index].name);
887 symname = loaderbuf + header.loader_strings_offset + imports[index].name;
888 namelen = 0;
889 for (s = symname; s < (symname + max); s++)
891 if (*s == '\0')
892 break;
893 if (! ISPRINT (*s))
894 goto error;
895 namelen++;
897 if (*s != '\0')
898 goto error;
900 name = bfd_alloc (abfd, strlen (sprefix) + namelen + 1);
901 if (name == NULL)
902 break;
904 snprintf (name, strlen (sprefix) + namelen + 1, "%s%s",
905 sprefix, symname);
906 sym.name = name;
909 sym.value = codepos;
910 sym.the_bfd = abfd;
911 sym.section = codesec;
912 sym.flags = 0;
913 sym.udata.i = 0;
915 codepos += 24;
917 if (csym != NULL)
918 *(csym[count]) = sym;
920 count++;
923 goto end;
925 end:
926 if (libraries != NULL)
927 free (libraries);
928 if (imports != NULL)
929 free (imports);
930 *nsym = count;
931 return 0;
933 error:
934 if (libraries != NULL)
935 free (libraries);
936 if (imports != NULL)
937 free (imports);
938 *nsym = count;
939 return -1;
942 static long bfd_pef_parse_symbols (abfd, csym)
943 bfd *abfd;
944 asymbol **csym;
946 unsigned long count = 0;
948 asection *codesec = NULL;
949 unsigned char *codebuf = NULL;
950 size_t codelen = 0;
952 asection *loadersec = NULL;
953 unsigned char *loaderbuf = NULL;
954 size_t loaderlen = 0;
956 codesec = bfd_get_section_by_name (abfd, "code");
957 if (codesec != NULL)
959 codelen = codesec->size;
960 codebuf = (unsigned char *) bfd_malloc (codelen);
961 if (bfd_seek (abfd, codesec->filepos, SEEK_SET) < 0)
962 goto end;
963 if (bfd_bread ((PTR) codebuf, codelen, abfd) != codelen)
964 goto end;
967 loadersec = bfd_get_section_by_name (abfd, "loader");
968 if (loadersec != NULL)
970 loaderlen = loadersec->size;
971 loaderbuf = (unsigned char *) bfd_malloc (loaderlen);
972 if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
973 goto end;
974 if (bfd_bread ((PTR) loaderbuf, loaderlen, abfd) != loaderlen)
975 goto end;
978 count = 0;
979 if (codesec != NULL)
981 unsigned long ncount = 0;
982 bfd_pef_parse_traceback_tables (abfd, codesec, codebuf, codelen,
983 &ncount, csym);
984 count += ncount;
987 if ((codesec != NULL) && (loadersec != NULL))
989 unsigned long ncount = 0;
990 bfd_pef_parse_function_stubs
991 (abfd, codesec, codebuf, codelen, loaderbuf, loaderlen, &ncount,
992 (csym != NULL) ? (csym + count) : NULL);
993 count += ncount;
996 if (csym != NULL)
997 csym[count] = NULL;
999 end:
1000 if (codebuf != NULL)
1001 free (codebuf);
1003 if (loaderbuf != NULL)
1004 free (loaderbuf);
1006 return count;
1009 static long
1010 bfd_pef_count_symbols (abfd)
1011 bfd *abfd;
1013 return bfd_pef_parse_symbols (abfd, NULL);
1016 static long
1017 bfd_pef_get_symtab_upper_bound (abfd)
1018 bfd *abfd;
1020 long nsyms = bfd_pef_count_symbols (abfd);
1021 if (nsyms < 0)
1022 return nsyms;
1023 return ((nsyms + 1) * sizeof (asymbol *));
1026 static long
1027 bfd_pef_canonicalize_symtab (abfd, alocation)
1028 bfd *abfd;
1029 asymbol **alocation;
1031 long i;
1032 asymbol *syms;
1033 long ret;
1035 long nsyms = bfd_pef_count_symbols (abfd);
1036 if (nsyms < 0)
1037 return nsyms;
1039 syms = bfd_alloc (abfd, nsyms * sizeof (asymbol));
1040 if (syms == NULL)
1041 return -1;
1043 for (i = 0; i < nsyms; i++)
1044 alocation[i] = &syms[i];
1046 alocation[nsyms] = NULL;
1048 ret = bfd_pef_parse_symbols (abfd, alocation);
1049 if (ret != nsyms)
1050 return 0;
1052 return ret;
1055 static asymbol *
1056 bfd_pef_make_empty_symbol (abfd)
1057 bfd *abfd;
1059 return (asymbol *) bfd_alloc (abfd, sizeof (asymbol));
1062 static void
1063 bfd_pef_get_symbol_info (abfd, symbol, ret)
1064 bfd *abfd ATTRIBUTE_UNUSED;
1065 asymbol *symbol;
1066 symbol_info *ret;
1068 bfd_symbol_info (symbol, ret);
1071 static int
1072 bfd_pef_sizeof_headers (abfd, exec)
1073 bfd *abfd ATTRIBUTE_UNUSED;
1074 bfd_boolean exec ATTRIBUTE_UNUSED;
1076 return 0;
1079 const bfd_target pef_vec =
1081 "pef", /* name */
1082 bfd_target_pef_flavour, /* flavour */
1083 BFD_ENDIAN_BIG, /* byteorder */
1084 BFD_ENDIAN_BIG, /* header_byteorder */
1085 (HAS_RELOC | EXEC_P | /* object flags */
1086 HAS_LINENO | HAS_DEBUG |
1087 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
1088 (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
1089 | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
1090 0, /* symbol_leading_char */
1091 ' ', /* ar_pad_char */
1092 16, /* ar_max_namelen */
1093 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1094 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1095 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1096 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1097 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1098 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1099 { /* bfd_check_format */
1100 _bfd_dummy_target,
1101 bfd_pef_object_p, /* bfd_check_format */
1102 _bfd_dummy_target,
1103 _bfd_dummy_target,
1105 { /* bfd_set_format */
1106 bfd_false,
1107 bfd_pef_mkobject,
1108 bfd_false,
1109 bfd_false,
1111 { /* bfd_write_contents */
1112 bfd_false,
1113 bfd_true,
1114 bfd_false,
1115 bfd_false,
1118 BFD_JUMP_TABLE_GENERIC (bfd_pef),
1119 BFD_JUMP_TABLE_COPY (_bfd_generic),
1120 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1121 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
1122 BFD_JUMP_TABLE_SYMBOLS (bfd_pef),
1123 BFD_JUMP_TABLE_RELOCS (bfd_pef),
1124 BFD_JUMP_TABLE_WRITE (bfd_pef),
1125 BFD_JUMP_TABLE_LINK (bfd_pef),
1126 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1128 NULL,
1130 NULL
1133 #define bfd_pef_xlib_close_and_cleanup _bfd_generic_close_and_cleanup
1134 #define bfd_pef_xlib_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
1135 #define bfd_pef_xlib_new_section_hook _bfd_generic_new_section_hook
1136 #define bfd_pef_xlib_get_section_contents _bfd_generic_get_section_contents
1137 #define bfd_pef_xlib_set_section_contents _bfd_generic_set_section_contents
1138 #define bfd_pef_xlib_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
1139 #define bfd_pef_xlib_set_section_contents_in_window _bfd_generic_set_section_contents_in_window
1141 static int
1142 bfd_pef_xlib_read_header (abfd, header)
1143 bfd *abfd;
1144 bfd_pef_xlib_header *header;
1146 unsigned char buf[76];
1148 bfd_seek (abfd, 0, SEEK_SET);
1150 if (bfd_bread ((PTR) buf, 76, abfd) != 76)
1151 return -1;
1153 header->tag1 = bfd_getb32 (buf);
1154 header->tag2 = bfd_getb32 (buf + 4);
1155 header->current_format = bfd_getb32 (buf + 8);
1156 header->container_strings_offset = bfd_getb32 (buf + 12);
1157 header->export_hash_offset = bfd_getb32 (buf + 16);
1158 header->export_key_offset = bfd_getb32 (buf + 20);
1159 header->export_symbol_offset = bfd_getb32 (buf + 24);
1160 header->export_names_offset = bfd_getb32 (buf + 28);
1161 header->export_hash_table_power = bfd_getb32 (buf + 32);
1162 header->exported_symbol_count = bfd_getb32 (buf + 36);
1163 header->frag_name_offset = bfd_getb32 (buf + 40);
1164 header->frag_name_length = bfd_getb32 (buf + 44);
1165 header->dylib_path_offset = bfd_getb32 (buf + 48);
1166 header->dylib_path_length = bfd_getb32 (buf + 52);
1167 header->cpu_family = bfd_getb32 (buf + 56);
1168 header->cpu_model = bfd_getb32 (buf + 60);
1169 header->date_time_stamp = bfd_getb32 (buf + 64);
1170 header->current_version = bfd_getb32 (buf + 68);
1171 header->old_definition_version = bfd_getb32 (buf + 72);
1172 header->old_implementation_version = bfd_getb32 (buf + 76);
1174 return 0;
1178 bfd_pef_xlib_scan (abfd, header)
1179 bfd *abfd;
1180 bfd_pef_xlib_header *header;
1182 bfd_pef_xlib_data_struct *mdata = NULL;
1184 mdata = ((bfd_pef_xlib_data_struct *)
1185 bfd_alloc (abfd, sizeof (bfd_pef_xlib_data_struct)));
1186 if (mdata == NULL)
1187 return -1;
1189 mdata->header = *header;
1191 abfd->flags = (abfd->xvec->object_flags
1192 | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
1194 abfd->tdata.pef_xlib_data = mdata;
1196 return 0;
1199 static const bfd_target *
1200 bfd_pef_xlib_object_p (abfd)
1201 bfd *abfd;
1203 struct bfd_preserve preserve;
1204 bfd_pef_xlib_header header;
1206 if (bfd_pef_xlib_read_header (abfd, &header) != 0)
1208 bfd_set_error (bfd_error_wrong_format);
1209 return NULL;
1212 if ((header.tag1 != BFD_PEF_XLIB_TAG1)
1213 || ((header.tag2 != BFD_PEF_VLIB_TAG2)
1214 && (header.tag2 != BFD_PEF_BLIB_TAG2)))
1216 bfd_set_error (bfd_error_wrong_format);
1217 return NULL;
1220 if (! bfd_preserve_save (abfd, &preserve))
1222 bfd_set_error (bfd_error_wrong_format);
1223 return NULL;
1226 if (bfd_pef_xlib_scan (abfd, &header) != 0)
1228 bfd_preserve_restore (abfd, &preserve);
1229 bfd_set_error (bfd_error_wrong_format);
1230 return NULL;
1233 bfd_preserve_finish (abfd, &preserve);
1234 return abfd->xvec;
1237 const bfd_target pef_xlib_vec =
1239 "pef-xlib", /* name */
1240 bfd_target_pef_xlib_flavour, /* flavour */
1241 BFD_ENDIAN_BIG, /* byteorder */
1242 BFD_ENDIAN_BIG, /* header_byteorder */
1243 (HAS_RELOC | EXEC_P | /* object flags */
1244 HAS_LINENO | HAS_DEBUG |
1245 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
1246 (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
1247 | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
1248 0, /* symbol_leading_char */
1249 ' ', /* ar_pad_char */
1250 16, /* ar_max_namelen */
1251 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1252 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1253 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1254 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1255 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1256 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1257 { /* bfd_check_format */
1258 _bfd_dummy_target,
1259 bfd_pef_xlib_object_p, /* bfd_check_format */
1260 _bfd_dummy_target,
1261 _bfd_dummy_target,
1263 { /* bfd_set_format */
1264 bfd_false,
1265 bfd_pef_mkobject,
1266 bfd_false,
1267 bfd_false,
1269 { /* bfd_write_contents */
1270 bfd_false,
1271 bfd_true,
1272 bfd_false,
1273 bfd_false,
1276 BFD_JUMP_TABLE_GENERIC (bfd_pef_xlib),
1277 BFD_JUMP_TABLE_COPY (_bfd_generic),
1278 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1279 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
1280 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
1281 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
1282 BFD_JUMP_TABLE_WRITE (_bfd_nowrite),
1283 BFD_JUMP_TABLE_LINK (_bfd_nolink),
1284 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1286 NULL,
1288 NULL