| 1 | /* Routines to help build PEI-format DLLs (Win32 etc) |
| 2 | Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. |
| 3 | Written by DJ Delorie <dj@cygnus.com> |
| 4 | |
| 5 | This file is part of GLD, the Gnu Linker. |
| 6 | |
| 7 | GLD 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, or (at your option) |
| 10 | any later version. |
| 11 | |
| 12 | GLD 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 GLD; see the file COPYING. If not, write to the Free |
| 19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
| 20 | 02111-1307, USA. */ |
| 21 | |
| 22 | #include "bfd.h" |
| 23 | #include "sysdep.h" |
| 24 | #include "bfdlink.h" |
| 25 | #include "libiberty.h" |
| 26 | |
| 27 | #include <time.h> |
| 28 | #include <ctype.h> |
| 29 | |
| 30 | #include "ld.h" |
| 31 | #include "ldexp.h" |
| 32 | #include "ldlang.h" |
| 33 | #include "ldwrite.h" |
| 34 | #include "ldmisc.h" |
| 35 | #include "ldgram.h" |
| 36 | #include "ldmain.h" |
| 37 | #include "ldemul.h" |
| 38 | #include "coff/internal.h" |
| 39 | #include "../bfd/libcoff.h" |
| 40 | #include "deffile.h" |
| 41 | #include "pe-dll.h" |
| 42 | |
| 43 | /************************************************************************ |
| 44 | |
| 45 | This file turns a regular Windows PE image into a DLL. Because of |
| 46 | the complexity of this operation, it has been broken down into a |
| 47 | number of separate modules which are all called by the main function |
| 48 | at the end of this file. This function is not re-entrant and is |
| 49 | normally only called once, so static variables are used to reduce |
| 50 | the number of parameters and return values required. |
| 51 | |
| 52 | See also: ld/emultempl/pe.em |
| 53 | |
| 54 | ************************************************************************/ |
| 55 | |
| 56 | /* for emultempl/pe.em */ |
| 57 | |
| 58 | def_file *pe_def_file = 0; |
| 59 | int pe_dll_export_everything = 0; |
| 60 | int pe_dll_do_default_excludes = 1; |
| 61 | int pe_dll_kill_ats = 0; |
| 62 | int pe_dll_stdcall_aliases = 0; |
| 63 | int pe_dll_warn_dup_exports = 0; |
| 64 | int pe_dll_compat_implib = 0; |
| 65 | |
| 66 | /************************************************************************ |
| 67 | |
| 68 | static variables and types |
| 69 | |
| 70 | ************************************************************************/ |
| 71 | |
| 72 | static bfd_vma image_base; |
| 73 | |
| 74 | static bfd *filler_bfd; |
| 75 | static struct sec *edata_s, *reloc_s; |
| 76 | static unsigned char *edata_d, *reloc_d; |
| 77 | static size_t edata_sz, reloc_sz; |
| 78 | |
| 79 | typedef struct { |
| 80 | char *target_name; |
| 81 | char *object_target; |
| 82 | unsigned int imagebase_reloc; |
| 83 | int pe_arch; |
| 84 | int bfd_arch; |
| 85 | int underscored; |
| 86 | } pe_details_type; |
| 87 | |
| 88 | #define PE_ARCH_i386 1 |
| 89 | #define PE_ARCH_sh 2 |
| 90 | #define PE_ARCH_mips 3 |
| 91 | #define PE_ARCH_arm 4 |
| 92 | |
| 93 | static pe_details_type pe_detail_list[] = { |
| 94 | { |
| 95 | "pei-i386", |
| 96 | "pe-i386", |
| 97 | 7 /* R_IMAGEBASE */, |
| 98 | PE_ARCH_i386, |
| 99 | bfd_arch_i386, |
| 100 | 1 |
| 101 | }, |
| 102 | { |
| 103 | "pei-shl", |
| 104 | "pe-shl", |
| 105 | 16 /* R_SH_IMAGEBASE */, |
| 106 | PE_ARCH_sh, |
| 107 | bfd_arch_sh, |
| 108 | 1 |
| 109 | }, |
| 110 | { |
| 111 | "pei-mips", |
| 112 | "pe-mips", |
| 113 | 34 /* MIPS_R_RVA */, |
| 114 | PE_ARCH_mips, |
| 115 | bfd_arch_mips, |
| 116 | 0 |
| 117 | }, |
| 118 | { |
| 119 | "pei-arm-little", |
| 120 | "pe-arm-little", |
| 121 | 11 /* ARM_RVA32 */, |
| 122 | PE_ARCH_arm, |
| 123 | bfd_arch_arm, |
| 124 | 0 |
| 125 | }, |
| 126 | { NULL, NULL, 0, 0, 0, 0 } |
| 127 | }; |
| 128 | |
| 129 | static pe_details_type *pe_details; |
| 130 | |
| 131 | #define U(str) (pe_details->underscored ? "_" str : str) |
| 132 | |
| 133 | void |
| 134 | pe_dll_id_target (target) |
| 135 | const char *target; |
| 136 | { |
| 137 | int i; |
| 138 | for (i=0; pe_detail_list[i].target_name; i++) |
| 139 | if (strcmp (pe_detail_list[i].target_name, target) == 0) |
| 140 | { |
| 141 | pe_details = pe_detail_list+i; |
| 142 | return; |
| 143 | } |
| 144 | einfo (_("%XUnsupported PEI architecture: %s\n"), target); |
| 145 | exit (1); |
| 146 | } |
| 147 | |
| 148 | /************************************************************************ |
| 149 | |
| 150 | Helper functions for qsort. Relocs must be sorted so that we can write |
| 151 | them out by pages. |
| 152 | |
| 153 | ************************************************************************/ |
| 154 | |
| 155 | typedef struct { |
| 156 | bfd_vma vma; |
| 157 | char type; |
| 158 | short extra; |
| 159 | } reloc_data_type; |
| 160 | |
| 161 | static int |
| 162 | reloc_sort (va, vb) |
| 163 | const void *va, *vb; |
| 164 | { |
| 165 | bfd_vma a = ((reloc_data_type *) va)->vma; |
| 166 | bfd_vma b = ((reloc_data_type *) vb)->vma; |
| 167 | return (a > b) ? 1 : ((a < b) ? -1 : 0); |
| 168 | } |
| 169 | |
| 170 | static int |
| 171 | pe_export_sort (va, vb) |
| 172 | const void *va, *vb; |
| 173 | { |
| 174 | def_file_export *a = (def_file_export *) va; |
| 175 | def_file_export *b = (def_file_export *) vb; |
| 176 | return strcmp (a->name, b->name); |
| 177 | } |
| 178 | |
| 179 | /************************************************************************ |
| 180 | |
| 181 | Read and process the .DEF file |
| 182 | |
| 183 | ************************************************************************/ |
| 184 | |
| 185 | /* These correspond to the entries in pe_def_file->exports[]. I use |
| 186 | exported_symbol_sections[i] to tag whether or not the symbol was |
| 187 | defined, since we can't export symbols we don't have. */ |
| 188 | |
| 189 | static bfd_vma *exported_symbol_offsets; |
| 190 | static struct sec **exported_symbol_sections; |
| 191 | |
| 192 | static int export_table_size; |
| 193 | static int count_exported; |
| 194 | static int count_exported_byname; |
| 195 | static int count_with_ordinals; |
| 196 | static const char *dll_name; |
| 197 | static int min_ordinal, max_ordinal; |
| 198 | static int *exported_symbols; |
| 199 | |
| 200 | typedef struct exclude_list_struct |
| 201 | { |
| 202 | char *string; |
| 203 | struct exclude_list_struct *next; |
| 204 | } |
| 205 | exclude_list_struct; |
| 206 | static struct exclude_list_struct *excludes = 0; |
| 207 | |
| 208 | void |
| 209 | pe_dll_add_excludes (new_excludes) |
| 210 | const char *new_excludes; |
| 211 | { |
| 212 | char *local_copy; |
| 213 | char *exclude_string; |
| 214 | |
| 215 | local_copy = xstrdup (new_excludes); |
| 216 | |
| 217 | exclude_string = strtok (local_copy, ",:"); |
| 218 | for (; exclude_string; exclude_string = strtok (NULL, ",:")) |
| 219 | { |
| 220 | struct exclude_list_struct *new_exclude; |
| 221 | |
| 222 | new_exclude = ((struct exclude_list_struct *) |
| 223 | xmalloc (sizeof (struct exclude_list_struct))); |
| 224 | new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1); |
| 225 | strcpy (new_exclude->string, exclude_string); |
| 226 | new_exclude->next = excludes; |
| 227 | excludes = new_exclude; |
| 228 | } |
| 229 | |
| 230 | free (local_copy); |
| 231 | } |
| 232 | |
| 233 | static int |
| 234 | auto_export (d, n) |
| 235 | def_file *d; |
| 236 | const char *n; |
| 237 | { |
| 238 | int i; |
| 239 | struct exclude_list_struct *ex; |
| 240 | for (i = 0; i < d->num_exports; i++) |
| 241 | if (strcmp (d->exports[i].name, n) == 0) |
| 242 | return 0; |
| 243 | if (pe_dll_do_default_excludes) |
| 244 | { |
| 245 | if (strcmp (n, "DllMain@12") == 0) |
| 246 | return 0; |
| 247 | if (strcmp (n, "DllEntryPoint@0") == 0) |
| 248 | return 0; |
| 249 | if (strcmp (n, "impure_ptr") == 0) |
| 250 | return 0; |
| 251 | } |
| 252 | for (ex = excludes; ex; ex = ex->next) |
| 253 | if (strcmp (n, ex->string) == 0) |
| 254 | return 0; |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | static void |
| 259 | process_def_file (abfd, info) |
| 260 | bfd *abfd ATTRIBUTE_UNUSED; |
| 261 | struct bfd_link_info *info; |
| 262 | { |
| 263 | int i, j; |
| 264 | struct bfd_link_hash_entry *blhe; |
| 265 | bfd *b; |
| 266 | struct sec *s; |
| 267 | def_file_export *e=0; |
| 268 | |
| 269 | if (!pe_def_file) |
| 270 | pe_def_file = def_file_empty (); |
| 271 | |
| 272 | /* First, run around to all the objects looking for the .drectve |
| 273 | sections, and push those into the def file too */ |
| 274 | |
| 275 | for (b = info->input_bfds; b; b = b->link_next) |
| 276 | { |
| 277 | s = bfd_get_section_by_name (b, ".drectve"); |
| 278 | if (s) |
| 279 | { |
| 280 | int size = bfd_get_section_size_before_reloc (s); |
| 281 | char *buf = xmalloc (size); |
| 282 | bfd_get_section_contents (b, s, buf, 0, size); |
| 283 | def_file_add_directive (pe_def_file, buf, size); |
| 284 | free (buf); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* Now, maybe export everything else the default way */ |
| 289 | |
| 290 | if (pe_dll_export_everything || pe_def_file->num_exports == 0) |
| 291 | { |
| 292 | for (b = info->input_bfds; b; b = b->link_next) |
| 293 | { |
| 294 | asymbol **symbols; |
| 295 | int nsyms, symsize; |
| 296 | |
| 297 | symsize = bfd_get_symtab_upper_bound (b); |
| 298 | symbols = (asymbol **) xmalloc (symsize); |
| 299 | nsyms = bfd_canonicalize_symtab (b, symbols); |
| 300 | |
| 301 | for (j = 0; j < nsyms; j++) |
| 302 | { |
| 303 | if ((symbols[j]->flags & (BSF_FUNCTION | BSF_GLOBAL)) |
| 304 | == (BSF_FUNCTION | BSF_GLOBAL)) |
| 305 | { |
| 306 | const char *sn = symbols[j]->name; |
| 307 | if (*sn == '_') |
| 308 | sn++; |
| 309 | if (auto_export (pe_def_file, sn)) |
| 310 | def_file_add_export (pe_def_file, sn, 0, -1); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | #undef NE |
| 317 | #define NE pe_def_file->num_exports |
| 318 | |
| 319 | /* Canonicalize the export list */ |
| 320 | |
| 321 | if (pe_dll_kill_ats) |
| 322 | { |
| 323 | for (i = 0; i < NE; i++) |
| 324 | { |
| 325 | if (strchr (pe_def_file->exports[i].name, '@')) |
| 326 | { |
| 327 | /* This will preserve internal_name, which may have been pointing |
| 328 | to the same memory as name, or might not have */ |
| 329 | char *tmp = xstrdup (pe_def_file->exports[i].name); |
| 330 | *(strchr (tmp, '@')) = 0; |
| 331 | pe_def_file->exports[i].name = tmp; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if (pe_dll_stdcall_aliases) |
| 337 | { |
| 338 | for (i = 0; i < NE; i++) |
| 339 | { |
| 340 | if (strchr (pe_def_file->exports[i].name, '@')) |
| 341 | { |
| 342 | char *tmp = xstrdup (pe_def_file->exports[i].name); |
| 343 | *(strchr (tmp, '@')) = 0; |
| 344 | if (auto_export (pe_def_file, tmp)) |
| 345 | def_file_add_export (pe_def_file, tmp, |
| 346 | pe_def_file->exports[i].internal_name, -1); |
| 347 | else |
| 348 | free (tmp); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | e = pe_def_file->exports; /* convenience, but watch out for it changing */ |
| 354 | |
| 355 | exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma)); |
| 356 | exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *)); |
| 357 | |
| 358 | memset (exported_symbol_sections, 0, NE * sizeof (struct sec *)); |
| 359 | max_ordinal = 0; |
| 360 | min_ordinal = 65536; |
| 361 | count_exported = 0; |
| 362 | count_exported_byname = 0; |
| 363 | count_with_ordinals = 0; |
| 364 | |
| 365 | qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort); |
| 366 | for (i = 0, j = 0; i < NE; i++) |
| 367 | { |
| 368 | if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0) |
| 369 | { |
| 370 | /* This is a duplicate. */ |
| 371 | if (e[j - 1].ordinal != -1 |
| 372 | && e[i].ordinal != -1 |
| 373 | && e[j - 1].ordinal != e[i].ordinal) |
| 374 | { |
| 375 | if (pe_dll_warn_dup_exports) |
| 376 | /* xgettext:c-format */ |
| 377 | einfo (_("%XError, duplicate EXPORT with oridinals: %s (%d vs %d)\n"), |
| 378 | e[j - 1].name, e[j - 1].ordinal, e[i].ordinal); |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | if (pe_dll_warn_dup_exports) |
| 383 | /* xgettext:c-format */ |
| 384 | einfo (_("Warning, duplicate EXPORT: %s\n"), |
| 385 | e[j - 1].name); |
| 386 | } |
| 387 | if (e[i].ordinal) |
| 388 | e[j - 1].ordinal = e[i].ordinal; |
| 389 | e[j - 1].flag_private |= e[i].flag_private; |
| 390 | e[j - 1].flag_constant |= e[i].flag_constant; |
| 391 | e[j - 1].flag_noname |= e[i].flag_noname; |
| 392 | e[j - 1].flag_data |= e[i].flag_data; |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | if (i != j) |
| 397 | e[j] = e[i]; |
| 398 | j++; |
| 399 | } |
| 400 | } |
| 401 | pe_def_file->num_exports = j; /* == NE */ |
| 402 | |
| 403 | for (i = 0; i < NE; i++) |
| 404 | { |
| 405 | char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2); |
| 406 | if (pe_details->underscored) |
| 407 | { |
| 408 | *name = '_'; |
| 409 | strcpy (name + 1, pe_def_file->exports[i].internal_name); |
| 410 | } |
| 411 | else |
| 412 | strcpy (name, pe_def_file->exports[i].internal_name); |
| 413 | |
| 414 | blhe = bfd_link_hash_lookup (info->hash, |
| 415 | name, |
| 416 | false, false, true); |
| 417 | |
| 418 | if (blhe |
| 419 | && (blhe->type == bfd_link_hash_defined |
| 420 | || (blhe->type == bfd_link_hash_common))) |
| 421 | { |
| 422 | count_exported++; |
| 423 | if (!pe_def_file->exports[i].flag_noname) |
| 424 | count_exported_byname++; |
| 425 | |
| 426 | /* Only fill in the sections. The actual offsets are computed |
| 427 | in fill_exported_offsets() after common symbols are laid |
| 428 | out. */ |
| 429 | if (blhe->type == bfd_link_hash_defined) |
| 430 | exported_symbol_sections[i] = blhe->u.def.section; |
| 431 | else |
| 432 | exported_symbol_sections[i] = blhe->u.c.p->section; |
| 433 | |
| 434 | if (pe_def_file->exports[i].ordinal != -1) |
| 435 | { |
| 436 | if (max_ordinal < pe_def_file->exports[i].ordinal) |
| 437 | max_ordinal = pe_def_file->exports[i].ordinal; |
| 438 | if (min_ordinal > pe_def_file->exports[i].ordinal) |
| 439 | min_ordinal = pe_def_file->exports[i].ordinal; |
| 440 | count_with_ordinals++; |
| 441 | } |
| 442 | } |
| 443 | else if (blhe && blhe->type == bfd_link_hash_undefined) |
| 444 | { |
| 445 | /* xgettext:c-format */ |
| 446 | einfo (_("%XCannot export %s: symbol not defined\n"), |
| 447 | pe_def_file->exports[i].internal_name); |
| 448 | } |
| 449 | else if (blhe) |
| 450 | { |
| 451 | /* xgettext:c-format */ |
| 452 | einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"), |
| 453 | pe_def_file->exports[i].internal_name, |
| 454 | blhe->type, bfd_link_hash_defined); |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | /* xgettext:c-format */ |
| 459 | einfo (_("%XCannot export %s: symbol not found\n"), |
| 460 | pe_def_file->exports[i].internal_name); |
| 461 | } |
| 462 | free (name); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /************************************************************************ |
| 467 | |
| 468 | Build the bfd that will contain .edata and .reloc sections |
| 469 | |
| 470 | ************************************************************************/ |
| 471 | |
| 472 | static void |
| 473 | build_filler_bfd (include_edata) |
| 474 | int include_edata; |
| 475 | { |
| 476 | lang_input_statement_type *filler_file; |
| 477 | filler_file = lang_add_input_file ("dll stuff", |
| 478 | lang_input_file_is_fake_enum, |
| 479 | NULL); |
| 480 | filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd); |
| 481 | if (filler_bfd == NULL |
| 482 | || !bfd_set_arch_mach (filler_bfd, |
| 483 | bfd_get_arch (output_bfd), |
| 484 | bfd_get_mach (output_bfd))) |
| 485 | { |
| 486 | einfo ("%X%P: can not create BFD %E\n"); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (include_edata) |
| 491 | { |
| 492 | edata_s = bfd_make_section_old_way (filler_bfd, ".edata"); |
| 493 | if (edata_s == NULL |
| 494 | || !bfd_set_section_flags (filler_bfd, edata_s, |
| 495 | (SEC_HAS_CONTENTS |
| 496 | | SEC_ALLOC |
| 497 | | SEC_LOAD |
| 498 | | SEC_KEEP |
| 499 | | SEC_IN_MEMORY))) |
| 500 | { |
| 501 | einfo ("%X%P: can not create .edata section: %E\n"); |
| 502 | return; |
| 503 | } |
| 504 | bfd_set_section_size (filler_bfd, edata_s, edata_sz); |
| 505 | } |
| 506 | |
| 507 | reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc"); |
| 508 | if (reloc_s == NULL |
| 509 | || !bfd_set_section_flags (filler_bfd, reloc_s, |
| 510 | (SEC_HAS_CONTENTS |
| 511 | | SEC_ALLOC |
| 512 | | SEC_LOAD |
| 513 | | SEC_KEEP |
| 514 | | SEC_IN_MEMORY))) |
| 515 | { |
| 516 | einfo ("%X%P: can not create .reloc section: %E\n"); |
| 517 | return; |
| 518 | } |
| 519 | bfd_set_section_size (filler_bfd, reloc_s, 0); |
| 520 | |
| 521 | ldlang_add_file (filler_file); |
| 522 | } |
| 523 | |
| 524 | /************************************************************************ |
| 525 | |
| 526 | Gather all the exported symbols and build the .edata section |
| 527 | |
| 528 | ************************************************************************/ |
| 529 | |
| 530 | static void |
| 531 | generate_edata (abfd, info) |
| 532 | bfd *abfd; |
| 533 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
| 534 | { |
| 535 | int i, next_ordinal; |
| 536 | int name_table_size = 0; |
| 537 | const char *dlnp; |
| 538 | |
| 539 | /* First, we need to know how many exported symbols there are, |
| 540 | and what the range of ordinals is. */ |
| 541 | |
| 542 | if (pe_def_file->name) |
| 543 | { |
| 544 | dll_name = pe_def_file->name; |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | dll_name = abfd->filename; |
| 549 | for (dlnp = dll_name; *dlnp; dlnp++) |
| 550 | { |
| 551 | if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':') |
| 552 | dll_name = dlnp + 1; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if (count_with_ordinals && max_ordinal > count_exported) |
| 557 | { |
| 558 | if (min_ordinal > max_ordinal - count_exported + 1) |
| 559 | min_ordinal = max_ordinal - count_exported + 1; |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | min_ordinal = 1; |
| 564 | max_ordinal = count_exported; |
| 565 | } |
| 566 | export_table_size = max_ordinal - min_ordinal + 1; |
| 567 | |
| 568 | exported_symbols = (int *) xmalloc (export_table_size * sizeof (int)); |
| 569 | for (i = 0; i < export_table_size; i++) |
| 570 | exported_symbols[i] = -1; |
| 571 | |
| 572 | /* Now we need to assign ordinals to those that don't have them */ |
| 573 | for (i = 0; i < NE; i++) |
| 574 | { |
| 575 | if (exported_symbol_sections[i]) |
| 576 | { |
| 577 | if (pe_def_file->exports[i].ordinal != -1) |
| 578 | { |
| 579 | int ei = pe_def_file->exports[i].ordinal - min_ordinal; |
| 580 | int pi = exported_symbols[ei]; |
| 581 | if (pi != -1) |
| 582 | { |
| 583 | /* xgettext:c-format */ |
| 584 | einfo (_("%XError, oridinal used twice: %d (%s vs %s)\n"), |
| 585 | pe_def_file->exports[i].ordinal, |
| 586 | pe_def_file->exports[i].name, |
| 587 | pe_def_file->exports[pi].name); |
| 588 | } |
| 589 | exported_symbols[ei] = i; |
| 590 | } |
| 591 | name_table_size += strlen (pe_def_file->exports[i].name) + 1; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | next_ordinal = min_ordinal; |
| 596 | for (i = 0; i < NE; i++) |
| 597 | if (exported_symbol_sections[i]) |
| 598 | if (pe_def_file->exports[i].ordinal == -1) |
| 599 | { |
| 600 | while (exported_symbols[next_ordinal - min_ordinal] != -1) |
| 601 | next_ordinal++; |
| 602 | exported_symbols[next_ordinal - min_ordinal] = i; |
| 603 | pe_def_file->exports[i].ordinal = next_ordinal; |
| 604 | } |
| 605 | |
| 606 | /* OK, now we can allocate some memory */ |
| 607 | |
| 608 | edata_sz = (40 /* directory */ |
| 609 | + 4 * export_table_size /* addresses */ |
| 610 | + 4 * count_exported_byname /* name ptrs */ |
| 611 | + 2 * count_exported_byname /* ordinals */ |
| 612 | + name_table_size + strlen (dll_name) + 1); |
| 613 | } |
| 614 | |
| 615 | /* Fill the exported symbol offsets. The preliminary work has already |
| 616 | been done in process_def_file(). */ |
| 617 | |
| 618 | static void |
| 619 | fill_exported_offsets (abfd, info) |
| 620 | bfd *abfd; |
| 621 | struct bfd_link_info *info; |
| 622 | { |
| 623 | int i, j; |
| 624 | struct bfd_link_hash_entry *blhe; |
| 625 | bfd *b; |
| 626 | struct sec *s; |
| 627 | def_file_export *e=0; |
| 628 | |
| 629 | for (i = 0; i < pe_def_file->num_exports; i++) |
| 630 | { |
| 631 | char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2); |
| 632 | if (pe_details->underscored) |
| 633 | { |
| 634 | *name = '_'; |
| 635 | strcpy (name + 1, pe_def_file->exports[i].internal_name); |
| 636 | } |
| 637 | else |
| 638 | strcpy (name, pe_def_file->exports[i].internal_name); |
| 639 | |
| 640 | blhe = bfd_link_hash_lookup (info->hash, |
| 641 | name, |
| 642 | false, false, true); |
| 643 | |
| 644 | if (blhe && (blhe->type == bfd_link_hash_defined)) |
| 645 | { |
| 646 | exported_symbol_offsets[i] = blhe->u.def.value; |
| 647 | } |
| 648 | free (name); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | static void |
| 653 | fill_edata (abfd, info) |
| 654 | bfd *abfd; |
| 655 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
| 656 | { |
| 657 | int i, hint; |
| 658 | unsigned char *edirectory; |
| 659 | unsigned long *eaddresses; |
| 660 | unsigned long *enameptrs; |
| 661 | unsigned short *eordinals; |
| 662 | unsigned char *enamestr; |
| 663 | time_t now; |
| 664 | |
| 665 | time (&now); |
| 666 | |
| 667 | edata_d = (unsigned char *) xmalloc (edata_sz); |
| 668 | |
| 669 | /* Note use of array pointer math here */ |
| 670 | edirectory = edata_d; |
| 671 | eaddresses = (unsigned long *) (edata_d + 40); |
| 672 | enameptrs = eaddresses + export_table_size; |
| 673 | eordinals = (unsigned short *) (enameptrs + count_exported_byname); |
| 674 | enamestr = (char *) (eordinals + count_exported_byname); |
| 675 | |
| 676 | #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base) |
| 677 | |
| 678 | memset (edata_d, 0, 40); |
| 679 | bfd_put_32 (abfd, now, edata_d + 4); |
| 680 | if (pe_def_file->version_major != -1) |
| 681 | { |
| 682 | bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8); |
| 683 | bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10); |
| 684 | } |
| 685 | bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12); |
| 686 | strcpy (enamestr, dll_name); |
| 687 | enamestr += strlen (enamestr) + 1; |
| 688 | bfd_put_32 (abfd, min_ordinal, edata_d + 16); |
| 689 | bfd_put_32 (abfd, export_table_size, edata_d + 20); |
| 690 | bfd_put_32 (abfd, count_exported_byname, edata_d + 24); |
| 691 | bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28); |
| 692 | bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32); |
| 693 | bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36); |
| 694 | |
| 695 | fill_exported_offsets (abfd, info); |
| 696 | |
| 697 | /* Ok, now for the filling in part */ |
| 698 | hint = 0; |
| 699 | for (i = 0; i < export_table_size; i++) |
| 700 | { |
| 701 | int s = exported_symbols[i]; |
| 702 | if (s != -1) |
| 703 | { |
| 704 | struct sec *ssec = exported_symbol_sections[s]; |
| 705 | unsigned long srva = (exported_symbol_offsets[s] |
| 706 | + ssec->output_section->vma |
| 707 | + ssec->output_offset); |
| 708 | |
| 709 | bfd_put_32 (abfd, srva - image_base, (void *) (eaddresses + i)); |
| 710 | if (!pe_def_file->exports[s].flag_noname) |
| 711 | { |
| 712 | char *ename = pe_def_file->exports[s].name; |
| 713 | bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs); |
| 714 | strcpy (enamestr, ename); |
| 715 | enamestr += strlen (enamestr) + 1; |
| 716 | bfd_put_16 (abfd, i, (void *) eordinals); |
| 717 | enameptrs++; |
| 718 | pe_def_file->exports[s].hint = hint++; |
| 719 | } |
| 720 | eordinals++; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /************************************************************************ |
| 726 | |
| 727 | Gather all the relocations and build the .reloc section |
| 728 | |
| 729 | ************************************************************************/ |
| 730 | |
| 731 | static void |
| 732 | generate_reloc (abfd, info) |
| 733 | bfd *abfd; |
| 734 | struct bfd_link_info *info; |
| 735 | { |
| 736 | |
| 737 | /* for .reloc stuff */ |
| 738 | reloc_data_type *reloc_data; |
| 739 | int total_relocs = 0; |
| 740 | int i; |
| 741 | unsigned long sec_page = (unsigned long) (-1); |
| 742 | unsigned long page_ptr, page_count; |
| 743 | int bi; |
| 744 | bfd *b; |
| 745 | struct sec *s; |
| 746 | |
| 747 | total_relocs = 0; |
| 748 | for (b = info->input_bfds; b; b = b->link_next) |
| 749 | for (s = b->sections; s; s = s->next) |
| 750 | total_relocs += s->reloc_count; |
| 751 | |
| 752 | reloc_data = (reloc_data_type *) xmalloc (total_relocs * sizeof (reloc_data_type)); |
| 753 | |
| 754 | total_relocs = 0; |
| 755 | bi = 0; |
| 756 | for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next) |
| 757 | { |
| 758 | arelent **relocs; |
| 759 | int relsize, nrelocs, i; |
| 760 | |
| 761 | for (s = b->sections; s; s = s->next) |
| 762 | { |
| 763 | unsigned long sec_vma = s->output_section->vma + s->output_offset; |
| 764 | asymbol **symbols; |
| 765 | int nsyms, symsize; |
| 766 | |
| 767 | /* if it's not loaded, we don't need to relocate it this way */ |
| 768 | if (!(s->output_section->flags & SEC_LOAD)) |
| 769 | continue; |
| 770 | |
| 771 | /* I don't know why there would be a reloc for these, but I've |
| 772 | seen it happen - DJ */ |
| 773 | if (s->output_section == &bfd_abs_section) |
| 774 | continue; |
| 775 | |
| 776 | if (s->output_section->vma == 0) |
| 777 | { |
| 778 | /* Huh? Shouldn't happen, but punt if it does */ |
| 779 | einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n", |
| 780 | s->output_section->name, s->output_section->index, |
| 781 | s->output_section->flags); |
| 782 | continue; |
| 783 | } |
| 784 | |
| 785 | symsize = bfd_get_symtab_upper_bound (b); |
| 786 | symbols = (asymbol **) xmalloc (symsize); |
| 787 | nsyms = bfd_canonicalize_symtab (b, symbols); |
| 788 | |
| 789 | relsize = bfd_get_reloc_upper_bound (b, s); |
| 790 | relocs = (arelent **) xmalloc ((size_t) relsize); |
| 791 | nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols); |
| 792 | |
| 793 | for (i = 0; i < nrelocs; i++) |
| 794 | { |
| 795 | if (!relocs[i]->howto->pc_relative |
| 796 | && relocs[i]->howto->type != pe_details->imagebase_reloc) |
| 797 | { |
| 798 | bfd_vma sym_vma; |
| 799 | struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr; |
| 800 | sym_vma = (relocs[i]->addend |
| 801 | + sym->value |
| 802 | + sym->section->vma |
| 803 | + sym->section->output_offset |
| 804 | + sym->section->output_section->vma); |
| 805 | reloc_data[total_relocs].vma = sec_vma + relocs[i]->address; |
| 806 | |
| 807 | #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift) |
| 808 | |
| 809 | switch BITS_AND_SHIFT (relocs[i]->howto->bitsize, |
| 810 | relocs[i]->howto->rightshift) |
| 811 | { |
| 812 | case BITS_AND_SHIFT (32, 0): |
| 813 | reloc_data[total_relocs].type = 3; |
| 814 | total_relocs++; |
| 815 | break; |
| 816 | case BITS_AND_SHIFT (16, 0): |
| 817 | reloc_data[total_relocs].type = 2; |
| 818 | total_relocs++; |
| 819 | break; |
| 820 | case BITS_AND_SHIFT (16, 16): |
| 821 | reloc_data[total_relocs].type = 4; |
| 822 | /* FIXME: we can't know the symbol's right value yet, |
| 823 | but we probably can safely assume that CE will relocate |
| 824 | us in 64k blocks, so leaving it zero is safe. */ |
| 825 | reloc_data[total_relocs].extra = 0; |
| 826 | total_relocs++; |
| 827 | break; |
| 828 | case BITS_AND_SHIFT (26, 2): |
| 829 | reloc_data[total_relocs].type = 5; |
| 830 | total_relocs++; |
| 831 | break; |
| 832 | default: |
| 833 | /* xgettext:c-format */ |
| 834 | einfo (_("%XError: %d-bit reloc in dll\n"), |
| 835 | relocs[i]->howto->bitsize); |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | free (relocs); |
| 841 | /* Warning: the allocated symbols are remembered in BFD and reused |
| 842 | later, so don't free them! */ |
| 843 | /* free (symbols); */ |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /* At this point, we have total_relocs relocation addresses in |
| 848 | reloc_addresses, which are all suitable for the .reloc section. |
| 849 | We must now create the new sections. */ |
| 850 | |
| 851 | qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort); |
| 852 | |
| 853 | for (i = 0; i < total_relocs; i++) |
| 854 | { |
| 855 | unsigned long this_page = (reloc_data[i].vma >> 12); |
| 856 | |
| 857 | if (this_page != sec_page) |
| 858 | { |
| 859 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */ |
| 860 | reloc_sz += 8; |
| 861 | sec_page = this_page; |
| 862 | } |
| 863 | |
| 864 | reloc_sz += 2; |
| 865 | |
| 866 | if (reloc_data[i].type == 4) |
| 867 | reloc_sz += 2; |
| 868 | } |
| 869 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */ |
| 870 | |
| 871 | reloc_d = (unsigned char *) xmalloc (reloc_sz); |
| 872 | |
| 873 | sec_page = (unsigned long) (-1); |
| 874 | reloc_sz = 0; |
| 875 | page_ptr = (unsigned long) (-1); |
| 876 | page_count = 0; |
| 877 | for (i = 0; i < total_relocs; i++) |
| 878 | { |
| 879 | unsigned long rva = reloc_data[i].vma - image_base; |
| 880 | unsigned long this_page = (rva & ~0xfff); |
| 881 | if (this_page != sec_page) |
| 882 | { |
| 883 | while (reloc_sz & 3) |
| 884 | reloc_d[reloc_sz++] = 0; |
| 885 | if (page_ptr != (unsigned long) (-1)) |
| 886 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4); |
| 887 | bfd_put_32 (abfd, this_page, reloc_d + reloc_sz); |
| 888 | page_ptr = reloc_sz; |
| 889 | reloc_sz += 8; |
| 890 | sec_page = this_page; |
| 891 | page_count = 0; |
| 892 | } |
| 893 | bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type<<12), |
| 894 | reloc_d + reloc_sz); |
| 895 | reloc_sz += 2; |
| 896 | if (reloc_data[i].type == 4) |
| 897 | { |
| 898 | bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz); |
| 899 | reloc_sz += 2; |
| 900 | } |
| 901 | page_count++; |
| 902 | } |
| 903 | while (reloc_sz & 3) |
| 904 | reloc_d[reloc_sz++] = 0; |
| 905 | if (page_ptr != (unsigned long) (-1)) |
| 906 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4); |
| 907 | while (reloc_sz < reloc_s->_raw_size) |
| 908 | reloc_d[reloc_sz++] = 0; |
| 909 | } |
| 910 | |
| 911 | /************************************************************************ |
| 912 | |
| 913 | Given the exiting def_file structure, print out a .DEF file that |
| 914 | corresponds to it. |
| 915 | |
| 916 | ************************************************************************/ |
| 917 | |
| 918 | static void |
| 919 | quoteput (s, f, needs_quotes) |
| 920 | char *s; |
| 921 | FILE * f; |
| 922 | int needs_quotes; |
| 923 | { |
| 924 | char *cp; |
| 925 | for (cp = s; *cp; cp++) |
| 926 | if (*cp == '\'' |
| 927 | || *cp == '"' |
| 928 | || *cp == '\\' |
| 929 | || isspace ((unsigned char) *cp) |
| 930 | || *cp == ',' |
| 931 | || *cp == ';') |
| 932 | needs_quotes = 1; |
| 933 | if (needs_quotes) |
| 934 | { |
| 935 | putc ('"', f); |
| 936 | while (*s) |
| 937 | { |
| 938 | if (*s == '"' || *s == '\\') |
| 939 | putc ('\\', f); |
| 940 | putc (*s, f); |
| 941 | s++; |
| 942 | } |
| 943 | putc ('"', f); |
| 944 | } |
| 945 | else |
| 946 | fputs (s, f); |
| 947 | } |
| 948 | |
| 949 | void |
| 950 | pe_dll_generate_def_file (pe_out_def_filename) |
| 951 | const char *pe_out_def_filename; |
| 952 | { |
| 953 | int i; |
| 954 | FILE *out = fopen (pe_out_def_filename, "w"); |
| 955 | if (out == NULL) |
| 956 | { |
| 957 | /* xgettext:c-format */ |
| 958 | einfo (_("%s: Can't open output def file %s\n"), |
| 959 | program_name, pe_out_def_filename); |
| 960 | } |
| 961 | |
| 962 | if (pe_def_file) |
| 963 | { |
| 964 | if (pe_def_file->name) |
| 965 | { |
| 966 | if (pe_def_file->is_dll) |
| 967 | fprintf (out, "LIBRARY "); |
| 968 | else |
| 969 | fprintf (out, "NAME "); |
| 970 | quoteput (pe_def_file->name, out, 1); |
| 971 | if (pe_data (output_bfd)->pe_opthdr.ImageBase) |
| 972 | fprintf (out, " BASE=0x%lx", |
| 973 | (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase); |
| 974 | fprintf (out, "\n"); |
| 975 | } |
| 976 | |
| 977 | if (pe_def_file->description) |
| 978 | { |
| 979 | fprintf (out, "DESCRIPTION "); |
| 980 | quoteput (pe_def_file->description, out, 1); |
| 981 | fprintf (out, "\n"); |
| 982 | } |
| 983 | |
| 984 | if (pe_def_file->version_minor != -1) |
| 985 | fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major, |
| 986 | pe_def_file->version_minor); |
| 987 | else if (pe_def_file->version_major != -1) |
| 988 | fprintf (out, "VERSION %d\n", pe_def_file->version_major); |
| 989 | |
| 990 | if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1) |
| 991 | fprintf (out, "\n"); |
| 992 | |
| 993 | if (pe_def_file->stack_commit != -1) |
| 994 | fprintf (out, "STACKSIZE 0x%x,0x%x\n", |
| 995 | pe_def_file->stack_reserve, pe_def_file->stack_commit); |
| 996 | else if (pe_def_file->stack_reserve != -1) |
| 997 | fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve); |
| 998 | if (pe_def_file->heap_commit != -1) |
| 999 | fprintf (out, "HEAPSIZE 0x%x,0x%x\n", |
| 1000 | pe_def_file->heap_reserve, pe_def_file->heap_commit); |
| 1001 | else if (pe_def_file->heap_reserve != -1) |
| 1002 | fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve); |
| 1003 | |
| 1004 | if (pe_def_file->num_section_defs > 0) |
| 1005 | { |
| 1006 | fprintf (out, "\nSECTIONS\n\n"); |
| 1007 | for (i = 0; i < pe_def_file->num_section_defs; i++) |
| 1008 | { |
| 1009 | fprintf (out, " "); |
| 1010 | quoteput (pe_def_file->section_defs[i].name, out, 0); |
| 1011 | if (pe_def_file->section_defs[i].class) |
| 1012 | { |
| 1013 | fprintf (out, " CLASS "); |
| 1014 | quoteput (pe_def_file->section_defs[i].class, out, 0); |
| 1015 | } |
| 1016 | if (pe_def_file->section_defs[i].flag_read) |
| 1017 | fprintf (out, " READ"); |
| 1018 | if (pe_def_file->section_defs[i].flag_write) |
| 1019 | fprintf (out, " WRITE"); |
| 1020 | if (pe_def_file->section_defs[i].flag_execute) |
| 1021 | fprintf (out, " EXECUTE"); |
| 1022 | if (pe_def_file->section_defs[i].flag_shared) |
| 1023 | fprintf (out, " SHARED"); |
| 1024 | fprintf (out, "\n"); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | if (pe_def_file->num_exports > 0) |
| 1029 | { |
| 1030 | fprintf (out, "\nEXPORTS\n\n"); |
| 1031 | for (i = 0; i < pe_def_file->num_exports; i++) |
| 1032 | { |
| 1033 | def_file_export *e = pe_def_file->exports + i; |
| 1034 | fprintf (out, " "); |
| 1035 | quoteput (e->name, out, 0); |
| 1036 | if (e->internal_name && strcmp (e->internal_name, e->name)) |
| 1037 | { |
| 1038 | fprintf (out, " = "); |
| 1039 | quoteput (e->internal_name, out, 0); |
| 1040 | } |
| 1041 | if (e->ordinal != -1) |
| 1042 | fprintf (out, " @%d", e->ordinal); |
| 1043 | if (e->flag_private) |
| 1044 | fprintf (out, " PRIVATE"); |
| 1045 | if (e->flag_constant) |
| 1046 | fprintf (out, " CONSTANT"); |
| 1047 | if (e->flag_noname) |
| 1048 | fprintf (out, " NONAME"); |
| 1049 | if (e->flag_data) |
| 1050 | fprintf (out, " DATA"); |
| 1051 | |
| 1052 | fprintf (out, "\n"); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | if (pe_def_file->num_imports > 0) |
| 1057 | { |
| 1058 | fprintf (out, "\nIMPORTS\n\n"); |
| 1059 | for (i = 0; i < pe_def_file->num_imports; i++) |
| 1060 | { |
| 1061 | def_file_import *im = pe_def_file->imports + i; |
| 1062 | fprintf (out, " "); |
| 1063 | if (im->internal_name |
| 1064 | && (!im->name || strcmp (im->internal_name, im->name))) |
| 1065 | { |
| 1066 | quoteput (im->internal_name, out, 0); |
| 1067 | fprintf (out, " = "); |
| 1068 | } |
| 1069 | quoteput (im->module->name, out, 0); |
| 1070 | fprintf (out, "."); |
| 1071 | if (im->name) |
| 1072 | quoteput (im->name, out, 0); |
| 1073 | else |
| 1074 | fprintf (out, "%d", im->ordinal); |
| 1075 | fprintf (out, "\n"); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | else |
| 1080 | fprintf (out, _("; no contents available\n")); |
| 1081 | |
| 1082 | if (fclose (out) == EOF) |
| 1083 | { |
| 1084 | /* xgettext:c-format */ |
| 1085 | einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | /************************************************************************ |
| 1090 | |
| 1091 | Generate the import library |
| 1092 | |
| 1093 | ************************************************************************/ |
| 1094 | |
| 1095 | static asymbol **symtab; |
| 1096 | static int symptr; |
| 1097 | static int tmp_seq; |
| 1098 | static const char *dll_filename; |
| 1099 | static char *dll_symname; |
| 1100 | |
| 1101 | #define UNDSEC (asection *) &bfd_und_section |
| 1102 | |
| 1103 | static asection * |
| 1104 | quick_section(abfd, name, flags, align) |
| 1105 | bfd *abfd; |
| 1106 | const char *name; |
| 1107 | int flags; |
| 1108 | int align; |
| 1109 | { |
| 1110 | asection *sec; |
| 1111 | asymbol *sym; |
| 1112 | |
| 1113 | sec = bfd_make_section_old_way (abfd, name); |
| 1114 | bfd_set_section_flags (abfd, sec, flags |
| 1115 | | SEC_ALLOC |
| 1116 | | SEC_LOAD |
| 1117 | | SEC_KEEP |
| 1118 | ); |
| 1119 | bfd_set_section_alignment (abfd, sec, align); |
| 1120 | /* remember to undo this before trying to link internally! */ |
| 1121 | sec->output_section = sec; |
| 1122 | |
| 1123 | sym = bfd_make_empty_symbol (abfd); |
| 1124 | symtab[symptr++] = sym; |
| 1125 | sym->name = sec->name; |
| 1126 | sym->section = sec; |
| 1127 | sym->flags = BSF_LOCAL; |
| 1128 | sym->value = 0; |
| 1129 | |
| 1130 | return sec; |
| 1131 | } |
| 1132 | |
| 1133 | static void |
| 1134 | quick_symbol (abfd, n1, n2, n3, sec, flags, addr) |
| 1135 | bfd *abfd; |
| 1136 | char *n1; |
| 1137 | char *n2; |
| 1138 | char *n3; |
| 1139 | asection *sec; |
| 1140 | int flags; |
| 1141 | int addr; |
| 1142 | { |
| 1143 | asymbol *sym; |
| 1144 | char *name = (char *) xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1); |
| 1145 | strcpy (name, n1); |
| 1146 | strcat (name, n2); |
| 1147 | strcat (name, n3); |
| 1148 | sym = bfd_make_empty_symbol (abfd); |
| 1149 | sym->name = name; |
| 1150 | sym->section = sec; |
| 1151 | sym->flags = flags; |
| 1152 | sym->value = addr; |
| 1153 | symtab[symptr++] = sym; |
| 1154 | } |
| 1155 | |
| 1156 | static arelent *reltab = 0; |
| 1157 | static int relcount = 0, relsize = 0; |
| 1158 | |
| 1159 | static void |
| 1160 | quick_reloc (abfd, address, which_howto, symidx) |
| 1161 | bfd *abfd; |
| 1162 | int address; |
| 1163 | int which_howto; |
| 1164 | int symidx; |
| 1165 | { |
| 1166 | if (relcount >= (relsize-1)) |
| 1167 | { |
| 1168 | relsize += 10; |
| 1169 | if (reltab) |
| 1170 | reltab = (arelent *) xrealloc (reltab, relsize * sizeof (arelent)); |
| 1171 | else |
| 1172 | reltab = (arelent *) xmalloc (relsize * sizeof (arelent)); |
| 1173 | } |
| 1174 | reltab[relcount].address = address; |
| 1175 | reltab[relcount].addend = 0; |
| 1176 | reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto); |
| 1177 | reltab[relcount].sym_ptr_ptr = symtab + symidx; |
| 1178 | relcount++; |
| 1179 | } |
| 1180 | |
| 1181 | static void |
| 1182 | save_relocs (asection *sec) |
| 1183 | { |
| 1184 | int i; |
| 1185 | sec->relocation = reltab; |
| 1186 | sec->reloc_count = relcount; |
| 1187 | sec->orelocation = (arelent **) xmalloc ((relcount+1) * sizeof (arelent *)); |
| 1188 | for (i=0; i<relcount; i++) |
| 1189 | sec->orelocation[i] = sec->relocation + i; |
| 1190 | sec->orelocation[relcount] = 0; |
| 1191 | sec->flags |= SEC_RELOC; |
| 1192 | reltab = 0; |
| 1193 | relcount = relsize = 0; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * .section .idata$2 |
| 1198 | * .global __head_my_dll |
| 1199 | * __head_my_dll: |
| 1200 | * .rva hname |
| 1201 | * .long 0 |
| 1202 | * .long 0 |
| 1203 | * .rva __my_dll_iname |
| 1204 | * .rva fthunk |
| 1205 | * |
| 1206 | * .section .idata$5 |
| 1207 | * .long 0 |
| 1208 | * fthunk: |
| 1209 | * |
| 1210 | * .section .idata$4 |
| 1211 | * .long 0 |
| 1212 | * hname: |
| 1213 | */ |
| 1214 | |
| 1215 | static bfd * |
| 1216 | make_head (parent) |
| 1217 | bfd *parent; |
| 1218 | { |
| 1219 | asection *id2, *id5, *id4; |
| 1220 | unsigned char *d2, *d5, *d4; |
| 1221 | char *oname; |
| 1222 | bfd *abfd; |
| 1223 | |
| 1224 | oname = (char *) xmalloc (20); |
| 1225 | sprintf (oname, "d%06d.o", tmp_seq); |
| 1226 | tmp_seq++; |
| 1227 | |
| 1228 | abfd = bfd_create (oname, parent); |
| 1229 | bfd_find_target (pe_details->object_target, abfd); |
| 1230 | bfd_make_writable (abfd); |
| 1231 | |
| 1232 | bfd_set_format (abfd, bfd_object); |
| 1233 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
| 1234 | |
| 1235 | symptr = 0; |
| 1236 | symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *)); |
| 1237 | id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2); |
| 1238 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2); |
| 1239 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); |
| 1240 | quick_symbol (abfd, U("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0); |
| 1241 | quick_symbol (abfd, U(""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0); |
| 1242 | |
| 1243 | /* OK, pay attention here. I got confused myself looking back at |
| 1244 | it. We create a four-byte section to mark the beginning of the |
| 1245 | list, and we include an offset of 4 in the section, so that the |
| 1246 | pointer to the list points to the *end* of this section, which is |
| 1247 | the start of the list of sections from other objects. */ |
| 1248 | |
| 1249 | bfd_set_section_size (abfd, id2, 20); |
| 1250 | d2 = (unsigned char *) xmalloc (20); |
| 1251 | id2->contents = d2; |
| 1252 | memset (d2, 0, 20); |
| 1253 | d2[0] = d2[16] = 4; /* reloc addend */ |
| 1254 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 2); |
| 1255 | quick_reloc (abfd, 12, BFD_RELOC_RVA, 4); |
| 1256 | quick_reloc (abfd, 16, BFD_RELOC_RVA, 1); |
| 1257 | save_relocs (id2); |
| 1258 | |
| 1259 | bfd_set_section_size (abfd, id5, 4); |
| 1260 | d5 = (unsigned char *) xmalloc (4); |
| 1261 | id5->contents = d5; |
| 1262 | memset (d5, 0, 4); |
| 1263 | |
| 1264 | bfd_set_section_size (abfd, id4, 4); |
| 1265 | d4 = (unsigned char *) xmalloc (4); |
| 1266 | id4->contents = d4; |
| 1267 | memset (d4, 0, 4); |
| 1268 | |
| 1269 | bfd_set_symtab (abfd, symtab, symptr); |
| 1270 | |
| 1271 | bfd_set_section_contents (abfd, id2, d2, 0, 20); |
| 1272 | bfd_set_section_contents (abfd, id5, d5, 0, 4); |
| 1273 | bfd_set_section_contents (abfd, id4, d4, 0, 4); |
| 1274 | |
| 1275 | bfd_make_readable (abfd); |
| 1276 | return abfd; |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * .section .idata$4 |
| 1281 | * .long 0 |
| 1282 | * .section .idata$5 |
| 1283 | * .long 0 |
| 1284 | * .section idata$7 |
| 1285 | * .global __my_dll_iname |
| 1286 | *__my_dll_iname: |
| 1287 | * .asciz "my.dll" |
| 1288 | */ |
| 1289 | |
| 1290 | static bfd * |
| 1291 | make_tail (parent) |
| 1292 | bfd *parent; |
| 1293 | { |
| 1294 | asection *id4, *id5, *id7; |
| 1295 | unsigned char *d4, *d5, *d7; |
| 1296 | int len; |
| 1297 | char *oname; |
| 1298 | bfd *abfd; |
| 1299 | |
| 1300 | oname = (char *) xmalloc (20); |
| 1301 | sprintf (oname, "d%06d.o", tmp_seq); |
| 1302 | tmp_seq++; |
| 1303 | |
| 1304 | abfd = bfd_create (oname, parent); |
| 1305 | bfd_find_target (pe_details->object_target, abfd); |
| 1306 | bfd_make_writable (abfd); |
| 1307 | |
| 1308 | bfd_set_format (abfd, bfd_object); |
| 1309 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
| 1310 | |
| 1311 | symptr = 0; |
| 1312 | symtab = (asymbol **) xmalloc (5 * sizeof (asymbol *)); |
| 1313 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); |
| 1314 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2); |
| 1315 | id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2); |
| 1316 | quick_symbol (abfd, U(""), dll_symname, "_iname", id7, BSF_GLOBAL, 0); |
| 1317 | |
| 1318 | bfd_set_section_size (abfd, id4, 4); |
| 1319 | d4 = (unsigned char *) xmalloc (4); |
| 1320 | id4->contents = d4; |
| 1321 | memset (d4, 0, 4); |
| 1322 | |
| 1323 | bfd_set_section_size (abfd, id5, 4); |
| 1324 | d5 = (unsigned char *) xmalloc (4); |
| 1325 | id5->contents = d5; |
| 1326 | memset (d5, 0, 4); |
| 1327 | |
| 1328 | len = strlen (dll_filename)+1; |
| 1329 | if (len & 1) |
| 1330 | len ++; |
| 1331 | bfd_set_section_size (abfd, id7, len); |
| 1332 | d7 = (unsigned char *) xmalloc (len); |
| 1333 | id7->contents = d7; |
| 1334 | strcpy (d7, dll_filename); |
| 1335 | |
| 1336 | bfd_set_symtab (abfd, symtab, symptr); |
| 1337 | |
| 1338 | bfd_set_section_contents (abfd, id4, d4, 0, 4); |
| 1339 | bfd_set_section_contents (abfd, id5, d5, 0, 4); |
| 1340 | bfd_set_section_contents (abfd, id7, d7, 0, len); |
| 1341 | |
| 1342 | bfd_make_readable (abfd); |
| 1343 | return abfd; |
| 1344 | } |
| 1345 | |
| 1346 | /* |
| 1347 | * .text |
| 1348 | * .global _function |
| 1349 | * .global ___imp_function |
| 1350 | * .global __imp__function |
| 1351 | *_function: |
| 1352 | * jmp *__imp__function: |
| 1353 | * |
| 1354 | * .section idata$7 |
| 1355 | * .long __head_my_dll |
| 1356 | * |
| 1357 | * .section .idata$5 |
| 1358 | *___imp_function: |
| 1359 | *__imp__function: |
| 1360 | *iat? |
| 1361 | * .section .idata$4 |
| 1362 | *iat? |
| 1363 | * .section .idata$6 |
| 1364 | *ID<ordinal>: |
| 1365 | * .short <hint> |
| 1366 | * .asciz "function" xlate? (add underscore, kill at) |
| 1367 | */ |
| 1368 | |
| 1369 | static unsigned char jmp_ix86_bytes[] = { |
| 1370 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 |
| 1371 | }; |
| 1372 | |
| 1373 | /* |
| 1374 | *_function: |
| 1375 | * mov.l ip+8,r0 |
| 1376 | * mov.l @r0,r0 |
| 1377 | * jmp @r0 |
| 1378 | * nop |
| 1379 | * .dw __imp_function |
| 1380 | */ |
| 1381 | |
| 1382 | static unsigned char jmp_sh_bytes[] = { |
| 1383 | 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 1384 | }; |
| 1385 | |
| 1386 | /* |
| 1387 | *_function: |
| 1388 | * lui $t0,<high:__imp_function> |
| 1389 | * lw $t0,<low:__imp_function> |
| 1390 | * jr $t0 |
| 1391 | * nop |
| 1392 | */ |
| 1393 | |
| 1394 | static unsigned char jmp_mips_bytes[] = { |
| 1395 | 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d, |
| 1396 | 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 |
| 1397 | }; |
| 1398 | |
| 1399 | static bfd * |
| 1400 | make_one (exp, parent) |
| 1401 | def_file_export *exp; |
| 1402 | bfd *parent; |
| 1403 | { |
| 1404 | asection *tx, *id7, *id5, *id4, *id6; |
| 1405 | unsigned char *td, *d7, *d5, *d4, *d6; |
| 1406 | int len; |
| 1407 | char *oname; |
| 1408 | bfd *abfd; |
| 1409 | unsigned char *jmp_bytes; |
| 1410 | int jmp_byte_count; |
| 1411 | |
| 1412 | switch (pe_details->pe_arch) |
| 1413 | { |
| 1414 | case PE_ARCH_i386: |
| 1415 | jmp_bytes = jmp_ix86_bytes; |
| 1416 | jmp_byte_count = sizeof (jmp_ix86_bytes); |
| 1417 | break; |
| 1418 | case PE_ARCH_sh: |
| 1419 | jmp_bytes = jmp_sh_bytes; |
| 1420 | jmp_byte_count = sizeof (jmp_sh_bytes); |
| 1421 | break; |
| 1422 | case PE_ARCH_mips: |
| 1423 | jmp_bytes = jmp_mips_bytes; |
| 1424 | jmp_byte_count = sizeof (jmp_mips_bytes); |
| 1425 | break; |
| 1426 | } |
| 1427 | |
| 1428 | oname = (char *) xmalloc (20); |
| 1429 | sprintf (oname, "d%06d.o", tmp_seq); |
| 1430 | tmp_seq++; |
| 1431 | |
| 1432 | abfd = bfd_create (oname, parent); |
| 1433 | bfd_find_target (pe_details->object_target, abfd); |
| 1434 | bfd_make_writable (abfd); |
| 1435 | |
| 1436 | bfd_set_format (abfd, bfd_object); |
| 1437 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
| 1438 | |
| 1439 | symptr = 0; |
| 1440 | symtab = (asymbol **) xmalloc (10 * sizeof (asymbol *)); |
| 1441 | tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2); |
| 1442 | id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2); |
| 1443 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2); |
| 1444 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); |
| 1445 | id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2); |
| 1446 | if (! exp->flag_data) |
| 1447 | quick_symbol (abfd, U(""), exp->internal_name, "", tx, BSF_GLOBAL, 0); |
| 1448 | quick_symbol (abfd, U("_head_"), dll_symname, "", UNDSEC, BSF_GLOBAL, 0); |
| 1449 | quick_symbol (abfd, U("__imp_"), exp->internal_name, "", id5, BSF_GLOBAL, 0); |
| 1450 | if (pe_dll_compat_implib) |
| 1451 | quick_symbol (abfd, U("__imp_"), exp->internal_name, "", |
| 1452 | id5, BSF_GLOBAL, 0); |
| 1453 | |
| 1454 | bfd_set_section_size (abfd, tx, jmp_byte_count); |
| 1455 | td = (unsigned char *) xmalloc (jmp_byte_count); |
| 1456 | tx->contents = td; |
| 1457 | memcpy (td, jmp_bytes, jmp_byte_count); |
| 1458 | switch (pe_details->pe_arch) |
| 1459 | { |
| 1460 | case PE_ARCH_i386: |
| 1461 | quick_reloc (abfd, 2, BFD_RELOC_32, 2); |
| 1462 | break; |
| 1463 | case PE_ARCH_sh: |
| 1464 | quick_reloc (abfd, 8, BFD_RELOC_32, 2); |
| 1465 | break; |
| 1466 | case PE_ARCH_mips: |
| 1467 | quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2); |
| 1468 | quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */ |
| 1469 | quick_reloc (abfd, 4, BFD_RELOC_LO16, 2); |
| 1470 | break; |
| 1471 | } |
| 1472 | save_relocs (tx); |
| 1473 | |
| 1474 | bfd_set_section_size (abfd, id7, 4); |
| 1475 | d7 = (unsigned char *) xmalloc (4); |
| 1476 | id7->contents = d7; |
| 1477 | memset (d7, 0, 4); |
| 1478 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 6); |
| 1479 | save_relocs (id7); |
| 1480 | |
| 1481 | bfd_set_section_size (abfd, id5, 4); |
| 1482 | d5 = (unsigned char *) xmalloc (4); |
| 1483 | id5->contents = d5; |
| 1484 | memset (d5, 0, 4); |
| 1485 | if (exp->flag_noname) |
| 1486 | { |
| 1487 | d5[0] = exp->ordinal; |
| 1488 | d5[1] = exp->ordinal >> 8; |
| 1489 | d5[3] = 0x80; |
| 1490 | } |
| 1491 | else |
| 1492 | { |
| 1493 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 4); |
| 1494 | save_relocs (id5); |
| 1495 | } |
| 1496 | |
| 1497 | bfd_set_section_size (abfd, id4, 4); |
| 1498 | d4 = (unsigned char *) xmalloc (4); |
| 1499 | id4->contents = d4; |
| 1500 | memset (d4, 0, 4); |
| 1501 | if (exp->flag_noname) |
| 1502 | { |
| 1503 | d5[0] = exp->ordinal; |
| 1504 | d5[1] = exp->ordinal >> 8; |
| 1505 | d5[3] = 0x80; |
| 1506 | } |
| 1507 | else |
| 1508 | { |
| 1509 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 4); |
| 1510 | save_relocs (id4); |
| 1511 | } |
| 1512 | |
| 1513 | if (exp->flag_noname) |
| 1514 | { |
| 1515 | len = 0; |
| 1516 | bfd_set_section_size (abfd, id6, 0); |
| 1517 | } |
| 1518 | else |
| 1519 | { |
| 1520 | len = strlen (exp->name) + 3; |
| 1521 | if (len & 1) |
| 1522 | len++; |
| 1523 | bfd_set_section_size (abfd, id6, len); |
| 1524 | d6 = (unsigned char *) xmalloc (len); |
| 1525 | id6->contents = d6; |
| 1526 | memset (d6, 0, len); |
| 1527 | d6[0] = exp->hint & 0xff; |
| 1528 | d6[1] = exp->hint >> 8; |
| 1529 | strcpy (d6+2, exp->name); |
| 1530 | } |
| 1531 | |
| 1532 | bfd_set_symtab (abfd, symtab, symptr); |
| 1533 | |
| 1534 | bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count); |
| 1535 | bfd_set_section_contents (abfd, id7, d7, 0, 4); |
| 1536 | bfd_set_section_contents (abfd, id5, d5, 0, 4); |
| 1537 | bfd_set_section_contents (abfd, id4, d4, 0, 4); |
| 1538 | if (!exp->flag_noname) |
| 1539 | bfd_set_section_contents (abfd, id6, d6, 0, len); |
| 1540 | |
| 1541 | bfd_make_readable (abfd); |
| 1542 | return abfd; |
| 1543 | } |
| 1544 | |
| 1545 | void |
| 1546 | pe_dll_generate_implib (def, impfilename) |
| 1547 | def_file *def; |
| 1548 | const char *impfilename; |
| 1549 | { |
| 1550 | int i; |
| 1551 | bfd *ar_head; |
| 1552 | bfd *ar_tail; |
| 1553 | bfd *outarch; |
| 1554 | bfd *head = 0; |
| 1555 | |
| 1556 | dll_filename = (def->name) ? def->name : dll_name; |
| 1557 | dll_symname = xstrdup (dll_filename); |
| 1558 | for (i=0; dll_symname[i]; i++) |
| 1559 | if (!isalnum ((unsigned char) dll_symname[i])) |
| 1560 | dll_symname[i] = '_'; |
| 1561 | |
| 1562 | unlink (impfilename); |
| 1563 | |
| 1564 | outarch = bfd_openw (impfilename, 0); |
| 1565 | |
| 1566 | if (!outarch) |
| 1567 | { |
| 1568 | /* xgettext:c-format */ |
| 1569 | einfo (_("%XCan't open .lib file: %s\n"), impfilename); |
| 1570 | return; |
| 1571 | } |
| 1572 | |
| 1573 | /* xgettext:c-format */ |
| 1574 | einfo (_("Creating library file: %s\n"), impfilename); |
| 1575 | |
| 1576 | bfd_set_format (outarch, bfd_archive); |
| 1577 | outarch->has_armap = 1; |
| 1578 | |
| 1579 | /* Work out a reasonable size of things to put onto one line. */ |
| 1580 | |
| 1581 | ar_head = make_head (outarch); |
| 1582 | |
| 1583 | for (i = 0; i<def->num_exports; i++) |
| 1584 | { |
| 1585 | /* The import library doesn't know about the internal name */ |
| 1586 | char *internal = def->exports[i].internal_name; |
| 1587 | bfd *n; |
| 1588 | def->exports[i].internal_name = def->exports[i].name; |
| 1589 | n = make_one (def->exports+i, outarch); |
| 1590 | n->next = head; |
| 1591 | head = n; |
| 1592 | def->exports[i].internal_name = internal; |
| 1593 | } |
| 1594 | |
| 1595 | ar_tail = make_tail (outarch); |
| 1596 | |
| 1597 | if (ar_head == NULL || ar_tail == NULL) |
| 1598 | return; |
| 1599 | |
| 1600 | /* Now stick them all into the archive */ |
| 1601 | |
| 1602 | ar_head->next = head; |
| 1603 | ar_tail->next = ar_head; |
| 1604 | head = ar_tail; |
| 1605 | |
| 1606 | if (! bfd_set_archive_head (outarch, head)) |
| 1607 | einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ())); |
| 1608 | |
| 1609 | if (! bfd_close (outarch)) |
| 1610 | einfo ("%Xbfd_close %s: %s\n", impfilename, bfd_errmsg (bfd_get_error ())); |
| 1611 | |
| 1612 | while (head != NULL) |
| 1613 | { |
| 1614 | bfd *n = head->next; |
| 1615 | bfd_close (head); |
| 1616 | head = n; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | static void |
| 1621 | add_bfd_to_link (abfd, name, link_info) |
| 1622 | bfd *abfd; |
| 1623 | char *name; |
| 1624 | struct bfd_link_info *link_info; |
| 1625 | { |
| 1626 | lang_input_statement_type *fake_file; |
| 1627 | fake_file = lang_add_input_file (name, |
| 1628 | lang_input_file_is_fake_enum, |
| 1629 | NULL); |
| 1630 | fake_file->the_bfd = abfd; |
| 1631 | ldlang_add_file (fake_file); |
| 1632 | if (!bfd_link_add_symbols (abfd, link_info)) |
| 1633 | einfo ("%Xaddsym %s: %s\n", name, bfd_errmsg (bfd_get_error ())); |
| 1634 | } |
| 1635 | |
| 1636 | void |
| 1637 | pe_process_import_defs (output_bfd, link_info) |
| 1638 | bfd *output_bfd; |
| 1639 | struct bfd_link_info *link_info; |
| 1640 | { |
| 1641 | def_file_module *module; |
| 1642 | pe_dll_id_target(bfd_get_target (output_bfd)); |
| 1643 | |
| 1644 | if (!pe_def_file) |
| 1645 | return; |
| 1646 | |
| 1647 | for (module = pe_def_file->modules; module; module = module->next) |
| 1648 | { |
| 1649 | int i, do_this_dll; |
| 1650 | |
| 1651 | dll_filename = module->name; |
| 1652 | dll_symname = xstrdup (module->name); |
| 1653 | for (i=0; dll_symname[i]; i++) |
| 1654 | if (!isalnum (dll_symname[i])) |
| 1655 | dll_symname[i] = '_'; |
| 1656 | |
| 1657 | do_this_dll = 0; |
| 1658 | |
| 1659 | for (i=0; i<pe_def_file->num_imports; i++) |
| 1660 | if (pe_def_file->imports[i].module == module) |
| 1661 | { |
| 1662 | def_file_export exp; |
| 1663 | struct bfd_link_hash_entry *blhe; |
| 1664 | |
| 1665 | /* see if we need this import */ |
| 1666 | char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2); |
| 1667 | sprintf (name, "%s%s", U(""), pe_def_file->imports[i].internal_name); |
| 1668 | blhe = bfd_link_hash_lookup (link_info->hash, name, |
| 1669 | false, false, false); |
| 1670 | free (name); |
| 1671 | if (blhe && blhe->type == bfd_link_hash_undefined) |
| 1672 | { |
| 1673 | bfd *one; |
| 1674 | /* we do */ |
| 1675 | if (!do_this_dll) |
| 1676 | { |
| 1677 | bfd *ar_head = make_head (output_bfd); |
| 1678 | add_bfd_to_link (ar_head, ar_head->filename, link_info); |
| 1679 | do_this_dll = 1; |
| 1680 | } |
| 1681 | exp.internal_name = pe_def_file->imports[i].internal_name; |
| 1682 | exp.name = pe_def_file->imports[i].name; |
| 1683 | exp.ordinal = pe_def_file->imports[i].ordinal; |
| 1684 | exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0; |
| 1685 | exp.flag_private = 0; |
| 1686 | exp.flag_constant = 0; |
| 1687 | exp.flag_data = 0; |
| 1688 | exp.flag_noname = exp.name ? 0 : 1; |
| 1689 | one = make_one (&exp, output_bfd); |
| 1690 | add_bfd_to_link (one, one->filename, link_info); |
| 1691 | } |
| 1692 | } |
| 1693 | if (do_this_dll) |
| 1694 | { |
| 1695 | bfd *ar_tail = make_tail (output_bfd); |
| 1696 | add_bfd_to_link (ar_tail, ar_tail->filename, link_info); |
| 1697 | } |
| 1698 | |
| 1699 | free (dll_symname); |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | /************************************************************************ |
| 1704 | |
| 1705 | We were handed a *.DLL file. Parse it and turn it into a set of |
| 1706 | IMPORTS directives in the def file. Return true if the file was |
| 1707 | handled, false if not. |
| 1708 | |
| 1709 | ************************************************************************/ |
| 1710 | |
| 1711 | static unsigned int |
| 1712 | pe_get16 (abfd, where) |
| 1713 | bfd *abfd; |
| 1714 | int where; |
| 1715 | { |
| 1716 | unsigned char b[2]; |
| 1717 | bfd_seek (abfd, where, SEEK_SET); |
| 1718 | bfd_read (b, 1, 2, abfd); |
| 1719 | return b[0] + (b[1]<<8); |
| 1720 | } |
| 1721 | |
| 1722 | static unsigned int |
| 1723 | pe_get32 (abfd, where) |
| 1724 | bfd *abfd; |
| 1725 | int where; |
| 1726 | { |
| 1727 | unsigned char b[4]; |
| 1728 | bfd_seek (abfd, where, SEEK_SET); |
| 1729 | bfd_read (b, 1, 4, abfd); |
| 1730 | return b[0] + (b[1]<<8) + (b[2]<<16) + (b[3]<<24); |
| 1731 | } |
| 1732 | |
| 1733 | #if 0 /* This is not currently used. */ |
| 1734 | |
| 1735 | static unsigned int |
| 1736 | pe_as16 (ptr) |
| 1737 | void *ptr; |
| 1738 | { |
| 1739 | unsigned char *b = ptr; |
| 1740 | return b[0] + (b[1]<<8); |
| 1741 | } |
| 1742 | |
| 1743 | #endif |
| 1744 | |
| 1745 | static unsigned int |
| 1746 | pe_as32 (ptr) |
| 1747 | void *ptr; |
| 1748 | { |
| 1749 | unsigned char *b = ptr; |
| 1750 | return b[0] + (b[1]<<8) + (b[2]<<16) + (b[3]<<24); |
| 1751 | } |
| 1752 | |
| 1753 | boolean |
| 1754 | pe_implied_import_dll (filename) |
| 1755 | const char *filename; |
| 1756 | { |
| 1757 | bfd *dll; |
| 1758 | unsigned long pe_header_offset, opthdr_ofs, num_entries, i; |
| 1759 | unsigned long export_rva, export_size, nsections, secptr, expptr; |
| 1760 | unsigned char *expdata, *erva; |
| 1761 | unsigned long name_rvas, ordinals, nexp, ordbase; |
| 1762 | const char *dll_name; |
| 1763 | |
| 1764 | /* No, I can't use bfd here. kernel32.dll puts its export table in |
| 1765 | the middle of the .rdata section. */ |
| 1766 | |
| 1767 | dll = bfd_openr (filename, pe_details->target_name); |
| 1768 | if (!dll) |
| 1769 | { |
| 1770 | einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ())); |
| 1771 | return false; |
| 1772 | } |
| 1773 | /* PEI dlls seem to be bfd_objects */ |
| 1774 | if (!bfd_check_format (dll, bfd_object)) |
| 1775 | { |
| 1776 | einfo ("%X%s: this doesn't appear to be a DLL\n", filename); |
| 1777 | return false; |
| 1778 | } |
| 1779 | |
| 1780 | dll_name = filename; |
| 1781 | for (i=0; filename[i]; i++) |
| 1782 | if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':') |
| 1783 | dll_name = filename + i + 1; |
| 1784 | |
| 1785 | pe_header_offset = pe_get32 (dll, 0x3c); |
| 1786 | opthdr_ofs = pe_header_offset + 4 + 20; |
| 1787 | num_entries = pe_get32 (dll, opthdr_ofs + 92); |
| 1788 | if (num_entries < 1) /* no exports */ |
| 1789 | return false; |
| 1790 | export_rva = pe_get32 (dll, opthdr_ofs + 96); |
| 1791 | export_size = pe_get32 (dll, opthdr_ofs + 100); |
| 1792 | nsections = pe_get16 (dll, pe_header_offset + 4 + 2); |
| 1793 | secptr = (pe_header_offset + 4 + 20 + |
| 1794 | pe_get16 (dll, pe_header_offset + 4 + 16)); |
| 1795 | expptr = 0; |
| 1796 | for (i=0; i<nsections; i++) |
| 1797 | { |
| 1798 | char sname[8]; |
| 1799 | unsigned long secptr1 = secptr + 40 * i; |
| 1800 | unsigned long vaddr = pe_get32 (dll, secptr1 + 12); |
| 1801 | unsigned long vsize = pe_get32 (dll, secptr1 + 16); |
| 1802 | unsigned long fptr = pe_get32 (dll, secptr1 + 20); |
| 1803 | bfd_seek(dll, secptr1, SEEK_SET); |
| 1804 | bfd_read(sname, 1, 8, dll); |
| 1805 | if (vaddr <= export_rva && vaddr+vsize > export_rva) |
| 1806 | { |
| 1807 | expptr = fptr + (export_rva - vaddr); |
| 1808 | if (export_rva + export_size > vaddr + vsize) |
| 1809 | export_size = vsize - (export_rva - vaddr); |
| 1810 | break; |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | expdata = (unsigned char *) xmalloc (export_size); |
| 1815 | bfd_seek (dll, expptr, SEEK_SET); |
| 1816 | bfd_read (expdata, 1, export_size, dll); |
| 1817 | erva = expdata - export_rva; |
| 1818 | |
| 1819 | if (pe_def_file == 0) |
| 1820 | pe_def_file = def_file_empty(); |
| 1821 | |
| 1822 | nexp = pe_as32 (expdata+24); |
| 1823 | name_rvas = pe_as32 (expdata+32); |
| 1824 | ordinals = pe_as32 (expdata+36); |
| 1825 | ordbase = pe_as32 (expdata+16); |
| 1826 | for (i=0; i<nexp; i++) |
| 1827 | { |
| 1828 | unsigned long name_rva = pe_as32 (erva+name_rvas+i*4); |
| 1829 | def_file_import *imp; |
| 1830 | imp = def_file_add_import (pe_def_file, erva+name_rva, dll_name, |
| 1831 | i, 0); |
| 1832 | } |
| 1833 | |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
| 1837 | /************************************************************************ |
| 1838 | |
| 1839 | These are the main functions, called from the emulation. The first |
| 1840 | is called after the bfds are read, so we can guess at how much space |
| 1841 | we need. The second is called after everything is placed, so we |
| 1842 | can put the right values in place. |
| 1843 | |
| 1844 | ************************************************************************/ |
| 1845 | |
| 1846 | void |
| 1847 | pe_dll_build_sections (abfd, info) |
| 1848 | bfd *abfd; |
| 1849 | struct bfd_link_info *info; |
| 1850 | { |
| 1851 | pe_dll_id_target (bfd_get_target (abfd)); |
| 1852 | process_def_file (abfd, info); |
| 1853 | |
| 1854 | generate_edata (abfd, info); |
| 1855 | build_filler_bfd (1); |
| 1856 | } |
| 1857 | |
| 1858 | void |
| 1859 | pe_exe_build_sections (abfd, info) |
| 1860 | bfd *abfd; |
| 1861 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
| 1862 | { |
| 1863 | pe_dll_id_target (bfd_get_target (abfd)); |
| 1864 | build_filler_bfd (0); |
| 1865 | } |
| 1866 | |
| 1867 | void |
| 1868 | pe_dll_fill_sections (abfd, info) |
| 1869 | bfd *abfd; |
| 1870 | struct bfd_link_info *info; |
| 1871 | { |
| 1872 | pe_dll_id_target (bfd_get_target (abfd)); |
| 1873 | image_base = pe_data (abfd)->pe_opthdr.ImageBase; |
| 1874 | |
| 1875 | generate_reloc (abfd, info); |
| 1876 | if (reloc_sz > 0) |
| 1877 | { |
| 1878 | bfd_set_section_size (filler_bfd, reloc_s, reloc_sz); |
| 1879 | |
| 1880 | /* Resize the sections. */ |
| 1881 | lang_size_sections (stat_ptr->head, abs_output_section, |
| 1882 | &stat_ptr->head, 0, (bfd_vma) 0, false); |
| 1883 | |
| 1884 | /* Redo special stuff. */ |
| 1885 | ldemul_after_allocation (); |
| 1886 | |
| 1887 | /* Do the assignments again. */ |
| 1888 | lang_do_assignments (stat_ptr->head, |
| 1889 | abs_output_section, |
| 1890 | (fill_type) 0, (bfd_vma) 0); |
| 1891 | } |
| 1892 | |
| 1893 | fill_edata (abfd, info); |
| 1894 | |
| 1895 | pe_data (abfd)->dll = 1; |
| 1896 | |
| 1897 | edata_s->contents = edata_d; |
| 1898 | reloc_s->contents = reloc_d; |
| 1899 | } |
| 1900 | |
| 1901 | void |
| 1902 | pe_exe_fill_sections (abfd, info) |
| 1903 | bfd *abfd; |
| 1904 | struct bfd_link_info *info; |
| 1905 | { |
| 1906 | pe_dll_id_target (bfd_get_target (abfd)); |
| 1907 | image_base = pe_data (abfd)->pe_opthdr.ImageBase; |
| 1908 | |
| 1909 | generate_reloc (abfd, info); |
| 1910 | if (reloc_sz > 0) |
| 1911 | { |
| 1912 | bfd_set_section_size (filler_bfd, reloc_s, reloc_sz); |
| 1913 | |
| 1914 | /* Resize the sections. */ |
| 1915 | lang_size_sections (stat_ptr->head, abs_output_section, |
| 1916 | &stat_ptr->head, 0, (bfd_vma) 0, false); |
| 1917 | |
| 1918 | /* Redo special stuff. */ |
| 1919 | ldemul_after_allocation (); |
| 1920 | |
| 1921 | /* Do the assignments again. */ |
| 1922 | lang_do_assignments (stat_ptr->head, |
| 1923 | abs_output_section, |
| 1924 | (fill_type) 0, (bfd_vma) 0); |
| 1925 | } |
| 1926 | reloc_s->contents = reloc_d; |
| 1927 | } |