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