dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnet/ilalink link_create.c,1.19,1.20 link_im


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnet/ilalink link_create.c,1.19,1.20 link_image.c,1.9,1.10 link_library.c,1.16,1.17 link_main.c,1.17,1.18 linker.h,1.25,1.26
Date: Mon, 16 Jun 2003 01:15:42 -0400

Update of /cvsroot/dotgnu-pnet/pnet/ilalink
In directory subversions:/tmp/cvs-serv6543/ilalink

Modified Files:
        link_create.c link_image.c link_library.c link_main.c linker.h 
Log Message:


Add public key and public key token support to the linker.


Index: link_create.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/ilalink/link_create.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -r1.19 -r1.20
*** link_create.c       13 Feb 2003 23:19:12 -0000      1.19
--- link_create.c       16 Jun 2003 05:15:40 -0000      1.20
***************
*** 319,330 ****
--- 319,387 ----
  }
  
+ /*
+  * Convert a hex string into a byte array.
+  */
+ static unsigned char *HexToBytes(ILLinker *linker, const char *str, int *len)
+ {
+       unsigned char *bytes = 0;
+       unsigned char *newBytes;
+       int value = 0;
+       int temp;
+       int digit = 0;
+       *len = 0;
+       while(*str != '\0')
+       {
+               if(*str >= '0' && *str <= '9')
+               {
+                       temp = (*str - '0');
+               }
+               else if(*str >= 'A' && *str <= 'F')
+               {
+                       temp = (*str - 'A' + 10);
+               }
+               else if(*str >= 'a' && *str <= 'f')
+               {
+                       temp = (*str - 'a' + 10);
+               }
+               else
+               {
+                       ++str;
+                       continue;
+               }
+               if(!digit)
+               {
+                       value = temp;
+                       digit = 1;
+               }
+               else
+               {
+                       value = value * 16 + temp;
+                       digit = 0;
+                       if((newBytes = (unsigned char *)ILRealloc(bytes, *len + 
1)) == 0)
+                       {
+                               ILFree(bytes);
+                               _ILLinkerOutOfMemory(linker);
+                               *len = 0;
+                               return 0;
+                       }
+                       bytes = newBytes;
+                       bytes[(*len)++] = (unsigned char)value;
+               }
+               ++str;
+       }
+       return bytes;
+ }
+ 
  int ILLinkerCreateModuleAndAssembly(ILLinker *linker,
                                                                        const 
char *moduleName,
                                                                        const 
char *assemblyName,
                                                                        
ILUInt16 *assemblyVersion,
+                                                                       const 
char *assemblyKey,
                                                                        int 
hashAlgorithm)
  {
        ILModule *module;
        ILAssembly *assembly;
+       unsigned char *bytes;
+       int lenBytes;
  
        /* Create the module */
***************
*** 345,348 ****
--- 402,431 ----
        ILAssemblySetVersion(assembly, assemblyVersion);
        ILAssemblySetHashAlgorithm(assembly, hashAlgorithm);
+       if(assemblyKey)
+       {
+               /* Recognise special builtin key values */
+               if(!strcmp(assemblyKey, "neutral"))
+               {
+                       assemblyKey = ILLinkerNeutralKey;
+               }
+               else if(!strcmp(assemblyKey, "ms"))
+               {
+                       assemblyKey = ILLinkerMicrosoftKey;
+               }
+ 
+               /* Convert the key from hex into a byte array */
+               bytes = HexToBytes(linker, assemblyKey, &lenBytes);
+               if(bytes)
+               {
+                       if(!ILAssemblySetOriginator
+                                       (assembly, bytes, (unsigned 
long)(long)lenBytes))
+                       {
+                               _ILLinkerOutOfMemory(linker);
+                       }
+                       ILAssemblySetAttrs(assembly, IL_META_ASSEM_PUBLIC_KEY,
+                                                                               
 IL_META_ASSEM_PUBLIC_KEY);
+                       ILFree(bytes);
+               }
+       }
  
        /* Create the "<Module>" type, which holds global functions and 
variables */

Index: link_image.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/ilalink/link_image.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -r1.9 -r1.10
*** link_image.c        25 Aug 2002 05:44:43 -0000      1.9
--- link_image.c        16 Jun 2003 05:15:40 -0000      1.10
***************
*** 26,29 ****
--- 26,44 ----
  
  /*
+  * Builtin public key values, to be used to fool Microsoft runtime
+  * engines into loading our assemblies without complaining about
+  * unimportant linking policy issues.
+  */
+ char const ILLinkerNeutralKey[] = "00000000000000000400000000000000";
+ char const ILLinkerNeutralKeyToken[] = "b77a5c561934e089";
+ char const ILLinkerMicrosoftKey[] =
+       "002400000480000094000000060200000024000052534131000400000100010007d1"
+       "fa57c4aed9f0a32e84aa0faefd0de9e8fd6aec8f87fb03766c834c99921eb23be79a"
+       "d9d5dcc1dd9ad236132102900b723cf980957fc4e177108fc607774f29e8320e92ea"
+       "05ece4e821c0a5efe8f1645c4c0c93c1ab99285d622caa652c1dfad63d745d6f2de5"
+       "f17e5eaf0fc4963d261c8a12436518206dc093344d5ad293";
+ char const ILLinkerMicrosoftKeyToken[] = "b03f5f7f11d50a3a";
+ 
+ /*
   * Process a single image that is to be linked into the final binary.
   */

Index: link_library.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/ilalink/link_library.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -r1.16 -r1.17
*** link_library.c      28 Feb 2003 00:04:20 -0000      1.16
--- link_library.c      16 Jun 2003 05:15:40 -0000      1.17
***************
*** 20,23 ****
--- 20,24 ----
  
  #include "linker.h"
+ #include "il_crypt.h"
  #ifdef HAVE_SYS_STAT_H
  #include <sys/stat.h>
***************
*** 180,183 ****
--- 181,188 ----
                ILFree(library->name);
                ILFree(library->filename);
+               if(library->publicKey)
+               {
+                       ILFree(library->publicKey);
+               }
                ILHashDestroy(library->classHash);
                ILHashDestroy(library->symbolHash);
***************
*** 285,288 ****
--- 290,295 ----
        ILLibrary *nextLibrary;
        ILAssembly *assem;
+       const void *originator;
+       unsigned long originatorLen;
  
        /* Scan the assembly definitions */
***************
*** 300,303 ****
--- 307,312 ----
                        return 0;
                }
+               nextLibrary->publicKey = 0;
+               nextLibrary->publicKeyLen = 0;
                nextLibrary->name = ILDupString(ILAssembly_Name(assem));
                if(!(nextLibrary->name))
***************
*** 327,330 ****
--- 336,350 ----
                else
                {
+                       originator = ILAssemblyGetOriginator(assem, 
&originatorLen);
+                       if(originator && originatorLen)
+                       {
+                               nextLibrary->publicKey =
+                                       (unsigned char *)ILMalloc(originatorLen 
+ 1);
+                               if(nextLibrary->publicKey)
+                               {
+                                       ILMemCpy(nextLibrary->publicKey, 
originator, originatorLen);
+                                       nextLibrary->publicKeyLen = 
(ILUInt32)originatorLen;
+                               }
+                       }
                        nextLibrary->classHash =
                                ILHashCreate(0, 
(ILHashComputeFunc)ClassHash_Compute,
***************
*** 334,337 ****
--- 354,361 ----
                        if(!(nextLibrary->classHash))
                        {
+                               if(nextLibrary->publicKey)
+                               {
+                                       ILFree(nextLibrary->publicKey);
+                               }
                                ILFree(nextLibrary->filename);
                                ILFree(nextLibrary->name);
***************
*** 348,351 ****
--- 372,379 ----
                        {
                                ILHashDestroy(nextLibrary->classHash);
+                               if(nextLibrary->publicKey)
+                               {
+                                       ILFree(nextLibrary->publicKey);
+                               }
                                ILFree(nextLibrary->filename);
                                ILFree(nextLibrary->name);
***************
*** 900,903 ****
--- 928,957 ----
  
  /*
+  * Set the public key token for an assembly reference.
+  */
+ static int SetAssemblyRefToken(ILAssembly *assem, unsigned char *key,
+                                                          ILUInt32 keyLen)
+ {
+       ILSHAContext sha;
+       unsigned char hash[IL_SHA_HASH_SIZE];
+       unsigned char token[8];
+       int posn;
+ 
+       /* Compute the SHA1 hash of the key value */
+       ILSHAInit(&sha);
+       ILSHAData(&sha, key, (unsigned long)keyLen);
+       ILSHAFinalize(&sha, hash);
+ 
+       /* The token is the last 8 bytes of the hash, reversed */
+       for(posn = 0; posn < 8; ++posn)
+       {
+               token[posn] = hash[IL_SHA_HASH_SIZE - posn - 1];
+       }
+ 
+       /* Set the token on the assembly reference record */
+       return ILAssemblySetOriginator(assem, token, 8);
+ }
+ 
+ /*
   * Make a type reference within an image.
   */
***************
*** 956,959 ****
--- 1010,1022 ----
                        }
                        ILAssemblySetVersion(assem, find->library->version);
+                       if(find->library->publicKey)
+                       {
+                               if(!SetAssemblyRefToken(assem, 
find->library->publicKey,
+                                                                       
find->library->publicKeyLen))
+                               {
+                                       _ILLinkerOutOfMemory(find->linker);
+                                       return 0;
+                               }
+                       }
                }
  

Index: link_main.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/ilalink/link_main.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -r1.17 -r1.18
*** link_main.c 28 Feb 2003 00:04:20 -0000      1.17
--- link_main.c 16 Jun 2003 05:15:40 -0000      1.18
***************
*** 89,92 ****
--- 89,95 ----
                "-fmodule-name=name          or -M name",
                "Specify the name of the module to embed in the output."},
+       {"-K", 'K', 1,
+               "-fassembly-key=key          or -K key",
+               "Specify the assembly's public key value."},
        {"-E", 'E', 1, 0, 0},
        {"--entry-point", 'E', 1,
***************
*** 194,197 ****
--- 197,201 ----
        ILUInt16 assemblyVersion[4] = {0, 0, 0, 0};
        char *moduleName = NULL;
+       const char *publicKey = NULL;
        char *entryPoint = NULL;
        int hashAlgorithm = IL_META_HASHALG_SHA1;
***************
*** 351,354 ****
--- 355,364 ----
                        break;
  
+                       case 'K':
+                       {
+                               publicKey = param;
+                       }
+                       break;
+ 
                        case 'E':
                        {
***************
*** 497,500 ****
--- 507,514 ----
                                        }
                                }
+                               else if(!strncmp(param, "assembly-key=", 13))
+                               {
+                                       publicKey = param + 13;
+                               }
                                else if(!strncmp(param, "module-name=", 12))
                                {
***************
*** 718,722 ****
        if(!ILLinkerCreateModuleAndAssembly(linker, moduleName,
                                                                                
assemblyName, assemblyVersion,
!                                                                               
hashAlgorithm))
        {
                ILLinkerDestroy(linker);
--- 732,736 ----
        if(!ILLinkerCreateModuleAndAssembly(linker, moduleName,
                                                                                
assemblyName, assemblyVersion,
!                                                                               
publicKey, hashAlgorithm))
        {
                ILLinkerDestroy(linker);

Index: linker.h
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/ilalink/linker.h,v
retrieving revision 1.25
retrieving revision 1.26
diff -C2 -r1.25 -r1.26
*** linker.h    13 Feb 2003 23:19:12 -0000      1.25
--- linker.h    16 Jun 2003 05:15:40 -0000      1.26
***************
*** 88,91 ****
--- 88,93 ----
        ILUInt16                version[4];             /* Version of the 
library's assembly */
        ILLibrary          *altNames;           /* Alternative names for the 
library */
+       unsigned char  *publicKey;              /* Public key value for the 
library */
+       ILUInt32                publicKeyLen;   /* Length of the public key 
value */
        ILHashTable    *classHash;              /* Hash table for class name 
lookup */
        ILHashTable    *symbolHash;             /* Hash table for global symbol 
lookup */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]