| 1 | /* Routines to help build PEI-format DLLs (Win32 etc) |
| 2 | Copyright 1998, 1999, 2000, 2001, 2002 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 | #include "safe-ctype.h" |
| 27 | |
| 28 | #include <time.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 "ldfile.h" |
| 38 | #include "ldemul.h" |
| 39 | #include "coff/internal.h" |
| 40 | #include "../bfd/libcoff.h" |
| 41 | #include "deffile.h" |
| 42 | #include "pe-dll.h" |
| 43 | |
| 44 | /* This file turns a regular Windows PE image into a DLL. Because of |
| 45 | the complexity of this operation, it has been broken down into a |
| 46 | number of separate modules which are all called by the main function |
| 47 | at the end of this file. This function is not re-entrant and is |
| 48 | normally only called once, so static variables are used to reduce |
| 49 | the number of parameters and return values required. |
| 50 | |
| 51 | See also: ld/emultempl/pe.em. */ |
| 52 | |
| 53 | /* Auto-import feature by Paul Sokolovsky |
| 54 | |
| 55 | Quick facts: |
| 56 | |
| 57 | 1. With this feature on, DLL clients can import variables from DLL |
| 58 | without any concern from their side (for example, without any source |
| 59 | code modifications). |
| 60 | |
| 61 | 2. This is done completely in bounds of the PE specification (to be fair, |
| 62 | there's a place where it pokes nose out of, but in practise it works). |
| 63 | So, resulting module can be used with any other PE compiler/linker. |
| 64 | |
| 65 | 3. Auto-import is fully compatible with standard import method and they |
| 66 | can be mixed together. |
| 67 | |
| 68 | 4. Overheads: space: 8 bytes per imported symbol, plus 20 for each |
| 69 | reference to it; load time: negligible; virtual/physical memory: should be |
| 70 | less than effect of DLL relocation, and I sincerely hope it doesn't affect |
| 71 | DLL sharability (too much). |
| 72 | |
| 73 | Idea |
| 74 | |
| 75 | The obvious and only way to get rid of dllimport insanity is to make client |
| 76 | access variable directly in the DLL, bypassing extra dereference. I.e., |
| 77 | whenever client contains someting like |
| 78 | |
| 79 | mov dll_var,%eax, |
| 80 | |
| 81 | address of dll_var in the command should be relocated to point into loaded |
| 82 | DLL. The aim is to make OS loader do so, and than make ld help with that. |
| 83 | Import section of PE made following way: there's a vector of structures |
| 84 | each describing imports from particular DLL. Each such structure points |
| 85 | to two other parellel vectors: one holding imported names, and one which |
| 86 | will hold address of corresponding imported name. So, the solution is |
| 87 | de-vectorize these structures, making import locations be sparse and |
| 88 | pointing directly into code. Before continuing, it is worth a note that, |
| 89 | while authors strives to make PE act ELF-like, there're some other people |
| 90 | make ELF act PE-like: elfvector, ;-) . |
| 91 | |
| 92 | Implementation |
| 93 | |
| 94 | For each reference of data symbol to be imported from DLL (to set of which |
| 95 | belong symbols with name <sym>, if __imp_<sym> is found in implib), the |
| 96 | import fixup entry is generated. That entry is of type |
| 97 | IMAGE_IMPORT_DESCRIPTOR and stored in .idata$3 subsection. Each |
| 98 | fixup entry contains pointer to symbol's address within .text section |
| 99 | (marked with __fuN_<sym> symbol, where N is integer), pointer to DLL name |
| 100 | (so, DLL name is referenced by multiple entries), and pointer to symbol |
| 101 | name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>) |
| 102 | pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly |
| 103 | containing imported name. Here comes that "om the edge" problem mentioned |
| 104 | above: PE specification rambles that name vector (OriginalFirstThunk) |
| 105 | should run in parallel with addresses vector (FirstThunk), i.e. that they |
| 106 | should have same number of elements and terminated with zero. We violate |
| 107 | this, since FirstThunk points directly into machine code. But in practise, |
| 108 | OS loader implemented the sane way: it goes thru OriginalFirstThunk and |
| 109 | puts addresses to FirstThunk, not something else. It once again should be |
| 110 | noted that dll and symbol name structures are reused across fixup entries |
| 111 | and should be there anyway to support standard import stuff, so sustained |
| 112 | overhead is 20 bytes per reference. Other question is whether having several |
| 113 | IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, it is |
| 114 | done even by native compiler/linker (libth32's functions are in fact reside |
| 115 | in windows9x kernel32.dll, so if you use it, you have two |
| 116 | IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether |
| 117 | referencing the same PE structures several times is valid. The answer is why |
| 118 | not, prohibitting that (detecting violation) would require more work on |
| 119 | behalf of loader than not doing it. |
| 120 | |
| 121 | See also: ld/emultempl/pe.em. */ |
| 122 | |
| 123 | static void |
| 124 | add_bfd_to_link PARAMS ((bfd *, const char *, struct bfd_link_info *)); |
| 125 | |
| 126 | /* For emultempl/pe.em. */ |
| 127 | |
| 128 | def_file * pe_def_file = 0; |
| 129 | int pe_dll_export_everything = 0; |
| 130 | int pe_dll_do_default_excludes = 1; |
| 131 | int pe_dll_kill_ats = 0; |
| 132 | int pe_dll_stdcall_aliases = 0; |
| 133 | int pe_dll_warn_dup_exports = 0; |
| 134 | int pe_dll_compat_implib = 0; |
| 135 | int pe_dll_extra_pe_debug = 0; |
| 136 | |
| 137 | /* Static variables and types. */ |
| 138 | |
| 139 | static bfd_vma image_base; |
| 140 | static bfd *filler_bfd; |
| 141 | static struct sec *edata_s, *reloc_s; |
| 142 | static unsigned char *edata_d, *reloc_d; |
| 143 | static size_t edata_sz, reloc_sz; |
| 144 | |
| 145 | typedef struct |
| 146 | { |
| 147 | char *target_name; |
| 148 | char *object_target; |
| 149 | unsigned int imagebase_reloc; |
| 150 | int pe_arch; |
| 151 | int bfd_arch; |
| 152 | int underscored; |
| 153 | } |
| 154 | pe_details_type; |
| 155 | |
| 156 | typedef struct |
| 157 | { |
| 158 | char *name; |
| 159 | int len; |
| 160 | } |
| 161 | autofilter_entry_type; |
| 162 | |
| 163 | #define PE_ARCH_i386 1 |
| 164 | #define PE_ARCH_sh 2 |
| 165 | #define PE_ARCH_mips 3 |
| 166 | #define PE_ARCH_arm 4 |
| 167 | #define PE_ARCH_arm_epoc 5 |
| 168 | |
| 169 | static pe_details_type pe_detail_list[] = |
| 170 | { |
| 171 | { |
| 172 | "pei-i386", |
| 173 | "pe-i386", |
| 174 | 7 /* R_IMAGEBASE */, |
| 175 | PE_ARCH_i386, |
| 176 | bfd_arch_i386, |
| 177 | 1 |
| 178 | }, |
| 179 | { |
| 180 | "pei-shl", |
| 181 | "pe-shl", |
| 182 | 16 /* R_SH_IMAGEBASE */, |
| 183 | PE_ARCH_sh, |
| 184 | bfd_arch_sh, |
| 185 | 1 |
| 186 | }, |
| 187 | { |
| 188 | "pei-mips", |
| 189 | "pe-mips", |
| 190 | 34 /* MIPS_R_RVA */, |
| 191 | PE_ARCH_mips, |
| 192 | bfd_arch_mips, |
| 193 | 0 |
| 194 | }, |
| 195 | { |
| 196 | "pei-arm-little", |
| 197 | "pe-arm-little", |
| 198 | 11 /* ARM_RVA32 */, |
| 199 | PE_ARCH_arm, |
| 200 | bfd_arch_arm, |
| 201 | 0 |
| 202 | }, |
| 203 | { |
| 204 | "epoc-pei-arm-little", |
| 205 | "epoc-pe-arm-little", |
| 206 | 11 /* ARM_RVA32 */, |
| 207 | PE_ARCH_arm_epoc, |
| 208 | bfd_arch_arm, |
| 209 | 0 |
| 210 | }, |
| 211 | { NULL, NULL, 0, 0, 0, 0 } |
| 212 | }; |
| 213 | |
| 214 | static pe_details_type *pe_details; |
| 215 | |
| 216 | static autofilter_entry_type autofilter_symbollist[] = |
| 217 | { |
| 218 | { "DllMain@12", 10 }, |
| 219 | { "DllEntryPoint@0", 15 }, |
| 220 | { "DllMainCRTStartup@12", 20 }, |
| 221 | { "_cygwin_dll_entry@12", 20 }, |
| 222 | { "_cygwin_crt0_common@8", 21 }, |
| 223 | { "_cygwin_noncygwin_dll_entry@12", 30 }, |
| 224 | { "impure_ptr", 10 }, |
| 225 | { NULL, 0 } |
| 226 | }; |
| 227 | |
| 228 | /* Do not specify library suffix explicitly, to allow for dllized versions. */ |
| 229 | static autofilter_entry_type autofilter_liblist[] = |
| 230 | { |
| 231 | { "libgcc.", 7 }, |
| 232 | { "libstdc++.", 10 }, |
| 233 | { "libmingw32.", 11 }, |
| 234 | { "libg2c.", 7 }, |
| 235 | { "libsupc++.", 10 }, |
| 236 | { "libobjc.", 8 }, |
| 237 | { NULL, 0 } |
| 238 | }; |
| 239 | |
| 240 | static autofilter_entry_type autofilter_objlist[] = |
| 241 | { |
| 242 | { "crt0.o", 6 }, |
| 243 | { "crt1.o", 6 }, |
| 244 | { "crt2.o", 6 }, |
| 245 | { "dllcrt1.o", 9 }, |
| 246 | { "dllcrt2.o", 9 }, |
| 247 | { "gcrt0.o", 7 }, |
| 248 | { "gcrt1.o", 7 }, |
| 249 | { "gcrt2.o", 7 }, |
| 250 | { "crtbegin.o", 10 }, |
| 251 | { "crtend.o", 8 }, |
| 252 | { NULL, 0 } |
| 253 | }; |
| 254 | |
| 255 | static autofilter_entry_type autofilter_symbolprefixlist[] = |
| 256 | { |
| 257 | /* { "__imp_", 6 }, */ |
| 258 | /* Do __imp_ explicitly to save time. */ |
| 259 | { "__rtti_", 7 }, |
| 260 | { "__builtin_", 10 }, |
| 261 | /* Don't export symbols specifying internal DLL layout. */ |
| 262 | { "_head_", 6 }, |
| 263 | { "_fmode", 6 }, |
| 264 | { "_impure_ptr", 11 }, |
| 265 | { "cygwin_attach_dll", 17 }, |
| 266 | { "cygwin_premain0", 15 }, |
| 267 | { "cygwin_premain1", 15 }, |
| 268 | { "cygwin_premain2", 15 }, |
| 269 | { "cygwin_premain3", 15 }, |
| 270 | { "environ", 7 }, |
| 271 | { NULL, 0 } |
| 272 | }; |
| 273 | |
| 274 | static autofilter_entry_type autofilter_symbolsuffixlist[] = |
| 275 | { |
| 276 | { "_iname", 6 }, |
| 277 | { NULL, 0 } |
| 278 | }; |
| 279 | |
| 280 | #define U(str) (pe_details->underscored ? "_" str : str) |
| 281 | |
| 282 | static int reloc_sort PARAMS ((const void *, const void *)); |
| 283 | static int pe_export_sort PARAMS ((const void *, const void *)); |
| 284 | static int auto_export PARAMS ((bfd *, def_file *, const char *)); |
| 285 | static void process_def_file PARAMS ((bfd *, struct bfd_link_info *)); |
| 286 | static void build_filler_bfd PARAMS ((int)); |
| 287 | static void generate_edata PARAMS ((bfd *, struct bfd_link_info *)); |
| 288 | static void fill_exported_offsets PARAMS ((bfd *, struct bfd_link_info *)); |
| 289 | static void fill_edata PARAMS ((bfd *, struct bfd_link_info *)); |
| 290 | static void generate_reloc PARAMS ((bfd *, struct bfd_link_info *)); |
| 291 | static void quoteput PARAMS ((char *, FILE *, int)); |
| 292 | static asection *quick_section PARAMS ((bfd *, const char *, int, int)); |
| 293 | static void quick_symbol |
| 294 | PARAMS ((bfd *, const char *, const char *, const char *, |
| 295 | asection *, int, int)); |
| 296 | static void quick_reloc PARAMS ((bfd *, int, int, int)); |
| 297 | static bfd *make_head PARAMS ((bfd *)); |
| 298 | static bfd *make_tail PARAMS ((bfd *)); |
| 299 | static bfd *make_one PARAMS ((def_file_export *, bfd *)); |
| 300 | static bfd *make_singleton_name_thunk PARAMS ((const char *, bfd *)); |
| 301 | static char *make_import_fixup_mark PARAMS ((arelent *)); |
| 302 | static bfd *make_import_fixup_entry |
| 303 | PARAMS ((const char *, const char *, const char *, bfd *)); |
| 304 | static unsigned int pe_get16 PARAMS ((bfd *, int)); |
| 305 | static unsigned int pe_get32 PARAMS ((bfd *, int)); |
| 306 | static unsigned int pe_as32 PARAMS ((void *)); |
| 307 | |
| 308 | void |
| 309 | pe_dll_id_target (target) |
| 310 | const char *target; |
| 311 | { |
| 312 | int i; |
| 313 | |
| 314 | for (i = 0; pe_detail_list[i].target_name; i++) |
| 315 | if (strcmp (pe_detail_list[i].target_name, target) == 0 |
| 316 | || strcmp (pe_detail_list[i].object_target, target) == 0) |
| 317 | { |
| 318 | pe_details = pe_detail_list + i; |
| 319 | return; |
| 320 | } |
| 321 | einfo (_("%XUnsupported PEI architecture: %s\n"), target); |
| 322 | exit (1); |
| 323 | } |
| 324 | |
| 325 | /* Helper functions for qsort. Relocs must be sorted so that we can write |
| 326 | them out by pages. */ |
| 327 | |
| 328 | typedef struct |
| 329 | { |
| 330 | bfd_vma vma; |
| 331 | char type; |
| 332 | short extra; |
| 333 | } |
| 334 | reloc_data_type; |
| 335 | |
| 336 | static int |
| 337 | reloc_sort (va, vb) |
| 338 | const void *va, *vb; |
| 339 | { |
| 340 | bfd_vma a = ((reloc_data_type *) va)->vma; |
| 341 | bfd_vma b = ((reloc_data_type *) vb)->vma; |
| 342 | |
| 343 | return (a > b) ? 1 : ((a < b) ? -1 : 0); |
| 344 | } |
| 345 | |
| 346 | static int |
| 347 | pe_export_sort (va, vb) |
| 348 | const void *va, *vb; |
| 349 | { |
| 350 | def_file_export *a = (def_file_export *) va; |
| 351 | def_file_export *b = (def_file_export *) vb; |
| 352 | |
| 353 | return strcmp (a->name, b->name); |
| 354 | } |
| 355 | |
| 356 | /* Read and process the .DEF file. */ |
| 357 | |
| 358 | /* These correspond to the entries in pe_def_file->exports[]. I use |
| 359 | exported_symbol_sections[i] to tag whether or not the symbol was |
| 360 | defined, since we can't export symbols we don't have. */ |
| 361 | |
| 362 | static bfd_vma *exported_symbol_offsets; |
| 363 | static struct sec **exported_symbol_sections; |
| 364 | static int export_table_size; |
| 365 | static int count_exported; |
| 366 | static int count_exported_byname; |
| 367 | static int count_with_ordinals; |
| 368 | static const char *dll_name; |
| 369 | static int min_ordinal, max_ordinal; |
| 370 | static int *exported_symbols; |
| 371 | |
| 372 | typedef struct exclude_list_struct |
| 373 | { |
| 374 | char *string; |
| 375 | struct exclude_list_struct *next; |
| 376 | int type; |
| 377 | } |
| 378 | exclude_list_struct; |
| 379 | |
| 380 | static struct exclude_list_struct *excludes = 0; |
| 381 | |
| 382 | void |
| 383 | pe_dll_add_excludes (new_excludes, type) |
| 384 | const char *new_excludes; |
| 385 | const int type; |
| 386 | { |
| 387 | char *local_copy; |
| 388 | char *exclude_string; |
| 389 | |
| 390 | local_copy = xstrdup (new_excludes); |
| 391 | |
| 392 | exclude_string = strtok (local_copy, ",:"); |
| 393 | for (; exclude_string; exclude_string = strtok (NULL, ",:")) |
| 394 | { |
| 395 | struct exclude_list_struct *new_exclude; |
| 396 | |
| 397 | new_exclude = ((struct exclude_list_struct *) |
| 398 | xmalloc (sizeof (struct exclude_list_struct))); |
| 399 | new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1); |
| 400 | strcpy (new_exclude->string, exclude_string); |
| 401 | new_exclude->type = type; |
| 402 | new_exclude->next = excludes; |
| 403 | excludes = new_exclude; |
| 404 | } |
| 405 | |
| 406 | free (local_copy); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* abfd is a bfd containing n (or NULL) |
| 411 | It can be used for contextual checks. */ |
| 412 | |
| 413 | static int |
| 414 | auto_export (abfd, d, n) |
| 415 | bfd *abfd; |
| 416 | def_file *d; |
| 417 | const char *n; |
| 418 | { |
| 419 | int i; |
| 420 | struct exclude_list_struct *ex; |
| 421 | autofilter_entry_type *afptr; |
| 422 | const char * libname = 0; |
| 423 | if (abfd && abfd->my_archive) |
| 424 | libname = lbasename (abfd->my_archive->filename); |
| 425 | |
| 426 | /* We should not re-export imported stuff. */ |
| 427 | if (strncmp (n, "_imp__", 6) == 0) |
| 428 | return 0; |
| 429 | |
| 430 | for (i = 0; i < d->num_exports; i++) |
| 431 | if (strcmp (d->exports[i].name, n) == 0) |
| 432 | return 0; |
| 433 | |
| 434 | if (pe_dll_do_default_excludes) |
| 435 | { |
| 436 | const char * p; |
| 437 | int len; |
| 438 | |
| 439 | if (pe_dll_extra_pe_debug) |
| 440 | printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n", |
| 441 | n, abfd, abfd->my_archive); |
| 442 | |
| 443 | /* First of all, make context checks: |
| 444 | Don't export anything from standard libs. */ |
| 445 | if (libname) |
| 446 | { |
| 447 | afptr = autofilter_liblist; |
| 448 | |
| 449 | while (afptr->name) |
| 450 | { |
| 451 | if (strncmp (libname, afptr->name, afptr->len) == 0 ) |
| 452 | return 0; |
| 453 | afptr++; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /* Next, exclude symbols from certain startup objects. */ |
| 458 | |
| 459 | if (abfd && (p = lbasename (abfd->filename))) |
| 460 | { |
| 461 | afptr = autofilter_objlist; |
| 462 | while (afptr->name) |
| 463 | { |
| 464 | if (strcmp (p, afptr->name) == 0) |
| 465 | return 0; |
| 466 | afptr++; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /* Don't try to blindly exclude all symbols |
| 471 | that begin with '__'; this was tried and |
| 472 | it is too restrictive. */ |
| 473 | |
| 474 | /* Then, exclude specific symbols. */ |
| 475 | afptr = autofilter_symbollist; |
| 476 | while (afptr->name) |
| 477 | { |
| 478 | if (strcmp (n, afptr->name) == 0) |
| 479 | return 0; |
| 480 | |
| 481 | afptr++; |
| 482 | } |
| 483 | |
| 484 | /* Next, exclude symbols starting with ... */ |
| 485 | afptr = autofilter_symbolprefixlist; |
| 486 | while (afptr->name) |
| 487 | { |
| 488 | if (strncmp (n, afptr->name, afptr->len) == 0) |
| 489 | return 0; |
| 490 | |
| 491 | afptr++; |
| 492 | } |
| 493 | |
| 494 | /* Finally, exclude symbols ending with ... */ |
| 495 | len = strlen (n); |
| 496 | afptr = autofilter_symbolsuffixlist; |
| 497 | while (afptr->name) |
| 498 | { |
| 499 | if ((len >= afptr->len) |
| 500 | /* Add 1 to insure match with trailing '\0'. */ |
| 501 | && strncmp (n + len - afptr->len, afptr->name, |
| 502 | afptr->len + 1) == 0) |
| 503 | return 0; |
| 504 | |
| 505 | afptr++; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | for (ex = excludes; ex; ex = ex->next) |
| 510 | { |
| 511 | if (ex->type == 1) /* exclude-libs */ |
| 512 | { |
| 513 | if (libname |
| 514 | && ((strcmp (libname, ex->string) == 0) |
| 515 | || (strcasecmp ("ALL", ex->string) == 0))) |
| 516 | return 0; |
| 517 | } |
| 518 | else if (strcmp (n, ex->string) == 0) |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | static void |
| 526 | process_def_file (abfd, info) |
| 527 | bfd *abfd ATTRIBUTE_UNUSED; |
| 528 | struct bfd_link_info *info; |
| 529 | { |
| 530 | int i, j; |
| 531 | struct bfd_link_hash_entry *blhe; |
| 532 | bfd *b; |
| 533 | struct sec *s; |
| 534 | def_file_export *e = 0; |
| 535 | |
| 536 | if (!pe_def_file) |
| 537 | pe_def_file = def_file_empty (); |
| 538 | |
| 539 | /* First, run around to all the objects looking for the .drectve |
| 540 | sections, and push those into the def file too. */ |
| 541 | for (b = info->input_bfds; b; b = b->link_next) |
| 542 | { |
| 543 | s = bfd_get_section_by_name (b, ".drectve"); |
| 544 | if (s) |
| 545 | { |
| 546 | int size = bfd_get_section_size_before_reloc (s); |
| 547 | char *buf = xmalloc (size); |
| 548 | |
| 549 | bfd_get_section_contents (b, s, buf, 0, size); |
| 550 | def_file_add_directive (pe_def_file, buf, size); |
| 551 | free (buf); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /* Now, maybe export everything else the default way. */ |
| 556 | if (pe_dll_export_everything || pe_def_file->num_exports == 0) |
| 557 | { |
| 558 | for (b = info->input_bfds; b; b = b->link_next) |
| 559 | { |
| 560 | asymbol **symbols; |
| 561 | int nsyms, symsize; |
| 562 | |
| 563 | symsize = bfd_get_symtab_upper_bound (b); |
| 564 | symbols = (asymbol **) xmalloc (symsize); |
| 565 | nsyms = bfd_canonicalize_symtab (b, symbols); |
| 566 | |
| 567 | for (j = 0; j < nsyms; j++) |
| 568 | { |
| 569 | /* We should export symbols which are either global or not |
| 570 | anything at all. (.bss data is the latter) |
| 571 | We should not export undefined symbols. */ |
| 572 | if (symbols[j]->section != &bfd_und_section |
| 573 | && ((symbols[j]->flags & BSF_GLOBAL) |
| 574 | || (symbols[j]->flags == BFD_FORT_COMM_DEFAULT_VALUE))) |
| 575 | { |
| 576 | const char *sn = symbols[j]->name; |
| 577 | |
| 578 | /* We should not re-export imported stuff. */ |
| 579 | { |
| 580 | char *name = (char *) xmalloc (strlen (sn) + 2 + 6); |
| 581 | sprintf (name, "%s%s", U("_imp_"), sn); |
| 582 | |
| 583 | blhe = bfd_link_hash_lookup (info->hash, name, |
| 584 | false, false, false); |
| 585 | free (name); |
| 586 | |
| 587 | if (blhe && blhe->type == bfd_link_hash_defined) |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | if (*sn == '_') |
| 592 | sn++; |
| 593 | |
| 594 | if (auto_export (b, pe_def_file, sn)) |
| 595 | { |
| 596 | def_file_export *p; |
| 597 | p=def_file_add_export (pe_def_file, sn, 0, -1); |
| 598 | /* Fill data flag properly, from dlltool.c. */ |
| 599 | p->flag_data = !(symbols[j]->flags & BSF_FUNCTION); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | #undef NE |
| 607 | #define NE pe_def_file->num_exports |
| 608 | |
| 609 | /* Canonicalize the export list. */ |
| 610 | if (pe_dll_kill_ats) |
| 611 | { |
| 612 | for (i = 0; i < NE; i++) |
| 613 | { |
| 614 | if (strchr (pe_def_file->exports[i].name, '@')) |
| 615 | { |
| 616 | /* This will preserve internal_name, which may have been |
| 617 | pointing to the same memory as name, or might not |
| 618 | have. */ |
| 619 | char *tmp = xstrdup (pe_def_file->exports[i].name); |
| 620 | |
| 621 | *(strchr (tmp, '@')) = 0; |
| 622 | pe_def_file->exports[i].name = tmp; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | if (pe_dll_stdcall_aliases) |
| 628 | { |
| 629 | for (i = 0; i < NE; i++) |
| 630 | { |
| 631 | if (strchr (pe_def_file->exports[i].name, '@')) |
| 632 | { |
| 633 | char *tmp = xstrdup (pe_def_file->exports[i].name); |
| 634 | |
| 635 | *(strchr (tmp, '@')) = 0; |
| 636 | if (auto_export (NULL, pe_def_file, tmp)) |
| 637 | def_file_add_export (pe_def_file, tmp, |
| 638 | pe_def_file->exports[i].internal_name, |
| 639 | -1); |
| 640 | else |
| 641 | free (tmp); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /* Convenience, but watch out for it changing. */ |
| 647 | e = pe_def_file->exports; |
| 648 | |
| 649 | exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma)); |
| 650 | exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *)); |
| 651 | |
| 652 | memset (exported_symbol_sections, 0, NE * sizeof (struct sec *)); |
| 653 | max_ordinal = 0; |
| 654 | min_ordinal = 65536; |
| 655 | count_exported = 0; |
| 656 | count_exported_byname = 0; |
| 657 | count_with_ordinals = 0; |
| 658 | |
| 659 | qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort); |
| 660 | for (i = 0, j = 0; i < NE; i++) |
| 661 | { |
| 662 | if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0) |
| 663 | { |
| 664 | /* This is a duplicate. */ |
| 665 | if (e[j - 1].ordinal != -1 |
| 666 | && e[i].ordinal != -1 |
| 667 | && e[j - 1].ordinal != e[i].ordinal) |
| 668 | { |
| 669 | if (pe_dll_warn_dup_exports) |
| 670 | /* xgettext:c-format */ |
| 671 | einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"), |
| 672 | e[j - 1].name, e[j - 1].ordinal, e[i].ordinal); |
| 673 | } |
| 674 | else |
| 675 | { |
| 676 | if (pe_dll_warn_dup_exports) |
| 677 | /* xgettext:c-format */ |
| 678 | einfo (_("Warning, duplicate EXPORT: %s\n"), |
| 679 | e[j - 1].name); |
| 680 | } |
| 681 | |
| 682 | if (e[i].ordinal != -1) |
| 683 | e[j - 1].ordinal = e[i].ordinal; |
| 684 | e[j - 1].flag_private |= e[i].flag_private; |
| 685 | e[j - 1].flag_constant |= e[i].flag_constant; |
| 686 | e[j - 1].flag_noname |= e[i].flag_noname; |
| 687 | e[j - 1].flag_data |= e[i].flag_data; |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | if (i != j) |
| 692 | e[j] = e[i]; |
| 693 | j++; |
| 694 | } |
| 695 | } |
| 696 | pe_def_file->num_exports = j; /* == NE */ |
| 697 | |
| 698 | for (i = 0; i < NE; i++) |
| 699 | { |
| 700 | char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2); |
| 701 | |
| 702 | if (pe_details->underscored) |
| 703 | { |
| 704 | *name = '_'; |
| 705 | strcpy (name + 1, pe_def_file->exports[i].internal_name); |
| 706 | } |
| 707 | else |
| 708 | strcpy (name, pe_def_file->exports[i].internal_name); |
| 709 | |
| 710 | blhe = bfd_link_hash_lookup (info->hash, |
| 711 | name, |
| 712 | false, false, true); |
| 713 | |
| 714 | if (blhe |
| 715 | && (blhe->type == bfd_link_hash_defined |
| 716 | || (blhe->type == bfd_link_hash_common))) |
| 717 | { |
| 718 | count_exported++; |
| 719 | if (!pe_def_file->exports[i].flag_noname) |
| 720 | count_exported_byname++; |
| 721 | |
| 722 | /* Only fill in the sections. The actual offsets are computed |
| 723 | in fill_exported_offsets() after common symbols are laid |
| 724 | out. */ |
| 725 | if (blhe->type == bfd_link_hash_defined) |
| 726 | exported_symbol_sections[i] = blhe->u.def.section; |
| 727 | else |
| 728 | exported_symbol_sections[i] = blhe->u.c.p->section; |
| 729 | |
| 730 | if (pe_def_file->exports[i].ordinal != -1) |
| 731 | { |
| 732 | if (max_ordinal < pe_def_file->exports[i].ordinal) |
| 733 | max_ordinal = pe_def_file->exports[i].ordinal; |
| 734 | if (min_ordinal > pe_def_file->exports[i].ordinal) |
| 735 | min_ordinal = pe_def_file->exports[i].ordinal; |
| 736 | count_with_ordinals++; |
| 737 | } |
| 738 | } |
| 739 | else if (blhe && blhe->type == bfd_link_hash_undefined) |
| 740 | { |
| 741 | /* xgettext:c-format */ |
| 742 | einfo (_("%XCannot export %s: symbol not defined\n"), |
| 743 | pe_def_file->exports[i].internal_name); |
| 744 | } |
| 745 | else if (blhe) |
| 746 | { |
| 747 | /* xgettext:c-format */ |
| 748 | einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"), |
| 749 | pe_def_file->exports[i].internal_name, |
| 750 | blhe->type, bfd_link_hash_defined); |
| 751 | } |
| 752 | else |
| 753 | { |
| 754 | /* xgettext:c-format */ |
| 755 | einfo (_("%XCannot export %s: symbol not found\n"), |
| 756 | pe_def_file->exports[i].internal_name); |
| 757 | } |
| 758 | free (name); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | /* Build the bfd that will contain .edata and .reloc sections. */ |
| 763 | |
| 764 | static void |
| 765 | build_filler_bfd (include_edata) |
| 766 | int include_edata; |
| 767 | { |
| 768 | lang_input_statement_type *filler_file; |
| 769 | filler_file = lang_add_input_file ("dll stuff", |
| 770 | lang_input_file_is_fake_enum, |
| 771 | NULL); |
| 772 | filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd); |
| 773 | if (filler_bfd == NULL |
| 774 | || !bfd_set_arch_mach (filler_bfd, |
| 775 | bfd_get_arch (output_bfd), |
| 776 | bfd_get_mach (output_bfd))) |
| 777 | { |
| 778 | einfo ("%X%P: can not create BFD %E\n"); |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | if (include_edata) |
| 783 | { |
| 784 | edata_s = bfd_make_section_old_way (filler_bfd, ".edata"); |
| 785 | if (edata_s == NULL |
| 786 | || !bfd_set_section_flags (filler_bfd, edata_s, |
| 787 | (SEC_HAS_CONTENTS |
| 788 | | SEC_ALLOC |
| 789 | | SEC_LOAD |
| 790 | | SEC_KEEP |
| 791 | | SEC_IN_MEMORY))) |
| 792 | { |
| 793 | einfo ("%X%P: can not create .edata section: %E\n"); |
| 794 | return; |
| 795 | } |
| 796 | bfd_set_section_size (filler_bfd, edata_s, edata_sz); |
| 797 | } |
| 798 | |
| 799 | reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc"); |
| 800 | if (reloc_s == NULL |
| 801 | || !bfd_set_section_flags (filler_bfd, reloc_s, |
| 802 | (SEC_HAS_CONTENTS |
| 803 | | SEC_ALLOC |
| 804 | | SEC_LOAD |
| 805 | | SEC_KEEP |
| 806 | | SEC_IN_MEMORY))) |
| 807 | { |
| 808 | einfo ("%X%P: can not create .reloc section: %E\n"); |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | bfd_set_section_size (filler_bfd, reloc_s, 0); |
| 813 | |
| 814 | ldlang_add_file (filler_file); |
| 815 | } |
| 816 | |
| 817 | /* Gather all the exported symbols and build the .edata section. */ |
| 818 | |
| 819 | static void |
| 820 | generate_edata (abfd, info) |
| 821 | bfd *abfd; |
| 822 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
| 823 | { |
| 824 | int i, next_ordinal; |
| 825 | int name_table_size = 0; |
| 826 | const char *dlnp; |
| 827 | |
| 828 | /* First, we need to know how many exported symbols there are, |
| 829 | and what the range of ordinals is. */ |
| 830 | if (pe_def_file->name) |
| 831 | dll_name = pe_def_file->name; |
| 832 | else |
| 833 | { |
| 834 | dll_name = abfd->filename; |
| 835 | |
| 836 | for (dlnp = dll_name; *dlnp; dlnp++) |
| 837 | if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':') |
| 838 | dll_name = dlnp + 1; |
| 839 | } |
| 840 | |
| 841 | if (count_with_ordinals && max_ordinal > count_exported) |
| 842 | { |
| 843 | if (min_ordinal > max_ordinal - count_exported + 1) |
| 844 | min_ordinal = max_ordinal - count_exported + 1; |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | min_ordinal = 1; |
| 849 | max_ordinal = count_exported; |
| 850 | } |
| 851 | |
| 852 | export_table_size = max_ordinal - min_ordinal + 1; |
| 853 | exported_symbols = (int *) xmalloc (export_table_size * sizeof (int)); |
| 854 | for (i = 0; i < export_table_size; i++) |
| 855 | exported_symbols[i] = -1; |
| 856 | |
| 857 | /* Now we need to assign ordinals to those that don't have them. */ |
| 858 | for (i = 0; i < NE; i++) |
| 859 | { |
| 860 | if (exported_symbol_sections[i]) |
| 861 | { |
| 862 | if (pe_def_file->exports[i].ordinal != -1) |
| 863 | { |
| 864 | int ei = pe_def_file->exports[i].ordinal - min_ordinal; |
| 865 | int pi = exported_symbols[ei]; |
| 866 | |
| 867 | if (pi != -1) |
| 868 | { |
| 869 | /* xgettext:c-format */ |
| 870 | einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"), |
| 871 | pe_def_file->exports[i].ordinal, |
| 872 | pe_def_file->exports[i].name, |
| 873 | pe_def_file->exports[pi].name); |
| 874 | } |
| 875 | exported_symbols[ei] = i; |
| 876 | } |
| 877 | name_table_size += strlen (pe_def_file->exports[i].name) + 1; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | next_ordinal = min_ordinal; |
| 882 | for (i = 0; i < NE; i++) |
| 883 | if (exported_symbol_sections[i]) |
| 884 | if (pe_def_file->exports[i].ordinal == -1) |
| 885 | { |
| 886 | while (exported_symbols[next_ordinal - min_ordinal] != -1) |
| 887 | next_ordinal++; |
| 888 | |
| 889 | exported_symbols[next_ordinal - min_ordinal] = i; |
| 890 | pe_def_file->exports[i].ordinal = next_ordinal; |
| 891 | } |
| 892 | |
| 893 | /* OK, now we can allocate some memory. */ |
| 894 | edata_sz = (40 /* directory */ |
| 895 | + 4 * export_table_size /* addresses */ |
| 896 | + 4 * count_exported_byname /* name ptrs */ |
| 897 | + 2 * count_exported_byname /* ordinals */ |
| 898 | + name_table_size + strlen (dll_name) + 1); |
| 899 | } |
| 900 | |
| 901 | /* Fill the exported symbol offsets. The preliminary work has already |
| 902 | been done in process_def_file(). */ |
| 903 | |
| 904 | static void |
| 905 | fill_exported_offsets (abfd, info) |
| 906 | bfd *abfd ATTRIBUTE_UNUSED; |
| 907 | struct bfd_link_info *info; |
| 908 | { |
| 909 | int i; |
| 910 | struct bfd_link_hash_entry *blhe; |
| 911 | |
| 912 | for (i = 0; i < pe_def_file->num_exports; i++) |
| 913 | { |
| 914 | char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2); |
| 915 | |
| 916 | if (pe_details->underscored) |
| 917 | { |
| 918 | *name = '_'; |
| 919 | strcpy (name + 1, pe_def_file->exports[i].internal_name); |
| 920 | } |
| 921 | else |
| 922 | strcpy (name, pe_def_file->exports[i].internal_name); |
| 923 | |
| 924 | blhe = bfd_link_hash_lookup (info->hash, |
| 925 | name, |
| 926 | false, false, true); |
| 927 | |
| 928 | if (blhe && (blhe->type == bfd_link_hash_defined)) |
| 929 | exported_symbol_offsets[i] = blhe->u.def.value; |
| 930 | |
| 931 | free (name); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | static void |
| 936 | fill_edata (abfd, info) |
| 937 | bfd *abfd; |
| 938 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
| 939 | { |
| 940 | int i, hint; |
| 941 | unsigned char *edirectory; |
| 942 | unsigned long *eaddresses; |
| 943 | unsigned long *enameptrs; |
| 944 | unsigned short *eordinals; |
| 945 | unsigned char *enamestr; |
| 946 | time_t now; |
| 947 | |
| 948 | time (&now); |
| 949 | |
| 950 | edata_d = (unsigned char *) xmalloc (edata_sz); |
| 951 | |
| 952 | /* Note use of array pointer math here. */ |
| 953 | edirectory = edata_d; |
| 954 | eaddresses = (unsigned long *) (edata_d + 40); |
| 955 | enameptrs = eaddresses + export_table_size; |
| 956 | eordinals = (unsigned short *) (enameptrs + count_exported_byname); |
| 957 | enamestr = (char *) (eordinals + count_exported_byname); |
| 958 | |
| 959 | #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base) |
| 960 | |
| 961 | memset (edata_d, 0, edata_sz); |
| 962 | bfd_put_32 (abfd, now, edata_d + 4); |
| 963 | if (pe_def_file->version_major != -1) |
| 964 | { |
| 965 | bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8); |
| 966 | bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10); |
| 967 | } |
| 968 | |
| 969 | bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12); |
| 970 | strcpy (enamestr, dll_name); |
| 971 | enamestr += strlen (enamestr) + 1; |
| 972 | bfd_put_32 (abfd, min_ordinal, edata_d + 16); |
| 973 | bfd_put_32 (abfd, export_table_size, edata_d + 20); |
| 974 | bfd_put_32 (abfd, count_exported_byname, edata_d + 24); |
| 975 | bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28); |
| 976 | bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32); |
| 977 | bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36); |
| 978 | |
| 979 | fill_exported_offsets (abfd, info); |
| 980 | |
| 981 | /* Ok, now for the filling in part. */ |
| 982 | hint = 0; |
| 983 | for (i = 0; i < export_table_size; i++) |
| 984 | { |
| 985 | int s = exported_symbols[i]; |
| 986 | |
| 987 | if (s != -1) |
| 988 | { |
| 989 | struct sec *ssec = exported_symbol_sections[s]; |
| 990 | unsigned long srva = (exported_symbol_offsets[s] |
| 991 | + ssec->output_section->vma |
| 992 | + ssec->output_offset); |
| 993 | int ord = pe_def_file->exports[s].ordinal; |
| 994 | |
| 995 | bfd_put_32 (abfd, srva - image_base, |
| 996 | (void *) (eaddresses + ord - min_ordinal)); |
| 997 | |
| 998 | if (!pe_def_file->exports[s].flag_noname) |
| 999 | { |
| 1000 | char *ename = pe_def_file->exports[s].name; |
| 1001 | bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs); |
| 1002 | enameptrs++; |
| 1003 | strcpy (enamestr, ename); |
| 1004 | enamestr += strlen (enamestr) + 1; |
| 1005 | bfd_put_16 (abfd, ord - min_ordinal, (void *) eordinals); |
| 1006 | eordinals++; |
| 1007 | pe_def_file->exports[s].hint = hint++; |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | static struct sec *current_sec; |
| 1015 | |
| 1016 | void |
| 1017 | pe_walk_relocs_of_symbol (info, name, cb) |
| 1018 | struct bfd_link_info *info; |
| 1019 | const char *name; |
| 1020 | int (*cb) (arelent *, asection *); |
| 1021 | { |
| 1022 | bfd *b; |
| 1023 | asection *s; |
| 1024 | |
| 1025 | for (b = info->input_bfds; b; b = b->link_next) |
| 1026 | { |
| 1027 | asymbol **symbols; |
| 1028 | int nsyms, symsize; |
| 1029 | |
| 1030 | symsize = bfd_get_symtab_upper_bound (b); |
| 1031 | symbols = (asymbol **) xmalloc (symsize); |
| 1032 | nsyms = bfd_canonicalize_symtab (b, symbols); |
| 1033 | |
| 1034 | for (s = b->sections; s; s = s->next) |
| 1035 | { |
| 1036 | arelent **relocs; |
| 1037 | int relsize, nrelocs, i; |
| 1038 | int flags = bfd_get_section_flags (b, s); |
| 1039 | |
| 1040 | /* Skip discarded linkonce sections. */ |
| 1041 | if (flags & SEC_LINK_ONCE |
| 1042 | && s->output_section == bfd_abs_section_ptr) |
| 1043 | continue; |
| 1044 | |
| 1045 | current_sec = s; |
| 1046 | |
| 1047 | relsize = bfd_get_reloc_upper_bound (b, s); |
| 1048 | relocs = (arelent **) xmalloc ((size_t) relsize); |
| 1049 | nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols); |
| 1050 | |
| 1051 | for (i = 0; i < nrelocs; i++) |
| 1052 | { |
| 1053 | struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr; |
| 1054 | |
| 1055 | if (!strcmp (name, sym->name)) |
| 1056 | cb (relocs[i], s); |
| 1057 | } |
| 1058 | |
| 1059 | free (relocs); |
| 1060 | |
| 1061 | /* Warning: the allocated symbols are remembered in BFD and reused |
| 1062 | later, so don't free them! */ |
| 1063 | /* free (symbols); */ |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | /* Gather all the relocations and build the .reloc section. */ |
| 1069 | |
| 1070 | static void |
| 1071 | generate_reloc (abfd, info) |
| 1072 | bfd *abfd; |
| 1073 | struct bfd_link_info *info; |
| 1074 | { |
| 1075 | |
| 1076 | /* For .reloc stuff. */ |
| 1077 | reloc_data_type *reloc_data; |
| 1078 | int total_relocs = 0; |
| 1079 | int i; |
| 1080 | unsigned long sec_page = (unsigned long) (-1); |
| 1081 | unsigned long page_ptr, page_count; |
| 1082 | int bi; |
| 1083 | bfd *b; |
| 1084 | struct sec *s; |
| 1085 | |
| 1086 | total_relocs = 0; |
| 1087 | for (b = info->input_bfds; b; b = b->link_next) |
| 1088 | for (s = b->sections; s; s = s->next) |
| 1089 | total_relocs += s->reloc_count; |
| 1090 | |
| 1091 | reloc_data = |
| 1092 | (reloc_data_type *) xmalloc (total_relocs * sizeof (reloc_data_type)); |
| 1093 | |
| 1094 | total_relocs = 0; |
| 1095 | bi = 0; |
| 1096 | for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next) |
| 1097 | { |
| 1098 | arelent **relocs; |
| 1099 | int relsize, nrelocs, i; |
| 1100 | |
| 1101 | for (s = b->sections; s; s = s->next) |
| 1102 | { |
| 1103 | unsigned long sec_vma = s->output_section->vma + s->output_offset; |
| 1104 | asymbol **symbols; |
| 1105 | int nsyms, symsize; |
| 1106 | |
| 1107 | /* If it's not loaded, we don't need to relocate it this way. */ |
| 1108 | if (!(s->output_section->flags & SEC_LOAD)) |
| 1109 | continue; |
| 1110 | |
| 1111 | /* I don't know why there would be a reloc for these, but I've |
| 1112 | seen it happen - DJ */ |
| 1113 | if (s->output_section == &bfd_abs_section) |
| 1114 | continue; |
| 1115 | |
| 1116 | if (s->output_section->vma == 0) |
| 1117 | { |
| 1118 | /* Huh? Shouldn't happen, but punt if it does. */ |
| 1119 | einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n", |
| 1120 | s->output_section->name, s->output_section->index, |
| 1121 | s->output_section->flags); |
| 1122 | continue; |
| 1123 | } |
| 1124 | |
| 1125 | symsize = bfd_get_symtab_upper_bound (b); |
| 1126 | symbols = (asymbol **) xmalloc (symsize); |
| 1127 | nsyms = bfd_canonicalize_symtab (b, symbols); |
| 1128 | |
| 1129 | relsize = bfd_get_reloc_upper_bound (b, s); |
| 1130 | relocs = (arelent **) xmalloc ((size_t) relsize); |
| 1131 | nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols); |
| 1132 | |
| 1133 | for (i = 0; i < nrelocs; i++) |
| 1134 | { |
| 1135 | if (pe_dll_extra_pe_debug) |
| 1136 | { |
| 1137 | struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr; |
| 1138 | printf ("rel: %s\n", sym->name); |
| 1139 | } |
| 1140 | if (!relocs[i]->howto->pc_relative |
| 1141 | && relocs[i]->howto->type != pe_details->imagebase_reloc) |
| 1142 | { |
| 1143 | bfd_vma sym_vma; |
| 1144 | struct symbol_cache_entry *sym = *relocs[i]->sym_ptr_ptr; |
| 1145 | |
| 1146 | sym_vma = (relocs[i]->addend |
| 1147 | + sym->value |
| 1148 | + sym->section->vma |
| 1149 | + sym->section->output_offset |
| 1150 | + sym->section->output_section->vma); |
| 1151 | reloc_data[total_relocs].vma = sec_vma + relocs[i]->address; |
| 1152 | |
| 1153 | #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift) |
| 1154 | |
| 1155 | switch BITS_AND_SHIFT (relocs[i]->howto->bitsize, |
| 1156 | relocs[i]->howto->rightshift) |
| 1157 | { |
| 1158 | case BITS_AND_SHIFT (32, 0): |
| 1159 | reloc_data[total_relocs].type = 3; |
| 1160 | total_relocs++; |
| 1161 | break; |
| 1162 | case BITS_AND_SHIFT (16, 0): |
| 1163 | reloc_data[total_relocs].type = 2; |
| 1164 | total_relocs++; |
| 1165 | break; |
| 1166 | case BITS_AND_SHIFT (16, 16): |
| 1167 | reloc_data[total_relocs].type = 4; |
| 1168 | /* FIXME: we can't know the symbol's right value |
| 1169 | yet, but we probably can safely assume that |
| 1170 | CE will relocate us in 64k blocks, so leaving |
| 1171 | it zero is safe. */ |
| 1172 | reloc_data[total_relocs].extra = 0; |
| 1173 | total_relocs++; |
| 1174 | break; |
| 1175 | case BITS_AND_SHIFT (26, 2): |
| 1176 | reloc_data[total_relocs].type = 5; |
| 1177 | total_relocs++; |
| 1178 | break; |
| 1179 | default: |
| 1180 | /* xgettext:c-format */ |
| 1181 | einfo (_("%XError: %d-bit reloc in dll\n"), |
| 1182 | relocs[i]->howto->bitsize); |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | free (relocs); |
| 1188 | /* Warning: the allocated symbols are remembered in BFD and |
| 1189 | reused later, so don't free them! */ |
| 1190 | #if 0 |
| 1191 | free (symbol); |
| 1192 | #endif |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | /* At this point, we have total_relocs relocation addresses in |
| 1197 | reloc_addresses, which are all suitable for the .reloc section. |
| 1198 | We must now create the new sections. */ |
| 1199 | qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort); |
| 1200 | |
| 1201 | for (i = 0; i < total_relocs; i++) |
| 1202 | { |
| 1203 | unsigned long this_page = (reloc_data[i].vma >> 12); |
| 1204 | |
| 1205 | if (this_page != sec_page) |
| 1206 | { |
| 1207 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */ |
| 1208 | reloc_sz += 8; |
| 1209 | sec_page = this_page; |
| 1210 | } |
| 1211 | |
| 1212 | reloc_sz += 2; |
| 1213 | |
| 1214 | if (reloc_data[i].type == 4) |
| 1215 | reloc_sz += 2; |
| 1216 | } |
| 1217 | |
| 1218 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */ |
| 1219 | reloc_d = (unsigned char *) xmalloc (reloc_sz); |
| 1220 | sec_page = (unsigned long) (-1); |
| 1221 | reloc_sz = 0; |
| 1222 | page_ptr = (unsigned long) (-1); |
| 1223 | page_count = 0; |
| 1224 | |
| 1225 | for (i = 0; i < total_relocs; i++) |
| 1226 | { |
| 1227 | unsigned long rva = reloc_data[i].vma - image_base; |
| 1228 | unsigned long this_page = (rva & ~0xfff); |
| 1229 | |
| 1230 | if (this_page != sec_page) |
| 1231 | { |
| 1232 | while (reloc_sz & 3) |
| 1233 | reloc_d[reloc_sz++] = 0; |
| 1234 | |
| 1235 | if (page_ptr != (unsigned long) (-1)) |
| 1236 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4); |
| 1237 | |
| 1238 | bfd_put_32 (abfd, this_page, reloc_d + reloc_sz); |
| 1239 | page_ptr = reloc_sz; |
| 1240 | reloc_sz += 8; |
| 1241 | sec_page = this_page; |
| 1242 | page_count = 0; |
| 1243 | } |
| 1244 | |
| 1245 | bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type << 12), |
| 1246 | reloc_d + reloc_sz); |
| 1247 | reloc_sz += 2; |
| 1248 | |
| 1249 | if (reloc_data[i].type == 4) |
| 1250 | { |
| 1251 | bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz); |
| 1252 | reloc_sz += 2; |
| 1253 | } |
| 1254 | |
| 1255 | page_count++; |
| 1256 | } |
| 1257 | |
| 1258 | while (reloc_sz & 3) |
| 1259 | reloc_d[reloc_sz++] = 0; |
| 1260 | |
| 1261 | if (page_ptr != (unsigned long) (-1)) |
| 1262 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4); |
| 1263 | |
| 1264 | while (reloc_sz < reloc_s->_raw_size) |
| 1265 | reloc_d[reloc_sz++] = 0; |
| 1266 | } |
| 1267 | |
| 1268 | /* Given the exiting def_file structure, print out a .DEF file that |
| 1269 | corresponds to it. */ |
| 1270 | |
| 1271 | static void |
| 1272 | quoteput (s, f, needs_quotes) |
| 1273 | char *s; |
| 1274 | FILE *f; |
| 1275 | int needs_quotes; |
| 1276 | { |
| 1277 | char *cp; |
| 1278 | |
| 1279 | for (cp = s; *cp; cp++) |
| 1280 | if (*cp == '\'' |
| 1281 | || *cp == '"' |
| 1282 | || *cp == '\\' |
| 1283 | || ISSPACE (*cp) |
| 1284 | || *cp == ',' |
| 1285 | || *cp == ';') |
| 1286 | needs_quotes = 1; |
| 1287 | |
| 1288 | if (needs_quotes) |
| 1289 | { |
| 1290 | putc ('"', f); |
| 1291 | |
| 1292 | while (*s) |
| 1293 | { |
| 1294 | if (*s == '"' || *s == '\\') |
| 1295 | putc ('\\', f); |
| 1296 | |
| 1297 | putc (*s, f); |
| 1298 | s++; |
| 1299 | } |
| 1300 | |
| 1301 | putc ('"', f); |
| 1302 | } |
| 1303 | else |
| 1304 | fputs (s, f); |
| 1305 | } |
| 1306 | |
| 1307 | void |
| 1308 | pe_dll_generate_def_file (pe_out_def_filename) |
| 1309 | const char *pe_out_def_filename; |
| 1310 | { |
| 1311 | int i; |
| 1312 | FILE *out = fopen (pe_out_def_filename, "w"); |
| 1313 | |
| 1314 | if (out == NULL) |
| 1315 | /* xgettext:c-format */ |
| 1316 | einfo (_("%s: Can't open output def file %s\n"), |
| 1317 | program_name, pe_out_def_filename); |
| 1318 | |
| 1319 | if (pe_def_file) |
| 1320 | { |
| 1321 | if (pe_def_file->name) |
| 1322 | { |
| 1323 | if (pe_def_file->is_dll) |
| 1324 | fprintf (out, "LIBRARY "); |
| 1325 | else |
| 1326 | fprintf (out, "NAME "); |
| 1327 | |
| 1328 | quoteput (pe_def_file->name, out, 1); |
| 1329 | |
| 1330 | if (pe_data (output_bfd)->pe_opthdr.ImageBase) |
| 1331 | fprintf (out, " BASE=0x%lx", |
| 1332 | (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase); |
| 1333 | fprintf (out, "\n"); |
| 1334 | } |
| 1335 | |
| 1336 | if (pe_def_file->description) |
| 1337 | { |
| 1338 | fprintf (out, "DESCRIPTION "); |
| 1339 | quoteput (pe_def_file->description, out, 1); |
| 1340 | fprintf (out, "\n"); |
| 1341 | } |
| 1342 | |
| 1343 | if (pe_def_file->version_minor != -1) |
| 1344 | fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major, |
| 1345 | pe_def_file->version_minor); |
| 1346 | else if (pe_def_file->version_major != -1) |
| 1347 | fprintf (out, "VERSION %d\n", pe_def_file->version_major); |
| 1348 | |
| 1349 | if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1) |
| 1350 | fprintf (out, "\n"); |
| 1351 | |
| 1352 | if (pe_def_file->stack_commit != -1) |
| 1353 | fprintf (out, "STACKSIZE 0x%x,0x%x\n", |
| 1354 | pe_def_file->stack_reserve, pe_def_file->stack_commit); |
| 1355 | else if (pe_def_file->stack_reserve != -1) |
| 1356 | fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve); |
| 1357 | |
| 1358 | if (pe_def_file->heap_commit != -1) |
| 1359 | fprintf (out, "HEAPSIZE 0x%x,0x%x\n", |
| 1360 | pe_def_file->heap_reserve, pe_def_file->heap_commit); |
| 1361 | else if (pe_def_file->heap_reserve != -1) |
| 1362 | fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve); |
| 1363 | |
| 1364 | if (pe_def_file->num_section_defs > 0) |
| 1365 | { |
| 1366 | fprintf (out, "\nSECTIONS\n\n"); |
| 1367 | |
| 1368 | for (i = 0; i < pe_def_file->num_section_defs; i++) |
| 1369 | { |
| 1370 | fprintf (out, " "); |
| 1371 | quoteput (pe_def_file->section_defs[i].name, out, 0); |
| 1372 | |
| 1373 | if (pe_def_file->section_defs[i].class) |
| 1374 | { |
| 1375 | fprintf (out, " CLASS "); |
| 1376 | quoteput (pe_def_file->section_defs[i].class, out, 0); |
| 1377 | } |
| 1378 | |
| 1379 | if (pe_def_file->section_defs[i].flag_read) |
| 1380 | fprintf (out, " READ"); |
| 1381 | |
| 1382 | if (pe_def_file->section_defs[i].flag_write) |
| 1383 | fprintf (out, " WRITE"); |
| 1384 | |
| 1385 | if (pe_def_file->section_defs[i].flag_execute) |
| 1386 | fprintf (out, " EXECUTE"); |
| 1387 | |
| 1388 | if (pe_def_file->section_defs[i].flag_shared) |
| 1389 | fprintf (out, " SHARED"); |
| 1390 | |
| 1391 | fprintf (out, "\n"); |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | if (pe_def_file->num_exports > 0) |
| 1396 | { |
| 1397 | fprintf (out, "EXPORTS\n"); |
| 1398 | |
| 1399 | for (i = 0; i < pe_def_file->num_exports; i++) |
| 1400 | { |
| 1401 | def_file_export *e = pe_def_file->exports + i; |
| 1402 | fprintf (out, " "); |
| 1403 | quoteput (e->name, out, 0); |
| 1404 | |
| 1405 | if (e->internal_name && strcmp (e->internal_name, e->name)) |
| 1406 | { |
| 1407 | fprintf (out, " = "); |
| 1408 | quoteput (e->internal_name, out, 0); |
| 1409 | } |
| 1410 | |
| 1411 | if (e->ordinal != -1) |
| 1412 | fprintf (out, " @%d", e->ordinal); |
| 1413 | |
| 1414 | if (e->flag_private) |
| 1415 | fprintf (out, " PRIVATE"); |
| 1416 | |
| 1417 | if (e->flag_constant) |
| 1418 | fprintf (out, " CONSTANT"); |
| 1419 | |
| 1420 | if (e->flag_noname) |
| 1421 | fprintf (out, " NONAME"); |
| 1422 | |
| 1423 | if (e->flag_data) |
| 1424 | fprintf (out, " DATA"); |
| 1425 | |
| 1426 | fprintf (out, "\n"); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | if (pe_def_file->num_imports > 0) |
| 1431 | { |
| 1432 | fprintf (out, "\nIMPORTS\n\n"); |
| 1433 | |
| 1434 | for (i = 0; i < pe_def_file->num_imports; i++) |
| 1435 | { |
| 1436 | def_file_import *im = pe_def_file->imports + i; |
| 1437 | fprintf (out, " "); |
| 1438 | |
| 1439 | if (im->internal_name |
| 1440 | && (!im->name || strcmp (im->internal_name, im->name))) |
| 1441 | { |
| 1442 | quoteput (im->internal_name, out, 0); |
| 1443 | fprintf (out, " = "); |
| 1444 | } |
| 1445 | |
| 1446 | quoteput (im->module->name, out, 0); |
| 1447 | fprintf (out, "."); |
| 1448 | |
| 1449 | if (im->name) |
| 1450 | quoteput (im->name, out, 0); |
| 1451 | else |
| 1452 | fprintf (out, "%d", im->ordinal); |
| 1453 | |
| 1454 | fprintf (out, "\n"); |
| 1455 | } |
| 1456 | } |
| 1457 | } |
| 1458 | else |
| 1459 | fprintf (out, _("; no contents available\n")); |
| 1460 | |
| 1461 | if (fclose (out) == EOF) |
| 1462 | /* xgettext:c-format */ |
| 1463 | einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename); |
| 1464 | } |
| 1465 | |
| 1466 | /* Generate the import library. */ |
| 1467 | |
| 1468 | static asymbol **symtab; |
| 1469 | static int symptr; |
| 1470 | static int tmp_seq; |
| 1471 | static const char *dll_filename; |
| 1472 | static char *dll_symname; |
| 1473 | |
| 1474 | #define UNDSEC (asection *) &bfd_und_section |
| 1475 | |
| 1476 | static asection * |
| 1477 | quick_section (abfd, name, flags, align) |
| 1478 | bfd *abfd; |
| 1479 | const char *name; |
| 1480 | int flags; |
| 1481 | int align; |
| 1482 | { |
| 1483 | asection *sec; |
| 1484 | asymbol *sym; |
| 1485 | |
| 1486 | sec = bfd_make_section_old_way (abfd, name); |
| 1487 | bfd_set_section_flags (abfd, sec, flags | SEC_ALLOC | SEC_LOAD | SEC_KEEP); |
| 1488 | bfd_set_section_alignment (abfd, sec, align); |
| 1489 | /* Remember to undo this before trying to link internally! */ |
| 1490 | sec->output_section = sec; |
| 1491 | |
| 1492 | sym = bfd_make_empty_symbol (abfd); |
| 1493 | symtab[symptr++] = sym; |
| 1494 | sym->name = sec->name; |
| 1495 | sym->section = sec; |
| 1496 | sym->flags = BSF_LOCAL; |
| 1497 | sym->value = 0; |
| 1498 | |
| 1499 | return sec; |
| 1500 | } |
| 1501 | |
| 1502 | static void |
| 1503 | quick_symbol (abfd, n1, n2, n3, sec, flags, addr) |
| 1504 | bfd *abfd; |
| 1505 | const char *n1; |
| 1506 | const char *n2; |
| 1507 | const char *n3; |
| 1508 | asection *sec; |
| 1509 | int flags; |
| 1510 | int addr; |
| 1511 | { |
| 1512 | asymbol *sym; |
| 1513 | char *name = (char *) xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1); |
| 1514 | |
| 1515 | strcpy (name, n1); |
| 1516 | strcat (name, n2); |
| 1517 | strcat (name, n3); |
| 1518 | sym = bfd_make_empty_symbol (abfd); |
| 1519 | sym->name = name; |
| 1520 | sym->section = sec; |
| 1521 | sym->flags = flags; |
| 1522 | sym->value = addr; |
| 1523 | symtab[symptr++] = sym; |
| 1524 | } |
| 1525 | |
| 1526 | static arelent *reltab = 0; |
| 1527 | static int relcount = 0, relsize = 0; |
| 1528 | |
| 1529 | static void |
| 1530 | quick_reloc (abfd, address, which_howto, symidx) |
| 1531 | bfd *abfd; |
| 1532 | int address; |
| 1533 | int which_howto; |
| 1534 | int symidx; |
| 1535 | { |
| 1536 | if (relcount >= (relsize - 1)) |
| 1537 | { |
| 1538 | relsize += 10; |
| 1539 | if (reltab) |
| 1540 | reltab = (arelent *) xrealloc (reltab, relsize * sizeof (arelent)); |
| 1541 | else |
| 1542 | reltab = (arelent *) xmalloc (relsize * sizeof (arelent)); |
| 1543 | } |
| 1544 | reltab[relcount].address = address; |
| 1545 | reltab[relcount].addend = 0; |
| 1546 | reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto); |
| 1547 | reltab[relcount].sym_ptr_ptr = symtab + symidx; |
| 1548 | relcount++; |
| 1549 | } |
| 1550 | |
| 1551 | static void |
| 1552 | save_relocs (asection *sec) |
| 1553 | { |
| 1554 | int i; |
| 1555 | |
| 1556 | sec->relocation = reltab; |
| 1557 | sec->reloc_count = relcount; |
| 1558 | sec->orelocation = (arelent **) xmalloc ((relcount + 1) * sizeof (arelent *)); |
| 1559 | for (i = 0; i < relcount; i++) |
| 1560 | sec->orelocation[i] = sec->relocation + i; |
| 1561 | sec->orelocation[relcount] = 0; |
| 1562 | sec->flags |= SEC_RELOC; |
| 1563 | reltab = 0; |
| 1564 | relcount = relsize = 0; |
| 1565 | } |
| 1566 | |
| 1567 | /* .section .idata$2 |
| 1568 | .global __head_my_dll |
| 1569 | __head_my_dll: |
| 1570 | .rva hname |
| 1571 | .long 0 |
| 1572 | .long 0 |
| 1573 | .rva __my_dll_iname |
| 1574 | .rva fthunk |
| 1575 | |
| 1576 | .section .idata$5 |
| 1577 | .long 0 |
| 1578 | fthunk: |
| 1579 | |
| 1580 | .section .idata$4 |
| 1581 | .long 0 |
| 1582 | hname: */ |
| 1583 | |
| 1584 | static bfd * |
| 1585 | make_head (parent) |
| 1586 | bfd *parent; |
| 1587 | { |
| 1588 | asection *id2, *id5, *id4; |
| 1589 | unsigned char *d2, *d5, *d4; |
| 1590 | char *oname; |
| 1591 | bfd *abfd; |
| 1592 | |
| 1593 | oname = (char *) xmalloc (20); |
| 1594 | sprintf (oname, "d%06d.o", tmp_seq); |
| 1595 | tmp_seq++; |
| 1596 | |
| 1597 | abfd = bfd_create (oname, parent); |
| 1598 | bfd_find_target (pe_details->object_target, abfd); |
| 1599 | bfd_make_writable (abfd); |
| 1600 | |
| 1601 | bfd_set_format (abfd, bfd_object); |
| 1602 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
| 1603 | |
| 1604 | symptr = 0; |
| 1605 | symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *)); |
| 1606 | id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2); |
| 1607 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2); |
| 1608 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); |
| 1609 | quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0); |
| 1610 | quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0); |
| 1611 | |
| 1612 | /* OK, pay attention here. I got confused myself looking back at |
| 1613 | it. We create a four-byte section to mark the beginning of the |
| 1614 | list, and we include an offset of 4 in the section, so that the |
| 1615 | pointer to the list points to the *end* of this section, which is |
| 1616 | the start of the list of sections from other objects. */ |
| 1617 | |
| 1618 | bfd_set_section_size (abfd, id2, 20); |
| 1619 | d2 = (unsigned char *) xmalloc (20); |
| 1620 | id2->contents = d2; |
| 1621 | memset (d2, 0, 20); |
| 1622 | d2[0] = d2[16] = 4; /* Reloc addend. */ |
| 1623 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 2); |
| 1624 | quick_reloc (abfd, 12, BFD_RELOC_RVA, 4); |
| 1625 | quick_reloc (abfd, 16, BFD_RELOC_RVA, 1); |
| 1626 | save_relocs (id2); |
| 1627 | |
| 1628 | bfd_set_section_size (abfd, id5, 4); |
| 1629 | d5 = (unsigned char *) xmalloc (4); |
| 1630 | id5->contents = d5; |
| 1631 | memset (d5, 0, 4); |
| 1632 | |
| 1633 | bfd_set_section_size (abfd, id4, 4); |
| 1634 | d4 = (unsigned char *) xmalloc (4); |
| 1635 | id4->contents = d4; |
| 1636 | memset (d4, 0, 4); |
| 1637 | |
| 1638 | bfd_set_symtab (abfd, symtab, symptr); |
| 1639 | |
| 1640 | bfd_set_section_contents (abfd, id2, d2, 0, 20); |
| 1641 | bfd_set_section_contents (abfd, id5, d5, 0, 4); |
| 1642 | bfd_set_section_contents (abfd, id4, d4, 0, 4); |
| 1643 | |
| 1644 | bfd_make_readable (abfd); |
| 1645 | return abfd; |
| 1646 | } |
| 1647 | |
| 1648 | /* .section .idata$4 |
| 1649 | .long 0 |
| 1650 | .section .idata$5 |
| 1651 | .long 0 |
| 1652 | .section idata$7 |
| 1653 | .global __my_dll_iname |
| 1654 | __my_dll_iname: |
| 1655 | .asciz "my.dll" */ |
| 1656 | |
| 1657 | static bfd * |
| 1658 | make_tail (parent) |
| 1659 | bfd *parent; |
| 1660 | { |
| 1661 | asection *id4, *id5, *id7; |
| 1662 | unsigned char *d4, *d5, *d7; |
| 1663 | int len; |
| 1664 | char *oname; |
| 1665 | bfd *abfd; |
| 1666 | |
| 1667 | oname = (char *) xmalloc (20); |
| 1668 | sprintf (oname, "d%06d.o", tmp_seq); |
| 1669 | tmp_seq++; |
| 1670 | |
| 1671 | abfd = bfd_create (oname, parent); |
| 1672 | bfd_find_target (pe_details->object_target, abfd); |
| 1673 | bfd_make_writable (abfd); |
| 1674 | |
| 1675 | bfd_set_format (abfd, bfd_object); |
| 1676 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
| 1677 | |
| 1678 | symptr = 0; |
| 1679 | symtab = (asymbol **) xmalloc (5 * sizeof (asymbol *)); |
| 1680 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); |
| 1681 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2); |
| 1682 | id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2); |
| 1683 | quick_symbol (abfd, U (""), dll_symname, "_iname", id7, BSF_GLOBAL, 0); |
| 1684 | |
| 1685 | bfd_set_section_size (abfd, id4, 4); |
| 1686 | d4 = (unsigned char *) xmalloc (4); |
| 1687 | id4->contents = d4; |
| 1688 | memset (d4, 0, 4); |
| 1689 | |
| 1690 | bfd_set_section_size (abfd, id5, 4); |
| 1691 | d5 = (unsigned char *) xmalloc (4); |
| 1692 | id5->contents = d5; |
| 1693 | memset (d5, 0, 4); |
| 1694 | |
| 1695 | len = strlen (dll_filename) + 1; |
| 1696 | if (len & 1) |
| 1697 | len++; |
| 1698 | bfd_set_section_size (abfd, id7, len); |
| 1699 | d7 = (unsigned char *) xmalloc (len); |
| 1700 | id7->contents = d7; |
| 1701 | strcpy (d7, dll_filename); |
| 1702 | |
| 1703 | bfd_set_symtab (abfd, symtab, symptr); |
| 1704 | |
| 1705 | bfd_set_section_contents (abfd, id4, d4, 0, 4); |
| 1706 | bfd_set_section_contents (abfd, id5, d5, 0, 4); |
| 1707 | bfd_set_section_contents (abfd, id7, d7, 0, len); |
| 1708 | |
| 1709 | bfd_make_readable (abfd); |
| 1710 | return abfd; |
| 1711 | } |
| 1712 | |
| 1713 | /* .text |
| 1714 | .global _function |
| 1715 | .global ___imp_function |
| 1716 | .global __imp__function |
| 1717 | _function: |
| 1718 | jmp *__imp__function: |
| 1719 | |
| 1720 | .section idata$7 |
| 1721 | .long __head_my_dll |
| 1722 | |
| 1723 | .section .idata$5 |
| 1724 | ___imp_function: |
| 1725 | __imp__function: |
| 1726 | iat? |
| 1727 | .section .idata$4 |
| 1728 | iat? |
| 1729 | .section .idata$6 |
| 1730 | ID<ordinal>: |
| 1731 | .short <hint> |
| 1732 | .asciz "function" xlate? (add underscore, kill at) */ |
| 1733 | |
| 1734 | static unsigned char jmp_ix86_bytes[] = |
| 1735 | { |
| 1736 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 |
| 1737 | }; |
| 1738 | |
| 1739 | /* _function: |
| 1740 | mov.l ip+8,r0 |
| 1741 | mov.l @r0,r0 |
| 1742 | jmp @r0 |
| 1743 | nop |
| 1744 | .dw __imp_function */ |
| 1745 | |
| 1746 | static unsigned char jmp_sh_bytes[] = |
| 1747 | { |
| 1748 | 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 1749 | }; |
| 1750 | |
| 1751 | /* _function: |
| 1752 | lui $t0,<high:__imp_function> |
| 1753 | lw $t0,<low:__imp_function> |
| 1754 | jr $t0 |
| 1755 | nop */ |
| 1756 | |
| 1757 | static unsigned char jmp_mips_bytes[] = |
| 1758 | { |
| 1759 | 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d, |
| 1760 | 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 |
| 1761 | }; |
| 1762 | |
| 1763 | static bfd * |
| 1764 | make_one (exp, parent) |
| 1765 | def_file_export *exp; |
| 1766 | bfd *parent; |
| 1767 | { |
| 1768 | asection *tx, *id7, *id5, *id4, *id6; |
| 1769 | unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL; |
| 1770 | int len; |
| 1771 | char *oname; |
| 1772 | bfd *abfd; |
| 1773 | unsigned char *jmp_bytes = NULL; |
| 1774 | int jmp_byte_count = 0; |
| 1775 | |
| 1776 | switch (pe_details->pe_arch) |
| 1777 | { |
| 1778 | case PE_ARCH_i386: |
| 1779 | jmp_bytes = jmp_ix86_bytes; |
| 1780 | jmp_byte_count = sizeof (jmp_ix86_bytes); |
| 1781 | break; |
| 1782 | case PE_ARCH_sh: |
| 1783 | jmp_bytes = jmp_sh_bytes; |
| 1784 | jmp_byte_count = sizeof (jmp_sh_bytes); |
| 1785 | break; |
| 1786 | case PE_ARCH_mips: |
| 1787 | jmp_bytes = jmp_mips_bytes; |
| 1788 | jmp_byte_count = sizeof (jmp_mips_bytes); |
| 1789 | break; |
| 1790 | default: |
| 1791 | abort (); |
| 1792 | } |
| 1793 | |
| 1794 | oname = (char *) xmalloc (20); |
| 1795 | sprintf (oname, "d%06d.o", tmp_seq); |
| 1796 | tmp_seq++; |
| 1797 | |
| 1798 | abfd = bfd_create (oname, parent); |
| 1799 | bfd_find_target (pe_details->object_target, abfd); |
| 1800 | bfd_make_writable (abfd); |
| 1801 | |
| 1802 | bfd_set_format (abfd, bfd_object); |
| 1803 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
| 1804 | |
| 1805 | symptr = 0; |
| 1806 | symtab = (asymbol **) xmalloc (11 * sizeof (asymbol *)); |
| 1807 | tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2); |
| 1808 | id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2); |
| 1809 | id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2); |
| 1810 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); |
| 1811 | id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2); |
| 1812 | if (! exp->flag_data) |
| 1813 | quick_symbol (abfd, U (""), exp->internal_name, "", tx, BSF_GLOBAL, 0); |
| 1814 | quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC, BSF_GLOBAL, 0); |
| 1815 | quick_symbol (abfd, U ("_imp__"), exp->internal_name, "", id5, BSF_GLOBAL, 0); |
| 1816 | /* Symbol to reference ord/name of imported |
| 1817 | symbol, used to implement auto-import. */ |
| 1818 | quick_symbol (abfd, U("_nm__"), exp->internal_name, "", id6, BSF_GLOBAL, 0); |
| 1819 | if (pe_dll_compat_implib) |
| 1820 | quick_symbol (abfd, U ("__imp_"), exp->internal_name, "", |
| 1821 | id5, BSF_GLOBAL, 0); |
| 1822 | |
| 1823 | if (! exp->flag_data) |
| 1824 | { |
| 1825 | bfd_set_section_size (abfd, tx, jmp_byte_count); |
| 1826 | td = (unsigned char *) xmalloc (jmp_byte_count); |
| 1827 | tx->contents = td; |
| 1828 | memcpy (td, jmp_bytes, jmp_byte_count); |
| 1829 | |
| 1830 | switch (pe_details->pe_arch) |
| 1831 | { |
| 1832 | case PE_ARCH_i386: |
| 1833 | quick_reloc (abfd, 2, BFD_RELOC_32, 2); |
| 1834 | break; |
| 1835 | case PE_ARCH_sh: |
| 1836 | quick_reloc (abfd, 8, BFD_RELOC_32, 2); |
| 1837 | break; |
| 1838 | case PE_ARCH_mips: |
| 1839 | quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2); |
| 1840 | quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */ |
| 1841 | quick_reloc (abfd, 4, BFD_RELOC_LO16, 2); |
| 1842 | break; |
| 1843 | default: |
| 1844 | abort (); |
| 1845 | } |
| 1846 | save_relocs (tx); |
| 1847 | } |
| 1848 | |
| 1849 | bfd_set_section_size (abfd, id7, 4); |
| 1850 | d7 = (unsigned char *) xmalloc (4); |
| 1851 | id7->contents = d7; |
| 1852 | memset (d7, 0, 4); |
| 1853 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 6); |
| 1854 | save_relocs (id7); |
| 1855 | |
| 1856 | bfd_set_section_size (abfd, id5, 4); |
| 1857 | d5 = (unsigned char *) xmalloc (4); |
| 1858 | id5->contents = d5; |
| 1859 | memset (d5, 0, 4); |
| 1860 | |
| 1861 | if (exp->flag_noname) |
| 1862 | { |
| 1863 | d5[0] = exp->ordinal; |
| 1864 | d5[1] = exp->ordinal >> 8; |
| 1865 | d5[3] = 0x80; |
| 1866 | } |
| 1867 | else |
| 1868 | { |
| 1869 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 4); |
| 1870 | save_relocs (id5); |
| 1871 | } |
| 1872 | |
| 1873 | bfd_set_section_size (abfd, id4, 4); |
| 1874 | d4 = (unsigned char *) xmalloc (4); |
| 1875 | id4->contents = d4; |
| 1876 | memset (d4, 0, 4); |
| 1877 | |
| 1878 | if (exp->flag_noname) |
| 1879 | { |
| 1880 | d4[0] = exp->ordinal; |
| 1881 | d4[1] = exp->ordinal >> 8; |
| 1882 | d4[3] = 0x80; |
| 1883 | } |
| 1884 | else |
| 1885 | { |
| 1886 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 4); |
| 1887 | save_relocs (id4); |
| 1888 | } |
| 1889 | |
| 1890 | if (exp->flag_noname) |
| 1891 | { |
| 1892 | len = 0; |
| 1893 | bfd_set_section_size (abfd, id6, 0); |
| 1894 | } |
| 1895 | else |
| 1896 | { |
| 1897 | len = strlen (exp->name) + 3; |
| 1898 | if (len & 1) |
| 1899 | len++; |
| 1900 | bfd_set_section_size (abfd, id6, len); |
| 1901 | d6 = (unsigned char *) xmalloc (len); |
| 1902 | id6->contents = d6; |
| 1903 | memset (d6, 0, len); |
| 1904 | d6[0] = exp->hint & 0xff; |
| 1905 | d6[1] = exp->hint >> 8; |
| 1906 | strcpy (d6 + 2, exp->name); |
| 1907 | } |
| 1908 | |
| 1909 | bfd_set_symtab (abfd, symtab, symptr); |
| 1910 | |
| 1911 | bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count); |
| 1912 | bfd_set_section_contents (abfd, id7, d7, 0, 4); |
| 1913 | bfd_set_section_contents (abfd, id5, d5, 0, 4); |
| 1914 | bfd_set_section_contents (abfd, id4, d4, 0, 4); |
| 1915 | if (!exp->flag_noname) |
| 1916 | bfd_set_section_contents (abfd, id6, d6, 0, len); |
| 1917 | |
| 1918 | bfd_make_readable (abfd); |
| 1919 | return abfd; |
| 1920 | } |
| 1921 | |
| 1922 | static bfd * |
| 1923 | make_singleton_name_thunk (import, parent) |
| 1924 | const char *import; |
| 1925 | bfd *parent; |
| 1926 | { |
| 1927 | /* Name thunks go to idata$4. */ |
| 1928 | asection *id4; |
| 1929 | unsigned char *d4; |
| 1930 | char *oname; |
| 1931 | bfd *abfd; |
| 1932 | |
| 1933 | oname = (char *) xmalloc (20); |
| 1934 | sprintf (oname, "nmth%06d.o", tmp_seq); |
| 1935 | tmp_seq++; |
| 1936 | |
| 1937 | abfd = bfd_create (oname, parent); |
| 1938 | bfd_find_target (pe_details->object_target, abfd); |
| 1939 | bfd_make_writable (abfd); |
| 1940 | |
| 1941 | bfd_set_format (abfd, bfd_object); |
| 1942 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
| 1943 | |
| 1944 | symptr = 0; |
| 1945 | symtab = (asymbol **) xmalloc (3 * sizeof (asymbol *)); |
| 1946 | id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2); |
| 1947 | quick_symbol (abfd, U ("_nm_thnk_"), import, "", id4, BSF_GLOBAL, 0); |
| 1948 | quick_symbol (abfd, U ("_nm_"), import, "", UNDSEC, BSF_GLOBAL, 0); |
| 1949 | |
| 1950 | bfd_set_section_size (abfd, id4, 8); |
| 1951 | d4 = (unsigned char *) xmalloc (4); |
| 1952 | id4->contents = d4; |
| 1953 | memset (d4, 0, 8); |
| 1954 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 2); |
| 1955 | save_relocs (id4); |
| 1956 | |
| 1957 | bfd_set_symtab (abfd, symtab, symptr); |
| 1958 | |
| 1959 | bfd_set_section_contents (abfd, id4, d4, 0, 8); |
| 1960 | |
| 1961 | bfd_make_readable (abfd); |
| 1962 | return abfd; |
| 1963 | } |
| 1964 | |
| 1965 | static char * |
| 1966 | make_import_fixup_mark (rel) |
| 1967 | arelent *rel; |
| 1968 | { |
| 1969 | /* We convert reloc to symbol, for later reference. */ |
| 1970 | static int counter; |
| 1971 | static char *fixup_name = NULL; |
| 1972 | static size_t buffer_len = 0; |
| 1973 | |
| 1974 | struct symbol_cache_entry *sym = *rel->sym_ptr_ptr; |
| 1975 | |
| 1976 | bfd *abfd = bfd_asymbol_bfd (sym); |
| 1977 | struct coff_link_hash_entry *myh = NULL; |
| 1978 | |
| 1979 | if (!fixup_name) |
| 1980 | { |
| 1981 | fixup_name = (char *) xmalloc (384); |
| 1982 | buffer_len = 384; |
| 1983 | } |
| 1984 | |
| 1985 | if (strlen (sym->name) + 25 > buffer_len) |
| 1986 | /* Assume 25 chars for "__fu" + counter + "_". If counter is |
| 1987 | bigger than 20 digits long, we've got worse problems than |
| 1988 | overflowing this buffer... */ |
| 1989 | { |
| 1990 | free (fixup_name); |
| 1991 | /* New buffer size is length of symbol, plus 25, but then |
| 1992 | rounded up to the nearest multiple of 128. */ |
| 1993 | buffer_len = ((strlen (sym->name) + 25) + 127) & ~127; |
| 1994 | fixup_name = (char *) xmalloc (buffer_len); |
| 1995 | } |
| 1996 | |
| 1997 | sprintf (fixup_name, "__fu%d_%s", counter++, sym->name); |
| 1998 | |
| 1999 | bfd_coff_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL, |
| 2000 | current_sec, /* sym->section, */ |
| 2001 | rel->address, NULL, true, false, |
| 2002 | (struct bfd_link_hash_entry **) &myh); |
| 2003 | |
| 2004 | #if 0 |
| 2005 | printf ("type:%d\n", myh->type); |
| 2006 | printf ("%s\n", myh->root.u.def.section->name); |
| 2007 | #endif |
| 2008 | return fixup_name; |
| 2009 | } |
| 2010 | |
| 2011 | /* .section .idata$3 |
| 2012 | .rva __nm_thnk_SYM (singleton thunk with name of func) |
| 2013 | .long 0 |
| 2014 | .long 0 |
| 2015 | .rva __my_dll_iname (name of dll) |
| 2016 | .rva __fuNN_SYM (pointer to reference (address) in text) */ |
| 2017 | |
| 2018 | static bfd * |
| 2019 | make_import_fixup_entry (name, fixup_name, dll_symname, parent) |
| 2020 | const char *name; |
| 2021 | const char *fixup_name; |
| 2022 | const char *dll_symname; |
| 2023 | bfd *parent; |
| 2024 | { |
| 2025 | asection *id3; |
| 2026 | unsigned char *d3; |
| 2027 | char *oname; |
| 2028 | bfd *abfd; |
| 2029 | |
| 2030 | oname = (char *) xmalloc (20); |
| 2031 | sprintf (oname, "fu%06d.o", tmp_seq); |
| 2032 | tmp_seq++; |
| 2033 | |
| 2034 | abfd = bfd_create (oname, parent); |
| 2035 | bfd_find_target (pe_details->object_target, abfd); |
| 2036 | bfd_make_writable (abfd); |
| 2037 | |
| 2038 | bfd_set_format (abfd, bfd_object); |
| 2039 | bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0); |
| 2040 | |
| 2041 | symptr = 0; |
| 2042 | symtab = (asymbol **) xmalloc (6 * sizeof (asymbol *)); |
| 2043 | id3 = quick_section (abfd, ".idata$3", SEC_HAS_CONTENTS, 2); |
| 2044 | |
| 2045 | #if 0 |
| 2046 | quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0); |
| 2047 | #endif |
| 2048 | quick_symbol (abfd, U ("_nm_thnk_"), name, "", UNDSEC, BSF_GLOBAL, 0); |
| 2049 | quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0); |
| 2050 | quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0); |
| 2051 | |
| 2052 | bfd_set_section_size (abfd, id3, 20); |
| 2053 | d3 = (unsigned char *) xmalloc (20); |
| 2054 | id3->contents = d3; |
| 2055 | memset (d3, 0, 20); |
| 2056 | |
| 2057 | quick_reloc (abfd, 0, BFD_RELOC_RVA, 1); |
| 2058 | quick_reloc (abfd, 12, BFD_RELOC_RVA, 2); |
| 2059 | quick_reloc (abfd, 16, BFD_RELOC_RVA, 3); |
| 2060 | save_relocs (id3); |
| 2061 | |
| 2062 | bfd_set_symtab (abfd, symtab, symptr); |
| 2063 | |
| 2064 | bfd_set_section_contents (abfd, id3, d3, 0, 20); |
| 2065 | |
| 2066 | bfd_make_readable (abfd); |
| 2067 | return abfd; |
| 2068 | } |
| 2069 | |
| 2070 | void |
| 2071 | pe_create_import_fixup (rel) |
| 2072 | arelent *rel; |
| 2073 | { |
| 2074 | char buf[300]; |
| 2075 | struct symbol_cache_entry *sym = *rel->sym_ptr_ptr; |
| 2076 | struct bfd_link_hash_entry *name_thunk_sym; |
| 2077 | const char *name = sym->name; |
| 2078 | char *fixup_name = make_import_fixup_mark (rel); |
| 2079 | |
| 2080 | sprintf (buf, U ("_nm_thnk_%s"), name); |
| 2081 | |
| 2082 | name_thunk_sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1); |
| 2083 | |
| 2084 | if (!name_thunk_sym || name_thunk_sym->type != bfd_link_hash_defined) |
| 2085 | { |
| 2086 | bfd *b = make_singleton_name_thunk (name, output_bfd); |
| 2087 | add_bfd_to_link (b, b->filename, &link_info); |
| 2088 | |
| 2089 | /* If we ever use autoimport, we have to cast text section writable. */ |
| 2090 | config.text_read_only = false; |
| 2091 | } |
| 2092 | |
| 2093 | { |
| 2094 | extern char * pe_data_import_dll; |
| 2095 | char * dll_symname = pe_data_import_dll ? pe_data_import_dll : "unknown"; |
| 2096 | |
| 2097 | bfd *b = make_import_fixup_entry (name, fixup_name, dll_symname, |
| 2098 | output_bfd); |
| 2099 | add_bfd_to_link (b, b->filename, &link_info); |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | |
| 2104 | void |
| 2105 | pe_dll_generate_implib (def, impfilename) |
| 2106 | def_file *def; |
| 2107 | const char *impfilename; |
| 2108 | { |
| 2109 | int i; |
| 2110 | bfd *ar_head; |
| 2111 | bfd *ar_tail; |
| 2112 | bfd *outarch; |
| 2113 | bfd *head = 0; |
| 2114 | |
| 2115 | dll_filename = (def->name) ? def->name : dll_name; |
| 2116 | dll_symname = xstrdup (dll_filename); |
| 2117 | for (i = 0; dll_symname[i]; i++) |
| 2118 | if (!ISALNUM (dll_symname[i])) |
| 2119 | dll_symname[i] = '_'; |
| 2120 | |
| 2121 | unlink (impfilename); |
| 2122 | |
| 2123 | outarch = bfd_openw (impfilename, 0); |
| 2124 | |
| 2125 | if (!outarch) |
| 2126 | { |
| 2127 | /* xgettext:c-format */ |
| 2128 | einfo (_("%XCan't open .lib file: %s\n"), impfilename); |
| 2129 | return; |
| 2130 | } |
| 2131 | |
| 2132 | /* xgettext:c-format */ |
| 2133 | einfo (_("Creating library file: %s\n"), impfilename); |
| 2134 | |
| 2135 | bfd_set_format (outarch, bfd_archive); |
| 2136 | outarch->has_armap = 1; |
| 2137 | |
| 2138 | /* Work out a reasonable size of things to put onto one line. */ |
| 2139 | ar_head = make_head (outarch); |
| 2140 | |
| 2141 | for (i = 0; i < def->num_exports; i++) |
| 2142 | { |
| 2143 | /* The import library doesn't know about the internal name. */ |
| 2144 | char *internal = def->exports[i].internal_name; |
| 2145 | bfd *n; |
| 2146 | |
| 2147 | def->exports[i].internal_name = def->exports[i].name; |
| 2148 | n = make_one (def->exports + i, outarch); |
| 2149 | n->next = head; |
| 2150 | head = n; |
| 2151 | def->exports[i].internal_name = internal; |
| 2152 | } |
| 2153 | |
| 2154 | ar_tail = make_tail (outarch); |
| 2155 | |
| 2156 | if (ar_head == NULL || ar_tail == NULL) |
| 2157 | return; |
| 2158 | |
| 2159 | /* Now stick them all into the archive. */ |
| 2160 | ar_head->next = head; |
| 2161 | ar_tail->next = ar_head; |
| 2162 | head = ar_tail; |
| 2163 | |
| 2164 | if (! bfd_set_archive_head (outarch, head)) |
| 2165 | einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ())); |
| 2166 | |
| 2167 | if (! bfd_close (outarch)) |
| 2168 | einfo ("%Xbfd_close %s: %s\n", impfilename, bfd_errmsg (bfd_get_error ())); |
| 2169 | |
| 2170 | while (head != NULL) |
| 2171 | { |
| 2172 | bfd *n = head->next; |
| 2173 | bfd_close (head); |
| 2174 | head = n; |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | static void |
| 2179 | add_bfd_to_link (abfd, name, link_info) |
| 2180 | bfd *abfd; |
| 2181 | const char *name; |
| 2182 | struct bfd_link_info *link_info; |
| 2183 | { |
| 2184 | lang_input_statement_type *fake_file; |
| 2185 | |
| 2186 | fake_file = lang_add_input_file (name, |
| 2187 | lang_input_file_is_fake_enum, |
| 2188 | NULL); |
| 2189 | fake_file->the_bfd = abfd; |
| 2190 | ldlang_add_file (fake_file); |
| 2191 | |
| 2192 | if (!bfd_link_add_symbols (abfd, link_info)) |
| 2193 | einfo ("%Xaddsym %s: %s\n", name, bfd_errmsg (bfd_get_error ())); |
| 2194 | } |
| 2195 | |
| 2196 | void |
| 2197 | pe_process_import_defs (output_bfd, link_info) |
| 2198 | bfd *output_bfd; |
| 2199 | struct bfd_link_info *link_info; |
| 2200 | { |
| 2201 | def_file_module *module; |
| 2202 | |
| 2203 | pe_dll_id_target (bfd_get_target (output_bfd)); |
| 2204 | |
| 2205 | if (!pe_def_file) |
| 2206 | return; |
| 2207 | |
| 2208 | for (module = pe_def_file->modules; module; module = module->next) |
| 2209 | { |
| 2210 | int i, do_this_dll; |
| 2211 | |
| 2212 | dll_filename = module->name; |
| 2213 | dll_symname = xstrdup (module->name); |
| 2214 | for (i = 0; dll_symname[i]; i++) |
| 2215 | if (!ISALNUM (dll_symname[i])) |
| 2216 | dll_symname[i] = '_'; |
| 2217 | |
| 2218 | do_this_dll = 0; |
| 2219 | |
| 2220 | for (i = 0; i < pe_def_file->num_imports; i++) |
| 2221 | if (pe_def_file->imports[i].module == module) |
| 2222 | { |
| 2223 | def_file_export exp; |
| 2224 | struct bfd_link_hash_entry *blhe; |
| 2225 | |
| 2226 | /* See if we need this import. */ |
| 2227 | char *name = (char *) xmalloc (strlen (pe_def_file->imports[i].internal_name) + 2 + 6); |
| 2228 | sprintf (name, "%s%s", U (""), pe_def_file->imports[i].internal_name); |
| 2229 | blhe = bfd_link_hash_lookup (link_info->hash, name, |
| 2230 | false, false, false); |
| 2231 | if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined)) |
| 2232 | { |
| 2233 | sprintf (name, "%s%s", U ("_imp__"), |
| 2234 | pe_def_file->imports[i].internal_name); |
| 2235 | blhe = bfd_link_hash_lookup (link_info->hash, name, |
| 2236 | false, false, false); |
| 2237 | } |
| 2238 | free (name); |
| 2239 | if (blhe && blhe->type == bfd_link_hash_undefined) |
| 2240 | { |
| 2241 | bfd *one; |
| 2242 | /* We do. */ |
| 2243 | if (!do_this_dll) |
| 2244 | { |
| 2245 | bfd *ar_head = make_head (output_bfd); |
| 2246 | add_bfd_to_link (ar_head, ar_head->filename, link_info); |
| 2247 | do_this_dll = 1; |
| 2248 | } |
| 2249 | exp.internal_name = pe_def_file->imports[i].internal_name; |
| 2250 | exp.name = pe_def_file->imports[i].name; |
| 2251 | exp.ordinal = pe_def_file->imports[i].ordinal; |
| 2252 | exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0; |
| 2253 | exp.flag_private = 0; |
| 2254 | exp.flag_constant = 0; |
| 2255 | exp.flag_data = 0; |
| 2256 | exp.flag_noname = exp.name ? 0 : 1; |
| 2257 | one = make_one (&exp, output_bfd); |
| 2258 | add_bfd_to_link (one, one->filename, link_info); |
| 2259 | } |
| 2260 | } |
| 2261 | if (do_this_dll) |
| 2262 | { |
| 2263 | bfd *ar_tail = make_tail (output_bfd); |
| 2264 | add_bfd_to_link (ar_tail, ar_tail->filename, link_info); |
| 2265 | } |
| 2266 | |
| 2267 | free (dll_symname); |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | /* We were handed a *.DLL file. Parse it and turn it into a set of |
| 2272 | IMPORTS directives in the def file. Return true if the file was |
| 2273 | handled, false if not. */ |
| 2274 | |
| 2275 | static unsigned int |
| 2276 | pe_get16 (abfd, where) |
| 2277 | bfd *abfd; |
| 2278 | int where; |
| 2279 | { |
| 2280 | unsigned char b[2]; |
| 2281 | |
| 2282 | bfd_seek (abfd, (file_ptr) where, SEEK_SET); |
| 2283 | bfd_bread (b, (bfd_size_type) 2, abfd); |
| 2284 | return b[0] + (b[1] << 8); |
| 2285 | } |
| 2286 | |
| 2287 | static unsigned int |
| 2288 | pe_get32 (abfd, where) |
| 2289 | bfd *abfd; |
| 2290 | int where; |
| 2291 | { |
| 2292 | unsigned char b[4]; |
| 2293 | |
| 2294 | bfd_seek (abfd, (file_ptr) where, SEEK_SET); |
| 2295 | bfd_bread (b, (bfd_size_type) 4, abfd); |
| 2296 | return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24); |
| 2297 | } |
| 2298 | |
| 2299 | #if 0 /* This is not currently used. */ |
| 2300 | |
| 2301 | static unsigned int |
| 2302 | pe_as16 (ptr) |
| 2303 | void *ptr; |
| 2304 | { |
| 2305 | unsigned char *b = ptr; |
| 2306 | |
| 2307 | return b[0] + (b[1] << 8); |
| 2308 | } |
| 2309 | |
| 2310 | #endif |
| 2311 | |
| 2312 | static unsigned int |
| 2313 | pe_as32 (ptr) |
| 2314 | void *ptr; |
| 2315 | { |
| 2316 | unsigned char *b = ptr; |
| 2317 | |
| 2318 | return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24); |
| 2319 | } |
| 2320 | |
| 2321 | boolean |
| 2322 | pe_implied_import_dll (filename) |
| 2323 | const char *filename; |
| 2324 | { |
| 2325 | bfd *dll; |
| 2326 | unsigned long pe_header_offset, opthdr_ofs, num_entries, i; |
| 2327 | unsigned long export_rva, export_size, nsections, secptr, expptr; |
| 2328 | unsigned char *expdata, *erva; |
| 2329 | unsigned long name_rvas, ordinals, nexp, ordbase; |
| 2330 | const char *dll_name; |
| 2331 | |
| 2332 | /* No, I can't use bfd here. kernel32.dll puts its export table in |
| 2333 | the middle of the .rdata section. */ |
| 2334 | dll = bfd_openr (filename, pe_details->target_name); |
| 2335 | if (!dll) |
| 2336 | { |
| 2337 | einfo ("%Xopen %s: %s\n", filename, bfd_errmsg (bfd_get_error ())); |
| 2338 | return false; |
| 2339 | } |
| 2340 | |
| 2341 | /* PEI dlls seem to be bfd_objects. */ |
| 2342 | if (!bfd_check_format (dll, bfd_object)) |
| 2343 | { |
| 2344 | einfo ("%X%s: this doesn't appear to be a DLL\n", filename); |
| 2345 | return false; |
| 2346 | } |
| 2347 | |
| 2348 | dll_name = filename; |
| 2349 | for (i = 0; filename[i]; i++) |
| 2350 | if (filename[i] == '/' || filename[i] == '\\' || filename[i] == ':') |
| 2351 | dll_name = filename + i + 1; |
| 2352 | |
| 2353 | pe_header_offset = pe_get32 (dll, 0x3c); |
| 2354 | opthdr_ofs = pe_header_offset + 4 + 20; |
| 2355 | num_entries = pe_get32 (dll, opthdr_ofs + 92); |
| 2356 | |
| 2357 | if (num_entries < 1) /* No exports. */ |
| 2358 | return false; |
| 2359 | |
| 2360 | export_rva = pe_get32 (dll, opthdr_ofs + 96); |
| 2361 | export_size = pe_get32 (dll, opthdr_ofs + 100); |
| 2362 | nsections = pe_get16 (dll, pe_header_offset + 4 + 2); |
| 2363 | secptr = (pe_header_offset + 4 + 20 + |
| 2364 | pe_get16 (dll, pe_header_offset + 4 + 16)); |
| 2365 | expptr = 0; |
| 2366 | |
| 2367 | for (i = 0; i < nsections; i++) |
| 2368 | { |
| 2369 | char sname[8]; |
| 2370 | unsigned long secptr1 = secptr + 40 * i; |
| 2371 | unsigned long vaddr = pe_get32 (dll, secptr1 + 12); |
| 2372 | unsigned long vsize = pe_get32 (dll, secptr1 + 16); |
| 2373 | unsigned long fptr = pe_get32 (dll, secptr1 + 20); |
| 2374 | |
| 2375 | bfd_seek (dll, (file_ptr) secptr1, SEEK_SET); |
| 2376 | bfd_bread (sname, (bfd_size_type) 8, dll); |
| 2377 | |
| 2378 | if (vaddr <= export_rva && vaddr + vsize > export_rva) |
| 2379 | { |
| 2380 | expptr = fptr + (export_rva - vaddr); |
| 2381 | if (export_rva + export_size > vaddr + vsize) |
| 2382 | export_size = vsize - (export_rva - vaddr); |
| 2383 | break; |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | expdata = (unsigned char *) xmalloc (export_size); |
| 2388 | bfd_seek (dll, (file_ptr) expptr, SEEK_SET); |
| 2389 | bfd_bread (expdata, (bfd_size_type) export_size, dll); |
| 2390 | erva = expdata - export_rva; |
| 2391 | |
| 2392 | if (pe_def_file == 0) |
| 2393 | pe_def_file = def_file_empty (); |
| 2394 | |
| 2395 | nexp = pe_as32 (expdata + 24); |
| 2396 | name_rvas = pe_as32 (expdata + 32); |
| 2397 | ordinals = pe_as32 (expdata + 36); |
| 2398 | ordbase = pe_as32 (expdata + 16); |
| 2399 | |
| 2400 | for (i = 0; i < nexp; i++) |
| 2401 | { |
| 2402 | unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4); |
| 2403 | def_file_import *imp; |
| 2404 | |
| 2405 | imp = def_file_add_import (pe_def_file, erva + name_rva, dll_name, |
| 2406 | i, 0); |
| 2407 | } |
| 2408 | |
| 2409 | return true; |
| 2410 | } |
| 2411 | |
| 2412 | /* These are the main functions, called from the emulation. The first |
| 2413 | is called after the bfds are read, so we can guess at how much space |
| 2414 | we need. The second is called after everything is placed, so we |
| 2415 | can put the right values in place. */ |
| 2416 | |
| 2417 | void |
| 2418 | pe_dll_build_sections (abfd, info) |
| 2419 | bfd *abfd; |
| 2420 | struct bfd_link_info *info; |
| 2421 | { |
| 2422 | pe_dll_id_target (bfd_get_target (abfd)); |
| 2423 | process_def_file (abfd, info); |
| 2424 | |
| 2425 | generate_edata (abfd, info); |
| 2426 | build_filler_bfd (1); |
| 2427 | } |
| 2428 | |
| 2429 | void |
| 2430 | pe_exe_build_sections (abfd, info) |
| 2431 | bfd *abfd; |
| 2432 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
| 2433 | { |
| 2434 | pe_dll_id_target (bfd_get_target (abfd)); |
| 2435 | build_filler_bfd (0); |
| 2436 | } |
| 2437 | |
| 2438 | void |
| 2439 | pe_dll_fill_sections (abfd, info) |
| 2440 | bfd *abfd; |
| 2441 | struct bfd_link_info *info; |
| 2442 | { |
| 2443 | pe_dll_id_target (bfd_get_target (abfd)); |
| 2444 | image_base = pe_data (abfd)->pe_opthdr.ImageBase; |
| 2445 | |
| 2446 | generate_reloc (abfd, info); |
| 2447 | if (reloc_sz > 0) |
| 2448 | { |
| 2449 | bfd_set_section_size (filler_bfd, reloc_s, reloc_sz); |
| 2450 | |
| 2451 | /* Resize the sections. */ |
| 2452 | lang_size_sections (stat_ptr->head, abs_output_section, |
| 2453 | &stat_ptr->head, 0, (bfd_vma) 0, NULL); |
| 2454 | |
| 2455 | /* Redo special stuff. */ |
| 2456 | ldemul_after_allocation (); |
| 2457 | |
| 2458 | /* Do the assignments again. */ |
| 2459 | lang_do_assignments (stat_ptr->head, |
| 2460 | abs_output_section, |
| 2461 | (fill_type *) 0, (bfd_vma) 0); |
| 2462 | } |
| 2463 | |
| 2464 | fill_edata (abfd, info); |
| 2465 | |
| 2466 | pe_data (abfd)->dll = 1; |
| 2467 | |
| 2468 | edata_s->contents = edata_d; |
| 2469 | reloc_s->contents = reloc_d; |
| 2470 | } |
| 2471 | |
| 2472 | void |
| 2473 | pe_exe_fill_sections (abfd, info) |
| 2474 | bfd *abfd; |
| 2475 | struct bfd_link_info *info; |
| 2476 | { |
| 2477 | pe_dll_id_target (bfd_get_target (abfd)); |
| 2478 | image_base = pe_data (abfd)->pe_opthdr.ImageBase; |
| 2479 | |
| 2480 | generate_reloc (abfd, info); |
| 2481 | if (reloc_sz > 0) |
| 2482 | { |
| 2483 | bfd_set_section_size (filler_bfd, reloc_s, reloc_sz); |
| 2484 | |
| 2485 | /* Resize the sections. */ |
| 2486 | lang_size_sections (stat_ptr->head, abs_output_section, |
| 2487 | &stat_ptr->head, 0, (bfd_vma) 0, NULL); |
| 2488 | |
| 2489 | /* Redo special stuff. */ |
| 2490 | ldemul_after_allocation (); |
| 2491 | |
| 2492 | /* Do the assignments again. */ |
| 2493 | lang_do_assignments (stat_ptr->head, |
| 2494 | abs_output_section, |
| 2495 | (fill_type *) 0, (bfd_vma) 0); |
| 2496 | } |
| 2497 | reloc_s->contents = reloc_d; |
| 2498 | } |