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