OSDN Git Service

bfd/
[pf3gnuchains/pf3gnuchains3x.git] / bfd / pef.c
1 /* PEF support for BFD.
2    Copyright 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
20
21 #include "safe-ctype.h"
22
23 #include "pef.h"
24 #include "pef-traceback.h"
25
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "libbfd.h"
29
30 #include "libiberty.h"
31
32 #ifndef BFD_IO_FUNCS
33 #define BFD_IO_FUNCS 0
34 #endif
35
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
65
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 *));
88
89 static void
90 bfd_pef_print_symbol (abfd, afile, symbol, how)
91      bfd *abfd;
92      PTR afile;
93      asymbol *symbol;
94      bfd_print_symbol_type how;
95 {
96   FILE *file = (FILE *) afile;
97
98   switch (how)
99     {
100     case bfd_print_symbol_name:
101       fprintf (file, "%s", symbol->name);
102       break;
103     default:
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)
107         {
108           char *buf = alloca (symbol->udata.i);
109           size_t offset = symbol->value + 4;
110           size_t len = symbol->udata.i;
111           int ret;
112
113           bfd_get_section_contents (abfd, symbol->section, buf, offset, len);
114           ret = bfd_pef_parse_traceback_table (abfd, symbol->section, buf,
115                                                len, 0, NULL, file);
116           if (ret < 0)
117             fprintf (file, " [ERROR]");
118         }
119     }
120 }
121
122 static void
123 bfd_pef_convert_architecture (architecture, type, subtype)
124      unsigned long architecture;
125      enum bfd_architecture *type;
126      unsigned long *subtype;
127 {
128   const unsigned long ARCH_POWERPC = 0x70777063; /* 'pwpc' */
129   const unsigned long ARCH_M68K = 0x6d36386b; /* 'm68k' */
130
131   *subtype = bfd_arch_unknown;
132   *type = bfd_arch_unknown;
133
134   if (architecture == ARCH_POWERPC)
135     *type = bfd_arch_powerpc;
136   else if (architecture == ARCH_M68K)
137     *type = bfd_arch_m68k;
138 }
139
140 static bfd_boolean
141 bfd_pef_mkobject (abfd)
142      bfd *abfd ATTRIBUTE_UNUSED;
143 {
144   return TRUE;
145 }
146
147 static int
148 bfd_pef_parse_traceback_table (abfd, section, buf, len, pos, sym, file)
149      bfd *abfd;
150      asection *section;
151      unsigned char *buf;
152      size_t len;
153      size_t pos;
154      asymbol *sym;
155      FILE *file;
156 {
157   struct traceback_table table;
158   size_t offset;
159   const char *s;
160   asymbol tmpsymbol;
161
162   if (sym == NULL)
163     sym = &tmpsymbol;
164
165   sym->name = NULL;
166   sym->value = 0;
167   sym->the_bfd = abfd;
168   sym->section = section;
169   sym->flags = 0;
170   sym->udata.i = 0;
171
172   /* memcpy is fine since all fields are unsigned char.  */
173
174   if ((pos + 8) > len)
175     return -1;
176   memcpy (&table, buf + pos, 8);
177
178   /* Calling code relies on returned symbols having a name and
179      correct offset.  */
180
181   if ((table.lang != TB_C) && (table.lang != TB_CPLUSPLUS))
182     return -1;
183
184   if (! (table.flags2 & TB_NAME_PRESENT))
185     return -1;
186
187   if (! table.flags1 & TB_HAS_TBOFF)
188     return -1;
189
190   offset = 8;
191
192   if ((table.flags5 & TB_FLOATPARAMS) || (table.fixedparams))
193     offset += 4;
194
195   if (table.flags1 & TB_HAS_TBOFF)
196     {
197       struct traceback_table_tboff off;
198
199       if ((pos + offset + 4) > len)
200         return -1;
201       off.tb_offset = bfd_getb32 (buf + pos + offset);
202       offset += 4;
203
204       /* Need to subtract 4 because the offset includes the 0x0L
205          preceding the table.  */
206
207       if (file != NULL)
208         fprintf (file, " [offset = 0x%lx]", off.tb_offset);
209
210       if ((file == NULL) && ((off.tb_offset + 4) > (pos + offset)))
211         return -1;
212
213       sym->value = pos - off.tb_offset - 4;
214     }
215
216   if (table.flags2 & TB_INT_HNDL)
217     offset += 4;
218
219   if (table.flags1 & TB_HAS_CTL)
220     {
221       struct traceback_table_anchors anchors;
222
223       if ((pos + offset + 4) > len)
224         return -1;
225       anchors.ctl_info = bfd_getb32 (buf + pos + offset);
226       offset += 4;
227
228       if (anchors.ctl_info > 1024)
229         return -1;
230
231       offset += anchors.ctl_info * 4;
232     }
233
234   if (table.flags2 & TB_NAME_PRESENT)
235     {
236       struct traceback_table_routine name;
237       char *namebuf;
238
239       if ((pos + offset + 2) > len)
240         return -1;
241       name.name_len = bfd_getb16 (buf + pos + offset);
242       offset += 2;
243
244       if (name.name_len > 4096)
245         return -1;
246
247       if ((pos + offset + name.name_len) > len)
248         return -1;
249
250       namebuf = (char *) bfd_alloc (abfd, name.name_len + 1);
251       if (namebuf == NULL)
252         return -1;
253
254       memcpy (namebuf, buf + pos + offset, name.name_len);
255       namebuf[name.name_len] = '\0';
256
257       /* Strip leading period inserted by compiler.  */
258       if (namebuf[0] == '.')
259         memmove (namebuf, namebuf + 1, name.name_len + 1);
260
261       sym->name = namebuf;
262
263       for (s = sym->name; (*s != '\0'); s++)
264         if (! ISPRINT (*s))
265           return -1;
266
267       offset += name.name_len;
268     }
269
270   if (table.flags2 & TB_USES_ALLOCA)
271     offset += 4;
272
273   if (table.flags4 & TB_HAS_VEC_INFO)
274     offset += 4;
275
276   if (file != NULL)
277     fprintf (file, " [length = 0x%lx]", (long) offset);
278
279   return offset;
280 }
281
282 static const char *bfd_pef_section_name (section)
283      bfd_pef_section *section;
284 {
285   switch (section->section_kind)
286     {
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";
297     }
298 }
299
300 static unsigned long bfd_pef_section_flags (section)
301      bfd_pef_section *section;
302 {
303   switch (section->section_kind)
304     {
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:
315     default:
316       return SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
317     }
318 }
319
320 static asection *
321 bfd_pef_make_bfd_section (abfd, section)
322      bfd *abfd;
323      bfd_pef_section *section;
324 {
325   asection *bfdsec;
326   const char *name = bfd_pef_section_name (section);
327
328   bfdsec = bfd_make_section_anyway (abfd, name);
329   if (bfdsec == NULL)
330     return NULL;
331
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;
337
338   bfdsec->flags = bfd_pef_section_flags (section);
339
340   return bfdsec;
341 }
342
343 int bfd_pef_parse_loader_header (abfd, buf, len, header)
344      bfd *abfd ATTRIBUTE_UNUSED;
345      unsigned char *buf;
346      size_t len;
347      bfd_pef_loader_header *header;
348 {
349   BFD_ASSERT (len == 56);
350
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);
365
366   return 0;
367 }
368
369 int bfd_pef_parse_imported_library (abfd, buf, len, header)
370      bfd *abfd ATTRIBUTE_UNUSED;
371      unsigned char *buf;
372      size_t len;
373      bfd_pef_imported_library *header;
374 {
375   BFD_ASSERT (len == 24);
376
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);
385
386   return 0;
387 }
388
389 int bfd_pef_parse_imported_symbol (abfd, buf, len, symbol)
390      bfd *abfd ATTRIBUTE_UNUSED;
391      unsigned char *buf;
392      size_t len;
393      bfd_pef_imported_symbol *symbol;
394 {
395   unsigned long value;
396
397   BFD_ASSERT (len == 4);
398
399   value = bfd_getb32 (buf);
400   symbol->class = value >> 24;
401   symbol->name = value & 0x00ffffff;
402
403   return 0;
404 }
405
406 int bfd_pef_scan_section (abfd, section)
407      bfd *abfd;
408      bfd_pef_section *section;
409 {
410   unsigned char buf[28];
411
412   bfd_seek (abfd, section->header_offset, SEEK_SET);
413   if (bfd_bread ((PTR) buf, 28, abfd) != 28)
414     return -1;
415
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];
426
427   section->bfd_section = bfd_pef_make_bfd_section (abfd, section);
428   if (section->bfd_section == NULL)
429     return -1;
430
431   return 0;
432 }
433
434 void
435 bfd_pef_print_loader_header (abfd, header, file)
436      bfd *abfd ATTRIBUTE_UNUSED;
437      bfd_pef_loader_header *header;
438      FILE *file;
439 {
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);
459 }
460
461 int
462 bfd_pef_print_loader_section (abfd, file)
463      bfd *abfd;
464      FILE *file;
465 {
466   bfd_pef_loader_header header;
467   asection *loadersec = NULL;
468   unsigned char *loaderbuf = NULL;
469   size_t loaderlen = 0;
470   int ret;
471
472   loadersec = bfd_get_section_by_name (abfd, "loader");
473   if (loadersec == NULL)
474     return -1;
475
476   loaderlen = loadersec->size;
477   loaderbuf = (unsigned char *) bfd_malloc (loaderlen);
478   if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
479     {
480       free (loaderbuf);
481       return -1;
482     }
483   if (bfd_bread ((PTR) loaderbuf, loaderlen, abfd) != loaderlen)
484     {
485       free (loaderbuf);
486       return -1;
487     }
488
489   if (loaderlen < 56)
490     {
491       free (loaderbuf);
492       return -1;
493     }
494   ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
495   if (ret < 0)
496     {
497       free (loaderbuf);
498       return -1;
499     }
500
501   bfd_pef_print_loader_header (abfd, &header, file);
502   return 0;
503 }
504
505 int
506 bfd_pef_scan_start_address (abfd)
507      bfd *abfd;
508 {
509   bfd_pef_loader_header header;
510   asection *section;
511
512   asection *loadersec = NULL;
513   unsigned char *loaderbuf = NULL;
514   size_t loaderlen = 0;
515   int ret;
516
517   loadersec = bfd_get_section_by_name (abfd, "loader");
518   if (loadersec == NULL)
519     goto end;
520
521   loaderlen = loadersec->size;
522   loaderbuf = (unsigned char *) bfd_malloc (loaderlen);
523   if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
524     goto error;
525   if (bfd_bread ((PTR) loaderbuf, loaderlen, abfd) != loaderlen)
526     goto error;
527
528   if (loaderlen < 56)
529     goto error;
530   ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
531   if (ret < 0)
532     goto error;
533
534   if (header.main_section < 0)
535     goto end;
536
537   for (section = abfd->sections; section != NULL; section = section->next)
538     if ((section->index + 1) == header.main_section)
539       break;
540
541   if (section == NULL)
542     goto error;
543
544   abfd->start_address = section->vma + header.main_offset;
545
546  end:
547   if (loaderbuf != NULL)
548     free (loaderbuf);
549   return 0;
550
551  error:
552   if (loaderbuf != NULL)
553     free (loaderbuf);
554   return -1;
555 }
556
557 int
558 bfd_pef_scan (abfd, header, mdata)
559      bfd *abfd;
560      bfd_pef_header *header;
561      bfd_pef_data_struct *mdata;
562 {
563   unsigned int i;
564   enum bfd_architecture cputype;
565   unsigned long cpusubtype;
566
567   mdata->header = *header;
568
569   bfd_pef_convert_architecture (header->architecture, &cputype, &cpusubtype);
570   if (cputype == bfd_arch_unknown)
571     {
572       fprintf (stderr, "bfd_pef_scan: unknown architecture 0x%lx\n",
573                header->architecture);
574       return -1;
575     }
576   bfd_set_arch_mach (abfd, cputype, cpusubtype);
577
578   mdata->header = *header;
579
580   abfd->flags = (abfd->xvec->object_flags
581                  | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
582
583   if (header->section_count != 0)
584     {
585       mdata->sections =
586         ((bfd_pef_section *)
587          bfd_alloc (abfd, header->section_count * sizeof (bfd_pef_section)));
588
589       if (mdata->sections == NULL)
590         return -1;
591
592       for (i = 0; i < header->section_count; i++)
593         {
594           bfd_pef_section *cur = &mdata->sections[i];
595           cur->header_offset = 40 + (i * 28);
596           if (bfd_pef_scan_section (abfd, cur) < 0)
597             return -1;
598         }
599     }
600
601   if (bfd_pef_scan_start_address (abfd) < 0)
602     {
603 #if 0
604       fprintf (stderr, "bfd_pef_scan: unable to scan start address: %s\n",
605                bfd_errmsg (bfd_get_error ()));
606       return -1;
607 #endif
608     }
609
610   abfd->tdata.pef_data = mdata;
611
612   return 0;
613 }
614
615 static int
616 bfd_pef_read_header (abfd, header)
617      bfd *abfd;
618      bfd_pef_header *header;
619 {
620   unsigned char buf[40];
621
622   bfd_seek (abfd, 0, SEEK_SET);
623
624   if (bfd_bread ((PTR) buf, 40, abfd) != 40)
625     return -1;
626
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);
638
639   return 0;
640 }
641
642 static const bfd_target *
643 bfd_pef_object_p (abfd)
644      bfd *abfd;
645 {
646   struct bfd_preserve preserve;
647   bfd_pef_header header;
648
649   preserve.marker = NULL;
650   if (bfd_pef_read_header (abfd, &header) != 0)
651     goto wrong;
652
653   if (header.tag1 != BFD_PEF_TAG1 || header.tag2 != BFD_PEF_TAG2)
654     goto wrong;
655
656   preserve.marker = bfd_zalloc (abfd, sizeof (bfd_pef_data_struct));
657   if (preserve.marker == NULL
658       || !bfd_preserve_save (abfd, &preserve))
659     goto fail;
660
661   if (bfd_pef_scan (abfd, &header,
662                     (bfd_pef_data_struct *) preserve.marker) != 0)
663     goto wrong;
664
665   bfd_preserve_finish (abfd, &preserve);
666   return abfd->xvec;
667
668  wrong:
669   bfd_set_error (bfd_error_wrong_format);
670
671  fail:
672   if (preserve.marker != NULL)
673     bfd_preserve_restore (abfd, &preserve);
674   return NULL;
675 }
676
677 static int bfd_pef_parse_traceback_tables (abfd, sec, buf, len, nsym, csym)
678      bfd *abfd;
679      asection *sec;
680      unsigned char *buf;
681      size_t len;
682      long *nsym;
683      asymbol **csym;
684 {
685   char *name;
686
687   asymbol function;
688   asymbol traceback;
689
690   const char *const tbprefix = "__traceback_";
691   size_t tbnamelen;
692
693   size_t pos = 0;
694   unsigned long count = 0;
695   int ret;
696
697   for (;;)
698     {
699       /* We're reading symbols two at a time.  */
700       if (csym && ((csym[count] == NULL) || (csym[count + 1] == NULL)))
701         break;
702
703       pos += 3;
704       pos -= (pos % 4);
705
706       while ((pos + 4) <= len)
707         {
708           if (bfd_getb32 (buf + pos) == 0)
709             break;
710           pos += 4;
711         }
712
713       if ((pos + 4) > len)
714         break;
715
716       ret = bfd_pef_parse_traceback_table (abfd, sec, buf, len, pos + 4,
717                                            &function, 0);
718       if (ret < 0)
719         {
720           /* Skip over 0x0L to advance to next possible traceback table.  */
721           pos += 4;
722           continue;
723         }
724
725       BFD_ASSERT (function.name != NULL);
726
727       /* Don't bother to compute the name if we are just
728          counting symbols.  */
729
730       if (csym)
731         {
732           tbnamelen = strlen (tbprefix) + strlen (function.name);
733           name = bfd_alloc (abfd, tbnamelen + 1);
734           if (name == NULL)
735             {
736               bfd_release (abfd, (PTR) function.name);
737               function.name = NULL;
738               break;
739             }
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;
745           traceback.flags = 0;
746           traceback.udata.i = ret;
747
748           *(csym[count]) = function;
749           *(csym[count + 1]) = traceback;
750         }
751
752       pos += ret;
753       count += 2;
754     }
755
756   *nsym = count;
757   return 0;
758 }
759
760 static int bfd_pef_parse_function_stub (abfd, buf, len, offset)
761      bfd *abfd ATTRIBUTE_UNUSED;
762      unsigned char *buf;
763      size_t len;
764      unsigned long *offset;
765 {
766   BFD_ASSERT (len == 24);
767
768   if ((bfd_getb32 (buf) & 0xffff0000) != 0x81820000)
769     return -1;
770   if (bfd_getb32 (buf + 4) != 0x90410014)
771     return -1;
772   if (bfd_getb32 (buf + 8) != 0x800c0000)
773     return -1;
774   if (bfd_getb32 (buf + 12) != 0x804c0004)
775     return -1;
776   if (bfd_getb32 (buf + 16) != 0x7c0903a6)
777     return -1;
778   if (bfd_getb32 (buf + 20) != 0x4e800420)
779     return -1;
780
781   if (offset != NULL)
782     *offset = (bfd_getb32 (buf) & 0x0000ffff) / 4;
783
784   return 0;
785 }
786
787 static int bfd_pef_parse_function_stubs (abfd, codesec, codebuf, codelen,
788                                          loaderbuf, loaderlen, nsym, csym)
789      bfd *abfd;
790      asection *codesec;
791      unsigned char *codebuf;
792      size_t codelen;
793      unsigned char *loaderbuf;
794      size_t loaderlen;
795      unsigned long *nsym;
796      asymbol **csym;
797 {
798   const char *const sprefix = "__stub_";
799
800   size_t codepos = 0;
801   unsigned long count = 0;
802
803   bfd_pef_loader_header header;
804   bfd_pef_imported_library *libraries = NULL;
805   bfd_pef_imported_symbol *imports = NULL;
806
807   unsigned long i;
808   int ret;
809
810   if (loaderlen < 56)
811     goto error;
812
813   ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
814   if (ret < 0)
815     goto error;
816
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));
821
822   if (loaderlen < (56 + (header.imported_library_count * 24)))
823     goto error;
824   for (i = 0; i < header.imported_library_count; i++)
825     {
826       ret = bfd_pef_parse_imported_library
827         (abfd, loaderbuf + 56 + (i * 24), 24, &libraries[i]);
828       if (ret < 0)
829         goto error;
830     }
831
832   if (loaderlen < (56 + (header.imported_library_count * 24)
833                    + (header.total_imported_symbol_count * 4)))
834     goto error;
835   for (i = 0; i < header.total_imported_symbol_count; i++)
836     {
837       ret = (bfd_pef_parse_imported_symbol
838              (abfd,
839               loaderbuf + 56 + (header.imported_library_count * 24) + (i * 4),
840               4, &imports[i]));
841       if (ret < 0)
842         goto error;
843     }
844
845   codepos = 0;
846
847   for (;;)
848     {
849       asymbol sym;
850       const char *symname;
851       char *name;
852       unsigned long index;
853       int ret;
854
855       if (csym && (csym[count] == NULL))
856         break;
857
858       codepos += 3;
859       codepos -= (codepos % 4);
860
861       while ((codepos + 4) <= codelen)
862         {
863           if ((bfd_getb32 (codebuf + codepos) & 0xffff0000) == 0x81820000)
864             break;
865           codepos += 4;
866         }
867
868       if ((codepos + 4) > codelen)
869         break;
870
871       ret = bfd_pef_parse_function_stub (abfd, codebuf + codepos, 24, &index);
872       if (ret < 0)
873         {
874           codepos += 24;
875           continue;
876         }
877
878       if (index >= header.total_imported_symbol_count)
879         {
880           codepos += 24;
881           continue;
882         }
883
884       {
885         size_t max, namelen;
886         const char *s;
887
888         if (loaderlen < (header.loader_strings_offset + imports[index].name))
889           goto error;
890
891         max = loaderlen - (header.loader_strings_offset + imports[index].name);
892         symname = loaderbuf + header.loader_strings_offset + imports[index].name;
893         namelen = 0;
894         for (s = symname; s < (symname + max); s++)
895           {
896             if (*s == '\0')
897               break;
898             if (! ISPRINT (*s))
899               goto error;
900             namelen++;
901           }
902         if (*s != '\0')
903           goto error;
904
905         name = bfd_alloc (abfd, strlen (sprefix) + namelen + 1);
906         if (name == NULL)
907           break;
908
909         snprintf (name, strlen (sprefix) + namelen + 1, "%s%s",
910                   sprefix, symname);
911         sym.name = name;
912       }
913
914       sym.value = codepos;
915       sym.the_bfd = abfd;
916       sym.section = codesec;
917       sym.flags = 0;
918       sym.udata.i = 0;
919
920       codepos += 24;
921
922       if (csym != NULL)
923         *(csym[count]) = sym;
924
925       count++;
926     }
927
928   goto end;
929
930  end:
931   if (libraries != NULL)
932     free (libraries);
933   if (imports != NULL)
934     free (imports);
935   *nsym = count;
936   return 0;
937
938  error:
939   if (libraries != NULL)
940     free (libraries);
941   if (imports != NULL)
942     free (imports);
943   *nsym = count;
944   return -1;
945 }
946
947 static long bfd_pef_parse_symbols (abfd, csym)
948      bfd *abfd;
949      asymbol **csym;
950 {
951   unsigned long count = 0;
952
953   asection *codesec = NULL;
954   unsigned char *codebuf = NULL;
955   size_t codelen = 0;
956
957   asection *loadersec = NULL;
958   unsigned char *loaderbuf = NULL;
959   size_t loaderlen = 0;
960
961   codesec = bfd_get_section_by_name (abfd, "code");
962   if (codesec != NULL)
963     {
964       codelen = codesec->size;
965       codebuf = (unsigned char *) bfd_malloc (codelen);
966       if (bfd_seek (abfd, codesec->filepos, SEEK_SET) < 0)
967         goto end;
968       if (bfd_bread ((PTR) codebuf, codelen, abfd) != codelen)
969         goto end;
970     }
971
972   loadersec = bfd_get_section_by_name (abfd, "loader");
973   if (loadersec != NULL)
974     {
975       loaderlen = loadersec->size;
976       loaderbuf = (unsigned char *) bfd_malloc (loaderlen);
977       if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
978         goto end;
979       if (bfd_bread ((PTR) loaderbuf, loaderlen, abfd) != loaderlen)
980         goto end;
981     }
982
983   count = 0;
984   if (codesec != NULL)
985     {
986       unsigned long ncount = 0;
987       bfd_pef_parse_traceback_tables (abfd, codesec, codebuf, codelen,
988                                       &ncount, csym);
989       count += ncount;
990     }
991
992   if ((codesec != NULL) && (loadersec != NULL))
993     {
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);
998       count += ncount;
999     }
1000
1001   if (csym != NULL)
1002     csym[count] = NULL;
1003
1004  end:
1005   if (codebuf != NULL)
1006     free (codebuf);
1007
1008   if (loaderbuf != NULL)
1009     free (loaderbuf);
1010
1011   return count;
1012 }
1013
1014 static long
1015 bfd_pef_count_symbols (abfd)
1016      bfd *abfd;
1017 {
1018   return bfd_pef_parse_symbols (abfd, NULL);
1019 }
1020
1021 static long
1022 bfd_pef_get_symtab_upper_bound (abfd)
1023      bfd *abfd;
1024 {
1025   long nsyms = bfd_pef_count_symbols (abfd);
1026   if (nsyms < 0)
1027     return nsyms;
1028   return ((nsyms + 1) * sizeof (asymbol *));
1029 }
1030
1031 static long
1032 bfd_pef_canonicalize_symtab (abfd, alocation)
1033      bfd *abfd;
1034      asymbol **alocation;
1035 {
1036   long i;
1037   asymbol *syms;
1038   long ret;
1039
1040   long nsyms = bfd_pef_count_symbols (abfd);
1041   if (nsyms < 0)
1042     return nsyms;
1043
1044   syms = bfd_alloc (abfd, nsyms * sizeof (asymbol));
1045   if (syms == NULL)
1046     return -1;
1047
1048   for (i = 0; i < nsyms; i++)
1049     alocation[i] = &syms[i];
1050
1051   alocation[nsyms] = NULL;
1052
1053   ret = bfd_pef_parse_symbols (abfd, alocation);
1054   if (ret != nsyms)
1055     return 0;
1056
1057   return ret;
1058 }
1059
1060 static asymbol *
1061 bfd_pef_make_empty_symbol (abfd)
1062      bfd *abfd;
1063 {
1064   return (asymbol *) bfd_alloc (abfd, sizeof (asymbol));
1065 }
1066
1067 static void
1068 bfd_pef_get_symbol_info (abfd, symbol, ret)
1069      bfd *abfd ATTRIBUTE_UNUSED;
1070      asymbol *symbol;
1071      symbol_info *ret;
1072 {
1073   bfd_symbol_info (symbol, ret);
1074 }
1075
1076 static int
1077 bfd_pef_sizeof_headers (abfd, exec)
1078      bfd *abfd ATTRIBUTE_UNUSED;
1079      bfd_boolean exec ATTRIBUTE_UNUSED;
1080 {
1081   return 0;
1082 }
1083
1084 const bfd_target pef_vec =
1085 {
1086   "pef",                        /* name */
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 */
1105     _bfd_dummy_target,
1106     bfd_pef_object_p,           /* bfd_check_format */
1107     _bfd_dummy_target,
1108     _bfd_dummy_target,
1109   },
1110   {                             /* bfd_set_format */
1111     bfd_false,
1112     bfd_pef_mkobject,
1113     bfd_false,
1114     bfd_false,
1115   },
1116   {                             /* bfd_write_contents */
1117     bfd_false,
1118     bfd_true,
1119     bfd_false,
1120     bfd_false,
1121   },
1122
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),
1132
1133   NULL,
1134
1135   NULL
1136 };
1137
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
1145
1146 static int
1147 bfd_pef_xlib_read_header (abfd, header)
1148      bfd *abfd;
1149      bfd_pef_xlib_header *header;
1150 {
1151   unsigned char buf[76];
1152
1153   bfd_seek (abfd, 0, SEEK_SET);
1154
1155   if (bfd_bread ((PTR) buf, 76, abfd) != 76)
1156     return -1;
1157
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);
1178
1179   return 0;
1180 }
1181
1182 int
1183 bfd_pef_xlib_scan (abfd, header)
1184      bfd *abfd;
1185      bfd_pef_xlib_header *header;
1186 {
1187   bfd_pef_xlib_data_struct *mdata = NULL;
1188
1189   mdata = ((bfd_pef_xlib_data_struct *)
1190            bfd_alloc (abfd, sizeof (bfd_pef_xlib_data_struct)));
1191   if (mdata == NULL)
1192     return -1;
1193
1194   mdata->header = *header;
1195
1196   abfd->flags = (abfd->xvec->object_flags
1197                  | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
1198
1199   abfd->tdata.pef_xlib_data = mdata;
1200
1201   return 0;
1202 }
1203
1204 static const bfd_target *
1205 bfd_pef_xlib_object_p (abfd)
1206      bfd *abfd;
1207 {
1208   struct bfd_preserve preserve;
1209   bfd_pef_xlib_header header;
1210
1211   if (bfd_pef_xlib_read_header (abfd, &header) != 0)
1212     {
1213       bfd_set_error (bfd_error_wrong_format);
1214       return NULL;
1215     }
1216
1217   if ((header.tag1 != BFD_PEF_XLIB_TAG1)
1218       || ((header.tag2 != BFD_PEF_VLIB_TAG2)
1219           && (header.tag2 != BFD_PEF_BLIB_TAG2)))
1220     {
1221       bfd_set_error (bfd_error_wrong_format);
1222       return NULL;
1223     }
1224
1225   if (! bfd_preserve_save (abfd, &preserve))
1226     {
1227       bfd_set_error (bfd_error_wrong_format);
1228       return NULL;
1229     }
1230
1231   if (bfd_pef_xlib_scan (abfd, &header) != 0)
1232     {
1233       bfd_preserve_restore (abfd, &preserve);
1234       bfd_set_error (bfd_error_wrong_format);
1235       return NULL;
1236     }
1237
1238   bfd_preserve_finish (abfd, &preserve);
1239   return abfd->xvec;
1240 }
1241
1242 const bfd_target pef_xlib_vec =
1243 {
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 */
1263     _bfd_dummy_target,
1264     bfd_pef_xlib_object_p,      /* bfd_check_format */
1265     _bfd_dummy_target,
1266     _bfd_dummy_target,
1267   },
1268   {                             /* bfd_set_format */
1269     bfd_false,
1270     bfd_pef_mkobject,
1271     bfd_false,
1272     bfd_false,
1273   },
1274   {                             /* bfd_write_contents */
1275     bfd_false,
1276     bfd_true,
1277     bfd_false,
1278     bfd_false,
1279   },
1280
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),
1290
1291   NULL,
1292
1293   NULL
1294 };