bfd/
[external/binutils.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_bfd_link_hash_table_create          _bfd_generic_link_hash_table_create
58 #define bfd_pef_bfd_link_hash_table_free            _bfd_generic_link_hash_table_free
59 #define bfd_pef_bfd_link_add_symbols                _bfd_generic_link_add_symbols
60 #define bfd_pef_bfd_link_just_syms                  _bfd_generic_link_just_syms
61 #define bfd_pef_bfd_final_link                      _bfd_generic_final_link
62 #define bfd_pef_bfd_link_split_section              _bfd_generic_link_split_section
63 #define bfd_pef_get_section_contents_in_window      _bfd_generic_get_section_contents_in_window
64
65 static void bfd_pef_print_symbol               PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
66 static void bfd_pef_convert_architecture       PARAMS ((unsigned long, enum bfd_architecture *, unsigned long *));
67 static bfd_boolean bfd_pef_mkobject            PARAMS ((bfd *));
68 static int bfd_pef_parse_traceback_table       PARAMS ((bfd *, asection *, unsigned char *, size_t, size_t, asymbol *, FILE *));
69 static const char *bfd_pef_section_name        PARAMS ((bfd_pef_section *));
70 static unsigned long bfd_pef_section_flags     PARAMS ((bfd_pef_section *));
71 static asection *bfd_pef_make_bfd_section      PARAMS ((bfd *, bfd_pef_section *));
72 static int bfd_pef_read_header                 PARAMS ((bfd *, bfd_pef_header *));
73 static const bfd_target *bfd_pef_object_p      PARAMS ((bfd *));
74 static int bfd_pef_parse_traceback_tables      PARAMS ((bfd *, asection *, unsigned char *, size_t, long *, asymbol **));
75 static int bfd_pef_parse_function_stub         PARAMS ((bfd *, unsigned char *, size_t, unsigned long *));
76 static int bfd_pef_parse_function_stubs        PARAMS ((bfd *, asection *, unsigned char *, size_t, unsigned char *, size_t, unsigned long *, asymbol **));
77 static long bfd_pef_parse_symbols              PARAMS ((bfd *, asymbol **));
78 static long bfd_pef_count_symbols              PARAMS ((bfd *));
79 static long bfd_pef_get_symtab_upper_bound     PARAMS ((bfd *));
80 static long bfd_pef_canonicalize_symtab        PARAMS ((bfd *, asymbol **));
81 static asymbol *bfd_pef_make_empty_symbol      PARAMS ((bfd *));
82 static void bfd_pef_get_symbol_info            PARAMS ((bfd *, asymbol *, symbol_info *));
83 static int bfd_pef_sizeof_headers              PARAMS ((bfd *, bfd_boolean));
84 static int bfd_pef_xlib_read_header            PARAMS ((bfd *, bfd_pef_xlib_header *));
85 static int bfd_pef_xlib_scan                   PARAMS ((bfd *, bfd_pef_xlib_header *));
86 static const bfd_target *bfd_pef_xlib_object_p PARAMS ((bfd *));
87
88 static void
89 bfd_pef_print_symbol (abfd, afile, symbol, how)
90      bfd *abfd;
91      PTR afile;
92      asymbol *symbol;
93      bfd_print_symbol_type how;
94 {
95   FILE *file = (FILE *) afile;
96
97   switch (how)
98     {
99     case bfd_print_symbol_name:
100       fprintf (file, "%s", symbol->name);
101       break;
102     default:
103       bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
104       fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
105       if (strncmp (symbol->name, "__traceback_", strlen ("__traceback_")) == 0)
106         {
107           char *buf = alloca (symbol->udata.i);
108           size_t offset = symbol->value + 4;
109           size_t len = symbol->udata.i;
110           int ret;
111
112           bfd_get_section_contents (abfd, symbol->section, buf, offset, len);
113           ret = bfd_pef_parse_traceback_table (abfd, symbol->section, buf,
114                                                len, 0, NULL, file);
115           if (ret < 0)
116             fprintf (file, " [ERROR]");
117         }
118     }
119 }
120
121 static void
122 bfd_pef_convert_architecture (architecture, type, subtype)
123      unsigned long architecture;
124      enum bfd_architecture *type;
125      unsigned long *subtype;
126 {
127   const unsigned long ARCH_POWERPC = 0x70777063; /* 'pwpc' */
128   const unsigned long ARCH_M68K = 0x6d36386b; /* 'm68k' */
129
130   *subtype = bfd_arch_unknown;
131   *type = bfd_arch_unknown;
132
133   if (architecture == ARCH_POWERPC)
134     *type = bfd_arch_powerpc;
135   else if (architecture == ARCH_M68K)
136     *type = bfd_arch_m68k;
137 }
138
139 static bfd_boolean
140 bfd_pef_mkobject (abfd)
141      bfd *abfd ATTRIBUTE_UNUSED;
142 {
143   return TRUE;
144 }
145
146 static int
147 bfd_pef_parse_traceback_table (abfd, section, buf, len, pos, sym, file)
148      bfd *abfd;
149      asection *section;
150      unsigned char *buf;
151      size_t len;
152      size_t pos;
153      asymbol *sym;
154      FILE *file;
155 {
156   struct traceback_table table;
157   size_t offset;
158   const char *s;
159   asymbol tmpsymbol;
160
161   if (sym == NULL)
162     sym = &tmpsymbol;
163
164   sym->name = NULL;
165   sym->value = 0;
166   sym->the_bfd = abfd;
167   sym->section = section;
168   sym->flags = 0;
169   sym->udata.i = 0;
170
171   /* memcpy is fine since all fields are unsigned char.  */
172
173   if ((pos + 8) > len)
174     return -1;
175   memcpy (&table, buf + pos, 8);
176
177   /* Calling code relies on returned symbols having a name and
178      correct offset.  */
179
180   if ((table.lang != TB_C) && (table.lang != TB_CPLUSPLUS))
181     return -1;
182
183   if (! (table.flags2 & TB_NAME_PRESENT))
184     return -1;
185
186   if (! table.flags1 & TB_HAS_TBOFF)
187     return -1;
188
189   offset = 8;
190
191   if ((table.flags5 & TB_FLOATPARAMS) || (table.fixedparams))
192     offset += 4;
193
194   if (table.flags1 & TB_HAS_TBOFF)
195     {
196       struct traceback_table_tboff off;
197
198       if ((pos + offset + 4) > len)
199         return -1;
200       off.tb_offset = bfd_getb32 (buf + pos + offset);
201       offset += 4;
202
203       /* Need to subtract 4 because the offset includes the 0x0L
204          preceding the table.  */
205
206       if (file != NULL)
207         fprintf (file, " [offset = 0x%lx]", off.tb_offset);
208
209       if ((file == NULL) && ((off.tb_offset + 4) > (pos + offset)))
210         return -1;
211
212       sym->value = pos - off.tb_offset - 4;
213     }
214
215   if (table.flags2 & TB_INT_HNDL)
216     offset += 4;
217
218   if (table.flags1 & TB_HAS_CTL)
219     {
220       struct traceback_table_anchors anchors;
221
222       if ((pos + offset + 4) > len)
223         return -1;
224       anchors.ctl_info = bfd_getb32 (buf + pos + offset);
225       offset += 4;
226
227       if (anchors.ctl_info > 1024)
228         return -1;
229
230       offset += anchors.ctl_info * 4;
231     }
232
233   if (table.flags2 & TB_NAME_PRESENT)
234     {
235       struct traceback_table_routine name;
236       char *namebuf;
237
238       if ((pos + offset + 2) > len)
239         return -1;
240       name.name_len = bfd_getb16 (buf + pos + offset);
241       offset += 2;
242
243       if (name.name_len > 4096)
244         return -1;
245
246       if ((pos + offset + name.name_len) > len)
247         return -1;
248
249       namebuf = (char *) bfd_alloc (abfd, name.name_len + 1);
250       if (namebuf == NULL)
251         return -1;
252
253       memcpy (namebuf, buf + pos + offset, name.name_len);
254       namebuf[name.name_len] = '\0';
255
256       /* Strip leading period inserted by compiler.  */
257       if (namebuf[0] == '.')
258         memmove (namebuf, namebuf + 1, name.name_len + 1);
259
260       sym->name = namebuf;
261
262       for (s = sym->name; (*s != '\0'); s++)
263         if (! ISPRINT (*s))
264           return -1;
265
266       offset += name.name_len;
267     }
268
269   if (table.flags2 & TB_USES_ALLOCA)
270     offset += 4;
271
272   if (table.flags4 & TB_HAS_VEC_INFO)
273     offset += 4;
274
275   if (file != NULL)
276     fprintf (file, " [length = 0x%lx]", (long) offset);
277
278   return offset;
279 }
280
281 static const char *bfd_pef_section_name (section)
282      bfd_pef_section *section;
283 {
284   switch (section->section_kind)
285     {
286     case BFD_PEF_SECTION_CODE: return "code";
287     case BFD_PEF_SECTION_UNPACKED_DATA: return "unpacked-data";
288     case BFD_PEF_SECTION_PACKED_DATA: return "packed-data";
289     case BFD_PEF_SECTION_CONSTANT: return "constant";
290     case BFD_PEF_SECTION_LOADER: return "loader";
291     case BFD_PEF_SECTION_DEBUG: return "debug";
292     case BFD_PEF_SECTION_EXEC_DATA: return "exec-data";
293     case BFD_PEF_SECTION_EXCEPTION: return "exception";
294     case BFD_PEF_SECTION_TRACEBACK: return "traceback";
295     default: return "unknown";
296     }
297 }
298
299 static unsigned long bfd_pef_section_flags (section)
300      bfd_pef_section *section;
301 {
302   switch (section->section_kind)
303     {
304     case BFD_PEF_SECTION_CODE:
305       return SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
306     case BFD_PEF_SECTION_UNPACKED_DATA:
307     case BFD_PEF_SECTION_PACKED_DATA:
308     case BFD_PEF_SECTION_CONSTANT:
309     case BFD_PEF_SECTION_LOADER:
310     case BFD_PEF_SECTION_DEBUG:
311     case BFD_PEF_SECTION_EXEC_DATA:
312     case BFD_PEF_SECTION_EXCEPTION:
313     case BFD_PEF_SECTION_TRACEBACK:
314     default:
315       return SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
316     }
317 }
318
319 static asection *
320 bfd_pef_make_bfd_section (abfd, section)
321      bfd *abfd;
322      bfd_pef_section *section;
323 {
324   asection *bfdsec;
325   const char *name = bfd_pef_section_name (section);
326
327   bfdsec = bfd_make_section_anyway (abfd, name);
328   if (bfdsec == NULL)
329     return NULL;
330
331   bfdsec->vma = section->default_address + section->container_offset;
332   bfdsec->lma = section->default_address + section->container_offset;
333   bfdsec->size = section->container_length;
334   bfdsec->filepos = section->container_offset;
335   bfdsec->alignment_power = section->alignment;
336
337   bfdsec->flags = bfd_pef_section_flags (section);
338
339   return bfdsec;
340 }
341
342 int bfd_pef_parse_loader_header (abfd, buf, len, header)
343      bfd *abfd ATTRIBUTE_UNUSED;
344      unsigned char *buf;
345      size_t len;
346      bfd_pef_loader_header *header;
347 {
348   BFD_ASSERT (len == 56);
349
350   header->main_section = bfd_getb32 (buf);
351   header->main_offset = bfd_getb32 (buf + 4);
352   header->init_section = bfd_getb32 (buf + 8);
353   header->init_offset = bfd_getb32 (buf + 12);
354   header->term_section = bfd_getb32 (buf + 16);
355   header->term_offset = bfd_getb32 (buf + 20);
356   header->imported_library_count = bfd_getb32 (buf + 24);
357   header->total_imported_symbol_count = bfd_getb32 (buf + 28);
358   header->reloc_section_count = bfd_getb32 (buf + 32);
359   header->reloc_instr_offset = bfd_getb32 (buf + 36);
360   header->loader_strings_offset = bfd_getb32 (buf + 40);
361   header->export_hash_offset = bfd_getb32 (buf + 44);
362   header->export_hash_table_power = bfd_getb32 (buf + 48);
363   header->exported_symbol_count = bfd_getb32 (buf + 52);
364
365   return 0;
366 }
367
368 int bfd_pef_parse_imported_library (abfd, buf, len, header)
369      bfd *abfd ATTRIBUTE_UNUSED;
370      unsigned char *buf;
371      size_t len;
372      bfd_pef_imported_library *header;
373 {
374   BFD_ASSERT (len == 24);
375
376   header->name_offset = bfd_getb32 (buf);
377   header->old_implementation_version = bfd_getb32 (buf + 4);
378   header->current_version = bfd_getb32 (buf + 8);
379   header->imported_symbol_count = bfd_getb32 (buf + 12);
380   header->first_imported_symbol = bfd_getb32 (buf + 16);
381   header->options = buf[20];
382   header->reserved_a = buf[21];
383   header->reserved_b = bfd_getb16 (buf + 22);
384
385   return 0;
386 }
387
388 int bfd_pef_parse_imported_symbol (abfd, buf, len, symbol)
389      bfd *abfd ATTRIBUTE_UNUSED;
390      unsigned char *buf;
391      size_t len;
392      bfd_pef_imported_symbol *symbol;
393 {
394   unsigned long value;
395
396   BFD_ASSERT (len == 4);
397
398   value = bfd_getb32 (buf);
399   symbol->class = value >> 24;
400   symbol->name = value & 0x00ffffff;
401
402   return 0;
403 }
404
405 int bfd_pef_scan_section (abfd, section)
406      bfd *abfd;
407      bfd_pef_section *section;
408 {
409   unsigned char buf[28];
410
411   bfd_seek (abfd, section->header_offset, SEEK_SET);
412   if (bfd_bread ((PTR) buf, 28, abfd) != 28)
413     return -1;
414
415   section->name_offset = bfd_h_get_32 (abfd, buf);
416   section->default_address = bfd_h_get_32 (abfd, buf + 4);
417   section->total_length = bfd_h_get_32 (abfd, buf + 8);
418   section->unpacked_length = bfd_h_get_32 (abfd, buf + 12);
419   section->container_length = bfd_h_get_32 (abfd, buf + 16);
420   section->container_offset = bfd_h_get_32 (abfd, buf + 20);
421   section->section_kind = buf[24];
422   section->share_kind = buf[25];
423   section->alignment = buf[26];
424   section->reserved = buf[27];
425
426   section->bfd_section = bfd_pef_make_bfd_section (abfd, section);
427   if (section->bfd_section == NULL)
428     return -1;
429
430   return 0;
431 }
432
433 void
434 bfd_pef_print_loader_header (abfd, header, file)
435      bfd *abfd ATTRIBUTE_UNUSED;
436      bfd_pef_loader_header *header;
437      FILE *file;
438 {
439   fprintf (file, "main_section: %ld\n", header->main_section);
440   fprintf (file, "main_offset: %lu\n", header->main_offset);
441   fprintf (file, "init_section: %ld\n", header->init_section);
442   fprintf (file, "init_offset: %lu\n", header->init_offset);
443   fprintf (file, "term_section: %ld\n", header->term_section);
444   fprintf (file, "term_offset: %lu\n", header->term_offset);
445   fprintf (file, "imported_library_count: %lu\n",
446            header->imported_library_count);
447   fprintf (file, "total_imported_symbol_count: %lu\n",
448            header->total_imported_symbol_count);
449   fprintf (file, "reloc_section_count: %lu\n", header->reloc_section_count);
450   fprintf (file, "reloc_instr_offset: %lu\n", header->reloc_instr_offset);
451   fprintf (file, "loader_strings_offset: %lu\n",
452            header->loader_strings_offset);
453   fprintf (file, "export_hash_offset: %lu\n", header->export_hash_offset);
454   fprintf (file, "export_hash_table_power: %lu\n",
455            header->export_hash_table_power);
456   fprintf (file, "exported_symbol_count: %lu\n",
457            header->exported_symbol_count);
458 }
459
460 int
461 bfd_pef_print_loader_section (abfd, file)
462      bfd *abfd;
463      FILE *file;
464 {
465   bfd_pef_loader_header header;
466   asection *loadersec = NULL;
467   unsigned char *loaderbuf = NULL;
468   size_t loaderlen = 0;
469   int ret;
470
471   loadersec = bfd_get_section_by_name (abfd, "loader");
472   if (loadersec == NULL)
473     return -1;
474
475   loaderlen = loadersec->size;
476   loaderbuf = (unsigned char *) bfd_malloc (loaderlen);
477   if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
478     {
479       free (loaderbuf);
480       return -1;
481     }
482   if (bfd_bread ((PTR) loaderbuf, loaderlen, abfd) != loaderlen)
483     {
484       free (loaderbuf);
485       return -1;
486     }
487
488   if (loaderlen < 56)
489     {
490       free (loaderbuf);
491       return -1;
492     }
493   ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
494   if (ret < 0)
495     {
496       free (loaderbuf);
497       return -1;
498     }
499
500   bfd_pef_print_loader_header (abfd, &header, file);
501   return 0;
502 }
503
504 int
505 bfd_pef_scan_start_address (abfd)
506      bfd *abfd;
507 {
508   bfd_pef_loader_header header;
509   asection *section;
510
511   asection *loadersec = NULL;
512   unsigned char *loaderbuf = NULL;
513   size_t loaderlen = 0;
514   int ret;
515
516   loadersec = bfd_get_section_by_name (abfd, "loader");
517   if (loadersec == NULL)
518     goto end;
519
520   loaderlen = loadersec->size;
521   loaderbuf = (unsigned char *) bfd_malloc (loaderlen);
522   if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
523     goto error;
524   if (bfd_bread ((PTR) loaderbuf, loaderlen, abfd) != loaderlen)
525     goto error;
526
527   if (loaderlen < 56)
528     goto error;
529   ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
530   if (ret < 0)
531     goto error;
532
533   if (header.main_section < 0)
534     goto end;
535
536   for (section = abfd->sections; section != NULL; section = section->next)
537     if ((section->index + 1) == header.main_section)
538       break;
539
540   if (section == NULL)
541     goto error;
542
543   abfd->start_address = section->vma + header.main_offset;
544
545  end:
546   if (loaderbuf != NULL)
547     free (loaderbuf);
548   return 0;
549
550  error:
551   if (loaderbuf != NULL)
552     free (loaderbuf);
553   return -1;
554 }
555
556 int
557 bfd_pef_scan (abfd, header, mdata)
558      bfd *abfd;
559      bfd_pef_header *header;
560      bfd_pef_data_struct *mdata;
561 {
562   unsigned int i;
563   enum bfd_architecture cputype;
564   unsigned long cpusubtype;
565
566   mdata->header = *header;
567
568   bfd_pef_convert_architecture (header->architecture, &cputype, &cpusubtype);
569   if (cputype == bfd_arch_unknown)
570     {
571       fprintf (stderr, "bfd_pef_scan: unknown architecture 0x%lx\n",
572                header->architecture);
573       return -1;
574     }
575   bfd_set_arch_mach (abfd, cputype, cpusubtype);
576
577   mdata->header = *header;
578
579   abfd->flags = (abfd->xvec->object_flags
580                  | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
581
582   if (header->section_count != 0)
583     {
584       mdata->sections =
585         ((bfd_pef_section *)
586          bfd_alloc (abfd, header->section_count * sizeof (bfd_pef_section)));
587
588       if (mdata->sections == NULL)
589         return -1;
590
591       for (i = 0; i < header->section_count; i++)
592         {
593           bfd_pef_section *cur = &mdata->sections[i];
594           cur->header_offset = 40 + (i * 28);
595           if (bfd_pef_scan_section (abfd, cur) < 0)
596             return -1;
597         }
598     }
599
600   if (bfd_pef_scan_start_address (abfd) < 0)
601     {
602 #if 0
603       fprintf (stderr, "bfd_pef_scan: unable to scan start address: %s\n",
604                bfd_errmsg (bfd_get_error ()));
605       return -1;
606 #endif
607     }
608
609   abfd->tdata.pef_data = mdata;
610
611   return 0;
612 }
613
614 static int
615 bfd_pef_read_header (abfd, header)
616      bfd *abfd;
617      bfd_pef_header *header;
618 {
619   unsigned char buf[40];
620
621   bfd_seek (abfd, 0, SEEK_SET);
622
623   if (bfd_bread ((PTR) buf, 40, abfd) != 40)
624     return -1;
625
626   header->tag1 = bfd_getb32 (buf);
627   header->tag2 = bfd_getb32 (buf + 4);
628   header->architecture = bfd_getb32 (buf + 8);
629   header->format_version = bfd_getb32 (buf + 12);
630   header->timestamp = bfd_getb32 (buf + 16);
631   header->old_definition_version = bfd_getb32 (buf + 20);
632   header->old_implementation_version = bfd_getb32 (buf + 24);
633   header->current_version = bfd_getb32 (buf + 28);
634   header->section_count = bfd_getb32 (buf + 32) + 1;
635   header->instantiated_section_count = bfd_getb32 (buf + 34);
636   header->reserved = bfd_getb32 (buf + 36);
637
638   return 0;
639 }
640
641 static const bfd_target *
642 bfd_pef_object_p (abfd)
643      bfd *abfd;
644 {
645   struct bfd_preserve preserve;
646   bfd_pef_header header;
647
648   preserve.marker = NULL;
649   if (bfd_pef_read_header (abfd, &header) != 0)
650     goto wrong;
651
652   if (header.tag1 != BFD_PEF_TAG1 || header.tag2 != BFD_PEF_TAG2)
653     goto wrong;
654
655   preserve.marker = bfd_zalloc (abfd, sizeof (bfd_pef_data_struct));
656   if (preserve.marker == NULL
657       || !bfd_preserve_save (abfd, &preserve))
658     goto fail;
659
660   if (bfd_pef_scan (abfd, &header,
661                     (bfd_pef_data_struct *) preserve.marker) != 0)
662     goto wrong;
663
664   bfd_preserve_finish (abfd, &preserve);
665   return abfd->xvec;
666
667  wrong:
668   bfd_set_error (bfd_error_wrong_format);
669
670  fail:
671   if (preserve.marker != NULL)
672     bfd_preserve_restore (abfd, &preserve);
673   return NULL;
674 }
675
676 static int bfd_pef_parse_traceback_tables (abfd, sec, buf, len, nsym, csym)
677      bfd *abfd;
678      asection *sec;
679      unsigned char *buf;
680      size_t len;
681      long *nsym;
682      asymbol **csym;
683 {
684   char *name;
685
686   asymbol function;
687   asymbol traceback;
688
689   const char *const tbprefix = "__traceback_";
690   size_t tbnamelen;
691
692   size_t pos = 0;
693   unsigned long count = 0;
694   int ret;
695
696   for (;;)
697     {
698       /* We're reading symbols two at a time.  */
699       if (csym && ((csym[count] == NULL) || (csym[count + 1] == NULL)))
700         break;
701
702       pos += 3;
703       pos -= (pos % 4);
704
705       while ((pos + 4) <= len)
706         {
707           if (bfd_getb32 (buf + pos) == 0)
708             break;
709           pos += 4;
710         }
711
712       if ((pos + 4) > len)
713         break;
714
715       ret = bfd_pef_parse_traceback_table (abfd, sec, buf, len, pos + 4,
716                                            &function, 0);
717       if (ret < 0)
718         {
719           /* Skip over 0x0L to advance to next possible traceback table.  */
720           pos += 4;
721           continue;
722         }
723
724       BFD_ASSERT (function.name != NULL);
725
726       /* Don't bother to compute the name if we are just
727          counting symbols.  */
728
729       if (csym)
730         {
731           tbnamelen = strlen (tbprefix) + strlen (function.name);
732           name = bfd_alloc (abfd, tbnamelen + 1);
733           if (name == NULL)
734             {
735               bfd_release (abfd, (PTR) function.name);
736               function.name = NULL;
737               break;
738             }
739           snprintf (name, tbnamelen + 1, "%s%s", tbprefix, function.name);
740           traceback.name = name;
741           traceback.value = pos;
742           traceback.the_bfd = abfd;
743           traceback.section = sec;
744           traceback.flags = 0;
745           traceback.udata.i = ret;
746
747           *(csym[count]) = function;
748           *(csym[count + 1]) = traceback;
749         }
750
751       pos += ret;
752       count += 2;
753     }
754
755   *nsym = count;
756   return 0;
757 }
758
759 static int bfd_pef_parse_function_stub (abfd, buf, len, offset)
760      bfd *abfd ATTRIBUTE_UNUSED;
761      unsigned char *buf;
762      size_t len;
763      unsigned long *offset;
764 {
765   BFD_ASSERT (len == 24);
766
767   if ((bfd_getb32 (buf) & 0xffff0000) != 0x81820000)
768     return -1;
769   if (bfd_getb32 (buf + 4) != 0x90410014)
770     return -1;
771   if (bfd_getb32 (buf + 8) != 0x800c0000)
772     return -1;
773   if (bfd_getb32 (buf + 12) != 0x804c0004)
774     return -1;
775   if (bfd_getb32 (buf + 16) != 0x7c0903a6)
776     return -1;
777   if (bfd_getb32 (buf + 20) != 0x4e800420)
778     return -1;
779
780   if (offset != NULL)
781     *offset = (bfd_getb32 (buf) & 0x0000ffff) / 4;
782
783   return 0;
784 }
785
786 static int bfd_pef_parse_function_stubs (abfd, codesec, codebuf, codelen,
787                                          loaderbuf, loaderlen, nsym, csym)
788      bfd *abfd;
789      asection *codesec;
790      unsigned char *codebuf;
791      size_t codelen;
792      unsigned char *loaderbuf;
793      size_t loaderlen;
794      unsigned long *nsym;
795      asymbol **csym;
796 {
797   const char *const sprefix = "__stub_";
798
799   size_t codepos = 0;
800   unsigned long count = 0;
801
802   bfd_pef_loader_header header;
803   bfd_pef_imported_library *libraries = NULL;
804   bfd_pef_imported_symbol *imports = NULL;
805
806   unsigned long i;
807   int ret;
808
809   if (loaderlen < 56)
810     goto error;
811
812   ret = bfd_pef_parse_loader_header (abfd, loaderbuf, 56, &header);
813   if (ret < 0)
814     goto error;
815
816   libraries = (bfd_pef_imported_library *) bfd_malloc
817     (header.imported_library_count * sizeof (bfd_pef_imported_library));
818   imports = (bfd_pef_imported_symbol *) bfd_malloc
819     (header.total_imported_symbol_count * sizeof (bfd_pef_imported_symbol));
820
821   if (loaderlen < (56 + (header.imported_library_count * 24)))
822     goto error;
823   for (i = 0; i < header.imported_library_count; i++)
824     {
825       ret = bfd_pef_parse_imported_library
826         (abfd, loaderbuf + 56 + (i * 24), 24, &libraries[i]);
827       if (ret < 0)
828         goto error;
829     }
830
831   if (loaderlen < (56 + (header.imported_library_count * 24)
832                    + (header.total_imported_symbol_count * 4)))
833     goto error;
834   for (i = 0; i < header.total_imported_symbol_count; i++)
835     {
836       ret = (bfd_pef_parse_imported_symbol
837              (abfd,
838               loaderbuf + 56 + (header.imported_library_count * 24) + (i * 4),
839               4, &imports[i]));
840       if (ret < 0)
841         goto error;
842     }
843
844   codepos = 0;
845
846   for (;;)
847     {
848       asymbol sym;
849       const char *symname;
850       char *name;
851       unsigned long index;
852       int ret;
853
854       if (csym && (csym[count] == NULL))
855         break;
856
857       codepos += 3;
858       codepos -= (codepos % 4);
859
860       while ((codepos + 4) <= codelen)
861         {
862           if ((bfd_getb32 (codebuf + codepos) & 0xffff0000) == 0x81820000)
863             break;
864           codepos += 4;
865         }
866
867       if ((codepos + 4) > codelen)
868         break;
869
870       ret = bfd_pef_parse_function_stub (abfd, codebuf + codepos, 24, &index);
871       if (ret < 0)
872         {
873           codepos += 24;
874           continue;
875         }
876
877       if (index >= header.total_imported_symbol_count)
878         {
879           codepos += 24;
880           continue;
881         }
882
883       {
884         size_t max, namelen;
885         const char *s;
886
887         if (loaderlen < (header.loader_strings_offset + imports[index].name))
888           goto error;
889
890         max = loaderlen - (header.loader_strings_offset + imports[index].name);
891         symname = loaderbuf + header.loader_strings_offset + imports[index].name;
892         namelen = 0;
893         for (s = symname; s < (symname + max); s++)
894           {
895             if (*s == '\0')
896               break;
897             if (! ISPRINT (*s))
898               goto error;
899             namelen++;
900           }
901         if (*s != '\0')
902           goto error;
903
904         name = bfd_alloc (abfd, strlen (sprefix) + namelen + 1);
905         if (name == NULL)
906           break;
907
908         snprintf (name, strlen (sprefix) + namelen + 1, "%s%s",
909                   sprefix, symname);
910         sym.name = name;
911       }
912
913       sym.value = codepos;
914       sym.the_bfd = abfd;
915       sym.section = codesec;
916       sym.flags = 0;
917       sym.udata.i = 0;
918
919       codepos += 24;
920
921       if (csym != NULL)
922         *(csym[count]) = sym;
923
924       count++;
925     }
926
927   goto end;
928
929  end:
930   if (libraries != NULL)
931     free (libraries);
932   if (imports != NULL)
933     free (imports);
934   *nsym = count;
935   return 0;
936
937  error:
938   if (libraries != NULL)
939     free (libraries);
940   if (imports != NULL)
941     free (imports);
942   *nsym = count;
943   return -1;
944 }
945
946 static long bfd_pef_parse_symbols (abfd, csym)
947      bfd *abfd;
948      asymbol **csym;
949 {
950   unsigned long count = 0;
951
952   asection *codesec = NULL;
953   unsigned char *codebuf = NULL;
954   size_t codelen = 0;
955
956   asection *loadersec = NULL;
957   unsigned char *loaderbuf = NULL;
958   size_t loaderlen = 0;
959
960   codesec = bfd_get_section_by_name (abfd, "code");
961   if (codesec != NULL)
962     {
963       codelen = codesec->size;
964       codebuf = (unsigned char *) bfd_malloc (codelen);
965       if (bfd_seek (abfd, codesec->filepos, SEEK_SET) < 0)
966         goto end;
967       if (bfd_bread ((PTR) codebuf, codelen, abfd) != codelen)
968         goto end;
969     }
970
971   loadersec = bfd_get_section_by_name (abfd, "loader");
972   if (loadersec != NULL)
973     {
974       loaderlen = loadersec->size;
975       loaderbuf = (unsigned char *) bfd_malloc (loaderlen);
976       if (bfd_seek (abfd, loadersec->filepos, SEEK_SET) < 0)
977         goto end;
978       if (bfd_bread ((PTR) loaderbuf, loaderlen, abfd) != loaderlen)
979         goto end;
980     }
981
982   count = 0;
983   if (codesec != NULL)
984     {
985       unsigned long ncount = 0;
986       bfd_pef_parse_traceback_tables (abfd, codesec, codebuf, codelen,
987                                       &ncount, csym);
988       count += ncount;
989     }
990
991   if ((codesec != NULL) && (loadersec != NULL))
992     {
993       unsigned long ncount = 0;
994       bfd_pef_parse_function_stubs
995         (abfd, codesec, codebuf, codelen, loaderbuf, loaderlen, &ncount,
996          (csym != NULL) ? (csym + count) : NULL);
997       count += ncount;
998     }
999
1000   if (csym != NULL)
1001     csym[count] = NULL;
1002
1003  end:
1004   if (codebuf != NULL)
1005     free (codebuf);
1006
1007   if (loaderbuf != NULL)
1008     free (loaderbuf);
1009
1010   return count;
1011 }
1012
1013 static long
1014 bfd_pef_count_symbols (abfd)
1015      bfd *abfd;
1016 {
1017   return bfd_pef_parse_symbols (abfd, NULL);
1018 }
1019
1020 static long
1021 bfd_pef_get_symtab_upper_bound (abfd)
1022      bfd *abfd;
1023 {
1024   long nsyms = bfd_pef_count_symbols (abfd);
1025   if (nsyms < 0)
1026     return nsyms;
1027   return ((nsyms + 1) * sizeof (asymbol *));
1028 }
1029
1030 static long
1031 bfd_pef_canonicalize_symtab (abfd, alocation)
1032      bfd *abfd;
1033      asymbol **alocation;
1034 {
1035   long i;
1036   asymbol *syms;
1037   long ret;
1038
1039   long nsyms = bfd_pef_count_symbols (abfd);
1040   if (nsyms < 0)
1041     return nsyms;
1042
1043   syms = bfd_alloc (abfd, nsyms * sizeof (asymbol));
1044   if (syms == NULL)
1045     return -1;
1046
1047   for (i = 0; i < nsyms; i++)
1048     alocation[i] = &syms[i];
1049
1050   alocation[nsyms] = NULL;
1051
1052   ret = bfd_pef_parse_symbols (abfd, alocation);
1053   if (ret != nsyms)
1054     return 0;
1055
1056   return ret;
1057 }
1058
1059 static asymbol *
1060 bfd_pef_make_empty_symbol (abfd)
1061      bfd *abfd;
1062 {
1063   return (asymbol *) bfd_alloc (abfd, sizeof (asymbol));
1064 }
1065
1066 static void
1067 bfd_pef_get_symbol_info (abfd, symbol, ret)
1068      bfd *abfd ATTRIBUTE_UNUSED;
1069      asymbol *symbol;
1070      symbol_info *ret;
1071 {
1072   bfd_symbol_info (symbol, ret);
1073 }
1074
1075 static int
1076 bfd_pef_sizeof_headers (abfd, exec)
1077      bfd *abfd ATTRIBUTE_UNUSED;
1078      bfd_boolean exec ATTRIBUTE_UNUSED;
1079 {
1080   return 0;
1081 }
1082
1083 const bfd_target pef_vec =
1084 {
1085   "pef",                        /* name */
1086   bfd_target_pef_flavour,       /* flavour */
1087   BFD_ENDIAN_BIG,               /* byteorder */
1088   BFD_ENDIAN_BIG,               /* header_byteorder */
1089   (HAS_RELOC | EXEC_P |         /* object flags */
1090    HAS_LINENO | HAS_DEBUG |
1091    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
1092   (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
1093    | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
1094   0,                            /* symbol_leading_char */
1095   ' ',                          /* ar_pad_char */
1096   16,                           /* ar_max_namelen */
1097   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1098   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1099   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
1100   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1101   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1102   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
1103   {                             /* bfd_check_format */
1104     _bfd_dummy_target,
1105     bfd_pef_object_p,           /* bfd_check_format */
1106     _bfd_dummy_target,
1107     _bfd_dummy_target,
1108   },
1109   {                             /* bfd_set_format */
1110     bfd_false,
1111     bfd_pef_mkobject,
1112     bfd_false,
1113     bfd_false,
1114   },
1115   {                             /* bfd_write_contents */
1116     bfd_false,
1117     bfd_true,
1118     bfd_false,
1119     bfd_false,
1120   },
1121
1122   BFD_JUMP_TABLE_GENERIC (bfd_pef),
1123   BFD_JUMP_TABLE_COPY (_bfd_generic),
1124   BFD_JUMP_TABLE_CORE (_bfd_nocore),
1125   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
1126   BFD_JUMP_TABLE_SYMBOLS (bfd_pef),
1127   BFD_JUMP_TABLE_RELOCS (bfd_pef),
1128   BFD_JUMP_TABLE_WRITE (bfd_pef),
1129   BFD_JUMP_TABLE_LINK (bfd_pef),
1130   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1131
1132   NULL,
1133
1134   NULL
1135 };
1136
1137 #define bfd_pef_xlib_close_and_cleanup _bfd_generic_close_and_cleanup
1138 #define bfd_pef_xlib_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
1139 #define bfd_pef_xlib_new_section_hook _bfd_generic_new_section_hook
1140 #define bfd_pef_xlib_get_section_contents _bfd_generic_get_section_contents
1141 #define bfd_pef_xlib_set_section_contents _bfd_generic_set_section_contents
1142 #define bfd_pef_xlib_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
1143 #define bfd_pef_xlib_set_section_contents_in_window _bfd_generic_set_section_contents_in_window
1144
1145 static int
1146 bfd_pef_xlib_read_header (abfd, header)
1147      bfd *abfd;
1148      bfd_pef_xlib_header *header;
1149 {
1150   unsigned char buf[76];
1151
1152   bfd_seek (abfd, 0, SEEK_SET);
1153
1154   if (bfd_bread ((PTR) buf, 76, abfd) != 76)
1155     return -1;
1156
1157   header->tag1 = bfd_getb32 (buf);
1158   header->tag2 = bfd_getb32 (buf + 4);
1159   header->current_format = bfd_getb32 (buf + 8);
1160   header->container_strings_offset = bfd_getb32 (buf + 12);
1161   header->export_hash_offset = bfd_getb32 (buf + 16);
1162   header->export_key_offset = bfd_getb32 (buf + 20);
1163   header->export_symbol_offset = bfd_getb32 (buf + 24);
1164   header->export_names_offset = bfd_getb32 (buf + 28);
1165   header->export_hash_table_power = bfd_getb32 (buf + 32);
1166   header->exported_symbol_count = bfd_getb32 (buf + 36);
1167   header->frag_name_offset = bfd_getb32 (buf + 40);
1168   header->frag_name_length = bfd_getb32 (buf + 44);
1169   header->dylib_path_offset = bfd_getb32 (buf + 48);
1170   header->dylib_path_length = bfd_getb32 (buf + 52);
1171   header->cpu_family = bfd_getb32 (buf + 56);
1172   header->cpu_model = bfd_getb32 (buf + 60);
1173   header->date_time_stamp = bfd_getb32 (buf + 64);
1174   header->current_version = bfd_getb32 (buf + 68);
1175   header->old_definition_version = bfd_getb32 (buf + 72);
1176   header->old_implementation_version = bfd_getb32 (buf + 76);
1177
1178   return 0;
1179 }
1180
1181 int
1182 bfd_pef_xlib_scan (abfd, header)
1183      bfd *abfd;
1184      bfd_pef_xlib_header *header;
1185 {
1186   bfd_pef_xlib_data_struct *mdata = NULL;
1187
1188   mdata = ((bfd_pef_xlib_data_struct *)
1189            bfd_alloc (abfd, sizeof (bfd_pef_xlib_data_struct)));
1190   if (mdata == NULL)
1191     return -1;
1192
1193   mdata->header = *header;
1194
1195   abfd->flags = (abfd->xvec->object_flags
1196                  | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
1197
1198   abfd->tdata.pef_xlib_data = mdata;
1199
1200   return 0;
1201 }
1202
1203 static const bfd_target *
1204 bfd_pef_xlib_object_p (abfd)
1205      bfd *abfd;
1206 {
1207   struct bfd_preserve preserve;
1208   bfd_pef_xlib_header header;
1209
1210   if (bfd_pef_xlib_read_header (abfd, &header) != 0)
1211     {
1212       bfd_set_error (bfd_error_wrong_format);
1213       return NULL;
1214     }
1215
1216   if ((header.tag1 != BFD_PEF_XLIB_TAG1)
1217       || ((header.tag2 != BFD_PEF_VLIB_TAG2)
1218           && (header.tag2 != BFD_PEF_BLIB_TAG2)))
1219     {
1220       bfd_set_error (bfd_error_wrong_format);
1221       return NULL;
1222     }
1223
1224   if (! bfd_preserve_save (abfd, &preserve))
1225     {
1226       bfd_set_error (bfd_error_wrong_format);
1227       return NULL;
1228     }
1229
1230   if (bfd_pef_xlib_scan (abfd, &header) != 0)
1231     {
1232       bfd_preserve_restore (abfd, &preserve);
1233       bfd_set_error (bfd_error_wrong_format);
1234       return NULL;
1235     }
1236
1237   bfd_preserve_finish (abfd, &preserve);
1238   return abfd->xvec;
1239 }
1240
1241 const bfd_target pef_xlib_vec =
1242 {
1243   "pef-xlib",                   /* name */
1244   bfd_target_pef_xlib_flavour,  /* flavour */
1245   BFD_ENDIAN_BIG,               /* byteorder */
1246   BFD_ENDIAN_BIG,               /* header_byteorder */
1247   (HAS_RELOC | EXEC_P |         /* object flags */
1248    HAS_LINENO | HAS_DEBUG |
1249    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
1250   (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
1251    | SEC_ROM | SEC_HAS_CONTENTS), /* section_flags */
1252   0,                            /* symbol_leading_char */
1253   ' ',                          /* ar_pad_char */
1254   16,                           /* ar_max_namelen */
1255   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1256   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1257   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
1258   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1259   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1260   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
1261   {                             /* bfd_check_format */
1262     _bfd_dummy_target,
1263     bfd_pef_xlib_object_p,      /* bfd_check_format */
1264     _bfd_dummy_target,
1265     _bfd_dummy_target,
1266   },
1267   {                             /* bfd_set_format */
1268     bfd_false,
1269     bfd_pef_mkobject,
1270     bfd_false,
1271     bfd_false,
1272   },
1273   {                             /* bfd_write_contents */
1274     bfd_false,
1275     bfd_true,
1276     bfd_false,
1277     bfd_false,
1278   },
1279
1280   BFD_JUMP_TABLE_GENERIC (bfd_pef_xlib),
1281   BFD_JUMP_TABLE_COPY (_bfd_generic),
1282   BFD_JUMP_TABLE_CORE (_bfd_nocore),
1283   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
1284   BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
1285   BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
1286   BFD_JUMP_TABLE_WRITE (_bfd_nowrite),
1287   BFD_JUMP_TABLE_LINK (_bfd_nolink),
1288   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1289
1290   NULL,
1291
1292   NULL
1293 };