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