Home | History | Annotate | Line # | Download | only in ld
      1       1.1     skrll /* Routines to help build PEI-format DLLs (Win32 etc)
      2  1.1.1.12  christos    Copyright (C) 1998-2026 Free Software Foundation, Inc.
      3       1.1     skrll    Written by DJ Delorie <dj (at) cygnus.com>
      4       1.1     skrll 
      5       1.1     skrll    This file is part of the GNU Binutils.
      6       1.1     skrll 
      7       1.1     skrll    This program is free software; you can redistribute it and/or modify
      8       1.1     skrll    it under the terms of the GNU General Public License as published by
      9       1.1     skrll    the Free Software Foundation; either version 3 of the License, or
     10       1.1     skrll    (at your option) any later version.
     11       1.1     skrll 
     12       1.1     skrll    This program is distributed in the hope that it will be useful,
     13       1.1     skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14       1.1     skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15       1.1     skrll    GNU General Public License for more details.
     16       1.1     skrll 
     17       1.1     skrll    You should have received a copy of the GNU General Public License
     18       1.1     skrll    along with this program; if not, write to the Free Software
     19       1.1     skrll    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20       1.1     skrll    MA 02110-1301, USA.  */
     21       1.1     skrll 
     22       1.1     skrll #include "sysdep.h"
     23       1.1     skrll #include "bfd.h"
     24       1.1     skrll #include "bfdlink.h"
     25       1.1     skrll #include "libiberty.h"
     26   1.1.1.3  christos #include "filenames.h"
     27       1.1     skrll #include "safe-ctype.h"
     28   1.1.1.8  christos #include "ctf-api.h"
     29       1.1     skrll 
     30       1.1     skrll #include <time.h>
     31       1.1     skrll 
     32       1.1     skrll #include "ld.h"
     33       1.1     skrll #include "ldexp.h"
     34       1.1     skrll #include "ldlang.h"
     35       1.1     skrll #include "ldwrite.h"
     36       1.1     skrll #include "ldmisc.h"
     37       1.1     skrll #include <ldgram.h>
     38       1.1     skrll #include "ldmain.h"
     39       1.1     skrll #include "ldfile.h"
     40       1.1     skrll #include "ldemul.h"
     41       1.1     skrll #include "coff/internal.h"
     42       1.1     skrll #include "../bfd/libcoff.h"
     43       1.1     skrll #include "deffile.h"
     44       1.1     skrll 
     45  1.1.1.10  christos #ifdef pe_use_plus
     46       1.1     skrll 
     47       1.1     skrll #define PE_IDATA4_SIZE	8
     48       1.1     skrll #define PE_IDATA5_SIZE	8
     49       1.1     skrll #include "pep-dll.h"
     50       1.1     skrll #undef  AOUTSZ
     51       1.1     skrll #define AOUTSZ		PEPAOUTSZ
     52       1.1     skrll #define PEAOUTHDR	PEPAOUTHDR
     53       1.1     skrll 
     54       1.1     skrll #else
     55       1.1     skrll 
     56       1.1     skrll #include "pe-dll.h"
     57       1.1     skrll 
     58       1.1     skrll #endif
     59       1.1     skrll 
     60       1.1     skrll #ifndef PE_IDATA4_SIZE
     61       1.1     skrll #define PE_IDATA4_SIZE	4
     62       1.1     skrll #endif
     63       1.1     skrll 
     64       1.1     skrll #ifndef PE_IDATA5_SIZE
     65       1.1     skrll #define PE_IDATA5_SIZE	4
     66       1.1     skrll #endif
     67       1.1     skrll 
     68       1.1     skrll /*  This file turns a regular Windows PE image into a DLL.  Because of
     69       1.1     skrll     the complexity of this operation, it has been broken down into a
     70       1.1     skrll     number of separate modules which are all called by the main function
     71       1.1     skrll     at the end of this file.  This function is not re-entrant and is
     72       1.1     skrll     normally only called once, so static variables are used to reduce
     73       1.1     skrll     the number of parameters and return values required.
     74       1.1     skrll 
     75       1.1     skrll     See also: ld/emultempl/pe.em and ld/emultempl/pep.em.  */
     76       1.1     skrll 
     77       1.1     skrll /*  Auto-import feature by Paul Sokolovsky
     78       1.1     skrll 
     79       1.1     skrll     Quick facts:
     80       1.1     skrll 
     81       1.1     skrll     1. With this feature on, DLL clients can import variables from DLL
     82       1.1     skrll     without any concern from their side (for example, without any source
     83       1.1     skrll     code modifications).
     84       1.1     skrll 
     85       1.1     skrll     2. This is done completely in bounds of the PE specification (to be fair,
     86       1.1     skrll     there's a place where it pokes nose out of, but in practice it works).
     87       1.1     skrll     So, resulting module can be used with any other PE compiler/linker.
     88       1.1     skrll 
     89       1.1     skrll     3. Auto-import is fully compatible with standard import method and they
     90       1.1     skrll     can be mixed together.
     91       1.1     skrll 
     92       1.1     skrll     4. Overheads: space: 8 bytes per imported symbol, plus 20 for each
     93       1.1     skrll     reference to it; load time: negligible; virtual/physical memory: should be
     94       1.1     skrll     less than effect of DLL relocation, and I sincerely hope it doesn't affect
     95       1.1     skrll     DLL sharability (too much).
     96       1.1     skrll 
     97       1.1     skrll     Idea
     98       1.1     skrll 
     99       1.1     skrll     The obvious and only way to get rid of dllimport insanity is to make client
    100       1.1     skrll     access variable directly in the DLL, bypassing extra dereference. I.e.,
    101       1.1     skrll     whenever client contains something like
    102       1.1     skrll 
    103       1.1     skrll     mov dll_var,%eax,
    104       1.1     skrll 
    105       1.1     skrll     address of dll_var in the command should be relocated to point into loaded
    106       1.1     skrll     DLL. The aim is to make OS loader do so, and than make ld help with that.
    107       1.1     skrll     Import section of PE made following way: there's a vector of structures
    108       1.1     skrll     each describing imports from particular DLL. Each such structure points
    109       1.1     skrll     to two other parallel vectors: one holding imported names, and one which
    110       1.1     skrll     will hold address of corresponding imported name. So, the solution is
    111       1.1     skrll     de-vectorize these structures, making import locations be sparse and
    112       1.1     skrll     pointing directly into code. Before continuing, it is worth a note that,
    113       1.1     skrll     while authors strives to make PE act ELF-like, there're some other people
    114       1.1     skrll     make ELF act PE-like: elfvector, ;-) .
    115       1.1     skrll 
    116       1.1     skrll     Implementation
    117       1.1     skrll 
    118       1.1     skrll     For each reference of data symbol to be imported from DLL (to set of which
    119       1.1     skrll     belong symbols with name <sym>, if __imp_<sym> is found in implib), the
    120       1.1     skrll     import fixup entry is generated. That entry is of type
    121       1.1     skrll     IMAGE_IMPORT_DESCRIPTOR and stored in .idata$2 subsection. Each
    122       1.1     skrll     fixup entry contains pointer to symbol's address within .text section
    123       1.1     skrll     (marked with __fuN_<sym> symbol, where N is integer), pointer to DLL name
    124       1.1     skrll     (so, DLL name is referenced by multiple entries), and pointer to symbol
    125       1.1     skrll     name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>)
    126       1.1     skrll     pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly
    127       1.1     skrll     containing imported name. Here comes that "on the edge" problem mentioned
    128       1.1     skrll     above: PE specification rambles that name vector (OriginalFirstThunk)
    129       1.1     skrll     should run in parallel with addresses vector (FirstThunk), i.e. that they
    130       1.1     skrll     should have same number of elements and terminated with zero. We violate
    131       1.1     skrll     this, since FirstThunk points directly into machine code. But in practice,
    132   1.1.1.6  christos     OS loader implemented the sane way: it goes through OriginalFirstThunk and
    133       1.1     skrll     puts addresses to FirstThunk, not something else. It once again should be
    134       1.1     skrll     noted that dll and symbol name structures are reused across fixup entries
    135       1.1     skrll     and should be there anyway to support standard import stuff, so sustained
    136       1.1     skrll     overhead is 20 bytes per reference. Other question is whether having several
    137       1.1     skrll     IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, it is
    138       1.1     skrll     done even by native compiler/linker (libth32's functions are in fact reside
    139       1.1     skrll     in windows9x kernel32.dll, so if you use it, you have two
    140       1.1     skrll     IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether
    141       1.1     skrll     referencing the same PE structures several times is valid. The answer is why
    142       1.1     skrll     not, prohibiting that (detecting violation) would require more work on
    143       1.1     skrll     behalf of loader than not doing it.
    144       1.1     skrll 
    145       1.1     skrll     See also: ld/emultempl/pe.em and ld/emultempl/pep.em.  */
    146       1.1     skrll 
    147       1.1     skrll static void add_bfd_to_link (bfd *, const char *, struct bfd_link_info *);
    148       1.1     skrll 
    149       1.1     skrll /* For emultempl/pe.em.  */
    150       1.1     skrll 
    151       1.1     skrll def_file * pe_def_file = 0;
    152       1.1     skrll int pe_dll_export_everything = 0;
    153   1.1.1.2  christos int pe_dll_exclude_all_symbols = 0;
    154       1.1     skrll int pe_dll_do_default_excludes = 1;
    155       1.1     skrll int pe_dll_kill_ats = 0;
    156       1.1     skrll int pe_dll_stdcall_aliases = 0;
    157       1.1     skrll int pe_dll_warn_dup_exports = 0;
    158       1.1     skrll int pe_dll_compat_implib = 0;
    159       1.1     skrll int pe_dll_extra_pe_debug = 0;
    160   1.1.1.2  christos int pe_use_nul_prefixed_import_tables = 0;
    161   1.1.1.2  christos int pe_use_coff_long_section_names = -1;
    162   1.1.1.2  christos int pe_leading_underscore = -1;
    163   1.1.1.9  christos int pe_dll_enable_reloc_section = 1;
    164       1.1     skrll 
    165       1.1     skrll /* Static variables and types.  */
    166       1.1     skrll 
    167       1.1     skrll static bfd_vma image_base;
    168       1.1     skrll static bfd *filler_bfd;
    169       1.1     skrll static struct bfd_section *edata_s, *reloc_s;
    170       1.1     skrll static unsigned char *edata_d, *reloc_d;
    171  1.1.1.10  christos static unsigned char *reloc_d = NULL;
    172  1.1.1.10  christos static size_t edata_sz, reloc_sz = 0;
    173       1.1     skrll static int runtime_pseudo_relocs_created = 0;
    174   1.1.1.9  christos static bool runtime_pseudp_reloc_v2_init = false;
    175       1.1     skrll 
    176       1.1     skrll typedef struct
    177       1.1     skrll {
    178       1.1     skrll   const char *name;
    179       1.1     skrll   int len;
    180       1.1     skrll }
    181       1.1     skrll autofilter_entry_type;
    182       1.1     skrll 
    183       1.1     skrll typedef struct
    184       1.1     skrll {
    185       1.1     skrll   const char *target_name;
    186       1.1     skrll   const char *object_target;
    187       1.1     skrll   unsigned int imagebase_reloc;
    188  1.1.1.11  christos   unsigned int secrel_reloc_lo;
    189  1.1.1.11  christos   unsigned int secrel_reloc_hi;
    190  1.1.1.11  christos   unsigned int section_reloc;
    191       1.1     skrll   int pe_arch;
    192       1.1     skrll   int bfd_arch;
    193   1.1.1.9  christos   bool underscored;
    194   1.1.1.2  christos   const autofilter_entry_type* autofilter_symbollist;
    195       1.1     skrll }
    196       1.1     skrll pe_details_type;
    197       1.1     skrll 
    198       1.1     skrll static const autofilter_entry_type autofilter_symbollist_generic[] =
    199       1.1     skrll {
    200       1.1     skrll   { STRING_COMMA_LEN ("_NULL_IMPORT_DESCRIPTOR") },
    201       1.1     skrll   /* Entry point symbols.  */
    202       1.1     skrll   { STRING_COMMA_LEN ("DllMain") },
    203       1.1     skrll   { STRING_COMMA_LEN ("DllMainCRTStartup") },
    204       1.1     skrll   { STRING_COMMA_LEN ("_DllMainCRTStartup") },
    205       1.1     skrll   /* Runtime pseudo-reloc.  */
    206       1.1     skrll   { STRING_COMMA_LEN ("_pei386_runtime_relocator") },
    207       1.1     skrll   { STRING_COMMA_LEN ("do_pseudo_reloc") },
    208       1.1     skrll   { NULL, 0 }
    209       1.1     skrll };
    210       1.1     skrll 
    211       1.1     skrll static const autofilter_entry_type autofilter_symbollist_i386[] =
    212       1.1     skrll {
    213       1.1     skrll   { STRING_COMMA_LEN ("_NULL_IMPORT_DESCRIPTOR") },
    214       1.1     skrll   /* Entry point symbols, and entry hooks.  */
    215       1.1     skrll   { STRING_COMMA_LEN ("cygwin_crt0") },
    216  1.1.1.10  christos #ifdef pe_use_plus
    217       1.1     skrll   { STRING_COMMA_LEN ("DllMain") },
    218       1.1     skrll   { STRING_COMMA_LEN ("DllEntryPoint") },
    219       1.1     skrll   { STRING_COMMA_LEN ("DllMainCRTStartup") },
    220       1.1     skrll   { STRING_COMMA_LEN ("_cygwin_dll_entry") },
    221       1.1     skrll   { STRING_COMMA_LEN ("_cygwin_crt0_common") },
    222       1.1     skrll   { STRING_COMMA_LEN ("_cygwin_noncygwin_dll_entry") },
    223       1.1     skrll #else
    224       1.1     skrll   { STRING_COMMA_LEN ("DllMain@12") },
    225       1.1     skrll   { STRING_COMMA_LEN ("DllEntryPoint@0") },
    226       1.1     skrll   { STRING_COMMA_LEN ("DllMainCRTStartup@12") },
    227       1.1     skrll   { STRING_COMMA_LEN ("_cygwin_dll_entry@12") },
    228       1.1     skrll   { STRING_COMMA_LEN ("_cygwin_crt0_common@8") },
    229       1.1     skrll   { STRING_COMMA_LEN ("_cygwin_noncygwin_dll_entry@12") },
    230       1.1     skrll   { STRING_COMMA_LEN ("cygwin_attach_dll") },
    231   1.1.1.2  christos #endif
    232       1.1     skrll   { STRING_COMMA_LEN ("cygwin_premain0") },
    233       1.1     skrll   { STRING_COMMA_LEN ("cygwin_premain1") },
    234       1.1     skrll   { STRING_COMMA_LEN ("cygwin_premain2") },
    235       1.1     skrll   { STRING_COMMA_LEN ("cygwin_premain3") },
    236       1.1     skrll   /* Runtime pseudo-reloc.  */
    237       1.1     skrll   { STRING_COMMA_LEN ("_pei386_runtime_relocator") },
    238       1.1     skrll   { STRING_COMMA_LEN ("do_pseudo_reloc") },
    239       1.1     skrll   /* Global vars that should not be exported.  */
    240       1.1     skrll   { STRING_COMMA_LEN ("impure_ptr") },
    241       1.1     skrll   { STRING_COMMA_LEN ("_impure_ptr") },
    242       1.1     skrll   { STRING_COMMA_LEN ("_fmode") },
    243       1.1     skrll   { STRING_COMMA_LEN ("environ") },
    244   1.1.1.4  christos   { STRING_COMMA_LEN ("__dso_handle") },
    245       1.1     skrll   { NULL, 0 }
    246       1.1     skrll };
    247       1.1     skrll 
    248  1.1.1.12  christos /* Internal identification of PE architectures.  */
    249  1.1.1.12  christos enum
    250  1.1.1.12  christos {
    251  1.1.1.12  christos   PE_ARCH_none,
    252  1.1.1.12  christos   PE_ARCH_i386,
    253  1.1.1.12  christos   PE_ARCH_sh,
    254  1.1.1.12  christos   PE_ARCH_arm,
    255  1.1.1.12  christos   PE_ARCH_arm_wince,
    256  1.1.1.12  christos   PE_ARCH_aarch64,
    257  1.1.1.12  christos   PE_ARCH_mcore,
    258  1.1.1.12  christos };
    259       1.1     skrll 
    260   1.1.1.2  christos /* Don't make it constant as underscore mode gets possibly overriden
    261   1.1.1.2  christos    by target or -(no-)leading-underscore option.  */
    262   1.1.1.2  christos static pe_details_type pe_detail_list[] =
    263       1.1     skrll {
    264       1.1     skrll   {
    265  1.1.1.10  christos #ifdef pe_use_plus
    266       1.1     skrll     "pei-x86-64",
    267       1.1     skrll     "pe-x86-64",
    268  1.1.1.11  christos     3 /* R_AMD64_IMAGEBASE */,
    269  1.1.1.11  christos     11 /* R_AMD64_SECREL32 */,
    270  1.1.1.11  christos     12 /* R_AMD64_SECREL7 */,
    271  1.1.1.11  christos     10 /* R_AMD64_SECTION */,
    272       1.1     skrll #else
    273       1.1     skrll     "pei-i386",
    274       1.1     skrll     "pe-i386",
    275       1.1     skrll     7 /* R_IMAGEBASE */,
    276  1.1.1.11  christos     11, 11 /* R_SECREL32 */,
    277  1.1.1.11  christos     10 /* R_SECTION */,
    278       1.1     skrll #endif
    279       1.1     skrll     PE_ARCH_i386,
    280       1.1     skrll     bfd_arch_i386,
    281  1.1.1.10  christos #ifdef pe_use_plus
    282   1.1.1.9  christos     false,
    283   1.1.1.2  christos #else
    284   1.1.1.9  christos     true,
    285   1.1.1.2  christos #endif
    286       1.1     skrll     autofilter_symbollist_i386
    287       1.1     skrll   },
    288  1.1.1.10  christos #ifdef pe_use_plus
    289   1.1.1.6  christos   {
    290   1.1.1.6  christos     "pei-x86-64",
    291   1.1.1.6  christos     "pe-bigobj-x86-64",
    292  1.1.1.11  christos     3 /* R_AMD64_IMAGEBASE */,
    293  1.1.1.11  christos     11 /* R_AMD64_SECREL32 */,
    294  1.1.1.11  christos     12 /* R_AMD64_SECREL7 */,
    295  1.1.1.11  christos     10 /* R_AMD64_SECTION */,
    296   1.1.1.6  christos     PE_ARCH_i386,
    297   1.1.1.6  christos     bfd_arch_i386,
    298   1.1.1.9  christos     false,
    299   1.1.1.9  christos     autofilter_symbollist_i386
    300   1.1.1.9  christos   },
    301   1.1.1.9  christos #else
    302   1.1.1.9  christos   {
    303   1.1.1.9  christos     "pei-i386",
    304   1.1.1.9  christos     "pe-bigobj-i386",
    305   1.1.1.9  christos     7 /* R_IMAGEBASE */,
    306  1.1.1.11  christos     11, 11 /* R_SECREL32 */,
    307  1.1.1.11  christos     10 /* R_SECTION */,
    308   1.1.1.9  christos     PE_ARCH_i386,
    309   1.1.1.9  christos     bfd_arch_i386,
    310   1.1.1.9  christos     true,
    311   1.1.1.6  christos     autofilter_symbollist_i386
    312   1.1.1.6  christos   },
    313       1.1     skrll   {
    314       1.1     skrll     "pei-shl",
    315       1.1     skrll     "pe-shl",
    316       1.1     skrll     16 /* R_SH_IMAGEBASE */,
    317  1.1.1.11  christos     ~0, 0, ~0, /* none */
    318       1.1     skrll     PE_ARCH_sh,
    319       1.1     skrll     bfd_arch_sh,
    320   1.1.1.9  christos     true,
    321       1.1     skrll     autofilter_symbollist_generic
    322       1.1     skrll   },
    323       1.1     skrll   {
    324  1.1.1.12  christos     "pei-mcore-little",
    325  1.1.1.12  christos     "pe-mcore-little",
    326  1.1.1.12  christos     7 /* IMAGE_REL_MCORE_RVA */,
    327  1.1.1.11  christos     ~0, 0, ~0, /* none */
    328  1.1.1.12  christos     PE_ARCH_mcore,
    329  1.1.1.12  christos     bfd_arch_mcore,
    330  1.1.1.12  christos     false,
    331  1.1.1.12  christos     autofilter_symbollist_generic
    332  1.1.1.12  christos   },
    333  1.1.1.12  christos   {
    334  1.1.1.12  christos     "pei-mcore-big",
    335  1.1.1.12  christos     "pe-mcore-big",
    336  1.1.1.12  christos     7 /* IMAGE_REL_MCORE_RVA */,
    337  1.1.1.12  christos     ~0, 0, ~0, /* none */
    338  1.1.1.12  christos     PE_ARCH_mcore,
    339  1.1.1.12  christos     bfd_arch_mcore,
    340   1.1.1.9  christos     false,
    341       1.1     skrll     autofilter_symbollist_generic
    342       1.1     skrll   },
    343       1.1     skrll   {
    344       1.1     skrll     "pei-arm-little",
    345       1.1     skrll     "pe-arm-little",
    346       1.1     skrll     11 /* ARM_RVA32 */,
    347  1.1.1.11  christos     ~0, 0, ~0, /* none */
    348       1.1     skrll     PE_ARCH_arm,
    349       1.1     skrll     bfd_arch_arm,
    350   1.1.1.9  christos     true,
    351       1.1     skrll     autofilter_symbollist_generic
    352       1.1     skrll   },
    353       1.1     skrll   {
    354       1.1     skrll     "pei-arm-wince-little",
    355       1.1     skrll     "pe-arm-wince-little",
    356       1.1     skrll     2,  /* ARM_RVA32 on Windows CE, see bfd/coff-arm.c.  */
    357  1.1.1.11  christos     15, 15, /* ARM_SECREL (dito) */
    358  1.1.1.11  christos     14, /* ARM_SECTION (dito) */
    359       1.1     skrll     PE_ARCH_arm_wince,
    360       1.1     skrll     bfd_arch_arm,
    361   1.1.1.9  christos     false,
    362       1.1     skrll     autofilter_symbollist_generic
    363       1.1     skrll   },
    364  1.1.1.12  christos #endif
    365  1.1.1.10  christos   {
    366  1.1.1.10  christos     "pei-aarch64-little",
    367  1.1.1.10  christos     "pe-aarch64-little",
    368  1.1.1.11  christos     2,  /* IMAGE_REL_ARM64_ADDR32NB */
    369  1.1.1.11  christos     8,  /* IMAGE_REL_ARM64_SECREL */
    370  1.1.1.11  christos     11, /* IMAGE_REL_ARM64_SECREL_LOW12L */
    371  1.1.1.11  christos     13, /* IMAGE_REL_ARM64_SECTION */
    372  1.1.1.10  christos     PE_ARCH_aarch64,
    373  1.1.1.10  christos     bfd_arch_aarch64,
    374  1.1.1.10  christos     false,
    375  1.1.1.10  christos     autofilter_symbollist_generic
    376  1.1.1.10  christos   },
    377  1.1.1.12  christos   {
    378  1.1.1.12  christos     "pei-aarch64-little",
    379  1.1.1.12  christos     "pe-bigobj-aarch64-little",
    380  1.1.1.12  christos     2,  /* IMAGE_REL_ARM64_ADDR32NB */
    381  1.1.1.12  christos     8,  /* IMAGE_REL_ARM64_SECREL */
    382  1.1.1.12  christos     11, /* IMAGE_REL_ARM64_SECREL_LOW12L */
    383  1.1.1.12  christos     13, /* IMAGE_REL_ARM64_SECTION */
    384  1.1.1.12  christos     PE_ARCH_aarch64,
    385  1.1.1.12  christos     bfd_arch_aarch64,
    386  1.1.1.12  christos     false,
    387  1.1.1.12  christos     autofilter_symbollist_generic
    388  1.1.1.12  christos   },
    389  1.1.1.11  christos   { NULL, NULL, 0, 0, 0, 0, 0, 0, false, NULL }
    390       1.1     skrll };
    391       1.1     skrll 
    392       1.1     skrll static const pe_details_type *pe_details;
    393       1.1     skrll 
    394       1.1     skrll /* Do not specify library suffix explicitly, to allow for dllized versions.  */
    395       1.1     skrll static const autofilter_entry_type autofilter_liblist[] =
    396       1.1     skrll {
    397       1.1     skrll   { STRING_COMMA_LEN ("libcegcc") },
    398       1.1     skrll   { STRING_COMMA_LEN ("libcygwin") },
    399       1.1     skrll   { STRING_COMMA_LEN ("libgcc") },
    400       1.1     skrll   { STRING_COMMA_LEN ("libgcc_s") },
    401       1.1     skrll   { STRING_COMMA_LEN ("libstdc++") },
    402       1.1     skrll   { STRING_COMMA_LEN ("libmingw32") },
    403       1.1     skrll   { STRING_COMMA_LEN ("libmingwex") },
    404       1.1     skrll   { STRING_COMMA_LEN ("libg2c") },
    405       1.1     skrll   { STRING_COMMA_LEN ("libsupc++") },
    406       1.1     skrll   { STRING_COMMA_LEN ("libobjc") },
    407       1.1     skrll   { STRING_COMMA_LEN ("libgcj") },
    408   1.1.1.6  christos   { STRING_COMMA_LEN ("libmsvcrt") },
    409   1.1.1.6  christos   { STRING_COMMA_LEN ("libmsvcrt-os") },
    410   1.1.1.9  christos   { STRING_COMMA_LEN ("libucrt") },
    411  1.1.1.11  christos   { STRING_COMMA_LEN ("libucrtapp") },
    412   1.1.1.6  christos   { STRING_COMMA_LEN ("libucrtbase") },
    413  1.1.1.11  christos   { STRING_COMMA_LEN ("libpthread") },
    414  1.1.1.11  christos   { STRING_COMMA_LEN ("libwinpthread") },
    415       1.1     skrll   { NULL, 0 }
    416       1.1     skrll };
    417       1.1     skrll 
    418       1.1     skrll /* Regardless of the suffix issue mentioned above, we must ensure that
    419       1.1     skrll   we do not falsely match on a leading substring, such as when libtool
    420       1.1     skrll   builds libstdc++ as a DLL using libsupc++convenience.a as an intermediate.
    421       1.1     skrll   This routine ensures that the leading part of the name matches and that
    422       1.1     skrll   it is followed by only an optional version suffix and a file extension,
    423       1.1     skrll   returning zero if so or -1 if not.  */
    424       1.1     skrll static int libnamencmp (const char *libname, const autofilter_entry_type *afptr)
    425       1.1     skrll {
    426   1.1.1.3  christos   if (filename_ncmp (libname, afptr->name, afptr->len))
    427       1.1     skrll     return -1;
    428       1.1     skrll 
    429       1.1     skrll   libname += afptr->len;
    430       1.1     skrll 
    431       1.1     skrll   /* Be liberal in interpreting what counts as a version suffix; we
    432       1.1     skrll     accept anything that has a dash to separate it from the name and
    433       1.1     skrll     begins with a digit.  */
    434       1.1     skrll   if (libname[0] == '-')
    435       1.1     skrll     {
    436       1.1     skrll       if (!ISDIGIT (*++libname))
    437       1.1     skrll 	return -1;
    438       1.1     skrll       /* Ensure the filename has an extension.  */
    439       1.1     skrll       while (*++libname != '.')
    440       1.1     skrll 	if (!*libname)
    441       1.1     skrll 	  return -1;
    442       1.1     skrll     }
    443       1.1     skrll   else if (libname[0] != '.')
    444       1.1     skrll     return -1;
    445       1.1     skrll 
    446       1.1     skrll   return 0;
    447       1.1     skrll }
    448       1.1     skrll 
    449       1.1     skrll static const autofilter_entry_type autofilter_objlist[] =
    450       1.1     skrll {
    451       1.1     skrll   { STRING_COMMA_LEN ("crt0.o") },
    452       1.1     skrll   { STRING_COMMA_LEN ("crt1.o") },
    453       1.1     skrll   { STRING_COMMA_LEN ("crt2.o") },
    454       1.1     skrll   { STRING_COMMA_LEN ("dllcrt1.o") },
    455       1.1     skrll   { STRING_COMMA_LEN ("dllcrt2.o") },
    456       1.1     skrll   { STRING_COMMA_LEN ("gcrt0.o") },
    457       1.1     skrll   { STRING_COMMA_LEN ("gcrt1.o") },
    458       1.1     skrll   { STRING_COMMA_LEN ("gcrt2.o") },
    459       1.1     skrll   { STRING_COMMA_LEN ("crtbegin.o") },
    460       1.1     skrll   { STRING_COMMA_LEN ("crtend.o") },
    461       1.1     skrll   { NULL, 0 }
    462       1.1     skrll };
    463       1.1     skrll 
    464       1.1     skrll static const autofilter_entry_type autofilter_symbolprefixlist[] =
    465       1.1     skrll {
    466       1.1     skrll   /* _imp_ is treated specially, as it is always underscored.  */
    467       1.1     skrll   /* { STRING_COMMA_LEN ("_imp_") },  */
    468       1.1     skrll   /* Don't export some c++ symbols.  */
    469       1.1     skrll   { STRING_COMMA_LEN ("__rtti_") },
    470       1.1     skrll   { STRING_COMMA_LEN ("__builtin_") },
    471       1.1     skrll   /* Don't re-export auto-imported symbols.  */
    472   1.1.1.2  christos   { STRING_COMMA_LEN ("__nm_") },
    473       1.1     skrll   /* Don't export symbols specifying internal DLL layout.  */
    474       1.1     skrll   { STRING_COMMA_LEN ("_head_") },
    475       1.1     skrll   { STRING_COMMA_LEN ("_IMPORT_DESCRIPTOR_") },
    476       1.1     skrll   /* Don't export section labels or artificial symbols
    477       1.1     skrll   (eg ".weak.foo".  */
    478       1.1     skrll   { STRING_COMMA_LEN (".") },
    479       1.1     skrll   { NULL, 0 }
    480       1.1     skrll };
    481       1.1     skrll 
    482       1.1     skrll static const autofilter_entry_type autofilter_symbolsuffixlist[] =
    483       1.1     skrll {
    484       1.1     skrll   { STRING_COMMA_LEN ("_iname") },
    485       1.1     skrll   { STRING_COMMA_LEN ("_NULL_THUNK_DATA") },
    486       1.1     skrll   { NULL, 0 }
    487       1.1     skrll };
    488       1.1     skrll 
    489       1.1     skrll #define U(str) (pe_details->underscored ? "_" str : str)
    490       1.1     skrll 
    491       1.1     skrll void
    492       1.1     skrll pe_dll_id_target (const char *target)
    493       1.1     skrll {
    494       1.1     skrll   int i;
    495       1.1     skrll 
    496       1.1     skrll   for (i = 0; pe_detail_list[i].target_name; i++)
    497       1.1     skrll     if (strcmp (pe_detail_list[i].target_name, target) == 0
    498       1.1     skrll 	|| strcmp (pe_detail_list[i].object_target, target) == 0)
    499       1.1     skrll       {
    500   1.1.1.2  christos 	int u = pe_leading_underscore; /* Underscoring mode. -1 for use default.  */
    501   1.1.1.2  christos 	if (u == -1)
    502   1.1.1.2  christos 	  bfd_get_target_info (target, NULL, NULL, &u, NULL);
    503   1.1.1.2  christos 	if (u == -1)
    504   1.1.1.2  christos 	  abort ();
    505   1.1.1.9  christos 	pe_detail_list[i].underscored = u != 0;
    506       1.1     skrll 	pe_details = pe_detail_list + i;
    507   1.1.1.2  christos 	pe_leading_underscore = (u != 0 ? 1 : 0);
    508       1.1     skrll 	return;
    509       1.1     skrll       }
    510   1.1.1.7  christos   einfo (_("%X%P: unsupported PEI architecture: %s\n"), target);
    511       1.1     skrll   exit (1);
    512       1.1     skrll }
    513       1.1     skrll 
    514       1.1     skrll /* Helper functions for qsort.  Relocs must be sorted so that we can write
    515       1.1     skrll    them out by pages.  */
    516       1.1     skrll 
    517       1.1     skrll typedef struct
    518       1.1     skrll   {
    519       1.1     skrll     bfd_vma vma;
    520       1.1     skrll     char type;
    521       1.1     skrll     short extra;
    522   1.1.1.8  christos     int idx;
    523       1.1     skrll   }
    524       1.1     skrll reloc_data_type;
    525       1.1     skrll 
    526       1.1     skrll static int
    527       1.1     skrll reloc_sort (const void *va, const void *vb)
    528       1.1     skrll {
    529   1.1.1.8  christos   const reloc_data_type *a = (const reloc_data_type *) va;
    530   1.1.1.8  christos   const reloc_data_type *b = (const reloc_data_type *) vb;
    531       1.1     skrll 
    532   1.1.1.8  christos   if (a->vma > b->vma)
    533   1.1.1.8  christos     return 1;
    534   1.1.1.8  christos   if (a->vma < b->vma)
    535   1.1.1.8  christos     return -1;
    536   1.1.1.8  christos   if (a->idx > b->idx)
    537   1.1.1.8  christos     return 1;
    538   1.1.1.8  christos   if (a->idx < b->idx)
    539   1.1.1.8  christos     return -1;
    540   1.1.1.8  christos   return 0;
    541       1.1     skrll }
    542       1.1     skrll 
    543       1.1     skrll static int
    544       1.1     skrll pe_export_sort (const void *va, const void *vb)
    545       1.1     skrll {
    546       1.1     skrll   const def_file_export *a = va;
    547       1.1     skrll   const def_file_export *b = vb;
    548   1.1.1.2  christos   char *an = a->name;
    549   1.1.1.2  christos   char *bn = b->name;
    550   1.1.1.2  christos   if (a->its_name)
    551   1.1.1.2  christos     an = a->its_name;
    552   1.1.1.2  christos   if (b->its_name)
    553   1.1.1.2  christos     bn = b->its_name;
    554       1.1     skrll 
    555   1.1.1.2  christos   return strcmp (an, bn);
    556       1.1     skrll }
    557       1.1     skrll 
    558       1.1     skrll /* Read and process the .DEF file.  */
    559       1.1     skrll 
    560       1.1     skrll /* These correspond to the entries in pe_def_file->exports[].  I use
    561       1.1     skrll    exported_symbol_sections[i] to tag whether or not the symbol was
    562       1.1     skrll    defined, since we can't export symbols we don't have.  */
    563       1.1     skrll 
    564       1.1     skrll static bfd_vma *exported_symbol_offsets;
    565       1.1     skrll static struct bfd_section **exported_symbol_sections;
    566       1.1     skrll static int export_table_size;
    567       1.1     skrll static int count_exported;
    568       1.1     skrll static int count_exported_byname;
    569       1.1     skrll static int count_with_ordinals;
    570   1.1.1.9  christos static const char *dll_filename;
    571       1.1     skrll static int min_ordinal, max_ordinal;
    572       1.1     skrll static int *exported_symbols;
    573       1.1     skrll 
    574       1.1     skrll typedef struct exclude_list_struct
    575       1.1     skrll   {
    576       1.1     skrll     char *string;
    577       1.1     skrll     struct exclude_list_struct *next;
    578   1.1.1.2  christos     exclude_type type;
    579       1.1     skrll   }
    580       1.1     skrll exclude_list_struct;
    581       1.1     skrll 
    582       1.1     skrll static struct exclude_list_struct *excludes = 0;
    583       1.1     skrll 
    584       1.1     skrll void
    585   1.1.1.2  christos pe_dll_add_excludes (const char *new_excludes, const exclude_type type)
    586       1.1     skrll {
    587       1.1     skrll   char *local_copy;
    588       1.1     skrll   char *exclude_string;
    589       1.1     skrll 
    590       1.1     skrll   local_copy = xstrdup (new_excludes);
    591       1.1     skrll 
    592       1.1     skrll   exclude_string = strtok (local_copy, ",:");
    593       1.1     skrll   for (; exclude_string; exclude_string = strtok (NULL, ",:"))
    594       1.1     skrll     {
    595       1.1     skrll       struct exclude_list_struct *new_exclude;
    596       1.1     skrll 
    597       1.1     skrll       new_exclude = xmalloc (sizeof (struct exclude_list_struct));
    598       1.1     skrll       new_exclude->string = xmalloc (strlen (exclude_string) + 1);
    599       1.1     skrll       strcpy (new_exclude->string, exclude_string);
    600       1.1     skrll       new_exclude->type = type;
    601       1.1     skrll       new_exclude->next = excludes;
    602       1.1     skrll       excludes = new_exclude;
    603       1.1     skrll     }
    604       1.1     skrll 
    605       1.1     skrll   free (local_copy);
    606       1.1     skrll }
    607       1.1     skrll 
    608   1.1.1.9  christos static bool
    609       1.1     skrll is_import (const char* n)
    610       1.1     skrll {
    611   1.1.1.9  christos   return (startswith (n, "__imp_"));
    612       1.1     skrll }
    613       1.1     skrll 
    614       1.1     skrll /* abfd is a bfd containing n (or NULL)
    615       1.1     skrll    It can be used for contextual checks.  */
    616       1.1     skrll 
    617       1.1     skrll static int
    618       1.1     skrll auto_export (bfd *abfd, def_file *d, const char *n)
    619       1.1     skrll {
    620   1.1.1.3  christos   def_file_export key;
    621       1.1     skrll   struct exclude_list_struct *ex;
    622       1.1     skrll   const autofilter_entry_type *afptr;
    623   1.1.1.3  christos   const char * libname = NULL;
    624   1.1.1.3  christos 
    625       1.1     skrll   if (abfd && abfd->my_archive)
    626   1.1.1.9  christos     libname = lbasename (bfd_get_filename (abfd->my_archive));
    627       1.1     skrll 
    628   1.1.1.3  christos   key.name = key.its_name = (char *) n;
    629   1.1.1.3  christos 
    630   1.1.1.3  christos   /* Return false if n is in the d->exports table.  */
    631   1.1.1.9  christos   if (d->num_exports != 0
    632   1.1.1.9  christos       && bsearch (&key, d->exports, d->num_exports,
    633   1.1.1.9  christos 		  sizeof (pe_def_file->exports[0]), pe_export_sort))
    634   1.1.1.3  christos     return 0;
    635       1.1     skrll 
    636       1.1     skrll   if (pe_dll_do_default_excludes)
    637       1.1     skrll     {
    638       1.1     skrll       const char * p;
    639       1.1     skrll       int    len;
    640       1.1     skrll 
    641       1.1     skrll       if (pe_dll_extra_pe_debug)
    642       1.1     skrll 	printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n",
    643       1.1     skrll 		n, abfd, abfd->my_archive);
    644       1.1     skrll 
    645       1.1     skrll       /* First of all, make context checks:
    646       1.1     skrll 	 Don't export anything from standard libs.  */
    647       1.1     skrll       if (libname)
    648       1.1     skrll 	{
    649       1.1     skrll 	  afptr = autofilter_liblist;
    650       1.1     skrll 
    651       1.1     skrll 	  while (afptr->name)
    652       1.1     skrll 	    {
    653       1.1     skrll 	      if (libnamencmp (libname, afptr) == 0 )
    654       1.1     skrll 		return 0;
    655       1.1     skrll 	      afptr++;
    656       1.1     skrll 	    }
    657       1.1     skrll 	}
    658       1.1     skrll 
    659       1.1     skrll       /* Next, exclude symbols from certain startup objects.  */
    660       1.1     skrll 
    661   1.1.1.9  christos       if (abfd && (p = lbasename (bfd_get_filename (abfd))))
    662       1.1     skrll 	{
    663       1.1     skrll 	  afptr = autofilter_objlist;
    664       1.1     skrll 	  while (afptr->name)
    665       1.1     skrll 	    {
    666       1.1     skrll 	      if (strcmp (p, afptr->name) == 0)
    667       1.1     skrll 		return 0;
    668       1.1     skrll 	      afptr++;
    669       1.1     skrll 	    }
    670       1.1     skrll 	}
    671       1.1     skrll 
    672       1.1     skrll       /* Don't try to blindly exclude all symbols
    673       1.1     skrll 	 that begin with '__'; this was tried and
    674       1.1     skrll 	 it is too restrictive.  Instead we have
    675       1.1     skrll 	 a target specific list to use:  */
    676   1.1.1.2  christos       afptr = pe_details->autofilter_symbollist;
    677       1.1     skrll 
    678       1.1     skrll       while (afptr->name)
    679       1.1     skrll 	{
    680       1.1     skrll 	  if (strcmp (n, afptr->name) == 0)
    681       1.1     skrll 	    return 0;
    682       1.1     skrll 
    683       1.1     skrll 	  afptr++;
    684       1.1     skrll 	}
    685       1.1     skrll 
    686       1.1     skrll       /* Next, exclude symbols starting with ...  */
    687       1.1     skrll       afptr = autofilter_symbolprefixlist;
    688       1.1     skrll       while (afptr->name)
    689       1.1     skrll 	{
    690       1.1     skrll 	  if (strncmp (n, afptr->name, afptr->len) == 0)
    691       1.1     skrll 	    return 0;
    692       1.1     skrll 
    693       1.1     skrll 	  afptr++;
    694       1.1     skrll 	}
    695       1.1     skrll 
    696       1.1     skrll       /* Finally, exclude symbols ending with ...  */
    697       1.1     skrll       len = strlen (n);
    698       1.1     skrll       afptr = autofilter_symbolsuffixlist;
    699       1.1     skrll       while (afptr->name)
    700       1.1     skrll 	{
    701       1.1     skrll 	  if ((len >= afptr->len)
    702       1.1     skrll 	      /* Add 1 to insure match with trailing '\0'.  */
    703       1.1     skrll 	      && strncmp (n + len - afptr->len, afptr->name,
    704       1.1     skrll 			  afptr->len + 1) == 0)
    705       1.1     skrll 	    return 0;
    706       1.1     skrll 
    707       1.1     skrll 	  afptr++;
    708       1.1     skrll 	}
    709       1.1     skrll     }
    710       1.1     skrll 
    711       1.1     skrll   for (ex = excludes; ex; ex = ex->next)
    712       1.1     skrll     {
    713   1.1.1.2  christos       if (ex->type == EXCLUDELIBS)
    714       1.1     skrll 	{
    715       1.1     skrll 	  if (libname
    716   1.1.1.3  christos 	      && ((filename_cmp (libname, ex->string) == 0)
    717       1.1     skrll 		   || (strcasecmp ("ALL", ex->string) == 0)))
    718       1.1     skrll 	    return 0;
    719       1.1     skrll 	}
    720   1.1.1.2  christos       else if (ex->type == EXCLUDEFORIMPLIB)
    721   1.1.1.2  christos 	{
    722   1.1.1.9  christos 	  if (filename_cmp (bfd_get_filename (abfd), ex->string) == 0)
    723   1.1.1.2  christos 	    return 0;
    724   1.1.1.2  christos 	}
    725       1.1     skrll       else if (strcmp (n, ex->string) == 0)
    726       1.1     skrll 	return 0;
    727       1.1     skrll     }
    728       1.1     skrll 
    729       1.1     skrll   return 1;
    730       1.1     skrll }
    731       1.1     skrll 
    732       1.1     skrll static void
    733   1.1.1.2  christos process_def_file_and_drectve (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
    734       1.1     skrll {
    735       1.1     skrll   int i, j;
    736  1.1.1.10  christos   unsigned int ui;
    737       1.1     skrll   struct bfd_link_hash_entry *blhe;
    738       1.1     skrll   bfd *b;
    739       1.1     skrll   struct bfd_section *s;
    740       1.1     skrll   def_file_export *e = 0;
    741   1.1.1.9  christos   bool resort_needed;
    742       1.1     skrll 
    743       1.1     skrll   if (!pe_def_file)
    744       1.1     skrll     pe_def_file = def_file_empty ();
    745       1.1     skrll 
    746       1.1     skrll   /* First, run around to all the objects looking for the .drectve
    747       1.1     skrll      sections, and push those into the def file too.  */
    748   1.1.1.4  christos   for (b = info->input_bfds; b; b = b->link.next)
    749       1.1     skrll     {
    750       1.1     skrll       s = bfd_get_section_by_name (b, ".drectve");
    751       1.1     skrll       if (s)
    752       1.1     skrll 	{
    753       1.1     skrll 	  long size = s->size;
    754       1.1     skrll 	  char *buf = xmalloc (size);
    755       1.1     skrll 
    756       1.1     skrll 	  bfd_get_section_contents (b, s, buf, 0, size);
    757       1.1     skrll 	  def_file_add_directive (pe_def_file, buf, size);
    758       1.1     skrll 	  free (buf);
    759       1.1     skrll 	}
    760       1.1     skrll     }
    761       1.1     skrll 
    762   1.1.1.2  christos   /* Process aligned common symbol information from the
    763   1.1.1.2  christos      .drectve sections now; common symbol allocation is
    764   1.1.1.2  christos      done before final link, so it will be too late to
    765   1.1.1.2  christos      process them in process_embedded_commands() called
    766   1.1.1.2  christos      from _bfd_coff_link_input_bfd().  */
    767   1.1.1.2  christos   if (pe_def_file->aligncomms)
    768   1.1.1.2  christos     {
    769   1.1.1.2  christos       def_file_aligncomm *ac = pe_def_file->aligncomms;
    770   1.1.1.2  christos       while (ac)
    771   1.1.1.2  christos 	{
    772   1.1.1.2  christos 	  struct coff_link_hash_entry *sym_hash;
    773   1.1.1.2  christos 	  sym_hash = coff_link_hash_lookup (coff_hash_table (info),
    774   1.1.1.9  christos 					    ac->symbol_name, false, false, false);
    775   1.1.1.2  christos 	  if (sym_hash && sym_hash->root.type == bfd_link_hash_common
    776   1.1.1.6  christos 	      && sym_hash->root.u.c.p->alignment_power < (unsigned) ac->alignment)
    777   1.1.1.2  christos 	    {
    778   1.1.1.2  christos 	      sym_hash->root.u.c.p->alignment_power = (unsigned) ac->alignment;
    779   1.1.1.2  christos 	    }
    780   1.1.1.2  christos 	  ac = ac->next;
    781   1.1.1.2  christos 	}
    782   1.1.1.2  christos     }
    783   1.1.1.2  christos 
    784  1.1.1.10  christos   if (pe_def_file->exclude_symbols)
    785  1.1.1.10  christos     {
    786  1.1.1.10  christos       for (ui = 0; ui < pe_def_file->num_exclude_symbols; ui++)
    787  1.1.1.10  christos 	{
    788  1.1.1.10  christos 	  pe_dll_add_excludes (pe_def_file->exclude_symbols[ui].symbol_name,
    789  1.1.1.10  christos 			       EXCLUDESYMS);
    790  1.1.1.10  christos 	}
    791  1.1.1.10  christos     }
    792  1.1.1.10  christos 
    793   1.1.1.2  christos   /* If we are building an executable and there is nothing
    794   1.1.1.2  christos      to export, we do not build an export table at all.  */
    795   1.1.1.4  christos   if (bfd_link_executable (info) && pe_def_file->num_exports == 0
    796   1.1.1.2  christos       && (!pe_dll_export_everything || pe_dll_exclude_all_symbols))
    797       1.1     skrll     return;
    798       1.1     skrll 
    799       1.1     skrll   /* Now, maybe export everything else the default way.  */
    800   1.1.1.2  christos   if ((pe_dll_export_everything || pe_def_file->num_exports == 0)
    801   1.1.1.2  christos       && !pe_dll_exclude_all_symbols)
    802       1.1     skrll     {
    803   1.1.1.4  christos       for (b = info->input_bfds; b; b = b->link.next)
    804       1.1     skrll 	{
    805       1.1     skrll 	  asymbol **symbols;
    806       1.1     skrll 	  int nsyms;
    807       1.1     skrll 
    808       1.1     skrll 	  if (!bfd_generic_link_read_symbols (b))
    809       1.1     skrll 	    {
    810  1.1.1.11  christos 	      fatal (_("%P: %pB: could not read symbols: %E\n"), b);
    811       1.1     skrll 	      return;
    812       1.1     skrll 	    }
    813       1.1     skrll 
    814       1.1     skrll 	  symbols = bfd_get_outsymbols (b);
    815       1.1     skrll 	  nsyms = bfd_get_symcount (b);
    816       1.1     skrll 
    817       1.1     skrll 	  for (j = 0; j < nsyms; j++)
    818       1.1     skrll 	    {
    819       1.1     skrll 	      /* We should export symbols which are either global or not
    820       1.1     skrll 		 anything at all.  (.bss data is the latter)
    821  1.1.1.12  christos 		 We should not export undefined symbols.
    822  1.1.1.12  christos 		 Compilers may generate template data (initializers) for
    823  1.1.1.12  christos 		 thread-local variables in .tls$* sections, but they are only
    824  1.1.1.12  christos 		 used by the DLL loader.  Symbols in those sections are used
    825  1.1.1.12  christos 		 to access thread-local variables, but only via offsets to the
    826  1.1.1.12  christos 		 beginning of the .tls output section.  These offsets can't be
    827  1.1.1.12  christos 		 exported.  PE targets not using BSF_THREAD_LOCAL, we need to
    828  1.1.1.12  christos 		 go by section name for now.  */
    829   1.1.1.9  christos 	      bool would_export
    830   1.1.1.3  christos 		= (symbols[j]->section != bfd_und_section_ptr
    831  1.1.1.12  christos 		   && !startswith (symbols[j]->section->name, ".tls$")
    832   1.1.1.3  christos 		   && ((symbols[j]->flags & BSF_GLOBAL)
    833   1.1.1.3  christos 		       || (symbols[j]->flags == 0)));
    834   1.1.1.3  christos 	      if (link_info.version_info && would_export)
    835   1.1.1.6  christos 		would_export
    836   1.1.1.6  christos 		  = !bfd_hide_sym_by_version (link_info.version_info,
    837   1.1.1.6  christos 					      symbols[j]->name);
    838   1.1.1.2  christos 	      if (would_export)
    839       1.1     skrll 		{
    840       1.1     skrll 		  const char *sn = symbols[j]->name;
    841       1.1     skrll 
    842       1.1     skrll 		  /* We should not re-export imported stuff.  */
    843       1.1     skrll 		  {
    844       1.1     skrll 		    char *name;
    845       1.1     skrll 		    if (is_import (sn))
    846   1.1.1.6  christos 		      continue;
    847       1.1     skrll 
    848       1.1     skrll 		    name = xmalloc (strlen ("__imp_") + strlen (sn) + 1);
    849       1.1     skrll 		    sprintf (name, "%s%s", "__imp_", sn);
    850       1.1     skrll 
    851       1.1     skrll 		    blhe = bfd_link_hash_lookup (info->hash, name,
    852   1.1.1.9  christos 						 false, false, false);
    853       1.1     skrll 		    free (name);
    854       1.1     skrll 
    855       1.1     skrll 		    if (blhe && blhe->type == bfd_link_hash_defined)
    856       1.1     skrll 		      continue;
    857       1.1     skrll 		  }
    858       1.1     skrll 
    859       1.1     skrll 		  if (pe_details->underscored && *sn == '_')
    860       1.1     skrll 		    sn++;
    861       1.1     skrll 
    862       1.1     skrll 		  if (auto_export (b, pe_def_file, sn))
    863       1.1     skrll 		    {
    864  1.1.1.10  christos 		      bool is_dup = false;
    865       1.1     skrll 		      def_file_export *p;
    866   1.1.1.3  christos 
    867   1.1.1.3  christos 		      p = def_file_add_export (pe_def_file, sn, 0, -1,
    868   1.1.1.3  christos 					       NULL, &is_dup);
    869       1.1     skrll 		      /* Fill data flag properly, from dlltool.c.  */
    870   1.1.1.3  christos 		      if (!is_dup)
    871   1.1.1.6  christos 			p->flag_data = !(symbols[j]->flags & BSF_FUNCTION);
    872       1.1     skrll 		    }
    873       1.1     skrll 		}
    874       1.1     skrll 	    }
    875       1.1     skrll 	}
    876       1.1     skrll     }
    877       1.1     skrll 
    878       1.1     skrll #undef NE
    879       1.1     skrll #define NE pe_def_file->num_exports
    880       1.1     skrll 
    881   1.1.1.2  christos   /* Don't create an empty export table.  */
    882   1.1.1.2  christos   if (NE == 0)
    883   1.1.1.2  christos     return;
    884   1.1.1.2  christos 
    885   1.1.1.9  christos   resort_needed = false;
    886   1.1.1.3  christos 
    887       1.1     skrll   /* Canonicalize the export list.  */
    888       1.1     skrll   if (pe_dll_kill_ats)
    889       1.1     skrll     {
    890       1.1     skrll       for (i = 0; i < NE; i++)
    891       1.1     skrll 	{
    892   1.1.1.4  christos 	  /* Check for fastcall/stdcall-decoration, but ignore
    893   1.1.1.4  christos 	     C++ mangled names.  */
    894   1.1.1.4  christos 	  if (pe_def_file->exports[i].name[0] != '?'
    895   1.1.1.4  christos 	      && strchr (pe_def_file->exports[i].name, '@'))
    896       1.1     skrll 	    {
    897       1.1     skrll 	      /* This will preserve internal_name, which may have been
    898       1.1     skrll 		 pointing to the same memory as name, or might not
    899       1.1     skrll 		 have.  */
    900       1.1     skrll 	      int lead_at = (*pe_def_file->exports[i].name == '@');
    901       1.1     skrll 	      char *tmp = xstrdup (pe_def_file->exports[i].name + lead_at);
    902   1.1.1.4  christos 	      char *tmp_at = strrchr (tmp, '@');
    903       1.1     skrll 
    904       1.1     skrll 	      if (tmp_at)
    905   1.1.1.6  christos 		*tmp_at = 0;
    906       1.1     skrll 	      else
    907   1.1.1.7  christos 		einfo (_("%X%P: cannot export %s: invalid export name\n"),
    908       1.1     skrll 		       pe_def_file->exports[i].name);
    909       1.1     skrll 	      pe_def_file->exports[i].name = tmp;
    910   1.1.1.9  christos 	      resort_needed = true;
    911       1.1     skrll 	    }
    912       1.1     skrll 	}
    913       1.1     skrll     }
    914       1.1     skrll 
    915   1.1.1.3  christos   /* Re-sort the exports table as we have possibly changed the order
    916   1.1.1.3  christos      by removing leading @.  */
    917   1.1.1.3  christos   if (resort_needed)
    918   1.1.1.3  christos     qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]),
    919   1.1.1.6  christos 	   pe_export_sort);
    920   1.1.1.3  christos 
    921       1.1     skrll   if (pe_dll_stdcall_aliases)
    922       1.1     skrll     {
    923       1.1     skrll       for (i = 0; i < NE; i++)
    924       1.1     skrll 	{
    925       1.1     skrll 	  if (is_import (pe_def_file->exports[i].name))
    926       1.1     skrll 	    continue;
    927       1.1     skrll 
    928       1.1     skrll 	  if (strchr (pe_def_file->exports[i].name, '@'))
    929       1.1     skrll 	    {
    930  1.1.1.10  christos 	      bool is_dup = true;
    931       1.1     skrll 	      int lead_at = (*pe_def_file->exports[i].name == '@');
    932       1.1     skrll 	      char *tmp = xstrdup (pe_def_file->exports[i].name + lead_at);
    933       1.1     skrll 
    934       1.1     skrll 	      *(strchr (tmp, '@')) = 0;
    935       1.1     skrll 	      if (auto_export (NULL, pe_def_file, tmp))
    936       1.1     skrll 		def_file_add_export (pe_def_file, tmp,
    937       1.1     skrll 				     pe_def_file->exports[i].internal_name,
    938   1.1.1.3  christos 				     -1, NULL, &is_dup);
    939   1.1.1.3  christos 	      if (is_dup)
    940   1.1.1.6  christos 		free (tmp);
    941       1.1     skrll 	    }
    942       1.1     skrll 	}
    943       1.1     skrll     }
    944       1.1     skrll 
    945       1.1     skrll   /* Convenience, but watch out for it changing.  */
    946       1.1     skrll   e = pe_def_file->exports;
    947       1.1     skrll 
    948       1.1     skrll   for (i = 0, j = 0; i < NE; i++)
    949       1.1     skrll     {
    950       1.1     skrll       if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
    951       1.1     skrll 	{
    952       1.1     skrll 	  /* This is a duplicate.  */
    953       1.1     skrll 	  if (e[j - 1].ordinal != -1
    954       1.1     skrll 	      && e[i].ordinal != -1
    955       1.1     skrll 	      && e[j - 1].ordinal != e[i].ordinal)
    956       1.1     skrll 	    {
    957       1.1     skrll 	      if (pe_dll_warn_dup_exports)
    958       1.1     skrll 		/* xgettext:c-format */
    959   1.1.1.7  christos 		einfo (_("%X%P: error, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
    960       1.1     skrll 		       e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
    961       1.1     skrll 	    }
    962       1.1     skrll 	  else
    963       1.1     skrll 	    {
    964       1.1     skrll 	      if (pe_dll_warn_dup_exports)
    965       1.1     skrll 		/* xgettext:c-format */
    966   1.1.1.7  christos 		einfo (_("%P: warning, duplicate EXPORT: %s\n"),
    967       1.1     skrll 		       e[j - 1].name);
    968       1.1     skrll 	    }
    969       1.1     skrll 
    970       1.1     skrll 	  if (e[i].ordinal != -1)
    971       1.1     skrll 	    e[j - 1].ordinal = e[i].ordinal;
    972       1.1     skrll 	  e[j - 1].flag_private |= e[i].flag_private;
    973       1.1     skrll 	  e[j - 1].flag_constant |= e[i].flag_constant;
    974       1.1     skrll 	  e[j - 1].flag_noname |= e[i].flag_noname;
    975       1.1     skrll 	  e[j - 1].flag_data |= e[i].flag_data;
    976   1.1.1.9  christos 	  free (e[i].name);
    977   1.1.1.9  christos 	  free (e[i].internal_name);
    978   1.1.1.9  christos 	  free (e[i].its_name);
    979       1.1     skrll 	}
    980       1.1     skrll       else
    981       1.1     skrll 	{
    982       1.1     skrll 	  if (i != j)
    983       1.1     skrll 	    e[j] = e[i];
    984       1.1     skrll 	  j++;
    985       1.1     skrll 	}
    986       1.1     skrll     }
    987       1.1     skrll   pe_def_file->num_exports = j;	/* == NE */
    988       1.1     skrll 
    989   1.1.1.3  christos   exported_symbol_offsets = xmalloc (NE * sizeof (bfd_vma));
    990   1.1.1.3  christos   exported_symbol_sections = xmalloc (NE * sizeof (struct bfd_section *));
    991   1.1.1.3  christos 
    992   1.1.1.3  christos   memset (exported_symbol_sections, 0, NE * sizeof (struct bfd_section *));
    993   1.1.1.3  christos   max_ordinal = 0;
    994   1.1.1.3  christos   min_ordinal = 65536;
    995   1.1.1.3  christos   count_exported = 0;
    996   1.1.1.3  christos   count_exported_byname = 0;
    997   1.1.1.3  christos   count_with_ordinals = 0;
    998   1.1.1.3  christos 
    999       1.1     skrll   for (i = 0; i < NE; i++)
   1000       1.1     skrll     {
   1001   1.1.1.5  christos       char *int_name = pe_def_file->exports[i].internal_name;
   1002       1.1     skrll       char *name;
   1003   1.1.1.5  christos 
   1004   1.1.1.5  christos       /* PR 19803: Make sure that any exported symbol does not get garbage collected.  */
   1005   1.1.1.5  christos       lang_add_gc_name (int_name);
   1006   1.1.1.5  christos 
   1007   1.1.1.5  christos       name = xmalloc (strlen (int_name) + 2);
   1008   1.1.1.5  christos       if (pe_details->underscored && int_name[0] != '@')
   1009       1.1     skrll 	{
   1010       1.1     skrll 	  *name = '_';
   1011   1.1.1.5  christos 	  strcpy (name + 1, int_name);
   1012   1.1.1.5  christos 
   1013   1.1.1.5  christos 	  /* PR 19803: The alias must be preserved as well.  */
   1014   1.1.1.5  christos 	  lang_add_gc_name (xstrdup (name));
   1015       1.1     skrll 	}
   1016       1.1     skrll       else
   1017   1.1.1.5  christos 	strcpy (name, int_name);
   1018       1.1     skrll 
   1019       1.1     skrll       blhe = bfd_link_hash_lookup (info->hash,
   1020       1.1     skrll 				   name,
   1021   1.1.1.9  christos 				   false, false, true);
   1022       1.1     skrll 
   1023       1.1     skrll       if (blhe
   1024       1.1     skrll 	  && (blhe->type == bfd_link_hash_defined
   1025       1.1     skrll 	      || (blhe->type == bfd_link_hash_common)))
   1026       1.1     skrll 	{
   1027       1.1     skrll 	  count_exported++;
   1028       1.1     skrll 	  if (!pe_def_file->exports[i].flag_noname)
   1029       1.1     skrll 	    count_exported_byname++;
   1030       1.1     skrll 
   1031       1.1     skrll 	  /* Only fill in the sections. The actual offsets are computed
   1032       1.1     skrll 	     in fill_exported_offsets() after common symbols are laid
   1033       1.1     skrll 	     out.  */
   1034       1.1     skrll 	  if (blhe->type == bfd_link_hash_defined)
   1035       1.1     skrll 	    exported_symbol_sections[i] = blhe->u.def.section;
   1036       1.1     skrll 	  else
   1037       1.1     skrll 	    exported_symbol_sections[i] = blhe->u.c.p->section;
   1038       1.1     skrll 
   1039       1.1     skrll 	  if (pe_def_file->exports[i].ordinal != -1)
   1040       1.1     skrll 	    {
   1041       1.1     skrll 	      if (max_ordinal < pe_def_file->exports[i].ordinal)
   1042       1.1     skrll 		max_ordinal = pe_def_file->exports[i].ordinal;
   1043       1.1     skrll 	      if (min_ordinal > pe_def_file->exports[i].ordinal)
   1044       1.1     skrll 		min_ordinal = pe_def_file->exports[i].ordinal;
   1045       1.1     skrll 	      count_with_ordinals++;
   1046       1.1     skrll 	    }
   1047       1.1     skrll 	}
   1048   1.1.1.2  christos       /* Check for forward exports.  These are indicated in DEF files by an
   1049   1.1.1.6  christos 	 export directive of the form NAME1 = MODULE-NAME.EXTERNAL-NAME
   1050   1.1.1.2  christos 	 but we must take care not to be fooled when the user wants to export
   1051   1.1.1.2  christos 	 a symbol that actually really has a dot in it, so we only check
   1052   1.1.1.2  christos 	 for them here, after real defined symbols have already been matched.  */
   1053   1.1.1.5  christos       else if (strchr (int_name, '.'))
   1054   1.1.1.2  christos 	{
   1055   1.1.1.2  christos 	  count_exported++;
   1056   1.1.1.2  christos 	  if (!pe_def_file->exports[i].flag_noname)
   1057   1.1.1.2  christos 	    count_exported_byname++;
   1058   1.1.1.2  christos 
   1059   1.1.1.2  christos 	  pe_def_file->exports[i].flag_forward = 1;
   1060   1.1.1.2  christos 
   1061   1.1.1.2  christos 	  if (pe_def_file->exports[i].ordinal != -1)
   1062   1.1.1.2  christos 	    {
   1063   1.1.1.2  christos 	      if (max_ordinal < pe_def_file->exports[i].ordinal)
   1064   1.1.1.2  christos 		max_ordinal = pe_def_file->exports[i].ordinal;
   1065   1.1.1.2  christos 	      if (min_ordinal > pe_def_file->exports[i].ordinal)
   1066   1.1.1.2  christos 		min_ordinal = pe_def_file->exports[i].ordinal;
   1067   1.1.1.2  christos 	      count_with_ordinals++;
   1068   1.1.1.2  christos 	    }
   1069   1.1.1.2  christos 	}
   1070       1.1     skrll       else if (blhe && blhe->type == bfd_link_hash_undefined)
   1071       1.1     skrll 	{
   1072       1.1     skrll 	  /* xgettext:c-format */
   1073   1.1.1.7  christos 	  einfo (_("%X%P: cannot export %s: symbol not defined\n"),
   1074   1.1.1.5  christos 		 int_name);
   1075       1.1     skrll 	}
   1076       1.1     skrll       else if (blhe)
   1077       1.1     skrll 	{
   1078       1.1     skrll 	  /* xgettext:c-format */
   1079   1.1.1.7  christos 	  einfo (_("%X%P: cannot export %s: symbol wrong type (%d vs %d)\n"),
   1080   1.1.1.5  christos 		 int_name,
   1081       1.1     skrll 		 blhe->type, bfd_link_hash_defined);
   1082       1.1     skrll 	}
   1083       1.1     skrll       else
   1084       1.1     skrll 	{
   1085       1.1     skrll 	  /* xgettext:c-format */
   1086   1.1.1.7  christos 	  einfo (_("%X%P: cannot export %s: symbol not found\n"),
   1087   1.1.1.5  christos 		 int_name);
   1088       1.1     skrll 	}
   1089       1.1     skrll       free (name);
   1090       1.1     skrll     }
   1091       1.1     skrll }
   1092       1.1     skrll 
   1093       1.1     skrll /* Build the bfd that will contain .edata and .reloc sections.  */
   1094       1.1     skrll 
   1095       1.1     skrll static void
   1096  1.1.1.10  christos build_filler_bfd (bool include_edata)
   1097       1.1     skrll {
   1098       1.1     skrll   lang_input_statement_type *filler_file;
   1099  1.1.1.10  christos 
   1100       1.1     skrll   filler_file = lang_add_input_file ("dll stuff",
   1101       1.1     skrll 				     lang_input_file_is_fake_enum,
   1102       1.1     skrll 				     NULL);
   1103       1.1     skrll   filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff",
   1104       1.1     skrll 						  link_info.output_bfd);
   1105       1.1     skrll   if (filler_bfd == NULL
   1106       1.1     skrll       || !bfd_set_arch_mach (filler_bfd,
   1107       1.1     skrll 			     bfd_get_arch (link_info.output_bfd),
   1108       1.1     skrll 			     bfd_get_mach (link_info.output_bfd)))
   1109       1.1     skrll     {
   1110  1.1.1.11  christos       fatal (_("%P: can not create BFD: %E\n"));
   1111       1.1     skrll       return;
   1112       1.1     skrll     }
   1113       1.1     skrll 
   1114       1.1     skrll   if (include_edata)
   1115       1.1     skrll     {
   1116       1.1     skrll       edata_s = bfd_make_section_old_way (filler_bfd, ".edata");
   1117       1.1     skrll       if (edata_s == NULL
   1118   1.1.1.8  christos 	  || !bfd_set_section_flags (edata_s, (SEC_HAS_CONTENTS
   1119  1.1.1.12  christos 					       | SEC_DATA
   1120   1.1.1.8  christos 					       | SEC_ALLOC
   1121   1.1.1.8  christos 					       | SEC_LOAD
   1122   1.1.1.8  christos 					       | SEC_KEEP
   1123   1.1.1.8  christos 					       | SEC_IN_MEMORY)))
   1124       1.1     skrll 	{
   1125   1.1.1.6  christos 	  einfo (_("%X%P: can not create .edata section: %E\n"));
   1126       1.1     skrll 	  return;
   1127       1.1     skrll 	}
   1128   1.1.1.8  christos       bfd_set_section_size (edata_s, edata_sz);
   1129       1.1     skrll     }
   1130       1.1     skrll 
   1131       1.1     skrll   reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc");
   1132       1.1     skrll   if (reloc_s == NULL
   1133   1.1.1.8  christos       || !bfd_set_section_flags (reloc_s, (SEC_HAS_CONTENTS
   1134  1.1.1.12  christos 					   | SEC_DATA
   1135   1.1.1.8  christos 					   | SEC_LOAD
   1136   1.1.1.8  christos 					   | SEC_KEEP
   1137   1.1.1.8  christos 					   | SEC_IN_MEMORY)))
   1138       1.1     skrll     {
   1139   1.1.1.6  christos       einfo (_("%X%P: can not create .reloc section: %E\n"));
   1140       1.1     skrll       return;
   1141       1.1     skrll     }
   1142       1.1     skrll 
   1143   1.1.1.8  christos   bfd_set_section_size (reloc_s, 0);
   1144       1.1     skrll 
   1145       1.1     skrll   ldlang_add_file (filler_file);
   1146       1.1     skrll }
   1147       1.1     skrll 
   1148       1.1     skrll /* Gather all the exported symbols and build the .edata section.  */
   1149       1.1     skrll 
   1150       1.1     skrll static void
   1151   1.1.1.9  christos generate_edata (void)
   1152       1.1     skrll {
   1153       1.1     skrll   int i, next_ordinal;
   1154       1.1     skrll   int name_table_size = 0;
   1155       1.1     skrll 
   1156       1.1     skrll   /* First, we need to know how many exported symbols there are,
   1157       1.1     skrll      and what the range of ordinals is.  */
   1158       1.1     skrll   if (count_with_ordinals && max_ordinal > count_exported)
   1159       1.1     skrll     {
   1160       1.1     skrll       if (min_ordinal > max_ordinal - count_exported + 1)
   1161       1.1     skrll 	min_ordinal = max_ordinal - count_exported + 1;
   1162       1.1     skrll     }
   1163       1.1     skrll   else
   1164       1.1     skrll     {
   1165       1.1     skrll       min_ordinal = 1;
   1166       1.1     skrll       max_ordinal = count_exported;
   1167       1.1     skrll     }
   1168       1.1     skrll 
   1169       1.1     skrll   export_table_size = max_ordinal - min_ordinal + 1;
   1170       1.1     skrll   exported_symbols = xmalloc (export_table_size * sizeof (int));
   1171       1.1     skrll   for (i = 0; i < export_table_size; i++)
   1172       1.1     skrll     exported_symbols[i] = -1;
   1173       1.1     skrll 
   1174       1.1     skrll   /* Now we need to assign ordinals to those that don't have them.  */
   1175       1.1     skrll   for (i = 0; i < NE; i++)
   1176       1.1     skrll     {
   1177   1.1.1.6  christos       if (exported_symbol_sections[i]
   1178   1.1.1.6  christos 	  || pe_def_file->exports[i].flag_forward)
   1179       1.1     skrll 	{
   1180       1.1     skrll 	  if (pe_def_file->exports[i].ordinal != -1)
   1181       1.1     skrll 	    {
   1182       1.1     skrll 	      int ei = pe_def_file->exports[i].ordinal - min_ordinal;
   1183       1.1     skrll 	      int pi = exported_symbols[ei];
   1184       1.1     skrll 
   1185       1.1     skrll 	      if (pi != -1)
   1186       1.1     skrll 		{
   1187       1.1     skrll 		  /* xgettext:c-format */
   1188   1.1.1.7  christos 		  einfo (_("%X%P: error: ordinal used twice: %d (%s vs %s)\n"),
   1189       1.1     skrll 			 pe_def_file->exports[i].ordinal,
   1190       1.1     skrll 			 pe_def_file->exports[i].name,
   1191       1.1     skrll 			 pe_def_file->exports[pi].name);
   1192       1.1     skrll 		}
   1193       1.1     skrll 	      exported_symbols[ei] = i;
   1194       1.1     skrll 	    }
   1195   1.1.1.2  christos 	  if (pe_def_file->exports[i].its_name)
   1196   1.1.1.2  christos 	    name_table_size += strlen (pe_def_file->exports[i].its_name) + 1;
   1197   1.1.1.2  christos 	  else
   1198   1.1.1.2  christos 	    name_table_size += strlen (pe_def_file->exports[i].name) + 1;
   1199       1.1     skrll 	}
   1200       1.1     skrll 
   1201       1.1     skrll       /* Reserve space for the forward name. */
   1202       1.1     skrll       if (pe_def_file->exports[i].flag_forward)
   1203       1.1     skrll 	{
   1204       1.1     skrll 	  name_table_size += strlen (pe_def_file->exports[i].internal_name) + 1;
   1205       1.1     skrll 	}
   1206       1.1     skrll     }
   1207       1.1     skrll 
   1208       1.1     skrll   next_ordinal = min_ordinal;
   1209       1.1     skrll   for (i = 0; i < NE; i++)
   1210   1.1.1.6  christos     if ((exported_symbol_sections[i]
   1211   1.1.1.6  christos 	 || pe_def_file->exports[i].flag_forward)
   1212   1.1.1.6  christos 	&& pe_def_file->exports[i].ordinal == -1)
   1213       1.1     skrll       {
   1214       1.1     skrll 	while (exported_symbols[next_ordinal - min_ordinal] != -1)
   1215       1.1     skrll 	  next_ordinal++;
   1216       1.1     skrll 
   1217       1.1     skrll 	exported_symbols[next_ordinal - min_ordinal] = i;
   1218       1.1     skrll 	pe_def_file->exports[i].ordinal = next_ordinal;
   1219       1.1     skrll       }
   1220       1.1     skrll 
   1221   1.1.1.6  christos   /* PR 12969: Check for more than 1^16 ordinals.  */
   1222   1.1.1.6  christos   if (max_ordinal > 65535 || next_ordinal > 65535)
   1223   1.1.1.6  christos     /* xgettext:c-format */
   1224   1.1.1.7  christos     einfo(_("%X%P: error: export ordinal too large: %d\n"),
   1225   1.1.1.6  christos 	  max_ordinal > next_ordinal ? max_ordinal : next_ordinal);
   1226   1.1.1.6  christos 
   1227       1.1     skrll   /* OK, now we can allocate some memory.  */
   1228       1.1     skrll   edata_sz = (40				/* directory */
   1229       1.1     skrll 	      + 4 * export_table_size		/* addresses */
   1230       1.1     skrll 	      + 4 * count_exported_byname	/* name ptrs */
   1231       1.1     skrll 	      + 2 * count_exported_byname	/* ordinals */
   1232   1.1.1.9  christos 	      + name_table_size + strlen (dll_filename) + 1);
   1233       1.1     skrll }
   1234       1.1     skrll 
   1235       1.1     skrll /* Fill the exported symbol offsets. The preliminary work has already
   1236   1.1.1.2  christos    been done in process_def_file_and_drectve().  */
   1237       1.1     skrll 
   1238       1.1     skrll static void
   1239       1.1     skrll fill_exported_offsets (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
   1240       1.1     skrll {
   1241       1.1     skrll   int i;
   1242       1.1     skrll   struct bfd_link_hash_entry *blhe;
   1243       1.1     skrll 
   1244       1.1     skrll   for (i = 0; i < pe_def_file->num_exports; i++)
   1245       1.1     skrll     {
   1246       1.1     skrll       char *name;
   1247       1.1     skrll 
   1248       1.1     skrll       name = xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
   1249       1.1     skrll       if (pe_details->underscored
   1250   1.1.1.6  christos 	  && *pe_def_file->exports[i].internal_name != '@')
   1251       1.1     skrll 	{
   1252       1.1     skrll 	  *name = '_';
   1253       1.1     skrll 	  strcpy (name + 1, pe_def_file->exports[i].internal_name);
   1254       1.1     skrll 	}
   1255       1.1     skrll       else
   1256       1.1     skrll 	strcpy (name, pe_def_file->exports[i].internal_name);
   1257       1.1     skrll 
   1258       1.1     skrll       blhe = bfd_link_hash_lookup (info->hash,
   1259       1.1     skrll 				   name,
   1260   1.1.1.9  christos 				   false, false, true);
   1261       1.1     skrll 
   1262       1.1     skrll       if (blhe && blhe->type == bfd_link_hash_defined)
   1263       1.1     skrll 	exported_symbol_offsets[i] = blhe->u.def.value;
   1264       1.1     skrll 
   1265       1.1     skrll       free (name);
   1266       1.1     skrll     }
   1267       1.1     skrll }
   1268       1.1     skrll 
   1269       1.1     skrll static void
   1270       1.1     skrll fill_edata (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
   1271       1.1     skrll {
   1272       1.1     skrll   int s, hint;
   1273       1.1     skrll   unsigned char *edirectory;
   1274       1.1     skrll   unsigned char *eaddresses;
   1275       1.1     skrll   unsigned char *enameptrs;
   1276       1.1     skrll   unsigned char *eordinals;
   1277       1.1     skrll   char *enamestr;
   1278       1.1     skrll 
   1279       1.1     skrll   edata_d = xmalloc (edata_sz);
   1280       1.1     skrll 
   1281       1.1     skrll   /* Note use of array pointer math here.  */
   1282       1.1     skrll   edirectory = edata_d;
   1283   1.1.1.2  christos   eaddresses = edirectory + 40;
   1284       1.1     skrll   enameptrs = eaddresses + 4 * export_table_size;
   1285       1.1     skrll   eordinals = enameptrs + 4 * count_exported_byname;
   1286       1.1     skrll   enamestr = (char *) eordinals + 2 * count_exported_byname;
   1287       1.1     skrll 
   1288       1.1     skrll #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) \
   1289       1.1     skrll 		   + edata_s->output_section->vma - image_base)
   1290       1.1     skrll 
   1291       1.1     skrll   memset (edata_d, 0, edata_sz);
   1292   1.1.1.4  christos 
   1293   1.1.1.9  christos   if (pe_data (abfd)->timestamp == -1)
   1294  1.1.1.10  christos     {
   1295  1.1.1.10  christos       time_t now = bfd_get_current_time (0);
   1296  1.1.1.10  christos       H_PUT_32 (abfd, now, edata_d + 4);
   1297  1.1.1.10  christos     }
   1298   1.1.1.9  christos   else
   1299   1.1.1.9  christos     H_PUT_32 (abfd, pe_data (abfd)->timestamp, edata_d + 4);
   1300   1.1.1.4  christos 
   1301       1.1     skrll   if (pe_def_file->version_major != -1)
   1302       1.1     skrll     {
   1303       1.1     skrll       bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8);
   1304       1.1     skrll       bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10);
   1305       1.1     skrll     }
   1306       1.1     skrll 
   1307       1.1     skrll   bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
   1308   1.1.1.9  christos   strcpy (enamestr, dll_filename);
   1309       1.1     skrll   enamestr += strlen (enamestr) + 1;
   1310       1.1     skrll   bfd_put_32 (abfd, min_ordinal, edata_d + 16);
   1311       1.1     skrll   bfd_put_32 (abfd, export_table_size, edata_d + 20);
   1312       1.1     skrll   bfd_put_32 (abfd, count_exported_byname, edata_d + 24);
   1313       1.1     skrll   bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28);
   1314       1.1     skrll   bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32);
   1315       1.1     skrll   bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36);
   1316       1.1     skrll 
   1317       1.1     skrll   fill_exported_offsets (abfd, info);
   1318       1.1     skrll 
   1319       1.1     skrll   /* Ok, now for the filling in part.
   1320       1.1     skrll      Scan alphabetically - ie the ordering in the exports[] table,
   1321       1.1     skrll      rather than by ordinal - the ordering in the exported_symbol[]
   1322       1.1     skrll      table.  See dlltool.c and:
   1323   1.1.1.6  christos 	http://sources.redhat.com/ml/binutils/2003-04/msg00379.html
   1324       1.1     skrll      for more information.  */
   1325       1.1     skrll   hint = 0;
   1326       1.1     skrll   for (s = 0; s < NE; s++)
   1327       1.1     skrll     {
   1328       1.1     skrll       struct bfd_section *ssec = exported_symbol_sections[s];
   1329   1.1.1.6  christos       if (pe_def_file->exports[s].ordinal != -1
   1330   1.1.1.6  christos 	  && (pe_def_file->exports[s].flag_forward || ssec != NULL))
   1331       1.1     skrll 	{
   1332       1.1     skrll 	  int ord = pe_def_file->exports[s].ordinal;
   1333       1.1     skrll 
   1334       1.1     skrll 	  if (pe_def_file->exports[s].flag_forward)
   1335       1.1     skrll 	    {
   1336       1.1     skrll 	      bfd_put_32 (abfd, ERVA (enamestr),
   1337   1.1.1.6  christos 			  eaddresses + 4 * (ord - min_ordinal));
   1338       1.1     skrll 
   1339       1.1     skrll 	      strcpy (enamestr, pe_def_file->exports[s].internal_name);
   1340       1.1     skrll 	      enamestr += strlen (pe_def_file->exports[s].internal_name) + 1;
   1341       1.1     skrll 	    }
   1342       1.1     skrll 	  else
   1343       1.1     skrll 	    {
   1344       1.1     skrll 	      bfd_vma srva = (exported_symbol_offsets[s]
   1345       1.1     skrll 				    + ssec->output_section->vma
   1346       1.1     skrll 				    + ssec->output_offset);
   1347       1.1     skrll 
   1348       1.1     skrll 	      bfd_put_32 (abfd, srva - image_base,
   1349   1.1.1.6  christos 			  eaddresses + 4 * (ord - min_ordinal));
   1350       1.1     skrll 	    }
   1351       1.1     skrll 
   1352       1.1     skrll 	  if (!pe_def_file->exports[s].flag_noname)
   1353       1.1     skrll 	    {
   1354       1.1     skrll 	      char *ename = pe_def_file->exports[s].name;
   1355   1.1.1.2  christos 	      if (pe_def_file->exports[s].its_name)
   1356   1.1.1.2  christos 		ename = pe_def_file->exports[s].its_name;
   1357       1.1     skrll 
   1358       1.1     skrll 	      bfd_put_32 (abfd, ERVA (enamestr), enameptrs);
   1359       1.1     skrll 	      enameptrs += 4;
   1360       1.1     skrll 	      strcpy (enamestr, ename);
   1361       1.1     skrll 	      enamestr += strlen (enamestr) + 1;
   1362       1.1     skrll 	      bfd_put_16 (abfd, ord - min_ordinal, eordinals);
   1363       1.1     skrll 	      eordinals += 2;
   1364       1.1     skrll 	      pe_def_file->exports[s].hint = hint++;
   1365       1.1     skrll 	    }
   1366       1.1     skrll 	}
   1367       1.1     skrll     }
   1368       1.1     skrll }
   1369       1.1     skrll 
   1370       1.1     skrll 
   1371       1.1     skrll static struct bfd_section *current_sec;
   1372       1.1     skrll 
   1373   1.1.1.7  christos static void
   1374   1.1.1.7  christos pe_walk_relocs (struct bfd_link_info *info,
   1375   1.1.1.7  christos 		char *name,
   1376   1.1.1.7  christos 		const char *symname,
   1377   1.1.1.7  christos 		struct bfd_hash_table *import_hash,
   1378   1.1.1.7  christos 		void (*cb) (arelent *, asection *, char *, const char *))
   1379       1.1     skrll {
   1380       1.1     skrll   bfd *b;
   1381       1.1     skrll   asection *s;
   1382       1.1     skrll 
   1383   1.1.1.4  christos   for (b = info->input_bfds; b; b = b->link.next)
   1384       1.1     skrll     {
   1385       1.1     skrll       asymbol **symbols;
   1386       1.1     skrll 
   1387       1.1     skrll       if (!bfd_generic_link_read_symbols (b))
   1388       1.1     skrll 	{
   1389  1.1.1.11  christos 	  fatal (_("%P: %pB: could not read symbols: %E\n"), b);
   1390       1.1     skrll 	  return;
   1391       1.1     skrll 	}
   1392       1.1     skrll 
   1393       1.1     skrll       symbols = bfd_get_outsymbols (b);
   1394       1.1     skrll 
   1395       1.1     skrll       for (s = b->sections; s; s = s->next)
   1396       1.1     skrll 	{
   1397       1.1     skrll 	  arelent **relocs;
   1398       1.1     skrll 	  int relsize, nrelocs, i;
   1399   1.1.1.8  christos 	  int flags = bfd_section_flags (s);
   1400       1.1     skrll 
   1401       1.1     skrll 	  /* Skip discarded linkonce sections.  */
   1402       1.1     skrll 	  if (flags & SEC_LINK_ONCE
   1403       1.1     skrll 	      && s->output_section == bfd_abs_section_ptr)
   1404       1.1     skrll 	    continue;
   1405       1.1     skrll 
   1406       1.1     skrll 	  current_sec = s;
   1407       1.1     skrll 
   1408       1.1     skrll 	  relsize = bfd_get_reloc_upper_bound (b, s);
   1409       1.1     skrll 	  relocs = xmalloc (relsize);
   1410       1.1     skrll 	  nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
   1411       1.1     skrll 
   1412       1.1     skrll 	  for (i = 0; i < nrelocs; i++)
   1413       1.1     skrll 	    {
   1414       1.1     skrll 	      struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
   1415       1.1     skrll 
   1416   1.1.1.7  christos 	      /* Warning: the callback needs to be passed NAME directly.  */
   1417   1.1.1.7  christos 	      if (import_hash)
   1418   1.1.1.7  christos 		{
   1419   1.1.1.9  christos 		  if (bfd_hash_lookup (import_hash, sym->name, false, false))
   1420   1.1.1.7  christos 		    {
   1421   1.1.1.7  christos 		      strcpy (name, sym->name);
   1422   1.1.1.7  christos 		      cb (relocs[i], s, name, symname);
   1423   1.1.1.7  christos 		    }
   1424   1.1.1.7  christos 		}
   1425   1.1.1.7  christos 	      else
   1426   1.1.1.7  christos 		{
   1427   1.1.1.7  christos 		  if (strcmp (name, sym->name) == 0)
   1428   1.1.1.7  christos 		    cb (relocs[i], s, name, symname);
   1429   1.1.1.7  christos 		}
   1430       1.1     skrll 	    }
   1431       1.1     skrll 
   1432       1.1     skrll 	  free (relocs);
   1433       1.1     skrll 
   1434       1.1     skrll 	  /* Warning: the allocated symbols are remembered in BFD and reused
   1435       1.1     skrll 	     later, so don't free them! */
   1436       1.1     skrll 	  /* free (symbols); */
   1437       1.1     skrll 	}
   1438       1.1     skrll     }
   1439       1.1     skrll }
   1440       1.1     skrll 
   1441   1.1.1.7  christos void
   1442   1.1.1.7  christos pe_find_data_imports (const char *symhead,
   1443   1.1.1.7  christos 		      void (*cb) (arelent *, asection *, char *, const char *))
   1444   1.1.1.7  christos {
   1445   1.1.1.7  christos   struct bfd_link_hash_entry *undef;
   1446   1.1.1.7  christos   const size_t headlen = strlen (symhead);
   1447   1.1.1.7  christos   size_t namelen = 0;
   1448   1.1.1.7  christos   char *buf, *name;
   1449   1.1.1.7  christos   struct bfd_hash_table *import_hash;
   1450   1.1.1.7  christos 
   1451   1.1.1.7  christos   for (undef = link_info.hash->undefs; undef; undef = undef->u.undef.next)
   1452   1.1.1.7  christos     if (undef->type == bfd_link_hash_undefined)
   1453   1.1.1.7  christos       {
   1454   1.1.1.7  christos 	size_t len = strlen (undef->root.string);
   1455   1.1.1.7  christos 	if (namelen < len)
   1456   1.1.1.7  christos 	  namelen = len;
   1457   1.1.1.7  christos       }
   1458   1.1.1.7  christos   if (namelen == 0)
   1459   1.1.1.7  christos     return;
   1460   1.1.1.7  christos 
   1461   1.1.1.7  christos   /* For the pseudo-relocation support version 2, we can collect the symbols
   1462   1.1.1.7  christos      that are subject to auto-import and adjust the relocations en masse.  */
   1463   1.1.1.7  christos   if (link_info.pei386_runtime_pseudo_reloc == 2)
   1464   1.1.1.7  christos     {
   1465   1.1.1.7  christos       import_hash
   1466   1.1.1.7  christos 	= (struct bfd_hash_table *) xmalloc (sizeof (struct bfd_hash_table));
   1467   1.1.1.7  christos       if (!bfd_hash_table_init (import_hash,
   1468   1.1.1.7  christos 				bfd_hash_newfunc,
   1469   1.1.1.7  christos 				sizeof (struct bfd_hash_entry)))
   1470  1.1.1.11  christos 	fatal (_("%P: bfd_hash_table_init failed: %E\n"));
   1471   1.1.1.7  christos     }
   1472   1.1.1.7  christos   else
   1473   1.1.1.7  christos     import_hash = NULL;
   1474   1.1.1.7  christos 
   1475   1.1.1.7  christos   /* We are being a bit cunning here.  The buffer will have space for
   1476   1.1.1.7  christos      prefixes at the beginning.  The prefix is modified here and in a
   1477   1.1.1.7  christos      number of functions called from this function.  */
   1478   1.1.1.7  christos #define PREFIX_LEN 32
   1479   1.1.1.7  christos   buf = xmalloc (PREFIX_LEN + namelen + 1);
   1480   1.1.1.7  christos   name = buf + PREFIX_LEN;
   1481   1.1.1.7  christos 
   1482   1.1.1.7  christos   for (undef = link_info.hash->undefs; undef; undef = undef->u.undef.next)
   1483   1.1.1.7  christos     if (undef->type == bfd_link_hash_undefined)
   1484   1.1.1.7  christos       {
   1485   1.1.1.7  christos 	struct bfd_link_hash_entry *sym;
   1486   1.1.1.7  christos 	char *impname;
   1487   1.1.1.7  christos 
   1488   1.1.1.7  christos 	if (pe_dll_extra_pe_debug)
   1489  1.1.1.10  christos 	  printf ("%s:%s\n", __func__, undef->root.string);
   1490   1.1.1.7  christos 
   1491   1.1.1.7  christos 	strcpy (name, undef->root.string);
   1492   1.1.1.7  christos 	impname = name - (sizeof "__imp_" - 1);
   1493   1.1.1.7  christos 	memcpy (impname, "__imp_", sizeof "__imp_" - 1);
   1494   1.1.1.7  christos 
   1495   1.1.1.7  christos 	sym = bfd_link_hash_lookup (link_info.hash, impname, 0, 0, 1);
   1496   1.1.1.7  christos 
   1497   1.1.1.7  christos 	if (sym && sym->type == bfd_link_hash_defined)
   1498   1.1.1.7  christos 	  {
   1499   1.1.1.7  christos 	    if (import_hash)
   1500   1.1.1.9  christos 	      bfd_hash_lookup (import_hash, undef->root.string, true, false);
   1501   1.1.1.7  christos 	    else
   1502   1.1.1.7  christos 	      {
   1503   1.1.1.7  christos 		bfd *b = sym->u.def.section->owner;
   1504   1.1.1.7  christos 		const char *symname = NULL;
   1505   1.1.1.7  christos 		asymbol **symbols;
   1506   1.1.1.7  christos 		int nsyms, i;
   1507   1.1.1.7  christos 
   1508   1.1.1.7  christos 		if (!bfd_generic_link_read_symbols (b))
   1509   1.1.1.7  christos 		  {
   1510  1.1.1.11  christos 		    fatal (_("%P: %pB: could not read symbols: %E\n"), b);
   1511   1.1.1.7  christos 		    return;
   1512   1.1.1.7  christos 		  }
   1513   1.1.1.7  christos 
   1514   1.1.1.7  christos 		symbols = bfd_get_outsymbols (b);
   1515   1.1.1.7  christos 		nsyms = bfd_get_symcount (b);
   1516   1.1.1.7  christos 
   1517   1.1.1.7  christos 		for (i = 0; i < nsyms; i++)
   1518   1.1.1.7  christos 		  if (strncmp (symbols[i]->name, symhead, headlen) == 0)
   1519   1.1.1.7  christos 		    {
   1520   1.1.1.7  christos 		      if (pe_dll_extra_pe_debug)
   1521   1.1.1.7  christos 			printf ("->%s\n", symbols[i]->name);
   1522   1.1.1.7  christos 
   1523   1.1.1.7  christos 		      symname = symbols[i]->name + headlen;
   1524   1.1.1.7  christos 		      break;
   1525   1.1.1.7  christos 		    }
   1526   1.1.1.7  christos 
   1527   1.1.1.7  christos 		/* If the symobl isn't part of an import table, there is no
   1528   1.1.1.7  christos 		   point in building a fixup, this would give rise to link
   1529   1.1.1.7  christos 		   errors for mangled symbols instead of the original one.  */
   1530   1.1.1.7  christos 		if (symname)
   1531   1.1.1.7  christos 		  pe_walk_relocs (&link_info, name, symname, NULL, cb);
   1532   1.1.1.7  christos 		else
   1533   1.1.1.7  christos 		  continue;
   1534   1.1.1.7  christos 	      }
   1535   1.1.1.7  christos 
   1536   1.1.1.7  christos 	    /* Let's differentiate it somehow from defined.  */
   1537   1.1.1.7  christos 	    undef->type = bfd_link_hash_defweak;
   1538   1.1.1.7  christos 	    undef->u.def.value = sym->u.def.value;
   1539   1.1.1.7  christos 	    undef->u.def.section = sym->u.def.section;
   1540   1.1.1.7  christos 
   1541   1.1.1.8  christos 	    /* We replace the original name with the __imp_ prefixed one, this
   1542   1.1.1.8  christos 	       1) may trash memory 2) leads to duplicate symbols.  But this is
   1543   1.1.1.8  christos 	       better than having a misleading name that can confuse GDB.  */
   1544   1.1.1.8  christos 	    undef->root.string = sym->root.string;
   1545   1.1.1.8  christos 
   1546   1.1.1.7  christos 	    if (link_info.pei386_auto_import == -1)
   1547   1.1.1.7  christos 	      {
   1548   1.1.1.9  christos 		static bool warned = false;
   1549   1.1.1.7  christos 
   1550   1.1.1.7  christos 		info_msg (_("Info: resolving %s by linking to %s "
   1551   1.1.1.7  christos 			    "(auto-import)\n"), name, impname);
   1552   1.1.1.7  christos 
   1553   1.1.1.7  christos 		/* PR linker/4844.  */
   1554   1.1.1.7  christos 		if (!warned)
   1555   1.1.1.7  christos 		  {
   1556   1.1.1.7  christos 		    einfo (_("%P: warning: auto-importing has been activated "
   1557   1.1.1.7  christos 			     "without --enable-auto-import specified on the "
   1558   1.1.1.7  christos 			     "command line; this should work unless it "
   1559   1.1.1.7  christos 			     "involves constant data structures referencing "
   1560   1.1.1.7  christos 			     "symbols from auto-imported DLLs\n"));
   1561   1.1.1.9  christos 		    warned = true;
   1562   1.1.1.7  christos 		  }
   1563   1.1.1.7  christos 	      }
   1564   1.1.1.7  christos 	  }
   1565   1.1.1.7  christos       }
   1566   1.1.1.7  christos 
   1567   1.1.1.7  christos   /* If we have the import hash table, walk the relocations only once.  */
   1568   1.1.1.7  christos   if (import_hash)
   1569   1.1.1.7  christos     {
   1570   1.1.1.7  christos       pe_walk_relocs (&link_info, name, NULL, import_hash, cb);
   1571   1.1.1.7  christos       bfd_hash_table_free (import_hash);
   1572   1.1.1.7  christos       free (import_hash);
   1573   1.1.1.7  christos     }
   1574   1.1.1.7  christos 
   1575   1.1.1.7  christos   free (buf);
   1576   1.1.1.7  christos }
   1577   1.1.1.7  christos 
   1578       1.1     skrll /* Gather all the relocations and build the .reloc section.  */
   1579       1.1     skrll 
   1580       1.1     skrll static void
   1581       1.1     skrll generate_reloc (bfd *abfd, struct bfd_link_info *info)
   1582       1.1     skrll {
   1583       1.1     skrll   /* For .reloc stuff.  */
   1584       1.1     skrll   reloc_data_type *reloc_data;
   1585       1.1     skrll   int total_relocs = 0;
   1586       1.1     skrll   int i;
   1587       1.1     skrll   bfd_vma sec_page = (bfd_vma) -1;
   1588  1.1.1.10  christos   bfd_vma page_ptr;
   1589       1.1     skrll   bfd *b;
   1590       1.1     skrll   struct bfd_section *s;
   1591       1.1     skrll 
   1592   1.1.1.9  christos   if (reloc_s == NULL || reloc_s->output_section == bfd_abs_section_ptr)
   1593   1.1.1.9  christos     return;
   1594  1.1.1.10  christos 
   1595  1.1.1.10  christos   /* Set an upper bound for the total number of relocations we will have to generate.  */
   1596       1.1     skrll   total_relocs = 0;
   1597   1.1.1.4  christos   for (b = info->input_bfds; b; b = b->link.next)
   1598       1.1     skrll     for (s = b->sections; s; s = s->next)
   1599       1.1     skrll       total_relocs += s->reloc_count;
   1600       1.1     skrll 
   1601       1.1     skrll   reloc_data = xmalloc (total_relocs * sizeof (reloc_data_type));
   1602       1.1     skrll 
   1603       1.1     skrll   total_relocs = 0;
   1604  1.1.1.10  christos   for (b = info->input_bfds; b; b = b->link.next)
   1605       1.1     skrll     {
   1606       1.1     skrll       arelent **relocs;
   1607   1.1.1.2  christos       int relsize, nrelocs;
   1608  1.1.1.10  christos       asymbol **symbols;
   1609  1.1.1.10  christos 
   1610  1.1.1.10  christos       if (!bfd_generic_link_read_symbols (b))
   1611  1.1.1.10  christos 	{
   1612  1.1.1.11  christos 	  fatal (_("%P: %pB: could not read symbols: %E\n"), b);
   1613  1.1.1.10  christos 	  return;
   1614  1.1.1.10  christos 	}
   1615  1.1.1.10  christos 
   1616  1.1.1.10  christos       symbols = bfd_get_outsymbols (b);
   1617       1.1     skrll 
   1618       1.1     skrll       for (s = b->sections; s; s = s->next)
   1619       1.1     skrll 	{
   1620  1.1.1.10  christos 	  bfd_vma sec_vma;
   1621       1.1     skrll 
   1622  1.1.1.10  christos 	  /* If the section is not going to be output, then ignore it.  */
   1623  1.1.1.10  christos 	  if (s->output_section == NULL)
   1624  1.1.1.10  christos 	    {
   1625  1.1.1.10  christos 	      /* FIXME: This should not happen.  Convert to the correct
   1626  1.1.1.10  christos 		 form here, but really, this should be investigated.  */
   1627  1.1.1.10  christos 	      s->output_section = bfd_abs_section_ptr;
   1628  1.1.1.10  christos 	      continue;
   1629  1.1.1.10  christos 	    }
   1630       1.1     skrll 
   1631       1.1     skrll 	  /* I don't know why there would be a reloc for these, but I've
   1632       1.1     skrll 	     seen it happen - DJ  */
   1633   1.1.1.3  christos 	  if (s->output_section == bfd_abs_section_ptr)
   1634       1.1     skrll 	    continue;
   1635       1.1     skrll 
   1636  1.1.1.10  christos 	  /* If it's not loaded, we don't need to relocate it this way.  */
   1637  1.1.1.10  christos 	  if (!(s->output_section->flags & SEC_LOAD))
   1638  1.1.1.10  christos 	    continue;
   1639  1.1.1.10  christos 
   1640  1.1.1.10  christos 	  /* This happens when linking with --just-symbols=<file>
   1641  1.1.1.10  christos 	     so do not generate an error.  */
   1642       1.1     skrll 	  if (s->output_section->vma == 0)
   1643  1.1.1.10  christos 	    continue;
   1644       1.1     skrll 
   1645  1.1.1.10  christos 	  sec_vma = s->output_section->vma + s->output_offset;
   1646       1.1     skrll 
   1647       1.1     skrll 	  relsize = bfd_get_reloc_upper_bound (b, s);
   1648       1.1     skrll 	  relocs = xmalloc (relsize);
   1649       1.1     skrll 	  nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
   1650       1.1     skrll 
   1651       1.1     skrll 	  for (i = 0; i < nrelocs; i++)
   1652       1.1     skrll 	    {
   1653       1.1     skrll 	      if (pe_dll_extra_pe_debug)
   1654       1.1     skrll 		{
   1655       1.1     skrll 		  struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
   1656       1.1     skrll 		  printf ("rel: %s\n", sym->name);
   1657       1.1     skrll 		}
   1658       1.1     skrll 	      if (!relocs[i]->howto->pc_relative
   1659  1.1.1.11  christos 		  && (bfd_get_flavour (b) != bfd_target_coff_flavour
   1660  1.1.1.11  christos 		      || (relocs[i]->howto->type != pe_details->imagebase_reloc
   1661  1.1.1.11  christos 			  && (relocs[i]->howto->type < pe_details->secrel_reloc_lo
   1662  1.1.1.11  christos 			      || relocs[i]->howto->type > pe_details->secrel_reloc_hi)
   1663  1.1.1.11  christos 			  && relocs[i]->howto->type != pe_details->section_reloc)))
   1664       1.1     skrll 		{
   1665       1.1     skrll 		  struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
   1666   1.1.1.9  christos 		  const struct bfd_link_hash_entry *blhe
   1667   1.1.1.9  christos 		    = bfd_wrapped_link_hash_lookup (abfd, info, sym->name,
   1668   1.1.1.9  christos 						    false, false, false);
   1669   1.1.1.2  christos 		  /* Don't create relocs for undefined weak symbols.  */
   1670  1.1.1.12  christos 		  if (sym->flags & BSF_WEAK)
   1671       1.1     skrll 		    {
   1672   1.1.1.2  christos 		      if (blhe && blhe->type == bfd_link_hash_undefweak)
   1673   1.1.1.2  christos 			{
   1674   1.1.1.2  christos 			  /* Check aux sym and see if it is defined or not. */
   1675   1.1.1.2  christos 			  struct coff_link_hash_entry *h, *h2;
   1676   1.1.1.2  christos 			  h = (struct coff_link_hash_entry *)blhe;
   1677   1.1.1.2  christos 			  if (h->symbol_class != C_NT_WEAK || h->numaux != 1)
   1678   1.1.1.2  christos 			    continue;
   1679   1.1.1.2  christos 			  h2 = h->auxbfd->tdata.coff_obj_data->sym_hashes
   1680  1.1.1.10  christos 						[h->aux->x_sym.x_tagndx.u32];
   1681   1.1.1.2  christos 			  /* We don't want a base reloc if the aux sym is not
   1682   1.1.1.2  christos 			     found, undefined, or if it is the constant ABS
   1683   1.1.1.2  christos 			     zero default value.  (We broaden that slightly by
   1684   1.1.1.2  christos 			     not testing the value, just the section; there's
   1685   1.1.1.2  christos 			     no reason we'd want a reference to any absolute
   1686   1.1.1.2  christos 			     address to get relocated during rebasing).  */
   1687   1.1.1.2  christos 			  if (!h2 || h2->root.type == bfd_link_hash_undefined
   1688   1.1.1.3  christos 				|| h2->root.u.def.section == bfd_abs_section_ptr)
   1689   1.1.1.2  christos 			    continue;
   1690   1.1.1.2  christos 			}
   1691   1.1.1.2  christos 		      else if (!blhe || blhe->type != bfd_link_hash_defined)
   1692   1.1.1.2  christos 			continue;
   1693       1.1     skrll 		    }
   1694   1.1.1.3  christos 		  /* Nor for Dwarf FDE references to discarded sections.  */
   1695  1.1.1.12  christos 		  if (bfd_is_abs_section (sym->section->output_section))
   1696   1.1.1.3  christos 		    {
   1697   1.1.1.3  christos 		      /* We only ignore relocs from .eh_frame sections, as
   1698   1.1.1.3  christos 			 they are discarded by the final link rather than
   1699   1.1.1.3  christos 			 resolved against the kept section.  */
   1700   1.1.1.3  christos 		      if (!strcmp (s->name, ".eh_frame"))
   1701   1.1.1.3  christos 			continue;
   1702   1.1.1.3  christos 		    }
   1703   1.1.1.9  christos 		  /* Nor for absolute symbols.  */
   1704  1.1.1.12  christos 		  if (blhe && ldexp_is_final_sym_absolute (blhe)
   1705  1.1.1.12  christos 		      && (!blhe->linker_def
   1706  1.1.1.12  christos 			  || (strcmp (sym->name, "__image_base__")
   1707  1.1.1.12  christos 			      && strcmp (sym->name, U ("__ImageBase")))))
   1708   1.1.1.9  christos 		    continue;
   1709       1.1     skrll 
   1710       1.1     skrll 		  reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
   1711   1.1.1.8  christos 		  reloc_data[total_relocs].idx = total_relocs;
   1712       1.1     skrll 
   1713   1.1.1.9  christos 		  /* Since we're only about to determine .reloc's size,
   1714   1.1.1.9  christos 		     subsequent output section VMA calculations will shift up
   1715   1.1.1.9  christos 		     sections at this or higher addresses.  Relocations for
   1716   1.1.1.9  christos 		     such sections would hence end up not being correct.  */
   1717   1.1.1.9  christos 		  if (reloc_data[total_relocs].vma
   1718   1.1.1.9  christos 		      >= reloc_s->output_section->vma)
   1719   1.1.1.9  christos 		    einfo (_("%P: base relocation for section `%s' above "
   1720   1.1.1.9  christos 			     ".reloc section\n"), s->output_section->name);
   1721   1.1.1.9  christos 
   1722       1.1     skrll #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
   1723       1.1     skrll 
   1724       1.1     skrll 		  switch BITS_AND_SHIFT (relocs[i]->howto->bitsize,
   1725       1.1     skrll 					 relocs[i]->howto->rightshift)
   1726       1.1     skrll 		    {
   1727  1.1.1.10  christos #ifdef pe_use_plus
   1728       1.1     skrll 		    case BITS_AND_SHIFT (64, 0):
   1729   1.1.1.9  christos 		      reloc_data[total_relocs].type = IMAGE_REL_BASED_DIR64;
   1730       1.1     skrll 		      total_relocs++;
   1731       1.1     skrll 		      break;
   1732       1.1     skrll #endif
   1733       1.1     skrll 		    case BITS_AND_SHIFT (32, 0):
   1734   1.1.1.9  christos 		      reloc_data[total_relocs].type = IMAGE_REL_BASED_HIGHLOW;
   1735       1.1     skrll 		      total_relocs++;
   1736       1.1     skrll 		      break;
   1737       1.1     skrll 		    case BITS_AND_SHIFT (16, 0):
   1738   1.1.1.9  christos 		      reloc_data[total_relocs].type = IMAGE_REL_BASED_LOW;
   1739       1.1     skrll 		      total_relocs++;
   1740       1.1     skrll 		      break;
   1741       1.1     skrll 		    case BITS_AND_SHIFT (26, 2):
   1742   1.1.1.9  christos 		      reloc_data[total_relocs].type =
   1743   1.1.1.9  christos                         IMAGE_REL_BASED_ARM_MOV32;
   1744       1.1     skrll 		      total_relocs++;
   1745       1.1     skrll 		      break;
   1746       1.1     skrll 		    case BITS_AND_SHIFT (24, 2):
   1747       1.1     skrll 		      /* FIXME: 0 is ARM_26D, it is defined in bfd/coff-arm.c
   1748       1.1     skrll 			 Those ARM_xxx definitions should go in proper
   1749       1.1     skrll 			 header someday.  */
   1750       1.1     skrll 		      if (relocs[i]->howto->type == 0
   1751       1.1     skrll 			  /* Older GNU linkers used 5 instead of 0 for this reloc.  */
   1752       1.1     skrll 			  || relocs[i]->howto->type == 5)
   1753       1.1     skrll 			/* This is an ARM_26D reloc, which is an ARM_26 reloc
   1754       1.1     skrll 			   that has already been fully processed during a
   1755       1.1     skrll 			   previous link stage, so ignore it here.  */
   1756       1.1     skrll 			break;
   1757       1.1     skrll 		      /* Fall through.  */
   1758       1.1     skrll 		    default:
   1759       1.1     skrll 		      /* xgettext:c-format */
   1760   1.1.1.7  christos 		      einfo (_("%X%P: error: %d-bit reloc in dll\n"),
   1761       1.1     skrll 			     relocs[i]->howto->bitsize);
   1762       1.1     skrll 		      break;
   1763       1.1     skrll 		    }
   1764       1.1     skrll 		}
   1765       1.1     skrll 	    }
   1766  1.1.1.10  christos 
   1767       1.1     skrll 	  free (relocs);
   1768       1.1     skrll 	  /* Warning: the allocated symbols are remembered in BFD and
   1769       1.1     skrll 	     reused later, so don't free them!  */
   1770       1.1     skrll 	}
   1771       1.1     skrll     }
   1772       1.1     skrll 
   1773  1.1.1.10  christos   /* This can happen for example when LTO has eliminated all code.  */
   1774  1.1.1.10  christos   if (total_relocs == 0)
   1775  1.1.1.10  christos     return;
   1776  1.1.1.10  christos 
   1777       1.1     skrll   /* At this point, we have total_relocs relocation addresses in
   1778       1.1     skrll      reloc_addresses, which are all suitable for the .reloc section.
   1779       1.1     skrll      We must now create the new sections.  */
   1780       1.1     skrll   qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort);
   1781       1.1     skrll 
   1782       1.1     skrll   for (i = 0; i < total_relocs; i++)
   1783       1.1     skrll     {
   1784       1.1     skrll       bfd_vma this_page = (reloc_data[i].vma >> 12);
   1785       1.1     skrll 
   1786       1.1     skrll       if (this_page != sec_page)
   1787       1.1     skrll 	{
   1788       1.1     skrll 	  reloc_sz = (reloc_sz + 3) & ~3;	/* 4-byte align.  */
   1789       1.1     skrll 	  reloc_sz += 8;
   1790       1.1     skrll 	  sec_page = this_page;
   1791       1.1     skrll 	}
   1792       1.1     skrll 
   1793       1.1     skrll       reloc_sz += 2;
   1794       1.1     skrll     }
   1795       1.1     skrll 
   1796       1.1     skrll   reloc_sz = (reloc_sz + 3) & ~3;	/* 4-byte align.  */
   1797       1.1     skrll   reloc_d = xmalloc (reloc_sz);
   1798  1.1.1.10  christos 
   1799  1.1.1.10  christos   page_ptr = sec_page = (bfd_vma) -1;
   1800       1.1     skrll   reloc_sz = 0;
   1801       1.1     skrll 
   1802       1.1     skrll   for (i = 0; i < total_relocs; i++)
   1803       1.1     skrll     {
   1804       1.1     skrll       bfd_vma rva = reloc_data[i].vma - image_base;
   1805       1.1     skrll       bfd_vma this_page = (rva & ~0xfff);
   1806       1.1     skrll 
   1807       1.1     skrll       if (this_page != sec_page)
   1808       1.1     skrll 	{
   1809       1.1     skrll 	  while (reloc_sz & 3)
   1810       1.1     skrll 	    reloc_d[reloc_sz++] = 0;
   1811       1.1     skrll 
   1812       1.1     skrll 	  if (page_ptr != (bfd_vma) -1)
   1813       1.1     skrll 	    bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
   1814       1.1     skrll 
   1815       1.1     skrll 	  bfd_put_32 (abfd, this_page, reloc_d + reloc_sz);
   1816       1.1     skrll 	  page_ptr = reloc_sz;
   1817       1.1     skrll 	  reloc_sz += 8;
   1818       1.1     skrll 	  sec_page = this_page;
   1819       1.1     skrll 	}
   1820       1.1     skrll 
   1821       1.1     skrll       bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type << 12),
   1822       1.1     skrll 		  reloc_d + reloc_sz);
   1823       1.1     skrll       reloc_sz += 2;
   1824       1.1     skrll 
   1825   1.1.1.9  christos       if (reloc_data[i].type == IMAGE_REL_BASED_HIGHADJ)
   1826       1.1     skrll 	{
   1827       1.1     skrll 	  bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
   1828       1.1     skrll 	  reloc_sz += 2;
   1829       1.1     skrll 	}
   1830       1.1     skrll     }
   1831       1.1     skrll 
   1832       1.1     skrll   while (reloc_sz & 3)
   1833       1.1     skrll     reloc_d[reloc_sz++] = 0;
   1834       1.1     skrll 
   1835       1.1     skrll   if (page_ptr != (bfd_vma) -1)
   1836       1.1     skrll     bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
   1837       1.1     skrll }
   1838       1.1     skrll 
   1839       1.1     skrll /* Given the exiting def_file structure, print out a .DEF file that
   1840       1.1     skrll    corresponds to it.  */
   1841       1.1     skrll 
   1842       1.1     skrll static void
   1843       1.1     skrll quoteput (char *s, FILE *f, int needs_quotes)
   1844       1.1     skrll {
   1845       1.1     skrll   char *cp;
   1846       1.1     skrll 
   1847       1.1     skrll   for (cp = s; *cp; cp++)
   1848       1.1     skrll     if (*cp == '\''
   1849       1.1     skrll 	|| *cp == '"'
   1850       1.1     skrll 	|| *cp == '\\'
   1851       1.1     skrll 	|| ISSPACE (*cp)
   1852       1.1     skrll 	|| *cp == ','
   1853       1.1     skrll 	|| *cp == ';')
   1854       1.1     skrll       needs_quotes = 1;
   1855       1.1     skrll 
   1856       1.1     skrll   if (needs_quotes)
   1857       1.1     skrll     {
   1858       1.1     skrll       putc ('"', f);
   1859       1.1     skrll 
   1860       1.1     skrll       while (*s)
   1861       1.1     skrll 	{
   1862       1.1     skrll 	  if (*s == '"' || *s == '\\')
   1863       1.1     skrll 	    putc ('\\', f);
   1864       1.1     skrll 
   1865       1.1     skrll 	  putc (*s, f);
   1866       1.1     skrll 	  s++;
   1867       1.1     skrll 	}
   1868       1.1     skrll 
   1869       1.1     skrll       putc ('"', f);
   1870       1.1     skrll     }
   1871       1.1     skrll   else
   1872       1.1     skrll     fputs (s, f);
   1873       1.1     skrll }
   1874       1.1     skrll 
   1875       1.1     skrll void
   1876       1.1     skrll pe_dll_generate_def_file (const char *pe_out_def_filename)
   1877       1.1     skrll {
   1878       1.1     skrll   int i;
   1879       1.1     skrll   FILE *out = fopen (pe_out_def_filename, "w");
   1880       1.1     skrll 
   1881       1.1     skrll   if (out == NULL)
   1882       1.1     skrll     /* xgettext:c-format */
   1883   1.1.1.7  christos     einfo (_("%P: can't open output def file %s\n"),
   1884   1.1.1.6  christos 	   pe_out_def_filename);
   1885       1.1     skrll 
   1886       1.1     skrll   if (pe_def_file)
   1887       1.1     skrll     {
   1888       1.1     skrll       if (pe_def_file->name)
   1889       1.1     skrll 	{
   1890       1.1     skrll 	  if (pe_def_file->is_dll)
   1891       1.1     skrll 	    fprintf (out, "LIBRARY ");
   1892       1.1     skrll 	  else
   1893       1.1     skrll 	    fprintf (out, "NAME ");
   1894       1.1     skrll 
   1895       1.1     skrll 	  quoteput (pe_def_file->name, out, 1);
   1896       1.1     skrll 
   1897       1.1     skrll 	  if (pe_data (link_info.output_bfd)->pe_opthdr.ImageBase)
   1898  1.1.1.10  christos 	    fprintf (out, " BASE=0x%" PRIx64,
   1899  1.1.1.10  christos 		     (uint64_t) pe_data (link_info.output_bfd)->pe_opthdr.ImageBase);
   1900       1.1     skrll 	  fprintf (out, "\n");
   1901       1.1     skrll 	}
   1902       1.1     skrll 
   1903       1.1     skrll       if (pe_def_file->description)
   1904       1.1     skrll 	{
   1905       1.1     skrll 	  fprintf (out, "DESCRIPTION ");
   1906       1.1     skrll 	  quoteput (pe_def_file->description, out, 1);
   1907       1.1     skrll 	  fprintf (out, "\n");
   1908       1.1     skrll 	}
   1909       1.1     skrll 
   1910       1.1     skrll       if (pe_def_file->version_minor != -1)
   1911       1.1     skrll 	fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major,
   1912       1.1     skrll 		 pe_def_file->version_minor);
   1913       1.1     skrll       else if (pe_def_file->version_major != -1)
   1914       1.1     skrll 	fprintf (out, "VERSION %d\n", pe_def_file->version_major);
   1915       1.1     skrll 
   1916       1.1     skrll       if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1)
   1917       1.1     skrll 	fprintf (out, "\n");
   1918       1.1     skrll 
   1919       1.1     skrll       if (pe_def_file->stack_commit != -1)
   1920       1.1     skrll 	fprintf (out, "STACKSIZE 0x%x,0x%x\n",
   1921       1.1     skrll 		 pe_def_file->stack_reserve, pe_def_file->stack_commit);
   1922       1.1     skrll       else if (pe_def_file->stack_reserve != -1)
   1923       1.1     skrll 	fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve);
   1924       1.1     skrll 
   1925       1.1     skrll       if (pe_def_file->heap_commit != -1)
   1926       1.1     skrll 	fprintf (out, "HEAPSIZE 0x%x,0x%x\n",
   1927       1.1     skrll 		 pe_def_file->heap_reserve, pe_def_file->heap_commit);
   1928       1.1     skrll       else if (pe_def_file->heap_reserve != -1)
   1929       1.1     skrll 	fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve);
   1930       1.1     skrll 
   1931       1.1     skrll       if (pe_def_file->num_section_defs > 0)
   1932       1.1     skrll 	{
   1933       1.1     skrll 	  fprintf (out, "\nSECTIONS\n\n");
   1934       1.1     skrll 
   1935       1.1     skrll 	  for (i = 0; i < pe_def_file->num_section_defs; i++)
   1936       1.1     skrll 	    {
   1937       1.1     skrll 	      fprintf (out, "    ");
   1938       1.1     skrll 	      quoteput (pe_def_file->section_defs[i].name, out, 0);
   1939       1.1     skrll 
   1940       1.1     skrll 	      if (pe_def_file->section_defs[i].class)
   1941       1.1     skrll 		{
   1942       1.1     skrll 		  fprintf (out, " CLASS ");
   1943       1.1     skrll 		  quoteput (pe_def_file->section_defs[i].class, out, 0);
   1944       1.1     skrll 		}
   1945       1.1     skrll 
   1946       1.1     skrll 	      if (pe_def_file->section_defs[i].flag_read)
   1947       1.1     skrll 		fprintf (out, " READ");
   1948       1.1     skrll 
   1949       1.1     skrll 	      if (pe_def_file->section_defs[i].flag_write)
   1950       1.1     skrll 		fprintf (out, " WRITE");
   1951       1.1     skrll 
   1952       1.1     skrll 	      if (pe_def_file->section_defs[i].flag_execute)
   1953       1.1     skrll 		fprintf (out, " EXECUTE");
   1954       1.1     skrll 
   1955       1.1     skrll 	      if (pe_def_file->section_defs[i].flag_shared)
   1956       1.1     skrll 		fprintf (out, " SHARED");
   1957       1.1     skrll 
   1958       1.1     skrll 	      fprintf (out, "\n");
   1959       1.1     skrll 	    }
   1960       1.1     skrll 	}
   1961       1.1     skrll 
   1962       1.1     skrll       if (pe_def_file->num_exports > 0)
   1963       1.1     skrll 	{
   1964       1.1     skrll 	  fprintf (out, "EXPORTS\n");
   1965       1.1     skrll 
   1966       1.1     skrll 	  for (i = 0; i < pe_def_file->num_exports; i++)
   1967       1.1     skrll 	    {
   1968       1.1     skrll 	      def_file_export *e = pe_def_file->exports + i;
   1969       1.1     skrll 	      fprintf (out, "    ");
   1970       1.1     skrll 	      quoteput (e->name, out, 0);
   1971       1.1     skrll 
   1972       1.1     skrll 	      if (e->internal_name && strcmp (e->internal_name, e->name))
   1973       1.1     skrll 		{
   1974       1.1     skrll 		  fprintf (out, " = ");
   1975       1.1     skrll 		  quoteput (e->internal_name, out, 0);
   1976       1.1     skrll 		}
   1977       1.1     skrll 
   1978       1.1     skrll 	      if (e->ordinal != -1)
   1979       1.1     skrll 		fprintf (out, " @%d", e->ordinal);
   1980       1.1     skrll 
   1981       1.1     skrll 	      if (e->flag_private)
   1982       1.1     skrll 		fprintf (out, " PRIVATE");
   1983       1.1     skrll 
   1984       1.1     skrll 	      if (e->flag_constant)
   1985       1.1     skrll 		fprintf (out, " CONSTANT");
   1986       1.1     skrll 
   1987       1.1     skrll 	      if (e->flag_noname)
   1988       1.1     skrll 		fprintf (out, " NONAME");
   1989       1.1     skrll 
   1990       1.1     skrll 	      if (e->flag_data)
   1991       1.1     skrll 		fprintf (out, " DATA");
   1992       1.1     skrll 
   1993       1.1     skrll 	      fprintf (out, "\n");
   1994       1.1     skrll 	    }
   1995       1.1     skrll 	}
   1996       1.1     skrll 
   1997       1.1     skrll       if (pe_def_file->num_imports > 0)
   1998       1.1     skrll 	{
   1999       1.1     skrll 	  fprintf (out, "\nIMPORTS\n\n");
   2000       1.1     skrll 
   2001       1.1     skrll 	  for (i = 0; i < pe_def_file->num_imports; i++)
   2002       1.1     skrll 	    {
   2003       1.1     skrll 	      def_file_import *im = pe_def_file->imports + i;
   2004       1.1     skrll 	      fprintf (out, "    ");
   2005       1.1     skrll 
   2006       1.1     skrll 	      if (im->internal_name
   2007       1.1     skrll 		  && (!im->name || strcmp (im->internal_name, im->name)))
   2008       1.1     skrll 		{
   2009       1.1     skrll 		  quoteput (im->internal_name, out, 0);
   2010       1.1     skrll 		  fprintf (out, " = ");
   2011       1.1     skrll 		}
   2012       1.1     skrll 
   2013       1.1     skrll 	      quoteput (im->module->name, out, 0);
   2014       1.1     skrll 	      fprintf (out, ".");
   2015       1.1     skrll 
   2016       1.1     skrll 	      if (im->name)
   2017       1.1     skrll 		quoteput (im->name, out, 0);
   2018       1.1     skrll 	      else
   2019       1.1     skrll 		fprintf (out, "%d", im->ordinal);
   2020       1.1     skrll 
   2021   1.1.1.2  christos 	      if (im->its_name)
   2022   1.1.1.2  christos 		{
   2023   1.1.1.2  christos 		  fprintf (out, " == ");
   2024   1.1.1.2  christos 		  quoteput (im->its_name, out, 0);
   2025   1.1.1.2  christos 		}
   2026   1.1.1.2  christos 
   2027       1.1     skrll 	      fprintf (out, "\n");
   2028       1.1     skrll 	    }
   2029       1.1     skrll 	}
   2030       1.1     skrll     }
   2031       1.1     skrll   else
   2032       1.1     skrll     fprintf (out, _("; no contents available\n"));
   2033       1.1     skrll 
   2034       1.1     skrll   if (fclose (out) == EOF)
   2035       1.1     skrll     /* xgettext:c-format */
   2036   1.1.1.7  christos     einfo (_("%P: error closing file `%s'\n"), pe_out_def_filename);
   2037       1.1     skrll }
   2038       1.1     skrll 
   2039       1.1     skrll /* Generate the import library.  */
   2040       1.1     skrll 
   2041       1.1     skrll static asymbol **symtab;
   2042       1.1     skrll static int symptr;
   2043       1.1     skrll static int tmp_seq;
   2044       1.1     skrll static char *dll_symname;
   2045       1.1     skrll 
   2046   1.1.1.3  christos #define UNDSEC bfd_und_section_ptr
   2047       1.1     skrll 
   2048       1.1     skrll static asection *
   2049       1.1     skrll quick_section (bfd *abfd, const char *name, int flags, int align)
   2050       1.1     skrll {
   2051       1.1     skrll   asection *sec;
   2052       1.1     skrll   asymbol *sym;
   2053       1.1     skrll 
   2054       1.1     skrll   sec = bfd_make_section_old_way (abfd, name);
   2055   1.1.1.8  christos   bfd_set_section_flags (sec, flags | SEC_ALLOC | SEC_LOAD | SEC_KEEP);
   2056   1.1.1.8  christos   bfd_set_section_alignment (sec, align);
   2057       1.1     skrll   /* Remember to undo this before trying to link internally!  */
   2058       1.1     skrll   sec->output_section = sec;
   2059       1.1     skrll 
   2060       1.1     skrll   sym = bfd_make_empty_symbol (abfd);
   2061       1.1     skrll   symtab[symptr++] = sym;
   2062       1.1     skrll   sym->name = sec->name;
   2063       1.1     skrll   sym->section = sec;
   2064       1.1     skrll   sym->flags = BSF_LOCAL;
   2065       1.1     skrll   sym->value = 0;
   2066       1.1     skrll 
   2067       1.1     skrll   return sec;
   2068       1.1     skrll }
   2069       1.1     skrll 
   2070       1.1     skrll static void
   2071       1.1     skrll quick_symbol (bfd *abfd,
   2072       1.1     skrll 	      const char *n1,
   2073       1.1     skrll 	      const char *n2,
   2074       1.1     skrll 	      const char *n3,
   2075       1.1     skrll 	      asection *sec,
   2076       1.1     skrll 	      int flags,
   2077       1.1     skrll 	      int addr)
   2078       1.1     skrll {
   2079       1.1     skrll   asymbol *sym;
   2080       1.1     skrll   char *name = xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
   2081       1.1     skrll 
   2082       1.1     skrll   strcpy (name, n1);
   2083       1.1     skrll   strcat (name, n2);
   2084       1.1     skrll   strcat (name, n3);
   2085       1.1     skrll   sym = bfd_make_empty_symbol (abfd);
   2086       1.1     skrll   sym->name = name;
   2087       1.1     skrll   sym->section = sec;
   2088       1.1     skrll   sym->flags = flags;
   2089       1.1     skrll   sym->value = addr;
   2090       1.1     skrll   symtab[symptr++] = sym;
   2091       1.1     skrll }
   2092       1.1     skrll 
   2093       1.1     skrll static arelent *reltab = 0;
   2094       1.1     skrll static int relcount = 0, relsize = 0;
   2095       1.1     skrll 
   2096       1.1     skrll static void
   2097       1.1     skrll quick_reloc (bfd *abfd, bfd_size_type address, int which_howto, int symidx)
   2098       1.1     skrll {
   2099       1.1     skrll   if (relcount >= relsize - 1)
   2100       1.1     skrll     {
   2101       1.1     skrll       relsize += 10;
   2102       1.1     skrll       if (reltab)
   2103       1.1     skrll 	reltab = xrealloc (reltab, relsize * sizeof (arelent));
   2104       1.1     skrll       else
   2105       1.1     skrll 	reltab = xmalloc (relsize * sizeof (arelent));
   2106       1.1     skrll     }
   2107       1.1     skrll   reltab[relcount].address = address;
   2108       1.1     skrll   reltab[relcount].addend = 0;
   2109       1.1     skrll   reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
   2110       1.1     skrll   reltab[relcount].sym_ptr_ptr = symtab + symidx;
   2111       1.1     skrll   relcount++;
   2112       1.1     skrll }
   2113       1.1     skrll 
   2114       1.1     skrll static void
   2115       1.1     skrll save_relocs (asection *sec)
   2116       1.1     skrll {
   2117       1.1     skrll   int i;
   2118       1.1     skrll 
   2119       1.1     skrll   sec->relocation = reltab;
   2120       1.1     skrll   sec->reloc_count = relcount;
   2121       1.1     skrll   sec->orelocation = xmalloc ((relcount + 1) * sizeof (arelent *));
   2122       1.1     skrll   for (i = 0; i < relcount; i++)
   2123       1.1     skrll     sec->orelocation[i] = sec->relocation + i;
   2124       1.1     skrll   sec->orelocation[relcount] = 0;
   2125       1.1     skrll   sec->flags |= SEC_RELOC;
   2126       1.1     skrll   reltab = 0;
   2127       1.1     skrll   relcount = relsize = 0;
   2128       1.1     skrll }
   2129       1.1     skrll 
   2130       1.1     skrll /*	.section	.idata$2
   2131   1.1.1.6  christos 	.global		__head_my_dll
   2132       1.1     skrll    __head_my_dll:
   2133   1.1.1.6  christos 	.rva		hname
   2134   1.1.1.6  christos 	.long		0
   2135   1.1.1.6  christos 	.long		0
   2136   1.1.1.6  christos 	.rva		__my_dll_iname
   2137   1.1.1.6  christos 	.rva		fthunk
   2138       1.1     skrll 
   2139   1.1.1.6  christos 	.section	.idata$5
   2140   1.1.1.6  christos 	.long		0
   2141       1.1     skrll    fthunk:
   2142       1.1     skrll 
   2143   1.1.1.6  christos 	.section	.idata$4
   2144   1.1.1.6  christos 	.long		0
   2145       1.1     skrll    hname:                              */
   2146       1.1     skrll 
   2147       1.1     skrll static bfd *
   2148       1.1     skrll make_head (bfd *parent)
   2149       1.1     skrll {
   2150       1.1     skrll   asection *id2, *id5, *id4;
   2151       1.1     skrll   unsigned char *d2, *d5, *d4;
   2152       1.1     skrll   char *oname;
   2153       1.1     skrll   bfd *abfd;
   2154       1.1     skrll 
   2155  1.1.1.11  christos   oname = xasprintf ("%s_d%06d.o", dll_symname, tmp_seq);
   2156       1.1     skrll   tmp_seq++;
   2157       1.1     skrll 
   2158       1.1     skrll   abfd = bfd_create (oname, parent);
   2159  1.1.1.10  christos   free (oname);
   2160       1.1     skrll   bfd_find_target (pe_details->object_target, abfd);
   2161       1.1     skrll   bfd_make_writable (abfd);
   2162       1.1     skrll 
   2163       1.1     skrll   bfd_set_format (abfd, bfd_object);
   2164       1.1     skrll   bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
   2165       1.1     skrll 
   2166       1.1     skrll   symptr = 0;
   2167       1.1     skrll   symtab = xmalloc (6 * sizeof (asymbol *));
   2168       1.1     skrll   id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
   2169       1.1     skrll   id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
   2170       1.1     skrll   id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
   2171       1.1     skrll   quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
   2172       1.1     skrll   quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
   2173       1.1     skrll 
   2174       1.1     skrll   /* OK, pay attention here.  I got confused myself looking back at
   2175       1.1     skrll      it.  We create a four-byte section to mark the beginning of the
   2176       1.1     skrll      list, and we include an offset of 4 in the section, so that the
   2177       1.1     skrll      pointer to the list points to the *end* of this section, which is
   2178       1.1     skrll      the start of the list of sections from other objects.  */
   2179       1.1     skrll 
   2180   1.1.1.8  christos   bfd_set_section_size (id2, 20);
   2181       1.1     skrll   d2 = xmalloc (20);
   2182       1.1     skrll   id2->contents = d2;
   2183       1.1     skrll   memset (d2, 0, 20);
   2184   1.1.1.2  christos   if (pe_use_nul_prefixed_import_tables)
   2185   1.1.1.2  christos     d2[0] = d2[16] = PE_IDATA5_SIZE; /* Reloc addend.  */
   2186       1.1     skrll   quick_reloc (abfd,  0, BFD_RELOC_RVA, 2);
   2187       1.1     skrll   quick_reloc (abfd, 12, BFD_RELOC_RVA, 4);
   2188       1.1     skrll   quick_reloc (abfd, 16, BFD_RELOC_RVA, 1);
   2189       1.1     skrll   save_relocs (id2);
   2190       1.1     skrll 
   2191   1.1.1.2  christos   if (pe_use_nul_prefixed_import_tables)
   2192   1.1.1.8  christos     bfd_set_section_size (id5, PE_IDATA5_SIZE);
   2193   1.1.1.2  christos   else
   2194   1.1.1.8  christos     bfd_set_section_size (id5, 0);
   2195       1.1     skrll   d5 = xmalloc (PE_IDATA5_SIZE);
   2196       1.1     skrll   id5->contents = d5;
   2197       1.1     skrll   memset (d5, 0, PE_IDATA5_SIZE);
   2198   1.1.1.2  christos   if (pe_use_nul_prefixed_import_tables)
   2199   1.1.1.8  christos     bfd_set_section_size (id4, PE_IDATA4_SIZE);
   2200   1.1.1.2  christos   else
   2201   1.1.1.8  christos     bfd_set_section_size (id4, 0);
   2202       1.1     skrll   d4 = xmalloc (PE_IDATA4_SIZE);
   2203       1.1     skrll   id4->contents = d4;
   2204       1.1     skrll   memset (d4, 0, PE_IDATA4_SIZE);
   2205       1.1     skrll 
   2206       1.1     skrll   bfd_set_symtab (abfd, symtab, symptr);
   2207       1.1     skrll 
   2208       1.1     skrll   bfd_set_section_contents (abfd, id2, d2, 0, 20);
   2209   1.1.1.2  christos   if (pe_use_nul_prefixed_import_tables)
   2210   1.1.1.2  christos     {
   2211   1.1.1.2  christos       bfd_set_section_contents (abfd, id5, d5, 0, PE_IDATA5_SIZE);
   2212   1.1.1.2  christos       bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE);
   2213   1.1.1.2  christos     }
   2214   1.1.1.2  christos   else
   2215   1.1.1.2  christos     {
   2216   1.1.1.2  christos       bfd_set_section_contents (abfd, id5, d5, 0, 0);
   2217   1.1.1.2  christos       bfd_set_section_contents (abfd, id4, d4, 0, 0);
   2218   1.1.1.2  christos     }
   2219       1.1     skrll 
   2220       1.1     skrll   bfd_make_readable (abfd);
   2221       1.1     skrll   return abfd;
   2222       1.1     skrll }
   2223       1.1     skrll 
   2224       1.1     skrll /*	.section	.idata$4
   2225   1.1.1.6  christos 	.long		0
   2226       1.1     skrll 	[.long		0] for PE+
   2227   1.1.1.6  christos 	.section	.idata$5
   2228   1.1.1.6  christos 	.long		0
   2229       1.1     skrll 	[.long		0] for PE+
   2230   1.1.1.6  christos 	.section	idata$7
   2231   1.1.1.6  christos 	.global		__my_dll_iname
   2232       1.1     skrll   __my_dll_iname:
   2233   1.1.1.6  christos 	.asciz		"my.dll"       */
   2234       1.1     skrll 
   2235       1.1     skrll static bfd *
   2236       1.1     skrll make_tail (bfd *parent)
   2237       1.1     skrll {
   2238       1.1     skrll   asection *id4, *id5, *id7;
   2239       1.1     skrll   unsigned char *d4, *d5, *d7;
   2240       1.1     skrll   int len;
   2241       1.1     skrll   char *oname;
   2242       1.1     skrll   bfd *abfd;
   2243       1.1     skrll 
   2244  1.1.1.11  christos   oname = xasprintf ("%s_d%06d.o", dll_symname, tmp_seq);
   2245       1.1     skrll   tmp_seq++;
   2246       1.1     skrll 
   2247       1.1     skrll   abfd = bfd_create (oname, parent);
   2248  1.1.1.10  christos   free (oname);
   2249       1.1     skrll   bfd_find_target (pe_details->object_target, abfd);
   2250       1.1     skrll   bfd_make_writable (abfd);
   2251       1.1     skrll 
   2252       1.1     skrll   bfd_set_format (abfd, bfd_object);
   2253       1.1     skrll   bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
   2254       1.1     skrll 
   2255       1.1     skrll   symptr = 0;
   2256       1.1     skrll   symtab = xmalloc (5 * sizeof (asymbol *));
   2257       1.1     skrll   id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
   2258       1.1     skrll   id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
   2259       1.1     skrll   id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
   2260       1.1     skrll   quick_symbol (abfd, U (""), dll_symname, "_iname", id7, BSF_GLOBAL, 0);
   2261       1.1     skrll 
   2262   1.1.1.8  christos   bfd_set_section_size (id4, PE_IDATA4_SIZE);
   2263       1.1     skrll   d4 = xmalloc (PE_IDATA4_SIZE);
   2264       1.1     skrll   id4->contents = d4;
   2265       1.1     skrll   memset (d4, 0, PE_IDATA4_SIZE);
   2266       1.1     skrll 
   2267   1.1.1.8  christos   bfd_set_section_size (id5, PE_IDATA5_SIZE);
   2268       1.1     skrll   d5 = xmalloc (PE_IDATA5_SIZE);
   2269       1.1     skrll   id5->contents = d5;
   2270       1.1     skrll   memset (d5, 0, PE_IDATA5_SIZE);
   2271       1.1     skrll 
   2272       1.1     skrll   len = strlen (dll_filename) + 1;
   2273       1.1     skrll   if (len & 1)
   2274       1.1     skrll     len++;
   2275   1.1.1.8  christos   bfd_set_section_size (id7, len);
   2276       1.1     skrll   d7 = xmalloc (len);
   2277       1.1     skrll   id7->contents = d7;
   2278       1.1     skrll   strcpy ((char *) d7, dll_filename);
   2279       1.1     skrll   /* If len was odd, the above
   2280       1.1     skrll      strcpy leaves behind an undefined byte. That is harmless,
   2281       1.1     skrll      but we set it to 0 just so the binary dumps are pretty.  */
   2282       1.1     skrll   d7[len - 1] = 0;
   2283       1.1     skrll 
   2284       1.1     skrll   bfd_set_symtab (abfd, symtab, symptr);
   2285       1.1     skrll 
   2286       1.1     skrll   bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE);
   2287       1.1     skrll   bfd_set_section_contents (abfd, id5, d5, 0, PE_IDATA5_SIZE);
   2288       1.1     skrll   bfd_set_section_contents (abfd, id7, d7, 0, len);
   2289       1.1     skrll 
   2290       1.1     skrll   bfd_make_readable (abfd);
   2291       1.1     skrll   return abfd;
   2292       1.1     skrll }
   2293       1.1     skrll 
   2294       1.1     skrll /*	.text
   2295   1.1.1.6  christos 	.global		_function
   2296   1.1.1.6  christos 	.global		___imp_function
   2297   1.1.1.6  christos 	.global		__imp__function
   2298       1.1     skrll   _function:
   2299   1.1.1.6  christos 	jmp		*__imp__function:
   2300       1.1     skrll 
   2301   1.1.1.6  christos 	.section	idata$7
   2302   1.1.1.6  christos 	.long		__head_my_dll
   2303       1.1     skrll 
   2304   1.1.1.6  christos 	.section	.idata$5
   2305       1.1     skrll   ___imp_function:
   2306       1.1     skrll   __imp__function:
   2307       1.1     skrll   iat?
   2308   1.1.1.6  christos 	.section	.idata$4
   2309       1.1     skrll   iat?
   2310   1.1.1.6  christos 	.section	.idata$6
   2311       1.1     skrll   ID<ordinal>:
   2312   1.1.1.6  christos 	.short		<hint>
   2313   1.1.1.6  christos 	.asciz		"function" xlate? (add underscore, kill at)  */
   2314       1.1     skrll 
   2315       1.1     skrll static const unsigned char jmp_ix86_bytes[] =
   2316       1.1     skrll {
   2317       1.1     skrll   0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
   2318       1.1     skrll };
   2319       1.1     skrll 
   2320  1.1.1.10  christos static const unsigned char jmp_aarch64_bytes[] =
   2321  1.1.1.10  christos {
   2322  1.1.1.10  christos   0x10, 0x00, 0x00, 0x90, /* adrp x16, 0        */
   2323  1.1.1.10  christos   0x10, 0x02, 0x00, 0x91, /* add x16, x16, #0x0 */
   2324  1.1.1.10  christos   0x10, 0x02, 0x40, 0xf9, /* ldr x16, [x16]     */
   2325  1.1.1.10  christos   0x00, 0x02, 0x1f, 0xd6  /* br x16             */
   2326  1.1.1.10  christos };
   2327  1.1.1.10  christos 
   2328       1.1     skrll /* _function:
   2329   1.1.1.6  christos 	mov.l	ip+8,r0
   2330   1.1.1.6  christos 	mov.l	@r0,r0
   2331   1.1.1.6  christos 	jmp	@r0
   2332   1.1.1.6  christos 	nop
   2333   1.1.1.6  christos 	.dw	__imp_function   */
   2334       1.1     skrll 
   2335       1.1     skrll static const unsigned char jmp_sh_bytes[] =
   2336       1.1     skrll {
   2337       1.1     skrll   0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
   2338       1.1     skrll };
   2339       1.1     skrll 
   2340       1.1     skrll static const unsigned char jmp_arm_bytes[] =
   2341       1.1     skrll {
   2342       1.1     skrll   0x00, 0xc0, 0x9f, 0xe5,	/* ldr  ip, [pc] */
   2343       1.1     skrll   0x00, 0xf0, 0x9c, 0xe5,	/* ldr  pc, [ip] */
   2344       1.1     skrll   0,    0,    0,    0
   2345       1.1     skrll };
   2346       1.1     skrll 
   2347       1.1     skrll 
   2348       1.1     skrll static bfd *
   2349   1.1.1.9  christos make_one (def_file_export *exp, bfd *parent, bool include_jmp_stub)
   2350       1.1     skrll {
   2351       1.1     skrll   asection *tx, *id7, *id5, *id4, *id6;
   2352       1.1     skrll   unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
   2353       1.1     skrll   int len;
   2354       1.1     skrll   char *oname;
   2355       1.1     skrll   bfd *abfd;
   2356       1.1     skrll   const unsigned char *jmp_bytes = NULL;
   2357       1.1     skrll   int jmp_byte_count = 0;
   2358  1.1.1.10  christos   const char *internal_name = exp->internal_name;
   2359  1.1.1.10  christos 
   2360  1.1.1.10  christos   if (!exp->flag_noname)
   2361  1.1.1.10  christos     {
   2362  1.1.1.10  christos       /* Check for a decorated symbol name */
   2363  1.1.1.10  christos       struct decoration_hash_entry *entry;
   2364  1.1.1.10  christos 
   2365  1.1.1.10  christos       entry = (struct decoration_hash_entry *)
   2366  1.1.1.10  christos 	      bfd_hash_lookup (&(coff_hash_table (&link_info)->decoration_hash),
   2367  1.1.1.10  christos 			       internal_name, false, false);
   2368  1.1.1.10  christos       if (entry)
   2369  1.1.1.10  christos 	{
   2370  1.1.1.10  christos 	  if (entry->decorated_link)
   2371  1.1.1.10  christos 	    {
   2372  1.1.1.10  christos 	      internal_name = entry->decorated_link->root.string;
   2373  1.1.1.10  christos 
   2374  1.1.1.10  christos 	      if (pe_details->underscored && internal_name[0] == '_')
   2375  1.1.1.10  christos 		internal_name++;
   2376  1.1.1.10  christos 	    }
   2377  1.1.1.10  christos 	  else
   2378  1.1.1.10  christos 	    {
   2379  1.1.1.10  christos 	      einfo (_("%P: error: NULL decorated name for %s\n"), internal_name);
   2380  1.1.1.10  christos 	    }
   2381  1.1.1.10  christos 	}
   2382  1.1.1.10  christos     }
   2383       1.1     skrll 
   2384       1.1     skrll   /* Include the jump stub section only if it is needed. A jump
   2385       1.1     skrll      stub is needed if the symbol being imported <sym> is a function
   2386       1.1     skrll      symbol and there is at least one undefined reference to that
   2387       1.1     skrll      symbol. In other words, if all the import references to <sym> are
   2388       1.1     skrll      explicitly through _declspec(dllimport) then the jump stub is not
   2389       1.1     skrll      needed.  */
   2390       1.1     skrll   if (include_jmp_stub)
   2391       1.1     skrll     {
   2392       1.1     skrll       switch (pe_details->pe_arch)
   2393       1.1     skrll 	{
   2394       1.1     skrll 	case PE_ARCH_i386:
   2395       1.1     skrll 	  jmp_bytes = jmp_ix86_bytes;
   2396       1.1     skrll 	  jmp_byte_count = sizeof (jmp_ix86_bytes);
   2397       1.1     skrll 	  break;
   2398       1.1     skrll 	case PE_ARCH_sh:
   2399       1.1     skrll 	  jmp_bytes = jmp_sh_bytes;
   2400       1.1     skrll 	  jmp_byte_count = sizeof (jmp_sh_bytes);
   2401       1.1     skrll 	  break;
   2402       1.1     skrll 	case PE_ARCH_arm:
   2403       1.1     skrll 	case PE_ARCH_arm_wince:
   2404       1.1     skrll 	  jmp_bytes = jmp_arm_bytes;
   2405       1.1     skrll 	  jmp_byte_count = sizeof (jmp_arm_bytes);
   2406       1.1     skrll 	  break;
   2407  1.1.1.10  christos 	case PE_ARCH_aarch64:
   2408  1.1.1.10  christos 	  jmp_bytes = jmp_aarch64_bytes;
   2409  1.1.1.10  christos 	  jmp_byte_count = sizeof (jmp_aarch64_bytes);
   2410  1.1.1.10  christos 	  break;
   2411       1.1     skrll 	default:
   2412       1.1     skrll 	  abort ();
   2413       1.1     skrll 	}
   2414       1.1     skrll     }
   2415       1.1     skrll 
   2416  1.1.1.11  christos   oname = xasprintf ("%s_d%06d.o", dll_symname, tmp_seq);
   2417       1.1     skrll   tmp_seq++;
   2418       1.1     skrll 
   2419       1.1     skrll   abfd = bfd_create (oname, parent);
   2420  1.1.1.10  christos   free (oname);
   2421       1.1     skrll   bfd_find_target (pe_details->object_target, abfd);
   2422       1.1     skrll   bfd_make_writable (abfd);
   2423       1.1     skrll 
   2424       1.1     skrll   bfd_set_format (abfd, bfd_object);
   2425       1.1     skrll   bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
   2426       1.1     skrll 
   2427       1.1     skrll   symptr = 0;
   2428   1.1.1.2  christos   symtab = xmalloc (12 * sizeof (asymbol *));
   2429   1.1.1.2  christos 
   2430   1.1.1.2  christos   tx  = quick_section (abfd, ".text", SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY, 2);
   2431       1.1     skrll   id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
   2432       1.1     skrll   id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
   2433       1.1     skrll   id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
   2434       1.1     skrll   id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
   2435       1.1     skrll 
   2436  1.1.1.10  christos   if  (*internal_name == '@')
   2437       1.1     skrll     {
   2438       1.1     skrll       quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
   2439       1.1     skrll 		    BSF_GLOBAL, 0);
   2440       1.1     skrll       if (include_jmp_stub)
   2441  1.1.1.10  christos 	quick_symbol (abfd, "", internal_name, "", tx, BSF_GLOBAL, 0);
   2442  1.1.1.10  christos       quick_symbol (abfd, "__imp_", internal_name, "", id5,
   2443       1.1     skrll 		    BSF_GLOBAL, 0);
   2444       1.1     skrll       /* Fastcall applies only to functions,
   2445       1.1     skrll 	 so no need for auto-import symbol.  */
   2446       1.1     skrll     }
   2447       1.1     skrll   else
   2448       1.1     skrll     {
   2449       1.1     skrll       quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
   2450       1.1     skrll 		    BSF_GLOBAL, 0);
   2451       1.1     skrll       if (include_jmp_stub)
   2452  1.1.1.10  christos 	quick_symbol (abfd, U (""), internal_name, "", tx,
   2453       1.1     skrll 		      BSF_GLOBAL, 0);
   2454  1.1.1.10  christos       quick_symbol (abfd, "__imp_", U (""), internal_name, id5,
   2455       1.1     skrll 		    BSF_GLOBAL, 0);
   2456       1.1     skrll       /* Symbol to reference ord/name of imported
   2457       1.1     skrll 	 data symbol, used to implement auto-import.  */
   2458       1.1     skrll       if (exp->flag_data)
   2459  1.1.1.10  christos 	quick_symbol (abfd, "__nm_", U (""), internal_name, id6,
   2460       1.1     skrll 		      BSF_GLOBAL,0);
   2461       1.1     skrll     }
   2462       1.1     skrll   if (pe_dll_compat_implib)
   2463  1.1.1.10  christos     quick_symbol (abfd, "___imp_", internal_name, "", id5,
   2464       1.1     skrll 		  BSF_GLOBAL, 0);
   2465       1.1     skrll 
   2466       1.1     skrll   if (include_jmp_stub)
   2467       1.1     skrll     {
   2468   1.1.1.8  christos       bfd_set_section_size (tx, jmp_byte_count);
   2469       1.1     skrll       td = xmalloc (jmp_byte_count);
   2470       1.1     skrll       tx->contents = td;
   2471       1.1     skrll       memcpy (td, jmp_bytes, jmp_byte_count);
   2472       1.1     skrll 
   2473       1.1     skrll       switch (pe_details->pe_arch)
   2474       1.1     skrll 	{
   2475       1.1     skrll 	case PE_ARCH_i386:
   2476  1.1.1.10  christos #ifdef pe_use_plus
   2477       1.1     skrll 	  quick_reloc (abfd, 2, BFD_RELOC_32_PCREL, 2);
   2478       1.1     skrll #else
   2479   1.1.1.2  christos 	  /* Mark this object as SAFESEH compatible.  */
   2480   1.1.1.2  christos 	  quick_symbol (abfd, "", "@feat.00", "", bfd_abs_section_ptr,
   2481   1.1.1.2  christos 			BSF_LOCAL, 1);
   2482   1.1.1.6  christos 	  quick_reloc (abfd, 2, BFD_RELOC_32, 2);
   2483       1.1     skrll #endif
   2484       1.1     skrll 	  break;
   2485       1.1     skrll 	case PE_ARCH_sh:
   2486       1.1     skrll 	  quick_reloc (abfd, 8, BFD_RELOC_32, 2);
   2487       1.1     skrll 	  break;
   2488       1.1     skrll 	case PE_ARCH_arm:
   2489   1.1.1.6  christos 	case PE_ARCH_arm_wince:
   2490       1.1     skrll 	  quick_reloc (abfd, 8, BFD_RELOC_32, 2);
   2491       1.1     skrll 	  break;
   2492  1.1.1.10  christos 	case PE_ARCH_aarch64:
   2493  1.1.1.10  christos 	  quick_reloc (abfd, 0, BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL, 2);
   2494  1.1.1.10  christos 	  quick_reloc (abfd, 4, BFD_RELOC_AARCH64_ADD_LO12, 2);
   2495  1.1.1.10  christos 	  break;
   2496       1.1     skrll 	default:
   2497       1.1     skrll 	  abort ();
   2498       1.1     skrll 	}
   2499       1.1     skrll       save_relocs (tx);
   2500       1.1     skrll     }
   2501       1.1     skrll   else
   2502   1.1.1.8  christos     bfd_set_section_size (tx, 0);
   2503       1.1     skrll 
   2504   1.1.1.8  christos   bfd_set_section_size (id7, 4);
   2505       1.1     skrll   d7 = xmalloc (4);
   2506       1.1     skrll   id7->contents = d7;
   2507       1.1     skrll   memset (d7, 0, 4);
   2508       1.1     skrll   quick_reloc (abfd, 0, BFD_RELOC_RVA, 5);
   2509       1.1     skrll   save_relocs (id7);
   2510       1.1     skrll 
   2511   1.1.1.8  christos   bfd_set_section_size (id5, PE_IDATA5_SIZE);
   2512       1.1     skrll   d5 = xmalloc (PE_IDATA5_SIZE);
   2513       1.1     skrll   id5->contents = d5;
   2514       1.1     skrll   memset (d5, 0, PE_IDATA5_SIZE);
   2515       1.1     skrll 
   2516       1.1     skrll   if (exp->flag_noname)
   2517       1.1     skrll     {
   2518       1.1     skrll       d5[0] = exp->ordinal;
   2519       1.1     skrll       d5[1] = exp->ordinal >> 8;
   2520       1.1     skrll       d5[PE_IDATA5_SIZE - 1] = 0x80;
   2521       1.1     skrll     }
   2522       1.1     skrll   else
   2523       1.1     skrll     {
   2524       1.1     skrll       quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
   2525       1.1     skrll       save_relocs (id5);
   2526       1.1     skrll     }
   2527       1.1     skrll 
   2528   1.1.1.8  christos   bfd_set_section_size (id4, PE_IDATA4_SIZE);
   2529       1.1     skrll   d4 = xmalloc (PE_IDATA4_SIZE);
   2530       1.1     skrll   id4->contents = d4;
   2531       1.1     skrll   memset (d4, 0, PE_IDATA4_SIZE);
   2532       1.1     skrll 
   2533       1.1     skrll   if (exp->flag_noname)
   2534       1.1     skrll     {
   2535       1.1     skrll       d4[0] = exp->ordinal;
   2536       1.1     skrll       d4[1] = exp->ordinal >> 8;
   2537       1.1     skrll       d4[PE_IDATA4_SIZE - 1] = 0x80;
   2538       1.1     skrll     }
   2539       1.1     skrll   else
   2540       1.1     skrll     {
   2541       1.1     skrll       quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
   2542       1.1     skrll       save_relocs (id4);
   2543       1.1     skrll     }
   2544       1.1     skrll 
   2545       1.1     skrll   if (exp->flag_noname)
   2546       1.1     skrll     {
   2547       1.1     skrll       len = 0;
   2548   1.1.1.8  christos       bfd_set_section_size (id6, 0);
   2549       1.1     skrll     }
   2550       1.1     skrll   else
   2551       1.1     skrll     {
   2552  1.1.1.11  christos       /* { short, asciz } = { hint, name }  */
   2553   1.1.1.2  christos       if (exp->its_name)
   2554   1.1.1.2  christos 	len = 2 + strlen (exp->its_name) + 1;
   2555   1.1.1.2  christos       else
   2556   1.1.1.2  christos 	len = 2 + strlen (exp->name) + 1;
   2557       1.1     skrll       if (len & 1)
   2558       1.1     skrll 	len++;
   2559   1.1.1.8  christos       bfd_set_section_size (id6, len);
   2560       1.1     skrll       d6 = xmalloc (len);
   2561       1.1     skrll       id6->contents = d6;
   2562       1.1     skrll       memset (d6, 0, len);
   2563  1.1.1.11  christos       d6[0] = exp->hint & 0xff;
   2564  1.1.1.11  christos       d6[1] = exp->hint >> 8;
   2565   1.1.1.2  christos       if (exp->its_name)
   2566   1.1.1.2  christos 	strcpy ((char*) d6 + 2, exp->its_name);
   2567   1.1.1.2  christos       else
   2568   1.1.1.2  christos 	strcpy ((char *) d6 + 2, exp->name);
   2569       1.1     skrll     }
   2570       1.1     skrll 
   2571       1.1     skrll   bfd_set_symtab (abfd, symtab, symptr);
   2572       1.1     skrll 
   2573       1.1     skrll   if (include_jmp_stub)
   2574       1.1     skrll     bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
   2575       1.1     skrll   bfd_set_section_contents (abfd, id7, d7, 0, 4);
   2576       1.1     skrll   bfd_set_section_contents (abfd, id5, d5, 0, PE_IDATA5_SIZE);
   2577       1.1     skrll   bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE);
   2578       1.1     skrll   if (!exp->flag_noname)
   2579       1.1     skrll     bfd_set_section_contents (abfd, id6, d6, 0, len);
   2580       1.1     skrll 
   2581       1.1     skrll   bfd_make_readable (abfd);
   2582       1.1     skrll   return abfd;
   2583       1.1     skrll }
   2584       1.1     skrll 
   2585       1.1     skrll static bfd *
   2586       1.1     skrll make_singleton_name_thunk (const char *import, bfd *parent)
   2587       1.1     skrll {
   2588       1.1     skrll   /* Name thunks go to idata$4.  */
   2589       1.1     skrll   asection *id4;
   2590       1.1     skrll   unsigned char *d4;
   2591       1.1     skrll   char *oname;
   2592       1.1     skrll   bfd *abfd;
   2593       1.1     skrll 
   2594  1.1.1.11  christos   oname = xasprintf ("%s_nmth%06d.o", dll_symname, tmp_seq);
   2595       1.1     skrll   tmp_seq++;
   2596       1.1     skrll 
   2597       1.1     skrll   abfd = bfd_create (oname, parent);
   2598  1.1.1.10  christos   free (oname);
   2599       1.1     skrll   bfd_find_target (pe_details->object_target, abfd);
   2600       1.1     skrll   bfd_make_writable (abfd);
   2601       1.1     skrll 
   2602       1.1     skrll   bfd_set_format (abfd, bfd_object);
   2603       1.1     skrll   bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
   2604       1.1     skrll 
   2605       1.1     skrll   symptr = 0;
   2606       1.1     skrll   symtab = xmalloc (3 * sizeof (asymbol *));
   2607       1.1     skrll   id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
   2608   1.1.1.2  christos   quick_symbol (abfd, "__nm_thnk_", import, "", id4, BSF_GLOBAL, 0);
   2609   1.1.1.2  christos   quick_symbol (abfd, "__nm_", import, "", UNDSEC, BSF_GLOBAL, 0);
   2610       1.1     skrll 
   2611       1.1     skrll   /* We need space for the real thunk and for the null terminator.  */
   2612   1.1.1.8  christos   bfd_set_section_size (id4, PE_IDATA4_SIZE * 2);
   2613       1.1     skrll   d4 = xmalloc (PE_IDATA4_SIZE * 2);
   2614       1.1     skrll   id4->contents = d4;
   2615       1.1     skrll   memset (d4, 0, PE_IDATA4_SIZE * 2);
   2616       1.1     skrll   quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
   2617       1.1     skrll   save_relocs (id4);
   2618       1.1     skrll 
   2619       1.1     skrll   bfd_set_symtab (abfd, symtab, symptr);
   2620       1.1     skrll 
   2621       1.1     skrll   bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE * 2);
   2622       1.1     skrll 
   2623       1.1     skrll   bfd_make_readable (abfd);
   2624       1.1     skrll   return abfd;
   2625       1.1     skrll }
   2626       1.1     skrll 
   2627   1.1.1.7  christos static const char *
   2628   1.1.1.6  christos make_import_fixup_mark (arelent *rel, char *name)
   2629       1.1     skrll {
   2630       1.1     skrll   /* We convert reloc to symbol, for later reference.  */
   2631   1.1.1.6  christos   static unsigned int counter;
   2632       1.1     skrll   struct bfd_symbol *sym = *rel->sym_ptr_ptr;
   2633       1.1     skrll   bfd *abfd = bfd_asymbol_bfd (sym);
   2634       1.1     skrll   struct bfd_link_hash_entry *bh;
   2635   1.1.1.9  christos   char *fixup_name, buf[256];
   2636   1.1.1.6  christos   size_t prefix_len;
   2637       1.1     skrll 
   2638   1.1.1.6  christos   /* "name" buffer has space before the symbol name for prefixes.  */
   2639   1.1.1.6  christos   sprintf (buf, "__fu%d_", counter++);
   2640   1.1.1.6  christos   prefix_len = strlen (buf);
   2641   1.1.1.6  christos   fixup_name = name - prefix_len;
   2642   1.1.1.6  christos   memcpy (fixup_name, buf, prefix_len);
   2643       1.1     skrll 
   2644       1.1     skrll   bh = NULL;
   2645  1.1.1.11  christos   _bfd_generic_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL,
   2646  1.1.1.11  christos 				    current_sec, /* sym->section, */
   2647  1.1.1.11  christos 				    rel->address, NULL, true, false, &bh);
   2648       1.1     skrll 
   2649   1.1.1.7  christos   return bh->root.string;
   2650       1.1     skrll }
   2651       1.1     skrll 
   2652       1.1     skrll /*	.section	.idata$2
   2653   1.1.1.6  christos 	.rva		__nm_thnk_SYM (singleton thunk with name of func)
   2654   1.1.1.6  christos 	.long		0
   2655   1.1.1.6  christos 	.long		0
   2656   1.1.1.6  christos 	.rva		__my_dll_iname (name of dll)
   2657   1.1.1.6  christos 	.rva		__fuNN_SYM (pointer to reference (address) in text)  */
   2658       1.1     skrll 
   2659       1.1     skrll static bfd *
   2660       1.1     skrll make_import_fixup_entry (const char *name,
   2661       1.1     skrll 			 const char *fixup_name,
   2662   1.1.1.2  christos 			 const char *symname,
   2663       1.1     skrll 			 bfd *parent)
   2664       1.1     skrll {
   2665       1.1     skrll   asection *id2;
   2666       1.1     skrll   unsigned char *d2;
   2667       1.1     skrll   char *oname;
   2668       1.1     skrll   bfd *abfd;
   2669       1.1     skrll 
   2670  1.1.1.11  christos   oname = xasprintf ("%s_fu%06d.o", dll_symname, tmp_seq);
   2671       1.1     skrll   tmp_seq++;
   2672       1.1     skrll 
   2673       1.1     skrll   abfd = bfd_create (oname, parent);
   2674  1.1.1.10  christos   free (oname);
   2675       1.1     skrll   bfd_find_target (pe_details->object_target, abfd);
   2676       1.1     skrll   bfd_make_writable (abfd);
   2677       1.1     skrll 
   2678       1.1     skrll   bfd_set_format (abfd, bfd_object);
   2679       1.1     skrll   bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
   2680       1.1     skrll 
   2681       1.1     skrll   symptr = 0;
   2682       1.1     skrll   symtab = xmalloc (6 * sizeof (asymbol *));
   2683       1.1     skrll   id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
   2684       1.1     skrll 
   2685   1.1.1.2  christos   quick_symbol (abfd, "__nm_thnk_", name, "", UNDSEC, BSF_GLOBAL, 0);
   2686   1.1.1.2  christos   quick_symbol (abfd, U (""), symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
   2687   1.1.1.7  christos   quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
   2688       1.1     skrll 
   2689   1.1.1.8  christos   bfd_set_section_size (id2, 20);
   2690       1.1     skrll   d2 = xmalloc (20);
   2691       1.1     skrll   id2->contents = d2;
   2692       1.1     skrll   memset (d2, 0, 20);
   2693       1.1     skrll 
   2694       1.1     skrll   quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
   2695       1.1     skrll   quick_reloc (abfd, 12, BFD_RELOC_RVA, 2);
   2696       1.1     skrll   quick_reloc (abfd, 16, BFD_RELOC_RVA, 3);
   2697       1.1     skrll   save_relocs (id2);
   2698       1.1     skrll 
   2699       1.1     skrll   bfd_set_symtab (abfd, symtab, symptr);
   2700       1.1     skrll 
   2701       1.1     skrll   bfd_set_section_contents (abfd, id2, d2, 0, 20);
   2702       1.1     skrll 
   2703       1.1     skrll   bfd_make_readable (abfd);
   2704       1.1     skrll   return abfd;
   2705       1.1     skrll }
   2706       1.1     skrll 
   2707       1.1     skrll /*	.section	.rdata_runtime_pseudo_reloc
   2708   1.1.1.6  christos 	.long		addend
   2709   1.1.1.6  christos 	.rva		__fuNN_SYM (pointer to reference (address) in text)  */
   2710       1.1     skrll 
   2711       1.1     skrll static bfd *
   2712       1.1     skrll make_runtime_pseudo_reloc (const char *name ATTRIBUTE_UNUSED,
   2713       1.1     skrll 			   const char *fixup_name,
   2714   1.1.1.2  christos 			   bfd_vma addend ATTRIBUTE_UNUSED,
   2715   1.1.1.2  christos 			   bfd_vma bitsize,
   2716       1.1     skrll 			   bfd *parent)
   2717       1.1     skrll {
   2718       1.1     skrll   asection *rt_rel;
   2719       1.1     skrll   unsigned char *rt_rel_d;
   2720       1.1     skrll   char *oname;
   2721       1.1     skrll   bfd *abfd;
   2722   1.1.1.7  christos   bfd_size_type size;
   2723   1.1.1.7  christos 
   2724  1.1.1.11  christos   oname = xasprintf ("%s_rtr%06d.o", dll_symname, tmp_seq);
   2725       1.1     skrll   tmp_seq++;
   2726       1.1     skrll 
   2727       1.1     skrll   abfd = bfd_create (oname, parent);
   2728  1.1.1.10  christos   free (oname);
   2729       1.1     skrll   bfd_find_target (pe_details->object_target, abfd);
   2730       1.1     skrll   bfd_make_writable (abfd);
   2731       1.1     skrll 
   2732       1.1     skrll   bfd_set_format (abfd, bfd_object);
   2733       1.1     skrll   bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
   2734       1.1     skrll 
   2735   1.1.1.2  christos   if (link_info.pei386_runtime_pseudo_reloc == 2)
   2736   1.1.1.2  christos     {
   2737   1.1.1.7  christos       if (runtime_pseudp_reloc_v2_init)
   2738   1.1.1.7  christos 	size = 3 * sizeof (asymbol *);
   2739   1.1.1.7  christos       else
   2740   1.1.1.7  christos 	size = 6 * sizeof (asymbol *);
   2741   1.1.1.2  christos     }
   2742   1.1.1.2  christos   else
   2743   1.1.1.7  christos     size = 2 * sizeof (asymbol *);
   2744   1.1.1.7  christos 
   2745   1.1.1.7  christos   symptr = 0;
   2746   1.1.1.7  christos   symtab = xmalloc (size);
   2747   1.1.1.7  christos 
   2748   1.1.1.7  christos   rt_rel
   2749   1.1.1.7  christos     = quick_section (abfd, ".rdata_runtime_pseudo_reloc", SEC_HAS_CONTENTS, 2);
   2750  1.1.1.10  christos   bfd_coff_set_long_section_names (abfd, true);
   2751       1.1     skrll 
   2752       1.1     skrll   quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
   2753       1.1     skrll 
   2754   1.1.1.2  christos   if (link_info.pei386_runtime_pseudo_reloc == 2)
   2755   1.1.1.2  christos     {
   2756   1.1.1.7  christos       size = 12;
   2757   1.1.1.7  christos       if (!runtime_pseudp_reloc_v2_init)
   2758   1.1.1.7  christos 	{
   2759   1.1.1.7  christos 	  size += 12;
   2760   1.1.1.9  christos 	  runtime_pseudp_reloc_v2_init = true;
   2761   1.1.1.7  christos 	}
   2762   1.1.1.7  christos 
   2763   1.1.1.2  christos       quick_symbol (abfd, "__imp_", name, "", UNDSEC, BSF_GLOBAL, 0);
   2764   1.1.1.2  christos 
   2765   1.1.1.8  christos       bfd_set_section_size (rt_rel, size);
   2766   1.1.1.2  christos       rt_rel_d = xmalloc (size);
   2767   1.1.1.2  christos       rt_rel->contents = rt_rel_d;
   2768   1.1.1.2  christos       memset (rt_rel_d, 0, size);
   2769   1.1.1.7  christos       quick_reloc (abfd, size - 8, BFD_RELOC_RVA, 1);
   2770   1.1.1.7  christos       quick_reloc (abfd, size - 12, BFD_RELOC_RVA, 2);
   2771   1.1.1.7  christos       bfd_put_32 (abfd, bitsize, rt_rel_d + (size - 4));
   2772   1.1.1.7  christos       if (size != 12)
   2773   1.1.1.7  christos 	bfd_put_32 (abfd, 1, rt_rel_d + 8);
   2774   1.1.1.2  christos       save_relocs (rt_rel);
   2775       1.1     skrll 
   2776   1.1.1.2  christos       bfd_set_symtab (abfd, symtab, symptr);
   2777       1.1     skrll 
   2778   1.1.1.2  christos       bfd_set_section_contents (abfd, rt_rel, rt_rel_d, 0, size);
   2779   1.1.1.7  christos     }
   2780   1.1.1.2  christos   else
   2781   1.1.1.7  christos     {
   2782   1.1.1.8  christos       bfd_set_section_size (rt_rel, 8);
   2783   1.1.1.2  christos       rt_rel_d = xmalloc (8);
   2784   1.1.1.2  christos       rt_rel->contents = rt_rel_d;
   2785   1.1.1.2  christos       memset (rt_rel_d, 0, 8);
   2786       1.1     skrll 
   2787   1.1.1.2  christos       bfd_put_32 (abfd, addend, rt_rel_d);
   2788   1.1.1.2  christos       quick_reloc (abfd, 4, BFD_RELOC_RVA, 1);
   2789       1.1     skrll 
   2790   1.1.1.2  christos       save_relocs (rt_rel);
   2791   1.1.1.2  christos 
   2792   1.1.1.2  christos       bfd_set_symtab (abfd, symtab, symptr);
   2793   1.1.1.2  christos 
   2794   1.1.1.2  christos       bfd_set_section_contents (abfd, rt_rel, rt_rel_d, 0, 8);
   2795   1.1.1.2  christos    }
   2796   1.1.1.7  christos 
   2797       1.1     skrll   bfd_make_readable (abfd);
   2798       1.1     skrll   return abfd;
   2799       1.1     skrll }
   2800       1.1     skrll 
   2801       1.1     skrll /*	.section	.rdata
   2802   1.1.1.6  christos 	.rva		__pei386_runtime_relocator  */
   2803       1.1     skrll 
   2804       1.1     skrll static bfd *
   2805       1.1     skrll pe_create_runtime_relocator_reference (bfd *parent)
   2806       1.1     skrll {
   2807       1.1     skrll   asection *extern_rt_rel;
   2808       1.1     skrll   unsigned char *extern_rt_rel_d;
   2809       1.1     skrll   char *oname;
   2810       1.1     skrll   bfd *abfd;
   2811       1.1     skrll 
   2812  1.1.1.11  christos   oname = xasprintf ("%s_ertr%06d.o", dll_symname, tmp_seq);
   2813       1.1     skrll   tmp_seq++;
   2814       1.1     skrll 
   2815       1.1     skrll   abfd = bfd_create (oname, parent);
   2816  1.1.1.10  christos   free (oname);
   2817       1.1     skrll   bfd_find_target (pe_details->object_target, abfd);
   2818       1.1     skrll   bfd_make_writable (abfd);
   2819       1.1     skrll 
   2820       1.1     skrll   bfd_set_format (abfd, bfd_object);
   2821       1.1     skrll   bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
   2822       1.1     skrll 
   2823       1.1     skrll   symptr = 0;
   2824       1.1     skrll   symtab = xmalloc (2 * sizeof (asymbol *));
   2825       1.1     skrll   extern_rt_rel = quick_section (abfd, ".rdata", SEC_HAS_CONTENTS, 2);
   2826       1.1     skrll 
   2827       1.1     skrll   quick_symbol (abfd, "", U ("_pei386_runtime_relocator"), "", UNDSEC,
   2828       1.1     skrll 		BSF_NO_FLAGS, 0);
   2829       1.1     skrll 
   2830   1.1.1.8  christos   bfd_set_section_size (extern_rt_rel, PE_IDATA5_SIZE);
   2831   1.1.1.4  christos   extern_rt_rel_d = xcalloc (1, PE_IDATA5_SIZE);
   2832       1.1     skrll   extern_rt_rel->contents = extern_rt_rel_d;
   2833       1.1     skrll 
   2834       1.1     skrll   quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
   2835       1.1     skrll   save_relocs (extern_rt_rel);
   2836       1.1     skrll 
   2837       1.1     skrll   bfd_set_symtab (abfd, symtab, symptr);
   2838       1.1     skrll 
   2839   1.1.1.2  christos   bfd_set_section_contents (abfd, extern_rt_rel, extern_rt_rel_d, 0, PE_IDATA5_SIZE);
   2840       1.1     skrll 
   2841       1.1     skrll   bfd_make_readable (abfd);
   2842       1.1     skrll   return abfd;
   2843       1.1     skrll }
   2844       1.1     skrll 
   2845       1.1     skrll void
   2846   1.1.1.7  christos pe_create_import_fixup (arelent *rel, asection *s, bfd_vma addend, char *name,
   2847   1.1.1.7  christos 			const char *symname)
   2848       1.1     skrll {
   2849   1.1.1.7  christos   const char *fixup_name = make_import_fixup_mark (rel, name);
   2850       1.1     skrll   bfd *b;
   2851   1.1.1.2  christos 
   2852   1.1.1.7  christos   /* This is the original implementation of the auto-import feature, which
   2853   1.1.1.7  christos      primarily relied on the OS loader to patch things up with some help
   2854   1.1.1.7  christos      from the pseudo-relocator to overcome the main limitation.  See the
   2855   1.1.1.7  christos      comment at the beginning of the file for an overview of the feature.  */
   2856   1.1.1.7  christos   if (link_info.pei386_runtime_pseudo_reloc != 2)
   2857   1.1.1.7  christos     {
   2858   1.1.1.7  christos       struct bfd_link_hash_entry *name_thunk_sym;
   2859   1.1.1.7  christos       /* name buffer is allocated with space at beginning for prefixes.  */
   2860   1.1.1.7  christos       char *thname = name - (sizeof "__nm_thnk_" - 1);
   2861   1.1.1.7  christos       memcpy (thname, "__nm_thnk_", sizeof "__nm_thnk_" - 1);
   2862   1.1.1.7  christos       name_thunk_sym = bfd_link_hash_lookup (link_info.hash, thname, 0, 0, 1);
   2863   1.1.1.2  christos 
   2864   1.1.1.7  christos       if (!(name_thunk_sym && name_thunk_sym->type == bfd_link_hash_defined))
   2865   1.1.1.6  christos 	{
   2866   1.1.1.7  christos 	  b = make_singleton_name_thunk (name, link_info.output_bfd);
   2867   1.1.1.9  christos 	  add_bfd_to_link (b, bfd_get_filename (b), &link_info);
   2868   1.1.1.7  christos 
   2869   1.1.1.7  christos 	  /* If we ever use autoimport, we have to cast text section writable.  */
   2870   1.1.1.9  christos 	  config.text_read_only = false;
   2871   1.1.1.6  christos 	  link_info.output_bfd->flags &= ~WP_TEXT;
   2872   1.1.1.6  christos 	}
   2873   1.1.1.7  christos 
   2874   1.1.1.7  christos       if (addend == 0 || link_info.pei386_runtime_pseudo_reloc == 1)
   2875   1.1.1.6  christos 	{
   2876   1.1.1.7  christos 	  b = make_import_fixup_entry (name, fixup_name, symname,
   2877   1.1.1.7  christos 				       link_info.output_bfd);
   2878   1.1.1.9  christos 	  add_bfd_to_link (b, bfd_get_filename (b), &link_info);
   2879   1.1.1.2  christos 	}
   2880       1.1     skrll     }
   2881       1.1     skrll 
   2882   1.1.1.7  christos   /* In the original implementation, the pseudo-relocator was only used when
   2883   1.1.1.7  christos      the addend was not null.  In the new implementation, the OS loader is
   2884   1.1.1.7  christos      completely bypassed and the pseudo-relocator does the entire work.  */
   2885   1.1.1.7  christos   if ((addend != 0 && link_info.pei386_runtime_pseudo_reloc == 1)
   2886   1.1.1.6  christos       || link_info.pei386_runtime_pseudo_reloc == 2)
   2887   1.1.1.6  christos     {
   2888   1.1.1.6  christos       if (pe_dll_extra_pe_debug)
   2889   1.1.1.6  christos 	printf ("creating runtime pseudo-reloc entry for %s (addend=%d)\n",
   2890   1.1.1.6  christos 		fixup_name, (int) addend);
   2891       1.1     skrll 
   2892   1.1.1.9  christos       b = make_runtime_pseudo_reloc (name, fixup_name, addend,
   2893   1.1.1.9  christos 				     rel->howto->bitsize,
   2894   1.1.1.6  christos 				     link_info.output_bfd);
   2895   1.1.1.9  christos       add_bfd_to_link (b, bfd_get_filename (b), &link_info);
   2896   1.1.1.6  christos 
   2897   1.1.1.7  christos       if (runtime_pseudo_relocs_created++ == 0)
   2898   1.1.1.6  christos 	{
   2899   1.1.1.6  christos 	  b = pe_create_runtime_relocator_reference (link_info.output_bfd);
   2900   1.1.1.9  christos 	  add_bfd_to_link (b, bfd_get_filename (b), &link_info);
   2901   1.1.1.6  christos 	}
   2902   1.1.1.6  christos     }
   2903   1.1.1.7  christos 
   2904   1.1.1.6  christos   else if (addend != 0)
   2905  1.1.1.10  christos     einfo (_("%X%P: %H: variable '%pT' can't be auto-imported; please read the documentation for ld's --enable-auto-import for details\n"),
   2906   1.1.1.7  christos 	   s->owner, s, rel->address, (*rel->sym_ptr_ptr)->name);
   2907       1.1     skrll }
   2908       1.1     skrll 
   2909       1.1     skrll void
   2910   1.1.1.2  christos pe_dll_generate_implib (def_file *def, const char *impfilename, struct bfd_link_info *info)
   2911       1.1     skrll {
   2912       1.1     skrll   int i;
   2913       1.1     skrll   bfd *ar_head;
   2914       1.1     skrll   bfd *ar_tail;
   2915       1.1     skrll   bfd *outarch;
   2916   1.1.1.2  christos   bfd *ibfd;
   2917       1.1     skrll   bfd *head = 0;
   2918       1.1     skrll 
   2919       1.1     skrll   unlink_if_ordinary (impfilename);
   2920       1.1     skrll 
   2921       1.1     skrll   outarch = bfd_openw (impfilename, 0);
   2922       1.1     skrll 
   2923       1.1     skrll   if (!outarch)
   2924       1.1     skrll     {
   2925       1.1     skrll       /* xgettext:c-format */
   2926   1.1.1.7  christos       einfo (_("%X%P: can't open .lib file: %s\n"), impfilename);
   2927       1.1     skrll       return;
   2928       1.1     skrll     }
   2929       1.1     skrll 
   2930   1.1.1.3  christos   if (verbose)
   2931   1.1.1.3  christos     /* xgettext:c-format */
   2932   1.1.1.3  christos     info_msg (_("Creating library file: %s\n"), impfilename);
   2933   1.1.1.2  christos 
   2934       1.1     skrll   bfd_set_format (outarch, bfd_archive);
   2935       1.1     skrll   outarch->has_armap = 1;
   2936       1.1     skrll 
   2937       1.1     skrll   /* Work out a reasonable size of things to put onto one line.  */
   2938       1.1     skrll   ar_head = make_head (outarch);
   2939       1.1     skrll 
   2940   1.1.1.2  christos   /* Iterate the input BFDs, looking for exclude-modules-for-implib.  */
   2941   1.1.1.4  christos   for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   2942   1.1.1.2  christos     {
   2943   1.1.1.2  christos       /* Iterate the exclude list.  */
   2944   1.1.1.2  christos       struct exclude_list_struct *ex;
   2945   1.1.1.2  christos       char found;
   2946   1.1.1.2  christos       for (ex = excludes, found = 0; ex && !found; ex = ex->next)
   2947   1.1.1.2  christos 	{
   2948   1.1.1.2  christos 	  if (ex->type != EXCLUDEFORIMPLIB)
   2949   1.1.1.2  christos 	    continue;
   2950   1.1.1.9  christos 	  found = (filename_cmp (ex->string, bfd_get_filename (ibfd)) == 0);
   2951   1.1.1.2  christos 	}
   2952   1.1.1.2  christos       /* If it matched, we must open a fresh BFD for it (the original
   2953   1.1.1.6  christos 	 input BFD is still needed for the DLL's final link) and add
   2954   1.1.1.6  christos 	 it into the archive member chain.  */
   2955   1.1.1.2  christos       if (found)
   2956   1.1.1.2  christos 	{
   2957   1.1.1.2  christos 	  bfd *newbfd = bfd_openr (ibfd->my_archive
   2958   1.1.1.9  christos 				   ? bfd_get_filename (ibfd->my_archive)
   2959   1.1.1.9  christos 				   : bfd_get_filename (ibfd), NULL);
   2960   1.1.1.2  christos 	  if (!newbfd)
   2961   1.1.1.2  christos 	    {
   2962   1.1.1.9  christos 	      einfo (_("%X%P: bfd_openr %s: %E\n"), bfd_get_filename (ibfd));
   2963   1.1.1.2  christos 	      return;
   2964   1.1.1.2  christos 	    }
   2965   1.1.1.2  christos 	  if (ibfd->my_archive)
   2966   1.1.1.2  christos 	    {
   2967   1.1.1.2  christos 	      /* Must now iterate through archive until we find the
   2968   1.1.1.2  christos 		required member.  A minor shame that we'll open the
   2969   1.1.1.2  christos 		archive once per member that we require from it, and
   2970   1.1.1.2  christos 		leak those archive bfds rather than reuse them.  */
   2971   1.1.1.2  christos 	      bfd *arbfd = newbfd;
   2972   1.1.1.2  christos 	      if (!bfd_check_format_matches (arbfd, bfd_archive, NULL))
   2973   1.1.1.2  christos 		{
   2974   1.1.1.7  christos 		  einfo (_("%X%P: %s(%s): can't find member in non-archive file"),
   2975   1.1.1.9  christos 			 bfd_get_filename (ibfd->my_archive),
   2976   1.1.1.9  christos 			 bfd_get_filename (ibfd));
   2977   1.1.1.2  christos 		  return;
   2978   1.1.1.2  christos 		}
   2979   1.1.1.2  christos 	      newbfd = NULL;
   2980   1.1.1.2  christos 	      while ((newbfd = bfd_openr_next_archived_file (arbfd, newbfd)) != 0)
   2981   1.1.1.2  christos 		{
   2982   1.1.1.9  christos 		  if (filename_cmp (bfd_get_filename (newbfd),
   2983   1.1.1.9  christos 				    bfd_get_filename (ibfd)) == 0)
   2984   1.1.1.2  christos 		    break;
   2985   1.1.1.2  christos 		}
   2986   1.1.1.2  christos 	      if (!newbfd)
   2987   1.1.1.2  christos 		{
   2988   1.1.1.7  christos 		  einfo (_("%X%P: %s(%s): can't find member in archive"),
   2989   1.1.1.9  christos 			 bfd_get_filename (ibfd->my_archive),
   2990   1.1.1.9  christos 			 bfd_get_filename (ibfd));
   2991   1.1.1.2  christos 		  return;
   2992   1.1.1.2  christos 		}
   2993   1.1.1.2  christos 	    }
   2994   1.1.1.2  christos 	  newbfd->archive_next = head;
   2995   1.1.1.2  christos 	  head = newbfd;
   2996   1.1.1.2  christos 	}
   2997   1.1.1.2  christos     }
   2998   1.1.1.2  christos 
   2999       1.1     skrll   for (i = 0; i < def->num_exports; i++)
   3000       1.1     skrll     {
   3001       1.1     skrll       /* The import library doesn't know about the internal name.  */
   3002       1.1     skrll       char *internal = def->exports[i].internal_name;
   3003       1.1     skrll       bfd *n;
   3004       1.1     skrll 
   3005   1.1.1.2  christos       /* Don't add PRIVATE entries to import lib.  */
   3006       1.1     skrll       if (pe_def_file->exports[i].flag_private)
   3007       1.1     skrll 	continue;
   3008   1.1.1.5  christos 
   3009       1.1     skrll       def->exports[i].internal_name = def->exports[i].name;
   3010   1.1.1.5  christos 
   3011   1.1.1.5  christos       /* PR 19803: If a symbol has been discard due to garbage
   3012   1.1.1.5  christos 	 collection then do not create any exports for it.  */
   3013   1.1.1.5  christos       {
   3014   1.1.1.5  christos 	struct coff_link_hash_entry *h;
   3015   1.1.1.5  christos 
   3016   1.1.1.5  christos 	h = coff_link_hash_lookup (coff_hash_table (info), internal,
   3017   1.1.1.9  christos 				   false, false, false);
   3018   1.1.1.5  christos 	if (h != NULL
   3019   1.1.1.5  christos 	    /* If the symbol is hidden and undefined then it
   3020   1.1.1.5  christos 	       has been swept up by garbage collection.  */
   3021   1.1.1.5  christos 	    && h->symbol_class == C_HIDDEN
   3022   1.1.1.5  christos 	    && h->root.u.def.section == bfd_und_section_ptr)
   3023   1.1.1.5  christos 	  continue;
   3024   1.1.1.5  christos 
   3025   1.1.1.5  christos 	/* If necessary, check with an underscore prefix as well.  */
   3026   1.1.1.5  christos 	if (pe_details->underscored && internal[0] != '@')
   3027   1.1.1.5  christos 	  {
   3028   1.1.1.5  christos 	    char *name;
   3029   1.1.1.5  christos 
   3030   1.1.1.5  christos 	    name = xmalloc (strlen (internal) + 2);
   3031   1.1.1.5  christos 	    sprintf (name, "_%s", internal);
   3032   1.1.1.5  christos 
   3033   1.1.1.5  christos 	    h = coff_link_hash_lookup (coff_hash_table (info), name,
   3034   1.1.1.9  christos 				       false, false, false);
   3035   1.1.1.5  christos 	    free (name);
   3036   1.1.1.5  christos 
   3037   1.1.1.5  christos 	    if (h != NULL
   3038   1.1.1.5  christos 		/* If the symbol is hidden and undefined then it
   3039   1.1.1.5  christos 		   has been swept up by garbage collection.  */
   3040   1.1.1.5  christos 		&& h->symbol_class == C_HIDDEN
   3041   1.1.1.5  christos 		&& h->root.u.def.section == bfd_und_section_ptr)
   3042   1.1.1.5  christos 	      continue;
   3043   1.1.1.5  christos 	  }
   3044   1.1.1.5  christos       }
   3045   1.1.1.5  christos 
   3046   1.1.1.9  christos       n = make_one (def->exports + i, outarch, !(def->exports + i)->flag_data);
   3047       1.1     skrll       n->archive_next = head;
   3048       1.1     skrll       head = n;
   3049       1.1     skrll       def->exports[i].internal_name = internal;
   3050       1.1     skrll     }
   3051       1.1     skrll 
   3052       1.1     skrll   ar_tail = make_tail (outarch);
   3053       1.1     skrll 
   3054       1.1     skrll   if (ar_head == NULL || ar_tail == NULL)
   3055       1.1     skrll     return;
   3056       1.1     skrll 
   3057       1.1     skrll   /* Now stick them all into the archive.  */
   3058       1.1     skrll   ar_head->archive_next = head;
   3059       1.1     skrll   ar_tail->archive_next = ar_head;
   3060       1.1     skrll   head = ar_tail;
   3061       1.1     skrll 
   3062       1.1     skrll   if (! bfd_set_archive_head (outarch, head))
   3063   1.1.1.7  christos     einfo ("%X%P: bfd_set_archive_head: %E\n");
   3064       1.1     skrll 
   3065       1.1     skrll   if (! bfd_close (outarch))
   3066   1.1.1.7  christos     einfo ("%X%P: bfd_close %s: %E\n", impfilename);
   3067       1.1     skrll }
   3068       1.1     skrll 
   3069   1.1.1.3  christos static int undef_count = 0;
   3070   1.1.1.3  christos 
   3071   1.1.1.3  christos struct key_value
   3072   1.1.1.3  christos {
   3073   1.1.1.3  christos   char *key;
   3074   1.1.1.3  christos   const char *oname;
   3075   1.1.1.3  christos };
   3076   1.1.1.3  christos 
   3077   1.1.1.3  christos static struct key_value *udef_table;
   3078   1.1.1.3  christos 
   3079   1.1.1.3  christos static int undef_sort_cmp (const void *l1, const void *r1)
   3080   1.1.1.3  christos {
   3081   1.1.1.3  christos   const struct key_value *l = l1;
   3082   1.1.1.3  christos   const struct key_value *r = r1;
   3083   1.1.1.3  christos 
   3084   1.1.1.3  christos   return strcmp (l->key, r->key);
   3085   1.1.1.3  christos }
   3086   1.1.1.3  christos 
   3087   1.1.1.3  christos static struct bfd_link_hash_entry *
   3088   1.1.1.3  christos pe_find_cdecl_alias_match (struct bfd_link_info *linfo, char *name)
   3089   1.1.1.3  christos {
   3090   1.1.1.3  christos   struct bfd_link_hash_entry *h = NULL;
   3091   1.1.1.3  christos   struct key_value *kv;
   3092   1.1.1.3  christos   struct key_value key;
   3093   1.1.1.5  christos   char *at, *lname = xmalloc (strlen (name) + 3);
   3094   1.1.1.4  christos 
   3095   1.1.1.3  christos   strcpy (lname, name);
   3096   1.1.1.3  christos 
   3097   1.1.1.3  christos   at = strchr (lname + (lname[0] == '@'), '@');
   3098   1.1.1.3  christos   if (at)
   3099   1.1.1.3  christos     at[1] = 0;
   3100   1.1.1.3  christos 
   3101   1.1.1.3  christos   key.key = lname;
   3102   1.1.1.3  christos   kv = bsearch (&key, udef_table, undef_count, sizeof (struct key_value),
   3103   1.1.1.3  christos 		undef_sort_cmp);
   3104   1.1.1.3  christos 
   3105   1.1.1.3  christos   if (kv)
   3106   1.1.1.3  christos     {
   3107   1.1.1.9  christos       h = bfd_link_hash_lookup (linfo->hash, kv->oname, false, false, false);
   3108   1.1.1.3  christos       if (h->type == bfd_link_hash_undefined)
   3109   1.1.1.6  christos 	goto return_h;
   3110   1.1.1.3  christos     }
   3111   1.1.1.5  christos 
   3112   1.1.1.3  christos   if (lname[0] == '?')
   3113   1.1.1.5  christos     goto return_NULL;
   3114   1.1.1.5  christos 
   3115   1.1.1.3  christos   if (at || lname[0] == '@')
   3116   1.1.1.3  christos     {
   3117   1.1.1.3  christos       if (lname[0] == '@')
   3118   1.1.1.6  christos 	{
   3119   1.1.1.3  christos 	  if (pe_details->underscored)
   3120   1.1.1.3  christos 	    lname[0] = '_';
   3121   1.1.1.3  christos 	  else
   3122   1.1.1.9  christos 	    /* Use memmove rather than strcpy as that
   3123   1.1.1.9  christos 	       can handle overlapping buffers.  */
   3124   1.1.1.9  christos 	    memmove (lname, lname + 1, strlen (lname));
   3125   1.1.1.3  christos 	  key.key = lname;
   3126   1.1.1.3  christos 	  kv = bsearch (&key, udef_table, undef_count,
   3127   1.1.1.3  christos 			sizeof (struct key_value), undef_sort_cmp);
   3128   1.1.1.3  christos 	  if (kv)
   3129   1.1.1.3  christos 	    {
   3130   1.1.1.9  christos 	      h = bfd_link_hash_lookup (linfo->hash, kv->oname, false, false, false);
   3131   1.1.1.3  christos 	      if (h->type == bfd_link_hash_undefined)
   3132   1.1.1.5  christos 		goto return_h;
   3133   1.1.1.3  christos 	    }
   3134   1.1.1.3  christos 	}
   3135   1.1.1.3  christos       if (at)
   3136   1.1.1.6  christos 	*strchr (lname, '@') = 0;
   3137   1.1.1.3  christos       key.key = lname;
   3138   1.1.1.3  christos       kv = bsearch (&key, udef_table, undef_count,
   3139   1.1.1.3  christos 		    sizeof (struct key_value), undef_sort_cmp);
   3140   1.1.1.3  christos       if (kv)
   3141   1.1.1.3  christos 	{
   3142   1.1.1.9  christos 	  h = bfd_link_hash_lookup (linfo->hash, kv->oname, false, false, false);
   3143   1.1.1.3  christos 	  if (h->type == bfd_link_hash_undefined)
   3144   1.1.1.5  christos 	    goto return_h;
   3145   1.1.1.3  christos 	}
   3146   1.1.1.5  christos       goto return_NULL;
   3147   1.1.1.3  christos     }
   3148   1.1.1.3  christos 
   3149   1.1.1.3  christos   strcat (lname, "@");
   3150   1.1.1.3  christos   key.key = lname;
   3151   1.1.1.3  christos   kv = bsearch (&key, udef_table, undef_count,
   3152   1.1.1.3  christos 		sizeof (struct key_value), undef_sort_cmp);
   3153   1.1.1.3  christos 
   3154   1.1.1.3  christos   if (kv)
   3155   1.1.1.3  christos     {
   3156   1.1.1.9  christos       h = bfd_link_hash_lookup (linfo->hash, kv->oname, false, false, false);
   3157   1.1.1.3  christos       if (h->type == bfd_link_hash_undefined)
   3158   1.1.1.5  christos 	goto return_h;
   3159   1.1.1.3  christos     }
   3160   1.1.1.3  christos 
   3161   1.1.1.3  christos   if (lname[0] == '_' && pe_details->underscored)
   3162   1.1.1.3  christos     lname[0] = '@';
   3163   1.1.1.3  christos   else
   3164   1.1.1.3  christos     {
   3165   1.1.1.3  christos       memmove (lname + 1, lname, strlen (lname) + 1);
   3166   1.1.1.3  christos       lname[0] = '@';
   3167   1.1.1.3  christos     }
   3168   1.1.1.3  christos   key.key = lname;
   3169   1.1.1.3  christos 
   3170   1.1.1.3  christos   kv = bsearch (&key, udef_table, undef_count,
   3171   1.1.1.3  christos 		sizeof (struct key_value), undef_sort_cmp);
   3172   1.1.1.3  christos 
   3173   1.1.1.3  christos   if (kv)
   3174   1.1.1.3  christos     {
   3175   1.1.1.9  christos       h = bfd_link_hash_lookup (linfo->hash, kv->oname, false, false, false);
   3176   1.1.1.3  christos       if (h->type == bfd_link_hash_undefined)
   3177   1.1.1.6  christos 	goto return_h;
   3178   1.1.1.3  christos     }
   3179   1.1.1.4  christos 
   3180   1.1.1.5  christos  return_NULL:
   3181   1.1.1.5  christos   h = NULL;
   3182   1.1.1.5  christos  return_h:
   3183   1.1.1.5  christos   free (lname);
   3184   1.1.1.5  christos   return h;
   3185   1.1.1.3  christos }
   3186   1.1.1.3  christos 
   3187   1.1.1.9  christos static bool
   3188   1.1.1.3  christos pe_undef_count (struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
   3189   1.1.1.6  christos 		void *inf ATTRIBUTE_UNUSED)
   3190   1.1.1.3  christos {
   3191   1.1.1.3  christos   if (h->type == bfd_link_hash_undefined)
   3192   1.1.1.3  christos     undef_count++;
   3193   1.1.1.9  christos   return true;
   3194   1.1.1.3  christos }
   3195   1.1.1.2  christos 
   3196   1.1.1.9  christos static bool
   3197   1.1.1.3  christos pe_undef_fill (struct bfd_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   3198   1.1.1.2  christos {
   3199   1.1.1.3  christos   if (h->type == bfd_link_hash_undefined)
   3200   1.1.1.2  christos     {
   3201   1.1.1.3  christos       char *at;
   3202   1.1.1.3  christos 
   3203   1.1.1.3  christos       udef_table[undef_count].key = xstrdup (h->root.string);
   3204   1.1.1.3  christos       at = strchr (udef_table[undef_count].key
   3205   1.1.1.3  christos 		   + (udef_table[undef_count].key[0] == '@'), '@');
   3206   1.1.1.3  christos       if (at)
   3207   1.1.1.6  christos 	at[1] = 0;
   3208   1.1.1.3  christos       udef_table[undef_count].oname = h->root.string;
   3209   1.1.1.3  christos       undef_count++;
   3210   1.1.1.2  christos     }
   3211   1.1.1.9  christos   return true;
   3212   1.1.1.2  christos }
   3213   1.1.1.2  christos 
   3214   1.1.1.3  christos static void
   3215   1.1.1.3  christos pe_create_undef_table (void)
   3216   1.1.1.2  christos {
   3217   1.1.1.3  christos   undef_count = 0;
   3218   1.1.1.3  christos 
   3219   1.1.1.3  christos   /* count undefined symbols */
   3220   1.1.1.3  christos 
   3221   1.1.1.3  christos   bfd_link_hash_traverse (link_info.hash, pe_undef_count, "");
   3222   1.1.1.3  christos 
   3223   1.1.1.3  christos   /* create and fill the corresponding table */
   3224   1.1.1.3  christos   udef_table = xmalloc (undef_count * sizeof (struct key_value));
   3225   1.1.1.3  christos 
   3226   1.1.1.3  christos   undef_count = 0;
   3227   1.1.1.3  christos   bfd_link_hash_traverse (link_info.hash, pe_undef_fill, "");
   3228   1.1.1.3  christos 
   3229   1.1.1.3  christos   /* sort items */
   3230   1.1.1.3  christos   qsort (udef_table, undef_count, sizeof (struct key_value), undef_sort_cmp);
   3231   1.1.1.2  christos }
   3232   1.1.1.2  christos 
   3233       1.1     skrll static void
   3234   1.1.1.2  christos add_bfd_to_link (bfd *abfd, const char *name, struct bfd_link_info *linfo)
   3235       1.1     skrll {
   3236       1.1     skrll   lang_input_statement_type *fake_file;
   3237       1.1     skrll 
   3238       1.1     skrll   fake_file = lang_add_input_file (name,
   3239       1.1     skrll 				   lang_input_file_is_fake_enum,
   3240       1.1     skrll 				   NULL);
   3241       1.1     skrll   fake_file->the_bfd = abfd;
   3242       1.1     skrll   ldlang_add_file (fake_file);
   3243       1.1     skrll 
   3244   1.1.1.2  christos   if (!bfd_link_add_symbols (abfd, linfo))
   3245   1.1.1.7  christos     einfo (_("%X%P: add symbols %s: %E\n"), name);
   3246       1.1     skrll }
   3247       1.1     skrll 
   3248       1.1     skrll void
   3249   1.1.1.2  christos pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
   3250       1.1     skrll {
   3251       1.1     skrll   pe_dll_id_target (bfd_get_target (output_bfd));
   3252       1.1     skrll 
   3253   1.1.1.9  christos   if (pe_def_file)
   3254       1.1     skrll     {
   3255   1.1.1.9  christos       int i, j;
   3256   1.1.1.9  christos       def_file_module *module;
   3257   1.1.1.9  christos       def_file_import *imp;
   3258   1.1.1.3  christos 
   3259   1.1.1.9  christos       imp = pe_def_file->imports;
   3260       1.1     skrll 
   3261   1.1.1.9  christos       pe_create_undef_table ();
   3262       1.1     skrll 
   3263   1.1.1.9  christos       for (module = pe_def_file->modules; module; module = module->next)
   3264   1.1.1.9  christos 	{
   3265   1.1.1.9  christos 	  int do_this_dll = 0;
   3266       1.1     skrll 
   3267   1.1.1.9  christos 	  for (i = 0; i < pe_def_file->num_imports; i++)
   3268   1.1.1.9  christos 	    if (imp[i].module == module)
   3269   1.1.1.9  christos 	      break;
   3270   1.1.1.9  christos 	  if (i >= pe_def_file->num_imports)
   3271   1.1.1.9  christos 	    continue;
   3272   1.1.1.3  christos 
   3273   1.1.1.9  christos 	  dll_filename = module->name;
   3274   1.1.1.9  christos 	  dll_symname = xstrdup (module->name);
   3275   1.1.1.9  christos 	  for (j = 0; dll_symname[j]; j++)
   3276   1.1.1.9  christos 	    if (!ISALNUM (dll_symname[j]))
   3277   1.1.1.9  christos 	      dll_symname[j] = '_';
   3278   1.1.1.9  christos 
   3279   1.1.1.9  christos 	  for (; i < pe_def_file->num_imports && imp[i].module == module; i++)
   3280   1.1.1.9  christos 	    {
   3281   1.1.1.9  christos 	      def_file_export exp;
   3282   1.1.1.9  christos 	      struct bfd_link_hash_entry *blhe;
   3283   1.1.1.9  christos 	      int lead_at = (*imp[i].internal_name == '@');
   3284   1.1.1.9  christos 	      /* See if we need this import.  */
   3285   1.1.1.9  christos 	      size_t len = strlen (imp[i].internal_name);
   3286   1.1.1.9  christos 	      char *name = xmalloc (len + 2 + 6);
   3287   1.1.1.9  christos 	      bool include_jmp_stub = false;
   3288   1.1.1.9  christos 	      bool is_cdecl = false;
   3289   1.1.1.9  christos 	      bool is_undef = false;
   3290   1.1.1.9  christos 
   3291   1.1.1.9  christos 	      if (!lead_at && strchr (imp[i].internal_name, '@') == NULL)
   3292   1.1.1.9  christos 		is_cdecl = true;
   3293   1.1.1.3  christos 
   3294   1.1.1.3  christos 	      if (lead_at)
   3295   1.1.1.9  christos 		sprintf (name, "%s", imp[i].internal_name);
   3296   1.1.1.3  christos 	      else
   3297   1.1.1.9  christos 		sprintf (name, "%s%s",U (""), imp[i].internal_name);
   3298   1.1.1.3  christos 
   3299   1.1.1.3  christos 	      blhe = bfd_link_hash_lookup (linfo->hash, name,
   3300   1.1.1.9  christos 					   false, false, false);
   3301       1.1     skrll 
   3302   1.1.1.9  christos 	      /* Include the jump stub for <sym> only if the <sym>
   3303   1.1.1.9  christos 		 is undefined.  */
   3304   1.1.1.9  christos 	      if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
   3305   1.1.1.9  christos 		{
   3306   1.1.1.9  christos 		  if (lead_at)
   3307   1.1.1.9  christos 		    sprintf (name, "%s%s", "__imp_", imp[i].internal_name);
   3308   1.1.1.9  christos 		  else
   3309   1.1.1.9  christos 		    sprintf (name, "%s%s%s", "__imp_", U (""),
   3310   1.1.1.9  christos 			     imp[i].internal_name);
   3311   1.1.1.9  christos 
   3312   1.1.1.9  christos 		  blhe = bfd_link_hash_lookup (linfo->hash, name,
   3313   1.1.1.9  christos 					       false, false, false);
   3314   1.1.1.9  christos 		  if (blhe)
   3315   1.1.1.9  christos 		    is_undef = (blhe->type == bfd_link_hash_undefined);
   3316  1.1.1.10  christos 
   3317  1.1.1.10  christos 		  if (is_cdecl && (!blhe || !is_undef))
   3318  1.1.1.10  christos 		    {
   3319  1.1.1.10  christos 		      blhe = pe_find_cdecl_alias_match (linfo, name + 6);
   3320  1.1.1.10  christos 		      include_jmp_stub = true;
   3321  1.1.1.10  christos 		      if (blhe)
   3322  1.1.1.10  christos 			is_undef = (blhe->type == bfd_link_hash_undefined);
   3323  1.1.1.10  christos 		    }
   3324   1.1.1.9  christos 		}
   3325   1.1.1.9  christos 	      else
   3326   1.1.1.9  christos 		{
   3327   1.1.1.9  christos 		  include_jmp_stub = true;
   3328   1.1.1.9  christos 		  is_undef = (blhe->type == bfd_link_hash_undefined);
   3329   1.1.1.9  christos 		}
   3330   1.1.1.3  christos 
   3331   1.1.1.9  christos 	      free (name);
   3332   1.1.1.9  christos 
   3333   1.1.1.9  christos 	      if (is_undef)
   3334   1.1.1.3  christos 		{
   3335   1.1.1.9  christos 		  bfd *one;
   3336   1.1.1.9  christos 		  /* We do.  */
   3337   1.1.1.9  christos 		  if (!do_this_dll)
   3338   1.1.1.9  christos 		    {
   3339   1.1.1.9  christos 		      bfd *ar_head = make_head (output_bfd);
   3340   1.1.1.9  christos 		      add_bfd_to_link (ar_head, bfd_get_filename (ar_head),
   3341   1.1.1.9  christos 				       linfo);
   3342   1.1.1.9  christos 		      do_this_dll = 1;
   3343   1.1.1.9  christos 		    }
   3344   1.1.1.9  christos 		  exp.internal_name = imp[i].internal_name;
   3345   1.1.1.9  christos 		  exp.name = imp[i].name;
   3346   1.1.1.9  christos 		  exp.its_name = imp[i].its_name;
   3347   1.1.1.9  christos 		  exp.ordinal = imp[i].ordinal;
   3348   1.1.1.9  christos 		  exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
   3349   1.1.1.9  christos 		  exp.flag_private = 0;
   3350   1.1.1.9  christos 		  exp.flag_constant = 0;
   3351   1.1.1.9  christos 		  exp.flag_data = imp[i].data;
   3352   1.1.1.9  christos 		  exp.flag_noname = exp.name ? 0 : 1;
   3353   1.1.1.9  christos 		  one = make_one (&exp, output_bfd,
   3354   1.1.1.9  christos 				  !exp.flag_data && include_jmp_stub);
   3355   1.1.1.9  christos 		  add_bfd_to_link (one, bfd_get_filename (one), linfo);
   3356   1.1.1.3  christos 		}
   3357   1.1.1.3  christos 	    }
   3358   1.1.1.9  christos 	  if (do_this_dll)
   3359   1.1.1.9  christos 	    {
   3360   1.1.1.9  christos 	      bfd *ar_tail = make_tail (output_bfd);
   3361   1.1.1.9  christos 	      add_bfd_to_link (ar_tail, bfd_get_filename (ar_tail), linfo);
   3362   1.1.1.9  christos 	    }
   3363   1.1.1.9  christos 
   3364   1.1.1.9  christos 	  free (dll_symname);
   3365   1.1.1.3  christos 	}
   3366   1.1.1.9  christos 
   3367   1.1.1.9  christos       while (undef_count)
   3368       1.1     skrll 	{
   3369   1.1.1.9  christos 	  --undef_count;
   3370   1.1.1.9  christos 	  free (udef_table[undef_count].key);
   3371       1.1     skrll 	}
   3372   1.1.1.9  christos       free (udef_table);
   3373       1.1     skrll     }
   3374   1.1.1.3  christos 
   3375   1.1.1.9  christos   if (pe_def_file && pe_def_file->name)
   3376   1.1.1.9  christos     dll_filename = pe_def_file->name;
   3377   1.1.1.9  christos   else
   3378   1.1.1.3  christos     {
   3379   1.1.1.9  christos       dll_filename = bfd_get_filename (output_bfd);
   3380   1.1.1.9  christos       for (const char *p = dll_filename; *p; p++)
   3381   1.1.1.9  christos 	if (*p == '\\' || *p == '/' || *p == ':')
   3382   1.1.1.9  christos 	  dll_filename = p + 1;
   3383   1.1.1.3  christos     }
   3384   1.1.1.9  christos   dll_symname = xstrdup (dll_filename);
   3385   1.1.1.9  christos   for (int i = 0; dll_symname[i]; i++)
   3386   1.1.1.9  christos     if (!ISALNUM (dll_symname[i]))
   3387   1.1.1.9  christos       dll_symname[i] = '_';
   3388       1.1     skrll }
   3389       1.1     skrll 
   3390       1.1     skrll /* We were handed a *.DLL file.  Parse it and turn it into a set of
   3391       1.1     skrll    IMPORTS directives in the def file.  Return TRUE if the file was
   3392       1.1     skrll    handled, FALSE if not.  */
   3393       1.1     skrll 
   3394       1.1     skrll static unsigned int
   3395  1.1.1.10  christos pe_get16 (bfd *abfd, int where, bool *fail)
   3396       1.1     skrll {
   3397       1.1     skrll   unsigned char b[2];
   3398       1.1     skrll 
   3399  1.1.1.10  christos   if (bfd_seek (abfd, where, SEEK_SET) != 0
   3400  1.1.1.10  christos       || bfd_read (b, 2, abfd) != 2)
   3401  1.1.1.10  christos     {
   3402  1.1.1.10  christos       *fail = true;
   3403  1.1.1.10  christos       return 0;
   3404  1.1.1.10  christos     }
   3405       1.1     skrll   return b[0] + (b[1] << 8);
   3406       1.1     skrll }
   3407       1.1     skrll 
   3408       1.1     skrll static unsigned int
   3409  1.1.1.10  christos pe_get32 (bfd *abfd, int where, bool *fail)
   3410       1.1     skrll {
   3411       1.1     skrll   unsigned char b[4];
   3412       1.1     skrll 
   3413  1.1.1.10  christos   if (bfd_seek (abfd, where, SEEK_SET) != 0
   3414  1.1.1.10  christos       || bfd_read (b, 4, abfd) != 4)
   3415  1.1.1.10  christos     {
   3416  1.1.1.10  christos       *fail = true;
   3417  1.1.1.10  christos       return 0;
   3418  1.1.1.10  christos     }
   3419   1.1.1.8  christos   return b[0] + (b[1] << 8) + (b[2] << 16) + ((unsigned) b[3] << 24);
   3420       1.1     skrll }
   3421       1.1     skrll 
   3422       1.1     skrll static unsigned int
   3423       1.1     skrll pe_as32 (void *ptr)
   3424       1.1     skrll {
   3425       1.1     skrll   unsigned char *b = ptr;
   3426       1.1     skrll 
   3427   1.1.1.8  christos   return b[0] + (b[1] << 8) + (b[2] << 16) + ((unsigned) b[3] << 24);
   3428       1.1     skrll }
   3429       1.1     skrll 
   3430   1.1.1.9  christos bool
   3431       1.1     skrll pe_implied_import_dll (const char *filename)
   3432       1.1     skrll {
   3433       1.1     skrll   bfd *dll;
   3434       1.1     skrll   bfd_vma pe_header_offset, opthdr_ofs, num_entries, i;
   3435       1.1     skrll   bfd_vma export_rva, export_size, nsections, secptr, expptr;
   3436       1.1     skrll   bfd_vma exp_funcbase;
   3437       1.1     skrll   unsigned char *expdata;
   3438       1.1     skrll   char *erva;
   3439   1.1.1.2  christos   bfd_vma name_rvas, nexp;
   3440   1.1.1.2  christos   const char *dllname;
   3441       1.1     skrll   /* Initialization with start > end guarantees that is_data
   3442       1.1     skrll      will not be set by mistake, and avoids compiler warning.  */
   3443       1.1     skrll   bfd_vma data_start = 1;
   3444       1.1     skrll   bfd_vma data_end = 0;
   3445       1.1     skrll   bfd_vma rdata_start = 1;
   3446       1.1     skrll   bfd_vma rdata_end = 0;
   3447       1.1     skrll   bfd_vma bss_start = 1;
   3448       1.1     skrll   bfd_vma bss_end = 0;
   3449   1.1.1.7  christos   int from;
   3450       1.1     skrll 
   3451       1.1     skrll   /* No, I can't use bfd here.  kernel32.dll puts its export table in
   3452       1.1     skrll      the middle of the .rdata section.  */
   3453       1.1     skrll   dll = bfd_openr (filename, pe_details->target_name);
   3454       1.1     skrll   if (!dll)
   3455       1.1     skrll     {
   3456   1.1.1.7  christos       einfo (_("%X%P: open %s: %E\n"), filename);
   3457   1.1.1.9  christos       return false;
   3458       1.1     skrll     }
   3459       1.1     skrll 
   3460   1.1.1.9  christos   track_dependency_files (filename);
   3461   1.1.1.9  christos 
   3462       1.1     skrll   /* PEI dlls seem to be bfd_objects.  */
   3463       1.1     skrll   if (!bfd_check_format (dll, bfd_object))
   3464       1.1     skrll     {
   3465  1.1.1.10  christos     notdll:
   3466   1.1.1.7  christos       einfo (_("%X%P: %s: this doesn't appear to be a DLL\n"), filename);
   3467   1.1.1.9  christos       return false;
   3468       1.1     skrll     }
   3469       1.1     skrll 
   3470   1.1.1.2  christos   /* Get pe_header, optional header and numbers of directory entries.  */
   3471  1.1.1.10  christos   bool fail = false;
   3472  1.1.1.10  christos   pe_header_offset = pe_get32 (dll, 0x3c, &fail);
   3473  1.1.1.10  christos   if (fail)
   3474  1.1.1.10  christos     goto notdll;
   3475       1.1     skrll   opthdr_ofs = pe_header_offset + 4 + 20;
   3476  1.1.1.10  christos #ifdef pe_use_plus
   3477  1.1.1.10  christos   /* NumberOfRvaAndSizes.  */
   3478  1.1.1.10  christos   num_entries = pe_get32 (dll, opthdr_ofs + 92 + 4 * 4, &fail);
   3479       1.1     skrll #else
   3480  1.1.1.10  christos   num_entries = pe_get32 (dll, opthdr_ofs + 92, &fail);
   3481       1.1     skrll #endif
   3482  1.1.1.10  christos   if (fail)
   3483  1.1.1.10  christos     goto notdll;
   3484       1.1     skrll 
   3485   1.1.1.2  christos   /* No import or export directory entry.  */
   3486   1.1.1.2  christos   if (num_entries < 1)
   3487   1.1.1.9  christos     return false;
   3488       1.1     skrll 
   3489  1.1.1.10  christos #ifdef pe_use_plus
   3490  1.1.1.10  christos   export_rva  = pe_get32 (dll, opthdr_ofs + 96 + 4 * 4, &fail);
   3491  1.1.1.10  christos   export_size = pe_get32 (dll, opthdr_ofs + 100 + 4 * 4, &fail);
   3492       1.1     skrll #else
   3493  1.1.1.10  christos   export_rva = pe_get32 (dll, opthdr_ofs + 96, &fail);
   3494  1.1.1.10  christos   export_size = pe_get32 (dll, opthdr_ofs + 100, &fail);
   3495       1.1     skrll #endif
   3496  1.1.1.10  christos   if (fail)
   3497  1.1.1.10  christos     goto notdll;
   3498   1.1.1.2  christos 
   3499   1.1.1.2  christos   /* No export table - nothing to export.  */
   3500   1.1.1.2  christos   if (export_size == 0)
   3501   1.1.1.9  christos     return false;
   3502   1.1.1.2  christos 
   3503  1.1.1.10  christos   nsections = pe_get16 (dll, pe_header_offset + 4 + 2, &fail);
   3504       1.1     skrll   secptr = (pe_header_offset + 4 + 20 +
   3505  1.1.1.10  christos 	    pe_get16 (dll, pe_header_offset + 4 + 16, &fail));
   3506  1.1.1.10  christos   if (fail)
   3507  1.1.1.10  christos     goto notdll;
   3508       1.1     skrll   expptr = 0;
   3509       1.1     skrll 
   3510       1.1     skrll   /* Get the rva and size of the export section.  */
   3511       1.1     skrll   for (i = 0; i < nsections; i++)
   3512       1.1     skrll     {
   3513       1.1     skrll       char sname[8];
   3514       1.1     skrll       bfd_vma secptr1 = secptr + 40 * i;
   3515  1.1.1.10  christos       bfd_vma vaddr = pe_get32 (dll, secptr1 + 12, &fail);
   3516  1.1.1.10  christos       bfd_vma vsize = pe_get32 (dll, secptr1 + 16, &fail);
   3517  1.1.1.10  christos       bfd_vma fptr = pe_get32 (dll, secptr1 + 20, &fail);
   3518  1.1.1.10  christos 
   3519  1.1.1.10  christos       if (fail
   3520  1.1.1.10  christos 	  || bfd_seek (dll, secptr1, SEEK_SET) != 0
   3521  1.1.1.10  christos 	  || bfd_read (sname, 8, dll) != 8)
   3522  1.1.1.10  christos 	goto notdll;
   3523       1.1     skrll 
   3524       1.1     skrll       if (vaddr <= export_rva && vaddr + vsize > export_rva)
   3525       1.1     skrll 	{
   3526       1.1     skrll 	  expptr = fptr + (export_rva - vaddr);
   3527       1.1     skrll 	  if (export_rva + export_size > vaddr + vsize)
   3528       1.1     skrll 	    export_size = vsize - (export_rva - vaddr);
   3529       1.1     skrll 	  break;
   3530       1.1     skrll 	}
   3531       1.1     skrll     }
   3532       1.1     skrll 
   3533       1.1     skrll   /* Scan sections and store the base and size of the
   3534       1.1     skrll      data and bss segments in data/base_start/end.  */
   3535       1.1     skrll   for (i = 0; i < nsections; i++)
   3536       1.1     skrll     {
   3537       1.1     skrll       bfd_vma secptr1 = secptr + 40 * i;
   3538  1.1.1.10  christos       bfd_vma vsize = pe_get32 (dll, secptr1 + 8, &fail);
   3539  1.1.1.10  christos       bfd_vma vaddr = pe_get32 (dll, secptr1 + 12, &fail);
   3540  1.1.1.10  christos       bfd_vma flags = pe_get32 (dll, secptr1 + 36, &fail);
   3541       1.1     skrll       char sec_name[9];
   3542       1.1     skrll 
   3543       1.1     skrll       sec_name[8] = '\0';
   3544  1.1.1.10  christos       if (fail
   3545  1.1.1.10  christos 	  || bfd_seek (dll, secptr1 + 0, SEEK_SET) != 0
   3546  1.1.1.10  christos 	  || bfd_read (sec_name, 8, dll) != 8)
   3547  1.1.1.10  christos 	goto notdll;
   3548       1.1     skrll 
   3549       1.1     skrll       if (strcmp(sec_name,".data") == 0)
   3550       1.1     skrll 	{
   3551       1.1     skrll 	  data_start = vaddr;
   3552       1.1     skrll 	  data_end = vaddr + vsize;
   3553       1.1     skrll 
   3554       1.1     skrll 	  if (pe_dll_extra_pe_debug)
   3555       1.1     skrll 	    printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
   3556  1.1.1.10  christos 		    __func__, sec_name, (unsigned long) vaddr,
   3557       1.1     skrll 		    (unsigned long) (vaddr + vsize), (unsigned long) flags);
   3558       1.1     skrll 	}
   3559       1.1     skrll       else if (strcmp(sec_name,".rdata") == 0)
   3560       1.1     skrll 	{
   3561       1.1     skrll 	  rdata_start = vaddr;
   3562       1.1     skrll 	  rdata_end = vaddr + vsize;
   3563       1.1     skrll 
   3564       1.1     skrll 	  if (pe_dll_extra_pe_debug)
   3565       1.1     skrll 	    printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
   3566  1.1.1.10  christos 		    __func__, sec_name, (unsigned long) vaddr,
   3567       1.1     skrll 		    (unsigned long) (vaddr + vsize), (unsigned long) flags);
   3568       1.1     skrll 	}
   3569       1.1     skrll       else if (strcmp (sec_name,".bss") == 0)
   3570       1.1     skrll 	{
   3571       1.1     skrll 	  bss_start = vaddr;
   3572       1.1     skrll 	  bss_end = vaddr + vsize;
   3573       1.1     skrll 
   3574       1.1     skrll 	  if (pe_dll_extra_pe_debug)
   3575       1.1     skrll 	    printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
   3576  1.1.1.10  christos 		    __func__, sec_name, (unsigned long) vaddr,
   3577       1.1     skrll 		    (unsigned long) (vaddr + vsize), (unsigned long) flags);
   3578       1.1     skrll 	}
   3579       1.1     skrll     }
   3580       1.1     skrll 
   3581       1.1     skrll   expdata = xmalloc (export_size);
   3582  1.1.1.10  christos   if (bfd_seek (dll, expptr, SEEK_SET) != 0
   3583  1.1.1.10  christos       || bfd_read (expdata, export_size, dll) != export_size)
   3584  1.1.1.10  christos     goto notdll;
   3585       1.1     skrll   erva = (char *) expdata - export_rva;
   3586       1.1     skrll 
   3587       1.1     skrll   if (pe_def_file == 0)
   3588       1.1     skrll     pe_def_file = def_file_empty ();
   3589       1.1     skrll 
   3590       1.1     skrll   nexp = pe_as32 (expdata + 24);
   3591       1.1     skrll   name_rvas = pe_as32 (expdata + 32);
   3592       1.1     skrll   exp_funcbase = pe_as32 (expdata + 28);
   3593       1.1     skrll 
   3594       1.1     skrll   /* Use internal dll name instead of filename
   3595       1.1     skrll      to enable symbolic dll linking.  */
   3596   1.1.1.2  christos   dllname = erva + pe_as32 (expdata + 12);
   3597       1.1     skrll 
   3598       1.1     skrll   /* Check to see if the dll has already been added to
   3599       1.1     skrll      the definition list and if so return without error.
   3600       1.1     skrll      This avoids multiple symbol definitions.  */
   3601   1.1.1.2  christos   if (def_get_module (pe_def_file, dllname))
   3602       1.1     skrll     {
   3603       1.1     skrll       if (pe_dll_extra_pe_debug)
   3604   1.1.1.2  christos 	printf ("%s is already loaded\n", dllname);
   3605   1.1.1.9  christos       return true;
   3606       1.1     skrll     }
   3607       1.1     skrll 
   3608   1.1.1.7  christos   /* This is an optimized version of the insertion loop, which avoids lots of
   3609   1.1.1.7  christos      calls to realloc and memmove from def_file_add_import.  */
   3610   1.1.1.7  christos   if ((from = def_file_add_import_from (pe_def_file, nexp,
   3611   1.1.1.7  christos 					erva + pe_as32 (erva + name_rvas),
   3612   1.1.1.7  christos 					dllname, 0, NULL, NULL)) >= 0)
   3613   1.1.1.7  christos     {
   3614   1.1.1.7  christos       for (i = 0; i < nexp; i++)
   3615   1.1.1.7  christos 	{
   3616   1.1.1.7  christos 	  /* Pointer to the names vector.  */
   3617   1.1.1.7  christos 	  bfd_vma name_rva = pe_as32 (erva + name_rvas + i * 4);
   3618   1.1.1.7  christos 	  def_file_import *imp;
   3619   1.1.1.7  christos 	  /* Pointer to the function address vector.  */
   3620   1.1.1.7  christos 	  bfd_vma func_rva = pe_as32 (erva + exp_funcbase + i * 4);
   3621   1.1.1.7  christos 	  /* is_data is true if the address is in the data, rdata or bss
   3622   1.1.1.7  christos 	     segment.  */
   3623   1.1.1.7  christos 	  const int is_data =
   3624   1.1.1.7  christos 	    (func_rva >= data_start && func_rva < data_end)
   3625   1.1.1.7  christos 	    || (func_rva >= rdata_start && func_rva < rdata_end)
   3626   1.1.1.7  christos 	    || (func_rva >= bss_start && func_rva < bss_end);
   3627   1.1.1.7  christos 
   3628   1.1.1.7  christos 	  imp = def_file_add_import_at (pe_def_file, from + i, erva + name_rva,
   3629   1.1.1.7  christos 					dllname, i, NULL, NULL);
   3630   1.1.1.7  christos 	  /* Mark symbol type.  */
   3631   1.1.1.7  christos 	  imp->data = is_data;
   3632   1.1.1.7  christos 
   3633   1.1.1.7  christos 	  if (pe_dll_extra_pe_debug)
   3634   1.1.1.7  christos 	    printf ("%s dll-name: %s sym: %s addr: 0x%lx %s\n",
   3635  1.1.1.10  christos 		    __func__, dllname, erva + name_rva,
   3636   1.1.1.7  christos 		    (unsigned long) func_rva, is_data ? "(data)" : "");
   3637   1.1.1.7  christos 	}
   3638   1.1.1.7  christos 
   3639   1.1.1.9  christos       return true;
   3640   1.1.1.7  christos     }
   3641   1.1.1.7  christos 
   3642       1.1     skrll   /* Iterate through the list of symbols.  */
   3643       1.1     skrll   for (i = 0; i < nexp; i++)
   3644       1.1     skrll     {
   3645       1.1     skrll       /* Pointer to the names vector.  */
   3646       1.1     skrll       bfd_vma name_rva = pe_as32 (erva + name_rvas + i * 4);
   3647       1.1     skrll       def_file_import *imp;
   3648       1.1     skrll       /* Pointer to the function address vector.  */
   3649       1.1     skrll       bfd_vma func_rva = pe_as32 (erva + exp_funcbase + i * 4);
   3650       1.1     skrll       int is_data = 0;
   3651       1.1     skrll 
   3652       1.1     skrll       /* Skip unwanted symbols, which are
   3653       1.1     skrll 	 exported in buggy auto-import releases.  */
   3654   1.1.1.9  christos       if (! startswith (erva + name_rva, "__nm_"))
   3655   1.1.1.6  christos 	{
   3656  1.1.1.10  christos 	  bool is_dup = false;
   3657   1.1.1.6  christos 	  /* is_data is true if the address is in the data, rdata or bss
   3658       1.1     skrll 	     segment.  */
   3659   1.1.1.6  christos 	  is_data =
   3660       1.1     skrll 	    (func_rva >= data_start && func_rva < data_end)
   3661       1.1     skrll 	    || (func_rva >= rdata_start && func_rva < rdata_end)
   3662       1.1     skrll 	    || (func_rva >= bss_start && func_rva < bss_end);
   3663       1.1     skrll 
   3664       1.1     skrll 	  imp = def_file_add_import (pe_def_file, erva + name_rva,
   3665   1.1.1.3  christos 				     dllname, i, NULL, NULL, &is_dup);
   3666   1.1.1.6  christos 	  /* Mark symbol type.  */
   3667   1.1.1.6  christos 	  if (!is_dup)
   3668   1.1.1.6  christos 	    imp->data = is_data;
   3669       1.1     skrll 
   3670   1.1.1.6  christos 	  if (pe_dll_extra_pe_debug)
   3671       1.1     skrll 	    printf ("%s dll-name: %s sym: %s addr: 0x%lx %s\n",
   3672  1.1.1.10  christos 		    __func__, dllname, erva + name_rva,
   3673       1.1     skrll 		    (unsigned long) func_rva, is_data ? "(data)" : "");
   3674   1.1.1.6  christos 	}
   3675       1.1     skrll     }
   3676       1.1     skrll 
   3677   1.1.1.9  christos   return true;
   3678       1.1     skrll }
   3679       1.1     skrll 
   3680   1.1.1.2  christos void
   3681   1.1.1.2  christos pe_output_file_set_long_section_names (bfd *abfd)
   3682   1.1.1.2  christos {
   3683   1.1.1.2  christos   if (pe_use_coff_long_section_names < 0)
   3684   1.1.1.2  christos     return;
   3685   1.1.1.2  christos   if (!bfd_coff_set_long_section_names (abfd, pe_use_coff_long_section_names))
   3686   1.1.1.7  christos     einfo (_("%X%P: error: can't use long section names on this arch\n"));
   3687   1.1.1.2  christos }
   3688   1.1.1.2  christos 
   3689       1.1     skrll /* These are the main functions, called from the emulation.  The first
   3690       1.1     skrll    is called after the bfds are read, so we can guess at how much space
   3691       1.1     skrll    we need.  The second is called after everything is placed, so we
   3692       1.1     skrll    can put the right values in place.  */
   3693       1.1     skrll 
   3694       1.1     skrll void
   3695       1.1     skrll pe_dll_build_sections (bfd *abfd, struct bfd_link_info *info)
   3696       1.1     skrll {
   3697       1.1     skrll   pe_dll_id_target (bfd_get_target (abfd));
   3698   1.1.1.2  christos   pe_output_file_set_long_section_names (abfd);
   3699   1.1.1.2  christos   process_def_file_and_drectve (abfd, info);
   3700       1.1     skrll 
   3701  1.1.1.10  christos   if (pe_def_file->num_exports == 0
   3702  1.1.1.10  christos       && (!bfd_link_pic (info) || pe_dll_exclude_all_symbols))
   3703   1.1.1.8  christos     {
   3704   1.1.1.8  christos       if (pe_dll_enable_reloc_section)
   3705   1.1.1.8  christos 	{
   3706  1.1.1.10  christos 	  build_filler_bfd (false /* edata not needed.  */);
   3707   1.1.1.8  christos 	  pe_output_file_set_long_section_names (filler_bfd);
   3708   1.1.1.8  christos 	}
   3709   1.1.1.8  christos       return;
   3710   1.1.1.8  christos     }
   3711       1.1     skrll 
   3712   1.1.1.9  christos   generate_edata ();
   3713  1.1.1.10  christos   build_filler_bfd (true /* edata is needed.  */);
   3714   1.1.1.2  christos   pe_output_file_set_long_section_names (filler_bfd);
   3715       1.1     skrll }
   3716       1.1     skrll 
   3717       1.1     skrll void
   3718       1.1     skrll pe_exe_build_sections (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
   3719       1.1     skrll {
   3720       1.1     skrll   pe_dll_id_target (bfd_get_target (abfd));
   3721   1.1.1.2  christos   pe_output_file_set_long_section_names (abfd);
   3722  1.1.1.12  christos   if (pe_dll_enable_reloc_section)
   3723  1.1.1.12  christos     {
   3724  1.1.1.12  christos       build_filler_bfd (false);
   3725  1.1.1.12  christos       pe_output_file_set_long_section_names (filler_bfd);
   3726  1.1.1.12  christos     }
   3727       1.1     skrll }
   3728       1.1     skrll 
   3729       1.1     skrll void
   3730       1.1     skrll pe_dll_fill_sections (bfd *abfd, struct bfd_link_info *info)
   3731       1.1     skrll {
   3732   1.1.1.8  christos   pe_exe_fill_sections (abfd, info);
   3733       1.1     skrll 
   3734   1.1.1.8  christos   if (edata_s)
   3735       1.1     skrll     {
   3736   1.1.1.8  christos       fill_edata (abfd, info);
   3737   1.1.1.8  christos       edata_s->contents = edata_d;
   3738       1.1     skrll     }
   3739       1.1     skrll 
   3740   1.1.1.4  christos   if (bfd_link_dll (info))
   3741       1.1     skrll     pe_data (abfd)->dll = 1;
   3742       1.1     skrll }
   3743       1.1     skrll 
   3744       1.1     skrll void
   3745       1.1     skrll pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
   3746       1.1     skrll {
   3747       1.1     skrll   pe_dll_id_target (bfd_get_target (abfd));
   3748   1.1.1.2  christos   pe_output_file_set_long_section_names (abfd);
   3749       1.1     skrll   image_base = pe_data (abfd)->pe_opthdr.ImageBase;
   3750       1.1     skrll 
   3751       1.1     skrll   generate_reloc (abfd, info);
   3752  1.1.1.10  christos 
   3753       1.1     skrll   if (reloc_sz > 0)
   3754       1.1     skrll     {
   3755   1.1.1.8  christos       bfd_set_section_size (reloc_s, reloc_sz);
   3756       1.1     skrll 
   3757       1.1     skrll       /* Resize the sections.  */
   3758       1.1     skrll       lang_reset_memory_regions ();
   3759   1.1.1.9  christos       lang_size_sections (NULL, true);
   3760       1.1     skrll 
   3761       1.1     skrll       /* Redo special stuff.  */
   3762       1.1     skrll       ldemul_after_allocation ();
   3763       1.1     skrll 
   3764       1.1     skrll       /* Do the assignments again.  */
   3765   1.1.1.2  christos       lang_do_assignments (lang_final_phase_enum);
   3766  1.1.1.10  christos 
   3767  1.1.1.10  christos       reloc_s->contents = reloc_d;
   3768  1.1.1.10  christos     }
   3769  1.1.1.10  christos   else if (reloc_s)
   3770  1.1.1.10  christos     {
   3771  1.1.1.10  christos       /* Do not emit an empty reloc section.  */
   3772  1.1.1.10  christos       bfd_set_section_flags (reloc_s, SEC_IN_MEMORY | SEC_EXCLUDE);
   3773  1.1.1.10  christos       reloc_s->output_section = bfd_abs_section_ptr;
   3774       1.1     skrll     }
   3775       1.1     skrll }
   3776       1.1     skrll 
   3777   1.1.1.9  christos bool
   3778       1.1     skrll pe_bfd_is_dll (bfd *abfd)
   3779       1.1     skrll {
   3780       1.1     skrll   return (bfd_get_format (abfd) == bfd_object
   3781   1.1.1.6  christos 	  && obj_pe (abfd)
   3782   1.1.1.6  christos 	  && pe_data (abfd)->dll);
   3783       1.1     skrll }
   3784