(linenum→info "unix/slp.c:2238")

binutils/2.18/ld/pe-dll.c

    1: /* Routines to help build PEI-format DLLs (Win32 etc)
    2:    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
    3:    Free Software Foundation, Inc.
    4:    Written by DJ Delorie <dj@cygnus.com>
    5: 
    6:    This file is part of the GNU Binutils.
    7: 
    8:    This program is free software; you can redistribute it and/or modify
    9:    it under the terms of the GNU General Public License as published by
   10:    the Free Software Foundation; either version 3 of the License, or
   11:    (at your option) any later version.
   12: 
   13:    This program is distributed in the hope that it will be useful,
   14:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16:    GNU General Public License for more details.
   17: 
   18:    You should have received a copy of the GNU General Public License
   19:    along with this program; if not, write to the Free Software
   20:    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   21:    MA 02110-1301, USA.  */
   22: 
   23: #include "sysdep.h"
   24: #include "bfd.h"
   25: #include "bfdlink.h"
   26: #include "libiberty.h"
   27: #include "safe-ctype.h"
   28: 
   29: #include <time.h>
   30: 
   31: #include "ld.h"
   32: #include "ldexp.h"
   33: #include "ldlang.h"
   34: #include "ldwrite.h"
   35: #include "ldmisc.h"
   36: #include <ldgram.h>
   37: #include "ldmain.h"
   38: #include "ldfile.h"
   39: #include "ldemul.h"
   40: #include "coff/internal.h"
   41: #include "../bfd/libcoff.h"
   42: #include "deffile.h"
   43: #include "pe-dll.h"
   44: 
   45: #ifdef pe_use_x86_64
   46: 
   47: #define PE_IDATA4_SIZE  8
   48: #define PE_IDATA5_SIZE  8
   49: #include "pep-dll.h"
   50: #undef  AOUTSZ
   51: #define AOUTSZ          PEPAOUTSZ
   52: #define PEAOUTHDR       PEPAOUTHDR
   53: 
   54: #else
   55: 
   56: #include "pe-dll.h"
   57: 
   58: #endif
   59: 
   60: #ifndef PE_IDATA4_SIZE
   61: #define PE_IDATA4_SIZE  4
   62: #endif
   63: 
   64: #ifndef PE_IDATA5_SIZE
   65: #define PE_IDATA5_SIZE  4
   66: #endif
   67: 
   68: /*  This file turns a regular Windows PE image into a DLL.  Because of
   69:     the complexity of this operation, it has been broken down into a
   70:     number of separate modules which are all called by the main function
   71:     at the end of this file.  This function is not re-entrant and is
   72:     normally only called once, so static variables are used to reduce
   73:     the number of parameters and return values required.
   74: 
   75:     See also: ld/emultempl/pe.em and ld/emultempl/pep.em.  */
   76: 
   77: /*  Auto-import feature by Paul Sokolovsky
   78: 
   79:     Quick facts:
   80: 
   81:     1. With this feature on, DLL clients can import variables from DLL
   82:     without any concern from their side (for example, without any source
   83:     code modifications).
   84: 
   85:     2. This is done completely in bounds of the PE specification (to be fair,
   86:     there's a place where it pokes nose out of, but in practice it works).
   87:     So, resulting module can be used with any other PE compiler/linker.
   88: 
   89:     3. Auto-import is fully compatible with standard import method and they
   90:     can be mixed together.
   91: 
   92:     4. Overheads: space: 8 bytes per imported symbol, plus 20 for each
   93:     reference to it; load time: negligible; virtual/physical memory: should be
   94:     less than effect of DLL relocation, and I sincerely hope it doesn't affect
   95:     DLL sharability (too much).
   96: 
   97:     Idea
   98: 
   99:     The obvious and only way to get rid of dllimport insanity is to make client
  100:     access variable directly in the DLL, bypassing extra dereference. I.e.,
  101:     whenever client contains something like
  102: 
  103:     mov dll_var,%eax,
  104: 
  105:     address of dll_var in the command should be relocated to point into loaded
  106:     DLL. The aim is to make OS loader do so, and than make ld help with that.
  107:     Import section of PE made following way: there's a vector of structures
  108:     each describing imports from particular DLL. Each such structure points
  109:     to two other parallel vectors: one holding imported names, and one which
  110:     will hold address of corresponding imported name. So, the solution is
  111:     de-vectorize these structures, making import locations be sparse and
  112:     pointing directly into code. Before continuing, it is worth a note that,
  113:     while authors strives to make PE act ELF-like, there're some other people
  114:     make ELF act PE-like: elfvector, ;-) .
  115: 
  116:     Implementation
  117: 
  118:     For each reference of data symbol to be imported from DLL (to set of which
  119:     belong symbols with name <sym>, if __imp_<sym> is found in implib), the
  120:     import fixup entry is generated. That entry is of type
  121:     IMAGE_IMPORT_DESCRIPTOR and stored in .idata$2 subsection. Each
  122:     fixup entry contains pointer to symbol's address within .text section
  123:     (marked with __fuN_<sym> symbol, where N is integer), pointer to DLL name
  124:     (so, DLL name is referenced by multiple entries), and pointer to symbol
  125:     name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>)
  126:     pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly
  127:     containing imported name. Here comes that "on the edge" problem mentioned
  128:     above: PE specification rambles that name vector (OriginalFirstThunk)
  129:     should run in parallel with addresses vector (FirstThunk), i.e. that they
  130:     should have same number of elements and terminated with zero. We violate
  131:     this, since FirstThunk points directly into machine code. But in practice,
  132:     OS loader implemented the sane way: it goes thru OriginalFirstThunk and
  133:     puts addresses to FirstThunk, not something else. It once again should be
  134:     noted that dll and symbol name structures are reused across fixup entries
  135:     and should be there anyway to support standard import stuff, so sustained
  136:     overhead is 20 bytes per reference. Other question is whether having several
  137:     IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, it is
  138:     done even by native compiler/linker (libth32's functions are in fact reside
  139:     in windows9x kernel32.dll, so if you use it, you have two
  140:     IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether
  141:     referencing the same PE structures several times is valid. The answer is why
  142:     not, prohibiting that (detecting violation) would require more work on
  143:     behalf of loader than not doing it.
  144: 
  145:     See also: ld/emultempl/pe.em and ld/emultempl/pep.em.  */
  146: 
  147: static void add_bfd_to_link (bfd *, const char *, struct bfd_link_info *);
  148: 
  149: /* For emultempl/pe.em.  */
  150: 
  151: def_file * pe_def_file = 0;
  152: int pe_dll_export_everything = 0;
  153: int pe_dll_do_default_excludes = 1;
  154: int pe_dll_kill_ats = 0;
  155: int pe_dll_stdcall_aliases = 0;
  156: int pe_dll_warn_dup_exports = 0;
  157: int pe_dll_compat_implib = 0;
  158: int pe_dll_extra_pe_debug = 0;
  159: 
  160: /* Static variables and types.  */
  161: 
  162: static bfd_vma image_base;
  163: static bfd *filler_bfd;
  164: static struct bfd_section *edata_s, *reloc_s;
  165: static unsigned char *edata_d, *reloc_d;
  166: static size_t edata_sz, reloc_sz;
  167: static int runtime_pseudo_relocs_created = 0;
  168: 
  169: typedef struct
  170: {
  171:   const char *name;
  172:   int len;
  173: }
  174: autofilter_entry_type;
  175: 
  176: typedef struct
  177: {
  178:   const char *target_name;
  179:   const char *object_target;
  180:   unsigned int imagebase_reloc;
  181:   int pe_arch;
  182:   int bfd_arch;
  183:   bfd_boolean underscored;
  184:   const autofilter_entry_type* autofilter_symbollist; 
  185: }
  186: pe_details_type;
  187: 
  188: static const autofilter_entry_type autofilter_symbollist_generic[] =
  189: {
  190:   { STRING_COMMA_LEN (".text") },
  191:   /* Entry point symbols.  */
  192:   { STRING_COMMA_LEN ("DllMain") },
  193:   { STRING_COMMA_LEN ("DllMainCRTStartup") },
  194:   { STRING_COMMA_LEN ("_DllMainCRTStartup") },
  195:   /* Runtime pseudo-reloc.  */
  196:   { STRING_COMMA_LEN ("_pei386_runtime_relocator") },
  197:   { STRING_COMMA_LEN ("do_pseudo_reloc") },
  198:   { STRING_COMMA_LEN (NULL) }
  199: };
  200: 
  201: static const autofilter_entry_type autofilter_symbollist_i386[] =
  202: {
  203:   { STRING_COMMA_LEN (".text") },
  204:   /* Entry point symbols, and entry hooks.  */
  205:   { STRING_COMMA_LEN ("cygwin_crt0") },
  206: #ifdef pe_use_x86_64
  207:   { STRING_COMMA_LEN ("DllMain") },
  208:   { STRING_COMMA_LEN ("DllEntryPoint") },
  209:   { STRING_COMMA_LEN ("DllMainCRTStartup") },
  210:   { STRING_COMMA_LEN ("_cygwin_dll_entry") },
  211:   { STRING_COMMA_LEN ("_cygwin_crt0_common") },
  212:   { STRING_COMMA_LEN ("_cygwin_noncygwin_dll_entry") },
  213: #else
  214:   { STRING_COMMA_LEN ("DllMain@12") },
  215:   { STRING_COMMA_LEN ("DllEntryPoint@0") },
  216:   { STRING_COMMA_LEN ("DllMainCRTStartup@12") },
  217:   { STRING_COMMA_LEN ("_cygwin_dll_entry@12") },
  218:   { STRING_COMMA_LEN ("_cygwin_crt0_common@8") },
  219:   { STRING_COMMA_LEN ("_cygwin_noncygwin_dll_entry@12") },
  220:   { STRING_COMMA_LEN ("cygwin_attach_dll") },
  221: #endif  
  222:   { STRING_COMMA_LEN ("cygwin_premain0") },
  223:   { STRING_COMMA_LEN ("cygwin_premain1") },
  224:   { STRING_COMMA_LEN ("cygwin_premain2") },
  225:   { STRING_COMMA_LEN ("cygwin_premain3") },
  226:   /* Runtime pseudo-reloc.  */
  227:   { STRING_COMMA_LEN ("_pei386_runtime_relocator") },
  228:   { STRING_COMMA_LEN ("do_pseudo_reloc") },
  229:   /* Global vars that should not be exported.  */
  230:   { STRING_COMMA_LEN ("impure_ptr") },
  231:   { STRING_COMMA_LEN ("_impure_ptr") },
  232:   { STRING_COMMA_LEN ("_fmode") },
  233:   { STRING_COMMA_LEN ("environ") },
  234:   { STRING_COMMA_LEN (NULL) }
  235: };
  236: 
  237: #define PE_ARCH_i386     1
  238: #define PE_ARCH_sh       2
  239: #define PE_ARCH_mips     3
  240: #define PE_ARCH_arm      4
  241: #define PE_ARCH_arm_epoc 5
  242: #define PE_ARCH_arm_wince 6
  243: 
  244: static const pe_details_type pe_detail_list[] =
  245: {
  246:   {
  247: #ifdef pe_use_x86_64
  248:     "pei-x86-64",
  249:     "pe-x86-64",
  250:     3 /* R_IMAGEBASE */,
  251: #else
  252:     "pei-i386",
  253:     "pe-i386",
  254:     7 /* R_IMAGEBASE */,
  255: #endif
  256:     PE_ARCH_i386,
  257:     bfd_arch_i386,
  258:     TRUE,
  259:     autofilter_symbollist_i386
  260:   },
  261:   {
  262:     "pei-shl",
  263:     "pe-shl",
  264:     16 /* R_SH_IMAGEBASE */,
  265:     PE_ARCH_sh,
  266:     bfd_arch_sh,
  267:     TRUE,
  268:     autofilter_symbollist_generic
  269:   },
  270:   {
  271:     "pei-mips",
  272:     "pe-mips",
  273:     34 /* MIPS_R_RVA */,
  274:     PE_ARCH_mips,
  275:     bfd_arch_mips,
  276:     FALSE,
  277:     autofilter_symbollist_generic
  278:   },
  279:   {
  280:     "pei-arm-little",
  281:     "pe-arm-little",
  282:     11 /* ARM_RVA32 */,
  283:     PE_ARCH_arm,
  284:     bfd_arch_arm,
  285:     TRUE,
  286:     autofilter_symbollist_generic
  287:   },
  288:   {
  289:     "epoc-pei-arm-little",
  290:     "epoc-pe-arm-little",
  291:     11 /* ARM_RVA32 */,
  292:     PE_ARCH_arm_epoc,
  293:     bfd_arch_arm,
  294:     FALSE,
  295:     autofilter_symbollist_generic
  296:   },
  297:   {
  298:     "pei-arm-wince-little",
  299:     "pe-arm-wince-little",
  300:     2,  /* ARM_RVA32 on Windows CE, see bfd/coff-arm.c.  */
  301:     PE_ARCH_arm_wince,
  302:     bfd_arch_arm,
  303:     FALSE,
  304:     autofilter_symbollist_generic
  305:   },
  306:   { NULL, NULL, 0, 0, 0, FALSE, NULL }
  307: };
  308: 
  309: static const pe_details_type *pe_details;
  310: 
  311: /* Do not specify library suffix explicitly, to allow for dllized versions.  */
  312: static const autofilter_entry_type autofilter_liblist[] =
  313: {
  314:   { STRING_COMMA_LEN ("libcegcc") },
  315:   { STRING_COMMA_LEN ("libcygwin") },
  316:   { STRING_COMMA_LEN ("libgcc") },
  317:   { STRING_COMMA_LEN ("libstdc++") },
  318:   { STRING_COMMA_LEN ("libmingw32") },
  319:   { STRING_COMMA_LEN ("libmingwex") },
  320:   { STRING_COMMA_LEN ("libg2c") },
  321:   { STRING_COMMA_LEN ("libsupc++") },
  322:   { STRING_COMMA_LEN ("libobjc") },
  323:   { STRING_COMMA_LEN ("libgcj") },
  324:   { STRING_COMMA_LEN (NULL) }
  325: };
  326: 
  327: static const autofilter_entry_type autofilter_objlist[] =
  328: {
  329:   { STRING_COMMA_LEN ("crt0.o") },
  330:   { STRING_COMMA_LEN ("crt1.o") },
  331:   { STRING_COMMA_LEN ("crt2.o") },
  332:   { STRING_COMMA_LEN ("dllcrt1.o") },
  333:   { STRING_COMMA_LEN ("dllcrt2.o") },
  334:   { STRING_COMMA_LEN ("gcrt0.o") },
  335:   { STRING_COMMA_LEN ("gcrt1.o") },
  336:   { STRING_COMMA_LEN ("gcrt2.o") },
  337:   { STRING_COMMA_LEN ("crtbegin.o") },
  338:   { STRING_COMMA_LEN ("crtend.o") },
  339:   { STRING_COMMA_LEN (NULL) }
  340: };
  341: 
  342: static const autofilter_entry_type autofilter_symbolprefixlist[] =
  343: {
  344:   /* _imp_ is treated specially, as it is always underscored.  */
  345:   /* { STRING_COMMA_LEN ("_imp_") },  */
  346:   /* Don't export some c++ symbols.  */
  347:   { STRING_COMMA_LEN ("__rtti_") },
  348:   { STRING_COMMA_LEN ("__builtin_") },
  349:   /* Don't re-export auto-imported symbols.  */
  350:   { STRING_COMMA_LEN ("_nm_") },
  351:   /* Don't export symbols specifying internal DLL layout.  */
  352:   { STRING_COMMA_LEN ("_head_") },
  353:   { STRING_COMMA_LEN (NULL) }
  354: };
  355: 
  356: static const autofilter_entry_type autofilter_symbolsuffixlist[] =
  357: {
  358:   { STRING_COMMA_LEN ("_iname") },
  359:   { STRING_COMMA_LEN (NULL) }
  360: };
  361: 
  362: #define U(str) (pe_details->underscored ? "_" str : str)
  363: 
  364: void
  365: pe_dll_id_target (const char *target)
  366: {
  367:   int i;
  368: 
  369:   for (i = 0; pe_detail_list[i].target_name; i++)
  370:     if (strcmp (pe_detail_list[i].target_name, target) == 0
  371:         || strcmp (pe_detail_list[i].object_target, target) == 0)
  372:       {
  373:         pe_details = pe_detail_list + i;
  374:         return;
  375:       }
  376:   einfo (_("%XUnsupported PEI architecture: %s\n"), target);
  377:   exit (1);
  378: }
  379: 
  380: /* Helper functions for qsort.  Relocs must be sorted so that we can write
  381:    them out by pages.  */
  382: 
  383: typedef struct
  384:   {
  385:     bfd_vma vma;
  386:     char type;
  387:     short extra;
  388:   }
  389: reloc_data_type;
  390: 
  391: static int
  392: reloc_sort (const void *va, const void *vb)
  393: {
  394:   bfd_vma a = ((const reloc_data_type *) va)->vma;
  395:   bfd_vma b = ((const reloc_data_type *) vb)->vma;
  396: 
  397:   return (a > b) ? 1 : ((a < b) ? -1 : 0);
  398: }
  399: 
  400: static int
  401: pe_export_sort (const void *va, const void *vb)
  402: {
  403:   const def_file_export *a = va;
  404:   const def_file_export *b = vb;
  405: 
  406:   return strcmp (a->name, b->name);
  407: }
  408: 
  409: /* Read and process the .DEF file.  */
  410: 
  411: /* These correspond to the entries in pe_def_file->exports[].  I use
  412:    exported_symbol_sections[i] to tag whether or not the symbol was
  413:    defined, since we can't export symbols we don't have.  */
  414: 
  415: static bfd_vma *exported_symbol_offsets;
  416: static struct bfd_section **exported_symbol_sections;
  417: static int export_table_size;
  418: static int count_exported;
  419: static int count_exported_byname;
  420: static int count_with_ordinals;
  421: static const char *dll_name;
  422: static int min_ordinal, max_ordinal;
  423: static int *exported_symbols;
  424: 
  425: typedef struct exclude_list_struct
  426:   {
  427:     char *string;
  428:     struct exclude_list_struct *next;
  429:     int type;
  430:   }
  431: exclude_list_struct;
  432: 
  433: static struct exclude_list_struct *excludes = 0;
  434: 
  435: void
  436: pe_dll_add_excludes (const char *new_excludes, const int type)
  437: {
  438:   char *local_copy;
  439:   char *exclude_string;
  440: 
  441:   local_copy = xstrdup (new_excludes);
  442: 
  443:   exclude_string = strtok (local_copy, ",:");
  444:   for (; exclude_string; exclude_string = strtok (NULL, ",:"))
  445:     {
  446:       struct exclude_list_struct *new_exclude;
  447: 
  448:       new_exclude = xmalloc (sizeof (struct exclude_list_struct));
  449:       new_exclude->string = xmalloc (strlen (exclude_string) + 1);
  450:       strcpy (new_exclude->string, exclude_string);
  451:       new_exclude->type = type;
  452:       new_exclude->next = excludes;
  453:       excludes = new_exclude;
  454:     }
  455: 
  456:   free (local_copy);
  457: }
  458: 
  459: static bfd_boolean
  460: is_import (const char* n)
  461: {
  462:   return (CONST_STRNEQ (n, "__imp_"));
  463: }
  464: 
  465: /* abfd is a bfd containing n (or NULL)
  466:    It can be used for contextual checks.  */
  467: 
  468: static int
  469: auto_export (bfd *abfd, def_file *d, const char *n)
  470: {
  471:   int i;
  472:   struct exclude_list_struct *ex;
  473:   const autofilter_entry_type *afptr;
  474:   const char * libname = 0;
  475:   if (abfd && abfd->my_archive)
  476:     libname = lbasename (abfd->my_archive->filename);
  477: 
  478:   for (i = 0; i < d->num_exports; i++)
  479:     if (strcmp (d->exports[i].name, n) == 0)
  480:       return 0;
  481: 
  482:   if (pe_dll_do_default_excludes)
  483:     {
  484:       const char * p;
  485:       int    len;
  486: 
  487:       if (pe_dll_extra_pe_debug)
  488:         printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n",
  489:                 n, abfd, abfd->my_archive);
  490: 
  491:       /* First of all, make context checks:
  492:          Don't export anything from standard libs.  */
  493:       if (libname)
  494:         {
  495:           afptr = autofilter_liblist;
  496: 
  497:           while (afptr->name)
  498:             {
  499:               if (strncmp (libname, afptr->name, afptr->len) == 0 )
  500:                 return 0;
  501:               afptr++;
  502:             }
  503:         }
  504: 
  505:       /* Next, exclude symbols from certain startup objects.  */
  506: 
  507:       if (abfd && (p = lbasename (abfd->filename)))
  508:         {
  509:           afptr = autofilter_objlist;
  510:           while (afptr->name)
  511:             {
  512:               if (strcmp (p, afptr->name) == 0)
  513:                 return 0;
  514:               afptr++;
  515:             }
  516:         }
  517: 
  518:       /* Don't try to blindly exclude all symbols
  519:          that begin with '__'; this was tried and
  520:          it is too restrictive.  Instead we have
  521:          a target specific list to use:  */
  522:       afptr = pe_details->autofilter_symbollist; 
  523: 
  524:       while (afptr->name)
  525:         {
  526:           if (strcmp (n, afptr->name) == 0)
  527:             return 0;
  528: 
  529:           afptr++;
  530:         }
  531: 
  532:       /* Next, exclude symbols starting with ...  */
  533:       afptr = autofilter_symbolprefixlist;
  534:       while (afptr->name)
  535:         {
  536:           if (strncmp (n, afptr->name, afptr->len) == 0)
  537:             return 0;
  538: 
  539:           afptr++;
  540:         }
  541: 
  542:       /* Finally, exclude symbols ending with ...  */
  543:       len = strlen (n);
  544:       afptr = autofilter_symbolsuffixlist;
  545:       while (afptr->name)
  546:         {
  547:           if ((len >= afptr->len)
  548:               /* Add 1 to insure match with trailing '\0'.  */
  549:               && strncmp (n + len - afptr->len, afptr->name,
  550:                           afptr->len + 1) == 0)
  551:             return 0;
  552: 
  553:           afptr++;
  554:         }
  555:     }
  556: 
  557:   for (ex = excludes; ex; ex = ex->next)
  558:     {
  559:       if (ex->type == 1) /* exclude-libs */
  560:         {
  561:           if (libname
  562:               && ((strcmp (libname, ex->string) == 0)
  563:                    || (strcasecmp ("ALL", ex->string) == 0)))
  564:             return 0;
  565:         }
  566:       else if (strcmp (n, ex->string) == 0)
  567:         return 0;
  568:     }
  569: 
  570:   return 1;
  571: }
  572: 
  573: static void
  574: process_def_file (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
  575: {
  576:   int i, j;
  577:   struct bfd_link_hash_entry *blhe;
  578:   bfd *b;
  579:   struct bfd_section *s;
  580:   def_file_export *e = 0;
  581: 
  582:   if (!pe_def_file)
  583:     pe_def_file = def_file_empty ();
  584: 
  585:   /* First, run around to all the objects looking for the .drectve
  586:      sections, and push those into the def file too.  */
  587:   for (b = info->input_bfds; b; b = b->link_next)
  588:     {
  589:       s = bfd_get_section_by_name (b, ".drectve");
  590:       if (s)
  591:         {
  592:           long size = s->size;
  593:           char *buf = xmalloc (size);
  594: 
  595:           bfd_get_section_contents (b, s, buf, 0, size);
  596:           def_file_add_directive (pe_def_file, buf, size);
  597:           free (buf);
  598:         }
  599:     }
  600: 
  601:   /* If we are not building a DLL, when there are no exports
  602:      we do not build an export table at all.  */
  603:   if (!pe_dll_export_everything && pe_def_file->num_exports == 0
  604:       && info->executable)
  605:     return;
  606: 
  607:   /* Now, maybe export everything else the default way.  */
  608:   if (pe_dll_export_everything || pe_def_file->num_exports == 0)
  609:     {
  610:       for (b = info->input_bfds; b; b = b->