PageRenderTime 68ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/mono/metadata/assembly.c

https://bitbucket.org/danipen/mono
C | 3268 lines | 2365 code | 507 blank | 396 comment | 658 complexity | 6d695da8da7d53c9e558f3341855ae41 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * assembly.c: Routines for loading assemblies.
  3. *
  4. * Author:
  5. * Miguel de Icaza (miguel@ximian.com)
  6. *
  7. * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
  8. * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  9. * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  10. */
  11. #include <config.h>
  12. #include <stdio.h>
  13. #include <glib.h>
  14. #include <errno.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "assembly.h"
  18. #include "image.h"
  19. #include "object-internals.h"
  20. #include <mono/metadata/loader.h>
  21. #include <mono/metadata/tabledefs.h>
  22. #include <mono/metadata/metadata-internals.h>
  23. #include <mono/metadata/profiler-private.h>
  24. #include <mono/metadata/class-internals.h>
  25. #include <mono/metadata/domain-internals.h>
  26. #include <mono/metadata/mono-endian.h>
  27. #include <mono/metadata/mono-debug.h>
  28. #include <mono/io-layer/io-layer.h>
  29. #include <mono/utils/mono-uri.h>
  30. #include <mono/metadata/mono-config.h>
  31. #include <mono/utils/mono-digest.h>
  32. #include <mono/utils/mono-logger-internal.h>
  33. #include <mono/utils/mono-path.h>
  34. #include <mono/metadata/reflection.h>
  35. #include <mono/metadata/coree.h>
  36. #include <mono/utils/mono-io-portability.h>
  37. #ifndef HOST_WIN32
  38. #include <sys/types.h>
  39. #include <unistd.h>
  40. #include <sys/stat.h>
  41. #endif
  42. #ifdef PLATFORM_MACOSX
  43. #include <mach-o/dyld.h>
  44. #endif
  45. /* AssemblyVersionMap: an assembly name and the assembly version set on which it is based */
  46. typedef struct {
  47. const char* assembly_name;
  48. guint8 version_set_index;
  49. } AssemblyVersionMap;
  50. /* the default search path is empty, the first slot is replaced with the computed value */
  51. static const char*
  52. default_path [] = {
  53. NULL,
  54. NULL
  55. };
  56. /* Contains the list of directories to be searched for assemblies (MONO_PATH) */
  57. static char **assemblies_path = NULL;
  58. /* Contains the list of directories that point to auxiliary GACs */
  59. static char **extra_gac_paths = NULL;
  60. #ifndef DISABLE_ASSEMBLY_REMAPPING
  61. /* The list of system assemblies what will be remapped to the running
  62. * runtime version. WARNING: this list must be sorted.
  63. * The integer number is an index in the MonoRuntimeInfo structure, whose
  64. * values can be found in domain.c - supported_runtimes. Look there
  65. * to understand what remapping will be made.
  66. */
  67. static const AssemblyVersionMap framework_assemblies [] = {
  68. {"Accessibility", 0},
  69. {"Commons.Xml.Relaxng", 0},
  70. {"I18N", 0},
  71. {"I18N.CJK", 0},
  72. {"I18N.MidEast", 0},
  73. {"I18N.Other", 0},
  74. {"I18N.Rare", 0},
  75. {"I18N.West", 0},
  76. {"Microsoft.VisualBasic", 1},
  77. {"Microsoft.VisualC", 1},
  78. {"Mono.Cairo", 0},
  79. {"Mono.CompilerServices.SymbolWriter", 0},
  80. {"Mono.Data", 0},
  81. {"Mono.Data.SybaseClient", 0},
  82. {"Mono.Data.Tds", 0},
  83. {"Mono.Data.TdsClient", 0},
  84. {"Mono.GetOptions", 0},
  85. {"Mono.Http", 0},
  86. {"Mono.Posix", 0},
  87. {"Mono.Security", 0},
  88. {"Mono.Security.Win32", 0},
  89. {"Mono.Xml.Ext", 0},
  90. {"Novell.Directory.Ldap", 0},
  91. {"Npgsql", 0},
  92. {"PEAPI", 0},
  93. {"System", 0},
  94. {"System.ComponentModel.DataAnnotations", 2},
  95. {"System.Configuration", 0},
  96. {"System.Configuration.Install", 0},
  97. {"System.Core", 2},
  98. {"System.Data", 0},
  99. {"System.Data.Linq", 2},
  100. {"System.Data.OracleClient", 0},
  101. {"System.Data.SqlXml", 0},
  102. {"System.Design", 0},
  103. {"System.DirectoryServices", 0},
  104. {"System.Drawing", 0},
  105. {"System.Drawing.Design", 0},
  106. {"System.EnterpriseServices", 0},
  107. {"System.Management", 0},
  108. {"System.Messaging", 0},
  109. {"System.Runtime.Remoting", 0},
  110. {"System.Runtime.Serialization", 3},
  111. {"System.Runtime.Serialization.Formatters.Soap", 0},
  112. {"System.Security", 0},
  113. {"System.ServiceProcess", 0},
  114. {"System.Transactions", 0},
  115. {"System.Web", 0},
  116. {"System.Web.Abstractions", 2},
  117. {"System.Web.Mobile", 0},
  118. {"System.Web.Routing", 2},
  119. {"System.Web.Services", 0},
  120. {"System.Windows.Forms", 0},
  121. {"System.Xml", 0},
  122. {"mscorlib", 0}
  123. };
  124. #endif
  125. /*
  126. * keeps track of loaded assemblies
  127. */
  128. static GList *loaded_assemblies = NULL;
  129. static MonoAssembly *corlib;
  130. #if defined(__native_client__)
  131. /* On Native Client, allow mscorlib to be loaded from memory */
  132. /* instead of loaded off disk. If these are not set, default */
  133. /* mscorlib loading will take place */
  134. /* NOTE: If mscorlib data is passed to mono in this way then */
  135. /* it needs to remain allocated during the use of mono. */
  136. static void *corlibData = NULL;
  137. static size_t corlibSize = 0;
  138. void
  139. mono_set_corlib_data (void *data, size_t size)
  140. {
  141. corlibData = data;
  142. corlibSize = size;
  143. }
  144. #endif
  145. /* This protects loaded_assemblies and image->references */
  146. #define mono_assemblies_lock() EnterCriticalSection (&assemblies_mutex)
  147. #define mono_assemblies_unlock() LeaveCriticalSection (&assemblies_mutex)
  148. static CRITICAL_SECTION assemblies_mutex;
  149. /* If defined, points to the bundled assembly information */
  150. const MonoBundledAssembly **bundles;
  151. /* Loaded assembly binding info */
  152. static GSList *loaded_assembly_bindings = NULL;
  153. static MonoAssembly*
  154. mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, gboolean refonly, gboolean postload);
  155. static MonoBoolean
  156. mono_assembly_is_in_gac (const gchar *filanem);
  157. static gchar*
  158. encode_public_tok (const guchar *token, gint32 len)
  159. {
  160. const static gchar allowed [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  161. gchar *res;
  162. int i;
  163. res = g_malloc (len * 2 + 1);
  164. for (i = 0; i < len; i++) {
  165. res [i * 2] = allowed [token [i] >> 4];
  166. res [i * 2 + 1] = allowed [token [i] & 0xF];
  167. }
  168. res [len * 2] = 0;
  169. return res;
  170. }
  171. /**
  172. * mono_public_tokens_are_equal:
  173. * @pubt1: first public key token
  174. * @pubt2: second public key token
  175. *
  176. * Compare two public key tokens and return #TRUE is they are equal and #FALSE
  177. * otherwise.
  178. */
  179. gboolean
  180. mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2)
  181. {
  182. return memcmp (pubt1, pubt2, 16) == 0;
  183. }
  184. /**
  185. * mono_set_assemblies_path:
  186. * @path: list of paths that contain directories where Mono will look for assemblies
  187. *
  188. * Use this method to override the standard assembly lookup system and
  189. * override any assemblies coming from the GAC. This is the method
  190. * that supports the MONO_PATH variable.
  191. *
  192. * Notice that MONO_PATH and this method are really a very bad idea as
  193. * it prevents the GAC from working and it prevents the standard
  194. * resolution mechanisms from working. Nonetheless, for some debugging
  195. * situations and bootstrapping setups, this is useful to have.
  196. */
  197. void
  198. mono_set_assemblies_path (const char* path)
  199. {
  200. char **splitted, **dest;
  201. splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
  202. if (assemblies_path)
  203. g_strfreev (assemblies_path);
  204. assemblies_path = dest = splitted;
  205. while (*splitted){
  206. if (**splitted)
  207. *dest++ = *splitted;
  208. splitted++;
  209. }
  210. *dest = *splitted;
  211. if (g_getenv ("MONO_DEBUG") == NULL)
  212. return;
  213. splitted = assemblies_path;
  214. while (*splitted) {
  215. if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
  216. g_warning ("'%s' in MONO_PATH doesn't exist or has wrong permissions.", *splitted);
  217. splitted++;
  218. }
  219. }
  220. /* Native Client can't get this info from an environment variable so */
  221. /* it's passed in to the runtime, or set manually by embedding code. */
  222. #ifdef __native_client__
  223. char* nacl_mono_path = NULL;
  224. #endif
  225. static void
  226. check_path_env (void)
  227. {
  228. const char* path;
  229. #ifdef __native_client__
  230. path = nacl_mono_path;
  231. #else
  232. path = g_getenv ("MONO_PATH");
  233. #endif
  234. if (!path || assemblies_path != NULL)
  235. return;
  236. mono_set_assemblies_path(path);
  237. }
  238. static void
  239. check_extra_gac_path_env (void) {
  240. const char *path;
  241. char **splitted, **dest;
  242. path = g_getenv ("MONO_GAC_PREFIX");
  243. if (!path)
  244. return;
  245. splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
  246. if (extra_gac_paths)
  247. g_strfreev (extra_gac_paths);
  248. extra_gac_paths = dest = splitted;
  249. while (*splitted){
  250. if (**splitted)
  251. *dest++ = *splitted;
  252. splitted++;
  253. }
  254. *dest = *splitted;
  255. if (g_getenv ("MONO_DEBUG") == NULL)
  256. return;
  257. while (*splitted) {
  258. if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
  259. g_warning ("'%s' in MONO_GAC_PREFIX doesn't exist or has wrong permissions.", *splitted);
  260. splitted++;
  261. }
  262. }
  263. static gboolean
  264. assembly_binding_maps_name (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname)
  265. {
  266. if (!info || !info->name)
  267. return FALSE;
  268. if (strcmp (info->name, aname->name))
  269. return FALSE;
  270. if (info->major != aname->major || info->minor != aname->minor)
  271. return FALSE;
  272. if ((info->culture != NULL && info->culture [0]) != (aname->culture != NULL && aname->culture [0]))
  273. return FALSE;
  274. if (info->culture && aname->culture && strcmp (info->culture, aname->culture))
  275. return FALSE;
  276. if (!mono_public_tokens_are_equal (info->public_key_token, aname->public_key_token))
  277. return FALSE;
  278. return TRUE;
  279. }
  280. static void
  281. mono_assembly_binding_info_free (MonoAssemblyBindingInfo *info)
  282. {
  283. if (!info)
  284. return;
  285. g_free (info->name);
  286. g_free (info->culture);
  287. }
  288. static void
  289. get_publisher_policy_info (MonoImage *image, MonoAssemblyName *aname, MonoAssemblyBindingInfo *binding_info)
  290. {
  291. MonoTableInfo *t;
  292. guint32 cols [MONO_MANIFEST_SIZE];
  293. const gchar *filename;
  294. gchar *subpath, *fullpath;
  295. t = &image->tables [MONO_TABLE_MANIFESTRESOURCE];
  296. /* MS Impl. accepts policy assemblies with more than
  297. * one manifest resource, and only takes the first one */
  298. if (t->rows < 1) {
  299. binding_info->is_valid = FALSE;
  300. return;
  301. }
  302. mono_metadata_decode_row (t, 0, cols, MONO_MANIFEST_SIZE);
  303. if ((cols [MONO_MANIFEST_IMPLEMENTATION] & MONO_IMPLEMENTATION_MASK) != MONO_IMPLEMENTATION_FILE) {
  304. binding_info->is_valid = FALSE;
  305. return;
  306. }
  307. filename = mono_metadata_string_heap (image, cols [MONO_MANIFEST_NAME]);
  308. g_assert (filename != NULL);
  309. subpath = g_path_get_dirname (image->name);
  310. fullpath = g_build_path (G_DIR_SEPARATOR_S, subpath, filename, NULL);
  311. mono_config_parse_publisher_policy (fullpath, binding_info);
  312. g_free (subpath);
  313. g_free (fullpath);
  314. /* Define the optional elements/attributes before checking */
  315. if (!binding_info->culture)
  316. binding_info->culture = g_strdup ("");
  317. /* Check that the most important elements/attributes exist */
  318. if (!binding_info->name || !binding_info->public_key_token [0] || !binding_info->has_old_version_bottom ||
  319. !binding_info->has_new_version || !assembly_binding_maps_name (binding_info, aname)) {
  320. mono_assembly_binding_info_free (binding_info);
  321. binding_info->is_valid = FALSE;
  322. return;
  323. }
  324. binding_info->is_valid = TRUE;
  325. }
  326. static int
  327. compare_versions (AssemblyVersionSet *v, MonoAssemblyName *aname)
  328. {
  329. if (v->major > aname->major)
  330. return 1;
  331. else if (v->major < aname->major)
  332. return -1;
  333. if (v->minor > aname->minor)
  334. return 1;
  335. else if (v->minor < aname->minor)
  336. return -1;
  337. if (v->build > aname->build)
  338. return 1;
  339. else if (v->build < aname->build)
  340. return -1;
  341. if (v->revision > aname->revision)
  342. return 1;
  343. else if (v->revision < aname->revision)
  344. return -1;
  345. return 0;
  346. }
  347. static gboolean
  348. check_policy_versions (MonoAssemblyBindingInfo *info, MonoAssemblyName *name)
  349. {
  350. if (!info->is_valid)
  351. return FALSE;
  352. /* If has_old_version_top doesn't exist, we don't have an interval */
  353. if (!info->has_old_version_top) {
  354. if (compare_versions (&info->old_version_bottom, name) == 0)
  355. return TRUE;
  356. return FALSE;
  357. }
  358. /* Check that the version defined by name is valid for the interval */
  359. if (compare_versions (&info->old_version_top, name) < 0)
  360. return FALSE;
  361. /* We should be greater or equal than the small version */
  362. if (compare_versions (&info->old_version_bottom, name) > 0)
  363. return FALSE;
  364. return TRUE;
  365. }
  366. /**
  367. * mono_assembly_names_equal:
  368. * @l: first assembly
  369. * @r: second assembly.
  370. *
  371. * Compares two MonoAssemblyNames and returns whether they are equal.
  372. *
  373. * This compares the names, the cultures, the release version and their
  374. * public tokens.
  375. *
  376. * Returns: TRUE if both assembly names are equal.
  377. */
  378. gboolean
  379. mono_assembly_names_equal (MonoAssemblyName *l, MonoAssemblyName *r)
  380. {
  381. if (!l->name || !r->name)
  382. return FALSE;
  383. if (strcmp (l->name, r->name))
  384. return FALSE;
  385. if (l->culture && r->culture && strcmp (l->culture, r->culture))
  386. return FALSE;
  387. if (l->major != r->major || l->minor != r->minor ||
  388. l->build != r->build || l->revision != r->revision)
  389. if (! ((l->major == 0 && l->minor == 0 && l->build == 0 && l->revision == 0) || (r->major == 0 && r->minor == 0 && r->build == 0 && r->revision == 0)))
  390. return FALSE;
  391. if (!l->public_key_token [0] || !r->public_key_token [0])
  392. return TRUE;
  393. if (!mono_public_tokens_are_equal (l->public_key_token, r->public_key_token))
  394. return FALSE;
  395. return TRUE;
  396. }
  397. static MonoAssembly *
  398. load_in_path (const char *basename, const char** search_path, MonoImageOpenStatus *status, MonoBoolean refonly)
  399. {
  400. int i;
  401. char *fullpath;
  402. MonoAssembly *result;
  403. for (i = 0; search_path [i]; ++i) {
  404. fullpath = g_build_filename (search_path [i], basename, NULL);
  405. result = mono_assembly_open_full (fullpath, status, refonly);
  406. g_free (fullpath);
  407. if (result)
  408. return result;
  409. }
  410. return NULL;
  411. }
  412. /**
  413. * mono_assembly_setrootdir:
  414. * @root_dir: The pathname of the root directory where we will locate assemblies
  415. *
  416. * This routine sets the internal default root directory for looking up
  417. * assemblies.
  418. *
  419. * This is used by Windows installations to compute dynamically the
  420. * place where the Mono assemblies are located.
  421. *
  422. */
  423. void
  424. mono_assembly_setrootdir (const char *root_dir)
  425. {
  426. /*
  427. * Override the MONO_ASSEMBLIES directory configured at compile time.
  428. */
  429. /* Leak if called more than once */
  430. default_path [0] = g_strdup (root_dir);
  431. }
  432. /**
  433. * mono_assembly_getrootdir:
  434. *
  435. * Obtains the root directory used for looking up assemblies.
  436. *
  437. * Returns: a string with the directory, this string should not be freed.
  438. */
  439. G_CONST_RETURN gchar *
  440. mono_assembly_getrootdir (void)
  441. {
  442. return default_path [0];
  443. }
  444. /**
  445. * mono_set_dirs:
  446. * @assembly_dir: the base directory for assemblies
  447. * @config_dir: the base directory for configuration files
  448. *
  449. * This routine is used internally and by developers embedding
  450. * the runtime into their own applications.
  451. *
  452. * There are a number of cases to consider: Mono as a system-installed
  453. * package that is available on the location preconfigured or Mono in
  454. * a relocated location.
  455. *
  456. * If you are using a system-installed Mono, you can pass NULL
  457. * to both parameters. If you are not, you should compute both
  458. * directory values and call this routine.
  459. *
  460. * The values for a given PREFIX are:
  461. *
  462. * assembly_dir: PREFIX/lib
  463. * config_dir: PREFIX/etc
  464. *
  465. * Notice that embedders that use Mono in a relocated way must
  466. * compute the location at runtime, as they will be in control
  467. * of where Mono is installed.
  468. */
  469. void
  470. mono_set_dirs (const char *assembly_dir, const char *config_dir)
  471. {
  472. #if defined (MONO_ASSEMBLIES)
  473. if (assembly_dir == NULL)
  474. assembly_dir = MONO_ASSEMBLIES;
  475. #endif
  476. #if defined (MONO_CFG_DIR)
  477. if (config_dir == NULL)
  478. config_dir = MONO_CFG_DIR;
  479. #endif
  480. mono_assembly_setrootdir (assembly_dir);
  481. mono_set_config_dir (config_dir);
  482. }
  483. #ifndef HOST_WIN32
  484. static char *
  485. compute_base (char *path)
  486. {
  487. char *p = strrchr (path, '/');
  488. if (p == NULL)
  489. return NULL;
  490. /* Not a well known Mono executable, we are embedded, cant guess the base */
  491. if (strcmp (p, "/mono") && strcmp (p, "/mono-sgen") && strcmp (p, "/pedump") && strcmp (p, "/monodis") && strcmp (p, "/mint") && strcmp (p, "/monodiet"))
  492. return NULL;
  493. *p = 0;
  494. p = strrchr (path, '/');
  495. if (p == NULL)
  496. return NULL;
  497. if (strcmp (p, "/bin") != 0)
  498. return NULL;
  499. *p = 0;
  500. return path;
  501. }
  502. static void
  503. fallback (void)
  504. {
  505. mono_set_dirs (MONO_ASSEMBLIES, MONO_CFG_DIR);
  506. }
  507. static G_GNUC_UNUSED void
  508. set_dirs (char *exe)
  509. {
  510. char *base;
  511. char *config, *lib, *mono;
  512. struct stat buf;
  513. /*
  514. * Only /usr prefix is treated specially
  515. */
  516. if (strncmp (exe, MONO_BINDIR, strlen (MONO_BINDIR)) == 0 || (base = compute_base (exe)) == NULL){
  517. fallback ();
  518. return;
  519. }
  520. config = g_build_filename (base, "etc", NULL);
  521. lib = g_build_filename (base, "lib", NULL);
  522. mono = g_build_filename (lib, "mono/2.0", NULL);
  523. if (stat (mono, &buf) == -1)
  524. fallback ();
  525. else {
  526. mono_set_dirs (lib, config);
  527. }
  528. g_free (config);
  529. g_free (lib);
  530. g_free (mono);
  531. }
  532. #endif /* HOST_WIN32 */
  533. /**
  534. * mono_set_rootdir:
  535. *
  536. * Registers the root directory for the Mono runtime, for Linux and Solaris 10,
  537. * this auto-detects the prefix where Mono was installed.
  538. */
  539. void
  540. mono_set_rootdir (void)
  541. {
  542. #if defined(HOST_WIN32) || (defined(PLATFORM_MACOSX) && !defined(TARGET_ARM))
  543. gchar *bindir, *installdir, *root, *name, *resolvedname, *config;
  544. #ifdef HOST_WIN32
  545. name = mono_get_module_file_name ((HMODULE) &__ImageBase);
  546. #else
  547. {
  548. /*
  549. * _NSGetExecutablePath may return -1 to indicate buf is not large
  550. * enough, but we ignore that case to avoid having to do extra dynamic
  551. * allocation for the path and hope that 4096 is enough - this is
  552. * ok in the Linux/Solaris case below at least...
  553. */
  554. gchar buf[4096];
  555. guint buf_size = sizeof (buf);
  556. if (_NSGetExecutablePath (buf, &buf_size) == 0)
  557. name = g_strdup (buf);
  558. if (name == NULL) {
  559. fallback ();
  560. return;
  561. }
  562. }
  563. #endif
  564. resolvedname = mono_path_resolve_symlinks (name);
  565. bindir = g_path_get_dirname (resolvedname);
  566. installdir = g_path_get_dirname (bindir);
  567. root = g_build_path (G_DIR_SEPARATOR_S, installdir, "lib", NULL);
  568. config = g_build_filename (root, "..", "etc", NULL);
  569. #ifdef HOST_WIN32
  570. mono_set_dirs (root, config);
  571. #else
  572. if (g_file_test (root, G_FILE_TEST_EXISTS) && g_file_test (config, G_FILE_TEST_EXISTS))
  573. mono_set_dirs (root, config);
  574. else
  575. fallback ();
  576. #endif
  577. g_free (config);
  578. g_free (root);
  579. g_free (installdir);
  580. g_free (bindir);
  581. g_free (name);
  582. g_free (resolvedname);
  583. #elif defined(DISABLE_MONO_AUTODETECTION)
  584. fallback ();
  585. #else
  586. char buf [4096];
  587. int s;
  588. char *str;
  589. /* Linux style */
  590. s = readlink ("/proc/self/exe", buf, sizeof (buf)-1);
  591. if (s != -1){
  592. buf [s] = 0;
  593. set_dirs (buf);
  594. return;
  595. }
  596. /* Solaris 10 style */
  597. str = g_strdup_printf ("/proc/%d/path/a.out", getpid ());
  598. s = readlink (str, buf, sizeof (buf)-1);
  599. g_free (str);
  600. if (s != -1){
  601. buf [s] = 0;
  602. set_dirs (buf);
  603. return;
  604. }
  605. fallback ();
  606. #endif
  607. }
  608. /**
  609. * mono_assemblies_init:
  610. *
  611. * Initialize global variables used by this module.
  612. */
  613. void
  614. mono_assemblies_init (void)
  615. {
  616. /*
  617. * Initialize our internal paths if we have not been initialized yet.
  618. * This happens when embedders use Mono.
  619. */
  620. if (mono_assembly_getrootdir () == NULL)
  621. mono_set_rootdir ();
  622. check_path_env ();
  623. check_extra_gac_path_env ();
  624. InitializeCriticalSection (&assemblies_mutex);
  625. }
  626. gboolean
  627. mono_assembly_fill_assembly_name (MonoImage *image, MonoAssemblyName *aname)
  628. {
  629. MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
  630. guint32 cols [MONO_ASSEMBLY_SIZE];
  631. if (!t->rows)
  632. return FALSE;
  633. mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
  634. aname->hash_len = 0;
  635. aname->hash_value = NULL;
  636. aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_NAME]);
  637. aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_CULTURE]);
  638. aname->flags = cols [MONO_ASSEMBLY_FLAGS];
  639. aname->major = cols [MONO_ASSEMBLY_MAJOR_VERSION];
  640. aname->minor = cols [MONO_ASSEMBLY_MINOR_VERSION];
  641. aname->build = cols [MONO_ASSEMBLY_BUILD_NUMBER];
  642. aname->revision = cols [MONO_ASSEMBLY_REV_NUMBER];
  643. aname->hash_alg = cols [MONO_ASSEMBLY_HASH_ALG];
  644. if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
  645. guchar* token = g_malloc (8);
  646. gchar* encoded;
  647. const gchar* pkey;
  648. int len;
  649. pkey = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
  650. len = mono_metadata_decode_blob_size (pkey, &pkey);
  651. aname->public_key = (guchar*)pkey;
  652. mono_digest_get_public_token (token, aname->public_key, len);
  653. encoded = encode_public_tok (token, 8);
  654. g_strlcpy ((char*)aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  655. g_free (encoded);
  656. g_free (token);
  657. }
  658. else {
  659. aname->public_key = NULL;
  660. memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  661. }
  662. if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
  663. aname->public_key = (guchar*)mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
  664. }
  665. else
  666. aname->public_key = 0;
  667. return TRUE;
  668. }
  669. /**
  670. * mono_stringify_assembly_name:
  671. * @aname: the assembly name.
  672. *
  673. * Convert @aname into its string format. The returned string is dynamically
  674. * allocated and should be freed by the caller.
  675. *
  676. * Returns: a newly allocated string with a string representation of
  677. * the assembly name.
  678. */
  679. char*
  680. mono_stringify_assembly_name (MonoAssemblyName *aname)
  681. {
  682. const char *quote = (aname->name && g_ascii_isspace (aname->name [0])) ? "\"" : "";
  683. return g_strdup_printf (
  684. "%s%s%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
  685. quote, aname->name, quote,
  686. aname->major, aname->minor, aname->build, aname->revision,
  687. aname->culture && *aname->culture? aname->culture: "neutral",
  688. aname->public_key_token [0] ? (char *)aname->public_key_token : "null",
  689. (aname->flags & ASSEMBLYREF_RETARGETABLE_FLAG) ? ", Retargetable=Yes" : "");
  690. }
  691. static gchar*
  692. assemblyref_public_tok (MonoImage *image, guint32 key_index, guint32 flags)
  693. {
  694. const gchar *public_tok;
  695. int len;
  696. public_tok = mono_metadata_blob_heap (image, key_index);
  697. len = mono_metadata_decode_blob_size (public_tok, &public_tok);
  698. if (flags & ASSEMBLYREF_FULL_PUBLIC_KEY_FLAG) {
  699. guchar token [8];
  700. mono_digest_get_public_token (token, (guchar*)public_tok, len);
  701. return encode_public_tok (token, 8);
  702. }
  703. return encode_public_tok ((guchar*)public_tok, len);
  704. }
  705. /**
  706. * mono_assembly_addref:
  707. * @assemnly: the assembly to reference
  708. *
  709. * This routine increments the reference count on a MonoAssembly.
  710. * The reference count is reduced every time the method mono_assembly_close() is
  711. * invoked.
  712. */
  713. void
  714. mono_assembly_addref (MonoAssembly *assembly)
  715. {
  716. InterlockedIncrement (&assembly->ref_count);
  717. }
  718. #define SILVERLIGHT_KEY "7cec85d7bea7798e"
  719. #define WINFX_KEY "31bf3856ad364e35"
  720. #define ECMA_KEY "b77a5c561934e089"
  721. #define MSFINAL_KEY "b03f5f7f11d50a3a"
  722. typedef struct {
  723. const char *name;
  724. const char *from;
  725. const char *to;
  726. } KeyRemapEntry;
  727. static KeyRemapEntry key_remap_table[] = {
  728. { "Microsoft.CSharp", WINFX_KEY, MSFINAL_KEY },
  729. { "System", SILVERLIGHT_KEY, ECMA_KEY },
  730. { "System.ComponentModel.Composition", WINFX_KEY, ECMA_KEY },
  731. { "System.ComponentModel.DataAnnotations", "ddd0da4d3e678217", WINFX_KEY },
  732. { "System.Core", SILVERLIGHT_KEY, ECMA_KEY },
  733. { "System.Numerics", WINFX_KEY, ECMA_KEY },
  734. { "System.Runtime.Serialization", SILVERLIGHT_KEY, ECMA_KEY },
  735. { "System.ServiceModel", WINFX_KEY, ECMA_KEY },
  736. { "System.ServiceModel.Web", SILVERLIGHT_KEY, WINFX_KEY },
  737. { "System.Windows", SILVERLIGHT_KEY, MSFINAL_KEY },
  738. { "System.Xml", SILVERLIGHT_KEY, ECMA_KEY },
  739. { "System.Xml.Linq", WINFX_KEY, ECMA_KEY },
  740. { "System.Xml.Serialization", WINFX_KEY, MSFINAL_KEY }
  741. };
  742. static void
  743. remap_keys (MonoAssemblyName *aname)
  744. {
  745. int i;
  746. for (i = 0; i < G_N_ELEMENTS (key_remap_table); i++) {
  747. const KeyRemapEntry *entry = &key_remap_table [i];
  748. if (strcmp (aname->name, entry->name) ||
  749. !mono_public_tokens_are_equal (aname->public_key_token, (const unsigned char*) entry->from))
  750. continue;
  751. memcpy (aname->public_key_token, entry->to, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  752. mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY,
  753. "Remapped public key token of retargetable assembly %s from %s to %s",
  754. aname->name, entry->from, entry->to);
  755. return;
  756. }
  757. }
  758. static MonoAssemblyName *
  759. mono_assembly_remap_version (MonoAssemblyName *aname, MonoAssemblyName *dest_aname)
  760. {
  761. const MonoRuntimeInfo *current_runtime;
  762. int pos, first, last;
  763. if (aname->name == NULL) return aname;
  764. current_runtime = mono_get_runtime_info ();
  765. if (aname->flags & ASSEMBLYREF_RETARGETABLE_FLAG) {
  766. const AssemblyVersionSet* vset;
  767. /* Remap to current runtime */
  768. vset = &current_runtime->version_sets [0];
  769. memcpy (dest_aname, aname, sizeof(MonoAssemblyName));
  770. dest_aname->major = vset->major;
  771. dest_aname->minor = vset->minor;
  772. dest_aname->build = vset->build;
  773. dest_aname->revision = vset->revision;
  774. dest_aname->flags &= ~ASSEMBLYREF_RETARGETABLE_FLAG;
  775. /* Remap assembly name */
  776. if (!strcmp (aname->name, "System.Net"))
  777. dest_aname->name = g_strdup ("System");
  778. remap_keys (dest_aname);
  779. mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY,
  780. "The request to load the retargetable assembly %s v%d.%d.%d.%d was remapped to %s v%d.%d.%d.%d",
  781. aname->name,
  782. aname->major, aname->minor, aname->build, aname->revision,
  783. dest_aname->name,
  784. vset->major, vset->minor, vset->build, vset->revision
  785. );
  786. return dest_aname;
  787. }
  788. #ifndef DISABLE_ASSEMBLY_REMAPPING
  789. first = 0;
  790. last = G_N_ELEMENTS (framework_assemblies) - 1;
  791. while (first <= last) {
  792. int res;
  793. pos = first + (last - first) / 2;
  794. res = strcmp (aname->name, framework_assemblies[pos].assembly_name);
  795. if (res == 0) {
  796. const AssemblyVersionSet* vset;
  797. int index = framework_assemblies[pos].version_set_index;
  798. g_assert (index < G_N_ELEMENTS (current_runtime->version_sets));
  799. vset = &current_runtime->version_sets [index];
  800. if (aname->major == vset->major && aname->minor == vset->minor &&
  801. aname->build == vset->build && aname->revision == vset->revision)
  802. return aname;
  803. if ((aname->major | aname->minor | aname->build | aname->revision) != 0)
  804. mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY,
  805. "The request to load the assembly %s v%d.%d.%d.%d was remapped to v%d.%d.%d.%d",
  806. aname->name,
  807. aname->major, aname->minor, aname->build, aname->revision,
  808. vset->major, vset->minor, vset->build, vset->revision
  809. );
  810. memcpy (dest_aname, aname, sizeof(MonoAssemblyName));
  811. dest_aname->major = vset->major;
  812. dest_aname->minor = vset->minor;
  813. dest_aname->build = vset->build;
  814. dest_aname->revision = vset->revision;
  815. return dest_aname;
  816. } else if (res < 0) {
  817. last = pos - 1;
  818. } else {
  819. first = pos + 1;
  820. }
  821. }
  822. #endif
  823. return aname;
  824. }
  825. /*
  826. * mono_assembly_get_assemblyref:
  827. *
  828. * Fill out ANAME with the assembly name of the INDEXth assembly reference in IMAGE.
  829. */
  830. void
  831. mono_assembly_get_assemblyref (MonoImage *image, int index, MonoAssemblyName *aname)
  832. {
  833. MonoTableInfo *t;
  834. guint32 cols [MONO_ASSEMBLYREF_SIZE];
  835. const char *hash;
  836. t = &image->tables [MONO_TABLE_ASSEMBLYREF];
  837. mono_metadata_decode_row (t, index, cols, MONO_ASSEMBLYREF_SIZE);
  838. hash = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLYREF_HASH_VALUE]);
  839. aname->hash_len = mono_metadata_decode_blob_size (hash, &hash);
  840. aname->hash_value = hash;
  841. aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME]);
  842. aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_CULTURE]);
  843. aname->flags = cols [MONO_ASSEMBLYREF_FLAGS];
  844. aname->major = cols [MONO_ASSEMBLYREF_MAJOR_VERSION];
  845. aname->minor = cols [MONO_ASSEMBLYREF_MINOR_VERSION];
  846. aname->build = cols [MONO_ASSEMBLYREF_BUILD_NUMBER];
  847. aname->revision = cols [MONO_ASSEMBLYREF_REV_NUMBER];
  848. if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]) {
  849. gchar *token = assemblyref_public_tok (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], aname->flags);
  850. g_strlcpy ((char*)aname->public_key_token, token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  851. g_free (token);
  852. } else {
  853. memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  854. }
  855. }
  856. void
  857. mono_assembly_load_reference (MonoImage *image, int index)
  858. {
  859. MonoAssembly *reference;
  860. MonoAssemblyName aname;
  861. MonoImageOpenStatus status;
  862. /*
  863. * image->references is shared between threads, so we need to access
  864. * it inside a critical section.
  865. */
  866. mono_assemblies_lock ();
  867. if (!image->references) {
  868. MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
  869. image->references = g_new0 (MonoAssembly *, t->rows + 1);
  870. }
  871. reference = image->references [index];
  872. mono_assemblies_unlock ();
  873. if (reference)
  874. return;
  875. mono_assembly_get_assemblyref (image, index, &aname);
  876. if (image->assembly && image->assembly->ref_only) {
  877. /* We use the loaded corlib */
  878. if (!strcmp (aname.name, "mscorlib"))
  879. reference = mono_assembly_load_full (&aname, image->assembly->basedir, &status, FALSE);
  880. else {
  881. reference = mono_assembly_loaded_full (&aname, TRUE);
  882. if (!reference)
  883. /* Try a postload search hook */
  884. reference = mono_assembly_invoke_search_hook_internal (&aname, TRUE, TRUE);
  885. }
  886. /*
  887. * Here we must advice that the error was due to
  888. * a non loaded reference using the ReflectionOnly api
  889. */
  890. if (!reference)
  891. reference = REFERENCE_MISSING;
  892. } else
  893. reference = mono_assembly_load (&aname, image->assembly? image->assembly->basedir: NULL, &status);
  894. if (reference == NULL){
  895. char *extra_msg;
  896. if (status == MONO_IMAGE_ERROR_ERRNO && errno == ENOENT) {
  897. extra_msg = g_strdup_printf ("The assembly was not found in the Global Assembly Cache, a path listed in the MONO_PATH environment variable, or in the location of the executing assembly (%s).\n", image->assembly != NULL ? image->assembly->basedir : "" );
  898. } else if (status == MONO_IMAGE_ERROR_ERRNO) {
  899. extra_msg = g_strdup_printf ("System error: %s\n", strerror (errno));
  900. } else if (status == MONO_IMAGE_MISSING_ASSEMBLYREF) {
  901. extra_msg = g_strdup ("Cannot find an assembly referenced from this one.\n");
  902. } else if (status == MONO_IMAGE_IMAGE_INVALID) {
  903. extra_msg = g_strdup ("The file exists but is not a valid assembly.\n");
  904. } else {
  905. extra_msg = g_strdup ("");
  906. }
  907. mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY, "The following assembly referenced from %s could not be loaded:\n"
  908. " Assembly: %s (assemblyref_index=%d)\n"
  909. " Version: %d.%d.%d.%d\n"
  910. " Public Key: %s\n%s",
  911. image->name, aname.name, index,
  912. aname.major, aname.minor, aname.build, aname.revision,
  913. strlen ((char*)aname.public_key_token) == 0 ? "(none)" : (char*)aname.public_key_token, extra_msg);
  914. g_free (extra_msg);
  915. }
  916. mono_assemblies_lock ();
  917. if (reference == NULL) {
  918. /* Flag as not found */
  919. reference = REFERENCE_MISSING;
  920. }
  921. if (!image->references [index]) {
  922. if (reference != REFERENCE_MISSING){
  923. mono_assembly_addref (reference);
  924. if (image->assembly)
  925. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly Ref addref %s[%p] -> %s[%p]: %d",
  926. image->assembly->aname.name, image->assembly, reference->aname.name, reference, reference->ref_count);
  927. } else {
  928. if (image->assembly)
  929. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Failed to load assembly %s[%p]\n",
  930. image->assembly->aname.name, image->assembly);
  931. }
  932. image->references [index] = reference;
  933. }
  934. mono_assemblies_unlock ();
  935. if (image->references [index] != reference) {
  936. /* Somebody loaded it before us */
  937. mono_assembly_close (reference);
  938. }
  939. }
  940. void
  941. mono_assembly_load_references (MonoImage *image, MonoImageOpenStatus *status)
  942. {
  943. /* This is a no-op now but it is part of the embedding API so we can't remove it */
  944. *status = MONO_IMAGE_OK;
  945. }
  946. typedef struct AssemblyLoadHook AssemblyLoadHook;
  947. struct AssemblyLoadHook {
  948. AssemblyLoadHook *next;
  949. MonoAssemblyLoadFunc func;
  950. gpointer user_data;
  951. };
  952. AssemblyLoadHook *assembly_load_hook = NULL;
  953. void
  954. mono_assembly_invoke_load_hook (MonoAssembly *ass)
  955. {
  956. AssemblyLoadHook *hook;
  957. for (hook = assembly_load_hook; hook; hook = hook->next) {
  958. hook->func (ass, hook->user_data);
  959. }
  960. }
  961. void
  962. mono_install_assembly_load_hook (MonoAssemblyLoadFunc func, gpointer user_data)
  963. {
  964. AssemblyLoadHook *hook;
  965. g_return_if_fail (func != NULL);
  966. hook = g_new0 (AssemblyLoadHook, 1);
  967. hook->func = func;
  968. hook->user_data = user_data;
  969. hook->next = assembly_load_hook;
  970. assembly_load_hook = hook;
  971. }
  972. static void
  973. free_assembly_load_hooks (void)
  974. {
  975. AssemblyLoadHook *hook, *next;
  976. for (hook = assembly_load_hook; hook; hook = next) {
  977. next = hook->next;
  978. g_free (hook);
  979. }
  980. }
  981. typedef struct AssemblySearchHook AssemblySearchHook;
  982. struct AssemblySearchHook {
  983. AssemblySearchHook *next;
  984. MonoAssemblySearchFunc func;
  985. gboolean refonly;
  986. gboolean postload;
  987. gpointer user_data;
  988. };
  989. AssemblySearchHook *assembly_search_hook = NULL;
  990. static MonoAssembly*
  991. mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, gboolean refonly, gboolean postload)
  992. {
  993. AssemblySearchHook *hook;
  994. for (hook = assembly_search_hook; hook; hook = hook->next) {
  995. if ((hook->refonly == refonly) && (hook->postload == postload)) {
  996. MonoAssembly *ass = hook->func (aname, hook->user_data);
  997. if (ass)
  998. return ass;
  999. }
  1000. }
  1001. return NULL;
  1002. }
  1003. MonoAssembly*
  1004. mono_assembly_invoke_search_hook (MonoAssemblyName *aname)
  1005. {
  1006. return mono_assembly_invoke_search_hook_internal (aname, FALSE, FALSE);
  1007. }
  1008. static void
  1009. mono_install_assembly_search_hook_internal (MonoAssemblySearchFunc func, gpointer user_data, gboolean refonly, gboolean postload)
  1010. {
  1011. AssemblySearchHook *hook;
  1012. g_return_if_fail (func != NULL);
  1013. hook = g_new0 (AssemblySearchHook, 1);
  1014. hook->func = func;
  1015. hook->user_data = user_data;
  1016. hook->refonly = refonly;
  1017. hook->postload = postload;
  1018. hook->next = assembly_search_hook;
  1019. assembly_search_hook = hook;
  1020. }
  1021. void
  1022. mono_install_assembly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
  1023. {
  1024. mono_install_assembly_search_hook_internal (func, user_data, FALSE, FALSE);
  1025. }
  1026. static void
  1027. free_assembly_search_hooks (void)
  1028. {
  1029. AssemblySearchHook *hook, *next;
  1030. for (hook = assembly_search_hook; hook; hook = next) {
  1031. next = hook->next;
  1032. g_free (hook);
  1033. }
  1034. }
  1035. void
  1036. mono_install_assembly_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
  1037. {
  1038. mono_install_assembly_search_hook_internal (func, user_data, TRUE, FALSE);
  1039. }
  1040. void
  1041. mono_install_assembly_postload_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
  1042. {
  1043. mono_install_assembly_search_hook_internal (func, user_data, FALSE, TRUE);
  1044. }
  1045. void
  1046. mono_install_assembly_postload_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
  1047. {
  1048. mono_install_assembly_search_hook_internal (func, user_data, TRUE, TRUE);
  1049. }
  1050. typedef struct AssemblyPreLoadHook AssemblyPreLoadHook;
  1051. struct AssemblyPreLoadHook {
  1052. AssemblyPreLoadHook *next;
  1053. MonoAssemblyPreLoadFunc func;
  1054. gpointer user_data;
  1055. };
  1056. static AssemblyPreLoadHook *assembly_preload_hook = NULL;
  1057. static AssemblyPreLoadHook *assembly_refonly_preload_hook = NULL;
  1058. static MonoAssembly *
  1059. invoke_assembly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
  1060. {
  1061. AssemblyPreLoadHook *hook;
  1062. MonoAssembly *assembly;
  1063. for (hook = assembly_preload_hook; hook; hook = hook->next) {
  1064. assembly = hook->func (aname, assemblies_path, hook->user_data);
  1065. if (assembly != NULL)
  1066. return assembly;
  1067. }
  1068. return NULL;
  1069. }
  1070. static MonoAssembly *
  1071. invoke_assembly_refonly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
  1072. {
  1073. AssemblyPreLoadHook *hook;
  1074. MonoAssembly *assembly;
  1075. for (hook = assembly_refonly_preload_hook; hook; hook = hook->next) {
  1076. assembly = hook->func (aname, assemblies_path, hook->user_data);
  1077. if (assembly != NULL)
  1078. return assembly;
  1079. }
  1080. return NULL;
  1081. }
  1082. void
  1083. mono_install_assembly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
  1084. {
  1085. AssemblyPreLoadHook *hook;
  1086. g_return_if_fail (func != NULL);
  1087. hook = g_new0 (AssemblyPreLoadHook, 1);
  1088. hook->func = func;
  1089. hook->user_data = user_data;
  1090. hook->next = assembly_preload_hook;
  1091. assembly_preload_hook = hook;
  1092. }
  1093. void
  1094. mono_install_assembly_refonly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
  1095. {
  1096. AssemblyPreLoadHook *hook;
  1097. g_return_if_fail (func != NULL);
  1098. hook = g_new0 (AssemblyPreLoadHook, 1);
  1099. hook->func = func;
  1100. hook->user_data = user_data;
  1101. hook->next = assembly_refonly_preload_hook;
  1102. assembly_refonly_preload_hook = hook;
  1103. }
  1104. static void
  1105. free_assembly_preload_hooks (void)
  1106. {
  1107. AssemblyPreLoadHook *hook, *next;
  1108. for (hook = assembly_preload_hook; hook; hook = next) {
  1109. next = hook->next;
  1110. g_free (hook);
  1111. }
  1112. for (hook = assembly_refonly_preload_hook; hook; hook = next) {
  1113. next = hook->next;
  1114. g_free (hook);
  1115. }
  1116. }
  1117. static gchar *
  1118. absolute_dir (const gchar *filename)
  1119. {
  1120. gchar *cwd;
  1121. gchar *mixed;
  1122. gchar **parts;
  1123. gchar *part;
  1124. GList *list, *tmp;
  1125. GString *result;
  1126. gchar *res;
  1127. gint i;
  1128. if (g_path_is_absolute (filename)) {
  1129. part = g_path_get_dirname (filename);
  1130. res = g_strconcat (part, G_DIR_SEPARATOR_S, NULL);
  1131. g_free (part);
  1132. return res;
  1133. }
  1134. cwd = g_get_current_dir ();
  1135. mixed = g_build_filename (cwd, filename, NULL);
  1136. parts = g_strsplit (mixed, G_DIR_SEPARATOR_S, 0);
  1137. g_free (mixed);
  1138. g_free (cwd);
  1139. list = NULL;
  1140. for (i = 0; (part = parts [i]) != NULL; i++) {
  1141. if (!strcmp (part, "."))
  1142. continue;
  1143. if (!strcmp (part, "..")) {
  1144. if (list && list->next) /* Don't remove root */
  1145. list = g_list_delete_link (list, list);
  1146. } else {
  1147. list = g_list_prepend (list, part);
  1148. }
  1149. }
  1150. result = g_string_new ("");
  1151. list = g_list_reverse (list);
  1152. /* Ignores last data pointer, which should be the filename */
  1153. for (tmp = list; tmp && tmp->next != NULL; tmp = tmp->next){
  1154. if (tmp->data)
  1155. g_string_append_printf (result, "%s%c", (char *) tmp->data,
  1156. G_DIR_SEPARATOR);
  1157. }
  1158. res = result->str;
  1159. g_string_free (result, FALSE);
  1160. g_list_free (list);
  1161. g_strfreev (parts);
  1162. if (*res == '\0') {
  1163. g_free (res);
  1164. return g_strdup (".");
  1165. }
  1166. return res;
  1167. }
  1168. /**
  1169. * mono_assembly_open_from_bundle:
  1170. * @filename: Filename requested
  1171. * @status: return value
  1172. *
  1173. * This routine tries to open the assembly specified by `filename' from the
  1174. * defined bundles, if found, returns the MonoImage for it, if not found
  1175. * returns NULL
  1176. */
  1177. MonoImage *
  1178. mono_assembly_open_from_bundle (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
  1179. {
  1180. int i;
  1181. char *name;
  1182. MonoImage *image = NULL;
  1183. /*
  1184. * we do a very simple search for bundled assemblies: it's not a general
  1185. * purpose assembly loading mechanism.
  1186. */
  1187. if (!bundles)
  1188. return NULL;
  1189. name = g_path_get_basename (filename);
  1190. mono_assemblies_lock ();
  1191. for (i = 0; !image && bundles [i]; ++i) {
  1192. if (strcmp (bundles [i]->name, name) == 0) {
  1193. image = mono_image_open_from_data_with_name ((char*)bundles [i]->data, bundles [i]->size, FALSE, status, refonly, name);
  1194. break;
  1195. }
  1196. }
  1197. mono_assemblies_unlock ();
  1198. g_free (name);
  1199. if (image) {
  1200. mono_image_addref (image);
  1201. return image;
  1202. }
  1203. return NULL;
  1204. }
  1205. MonoAssembly *
  1206. mono_assembly_open_full (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
  1207. {
  1208. MonoImage *image;
  1209. MonoAssembly *ass;
  1210. MonoImageOpenStatus def_status;
  1211. gchar *fname;
  1212. gchar *new_fname;
  1213. g_return_val_if_fail (filename != NULL, NULL);
  1214. if (!status)
  1215. status = &def_status;
  1216. *status = MONO_IMAGE_OK;
  1217. if (strncmp (filename, "file://", 7) == 0) {
  1218. GError *error = NULL;
  1219. gchar *uri = (gchar *) filename;
  1220. gchar *tmpuri;
  1221. /*
  1222. * MS allows file://c:/... and fails on file://localhost/c:/...
  1223. * They also throw an IndexOutOfRangeException if "file://"
  1224. */
  1225. if (uri [7] != '/')
  1226. uri = g_strdup_printf ("file:///%s", uri + 7);
  1227. tmpuri = uri;
  1228. uri = mono_escape_uri_string (tmpuri);
  1229. fname = g_filename_from_uri (uri, NULL, &error);
  1230. g_free (uri);
  1231. if (tmpuri != filename)
  1232. g_free (tmpuri);
  1233. if (error != NULL) {
  1234. g_warning ("%s\n", error->message);
  1235. g_error_free (error);
  1236. fname = g_strdup (filename);
  1237. }
  1238. } else {
  1239. fname = g_strdup (filename);
  1240. }
  1241. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
  1242. "Assembly Loader probing location: '%s'.", fname);
  1243. new_fname = NULL;
  1244. if (!mono_assembly_is_in_gac (fname))
  1245. new_fname = mono_make_shadow_copy (fname);
  1246. if (new_fname && new_fname != fname) {
  1247. g_free (fname);
  1248. fname = new_fname;
  1249. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
  1250. "Assembly Loader shadow-copied assembly to: '%s'.", fname);
  1251. }
  1252. image = NULL;
  1253. if (bundles != NULL)
  1254. image = mono_assembly_open_from_bundle (fname, status, refonly);
  1255. if (!image)
  1256. image = mono_image_open_full (fname, status, refonly);
  1257. if (!image){
  1258. if (*status == MONO_IMAGE_OK)
  1259. *status = MONO_IMAGE_ERROR_ERRNO;
  1260. g_free (fname);
  1261. return NULL;
  1262. }
  1263. if (image->assembly) {
  1264. /* Already loaded by another appdomain */
  1265. mono_assembly_invoke_load_hook (image->assembly);
  1266. mono_image_close (image);
  1267. g_free (fname);
  1268. return image->assembly;
  1269. }
  1270. ass = mono_assembly_load_from_full (image, fname, status, refonly);
  1271. if (ass) {
  1272. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
  1273. "Assembly Loader loaded assembly from location: '%s'.", filename);
  1274. if (!refonly)
  1275. mono_config_for_assembly (ass->image);
  1276. }
  1277. /* Clear the reference added by mono_image_open */
  1278. mono_image_close (image);
  1279. g_free (fname);
  1280. return ass;
  1281. }
  1282. static void
  1283. free_item (gpointer val, gpointer user_data)
  1284. {
  1285. g_free (val);
  1286. }
  1287. /*
  1288. * mono_assembly_load_friends:
  1289. * @ass: an assembly
  1290. *
  1291. * Load the list of friend assemblies that are allowed to access
  1292. * the assembly's internal types and members. They are stored as assembly
  1293. * names in custom attributes.
  1294. *
  1295. * This is an internal method, we need this because when we load mscorlib
  1296. * we do not have the mono_defaults.internals_visible_class loaded yet,
  1297. * so we need to load these after we initialize the runtime.
  1298. *
  1299. * LOCKING: Acquires the assemblies lock plus the loader lock.
  1300. */
  1301. void
  1302. mono_assembly_load_friends (MonoAssembly* ass)
  1303. {
  1304. int i;
  1305. MonoCustomAttrInfo* attrs;
  1306. GSList *list;
  1307. if (ass->friend_assembly_names_inited)
  1308. return;
  1309. attrs = mono_custom_attrs_from_assembly (ass);
  1310. if (!attrs) {
  1311. mono_assemblies_lock ();
  1312. ass->friend_assembly_names_inited = TRUE;
  1313. mono_assemblies_unlock ();
  1314. return;
  1315. }
  1316. mono_assemblies_lock ();
  1317. if (ass->friend_assembly_names_inited) {
  1318. mono_assemblies_unlock ();
  1319. return;
  1320. }
  1321. mono_assemblies_unlock ();
  1322. list = NULL;
  1323. /*
  1324. * We build the list outside the assemblies lock, the worse that can happen
  1325. * is that we'll need to free the allocated list.
  1326. */
  1327. for (i = 0; i < attrs->num_attrs; ++i) {
  1328. MonoCustomAttrEntry *attr = &attrs->attrs [i];
  1329. MonoAssemblyName *aname;
  1330. const gchar *data;
  1331. guint slen;
  1332. /* Do some sanity checking */
  1333. if (!attr->ctor || attr->ctor->klass != mono_defaults.internals_visible_class)
  1334. continue;
  1335. if (attr->data_size < 4)
  1336. continue;
  1337. data = (const char*)attr->data;
  1338. /* 0xFF means null string, see custom attr format */
  1339. if (data [0] != 1 || data [1] != 0 || (data [2] & 0xFF) == 0xFF)
  1340. continue;
  1341. slen = mono_metadata_decode_value (data + 2, &data);
  1342. aname = g_new0 (MonoAssemblyName, 1);
  1343. /*g_print ("friend ass: %s\n", data);*/
  1344. if (mono_assembly_name_parse_full (data, aname, TRUE, NULL, NULL)) {
  1345. list = g_slist_prepend (list, aname);
  1346. } else {
  1347. g_free (aname);
  1348. }
  1349. }
  1350. mono_custom_attrs_free (attrs);
  1351. mono_assemblies_lock ();
  1352. if (ass->friend_assembly_names_inited) {
  1353. mono_assemblies_unlock ();
  1354. g_slist_foreach (list, free_item, NULL);
  1355. g_slist_free (list);
  1356. return;
  1357. }
  1358. ass->friend_assembly_names = list;
  1359. /* Because of the double checked locking pattern above */
  1360. mono_memory_barrier ();
  1361. ass->friend_assembly_names_inited = TRUE;
  1362. mono_assemblies_unlock ();
  1363. }
  1364. /**
  1365. * mono_assembly_open:
  1366. * @filename: Opens the assembly pointed out by this name
  1367. * @status: where a status code can be returned
  1368. *
  1369. * mono_assembly_open opens the PE-image pointed by @filename, and
  1370. * loads any external assemblies referenced by it.
  1371. *
  1372. * Return: a pointer to the MonoAssembly if @filename contains a valid
  1373. * assembly or NULL on error. Details about the error are stored in the
  1374. * @status variable.
  1375. */
  1376. MonoAssembly *
  1377. mono_assembly_open (const char *filename, MonoImageOpenStatus *status)
  1378. {
  1379. return mono_assembly_open_full (filename, status, FALSE);
  1380. }
  1381. MonoAssembly *
  1382. mono_assembly_load_from_full (MonoImage *image, const char*fname,
  1383. MonoImageOpenStatus *status, gboolean refonly)
  1384. {
  1385. MonoAssembly *ass, *ass2;
  1386. char *base_dir;
  1387. if (!image->tables [MONO_TABLE_ASSEMBLY].rows) {
  1388. /* 'image' doesn't have a manifest -- maybe someone is trying to Assembly.Load a .netmodule */
  1389. *status = MONO_IMAGE_IMAGE_INVALID;
  1390. return NULL;
  1391. }
  1392. #if defined (HOST_WIN32)
  1393. {
  1394. gchar *tmp_fn;
  1395. int i;
  1396. tmp_fn = g_strdup (fname);
  1397. for (i = strlen (tmp_fn) - 1; i >= 0; i--) {
  1398. if (tmp_fn [i] == '/')
  1399. tmp_fn [i] = '\\';
  1400. }
  1401. base_dir = absolute_dir (tmp_fn);
  1402. g_free (tmp_fn);
  1403. }
  1404. #else
  1405. base_dir = absolute_dir (fname);
  1406. #endif
  1407. /*
  1408. * Create assembly struct, and enter it into the assembly cache
  1409. */
  1410. ass = g_new0 (MonoAssembly, 1);
  1411. ass->basedir = base_dir;
  1412. ass->ref_only = refonly;
  1413. ass->image = image;
  1414. mono_profiler_assembly_event (ass, MONO_PROFILE_START_LOAD);
  1415. mono_assembly_fill_assembly_name (image, &ass->aname);
  1416. if (mono_defaults.corlib && strcmp (ass->aname.name, "mscorlib") == 0) {
  1417. // MS.NET doesn't support loading other mscorlibs
  1418. g_free (ass);
  1419. g_free (base_dir);
  1420. mono_image_addref (mono_defaults.corlib);
  1421. *status = MONO_IMAGE_OK;
  1422. return mono_defaults.corlib->assembly;
  1423. }
  1424. /* Add a non-temporary reference because of ass->image */
  1425. mono_image_addref (image);
  1426. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Image addref %s[%p] -> %s[%p]: %d", ass->aname.name, ass, image->name, image, image->ref_count);
  1427. /*
  1428. * The load hooks might take locks so we can't call them while holding the
  1429. * assemblies lock.
  1430. */
  1431. if (ass->aname.name) {
  1432. ass2 = mono_assembly_invoke_search_hook_internal (&ass->aname, refonly, FALSE);
  1433. if (ass2) {
  1434. g_free (ass);
  1435. g_free (base_dir);
  1436. mono_image_close (image);
  1437. *status = MONO_IMAGE_OK;
  1438. return ass2;
  1439. }
  1440. }
  1441. mono_assemblies_lock ();
  1442. if (image->assembly) {
  1443. /*
  1444. * This means another thread has already loaded the assembly, but not yet
  1445. * called the load hooks so the search hook can't find the assembly.
  1446. */
  1447. mono_assemblies_unlock ();
  1448. ass2 = image->assembly;
  1449. g_free (ass);
  1450. g_free (base_dir);
  1451. mono_image_close (image);
  1452. *status = MONO_IMAGE_OK;
  1453. return ass2;
  1454. }
  1455. image->assembly = ass;
  1456. loaded_assemblies = g_list_prepend (loaded_assemblies, ass);
  1457. mono_assemblies_unlock ();
  1458. #ifdef HOST_WIN32
  1459. if (image->is_module_handle)
  1460. mono_image_fixup_vtable (image);
  1461. #endif
  1462. mono_assembly_invoke_load_hook (ass);
  1463. mono_profiler_assembly_loaded (ass, MONO_PROFILE_OK);
  1464. return ass;
  1465. }
  1466. MonoAssembly *
  1467. mono_assembly_load_from (MonoImage *image, const char *fname,
  1468. MonoImageOpenStatus *status)
  1469. {
  1470. return mono_assembly_load_from_full (image, fname, status, FALSE);
  1471. }
  1472. /**
  1473. * mono_assembly_name_free:
  1474. * @aname: assembly name to free
  1475. *
  1476. * Frees the provided assembly name object.
  1477. * (it does not frees the object itself, only the name members).
  1478. */
  1479. void
  1480. mono_assembly_name_free (MonoAssemblyName *aname)
  1481. {
  1482. if (aname == NULL)
  1483. return;
  1484. g_free ((void *) aname->name);
  1485. g_free ((void *) aname->culture);
  1486. g_free ((void *) aname->hash_value);
  1487. }
  1488. static gboolean
  1489. parse_public_key (const gchar *key, gchar** pubkey, gboolean *is_ecma)
  1490. {
  1491. const gchar *pkey;
  1492. gchar header [16], val, *arr;
  1493. gint i, j, offset, bitlen, keylen, pkeylen;
  1494. keylen = strlen (key) >> 1;
  1495. if (keylen < 1)
  1496. return FALSE;
  1497. /* allow the ECMA standard key */
  1498. if (strcmp (key, "00000000000000000400000000000000") == 0) {
  1499. if (pubkey) {
  1500. *pubkey = g_strdup (key);
  1501. *is_ecma = TRUE;
  1502. }
  1503. return TRUE;
  1504. }
  1505. *is_ecma = FALSE;
  1506. val = g_ascii_xdigit_value (key [0]) << 4;
  1507. val |= g_ascii_xdigit_value (key [1]);
  1508. switch (val) {
  1509. case 0x00:
  1510. if (keylen < 13)
  1511. return FALSE;
  1512. val = g_ascii_xdigit_value (key [24]);
  1513. val |= g_ascii_xdigit_value (key [25]);
  1514. if (val != 0x06)
  1515. return FALSE;
  1516. pkey = key + 24;
  1517. break;
  1518. case 0x06:
  1519. pkey = key;
  1520. break;
  1521. default:
  1522. return FALSE;
  1523. }
  1524. /* We need the first 16 bytes
  1525. * to check whether this key is valid or not */
  1526. pkeylen = strlen (pkey) >> 1;
  1527. if (pkeylen < 16)
  1528. return FALSE;
  1529. for (i = 0, j = 0; i < 16; i++) {
  1530. header [i] = g_ascii_xdigit_value (pkey [j++]) << 4;
  1531. header [i] |= g_ascii_xdigit_value (pkey [j++]);
  1532. }
  1533. if (header [0] != 0x06 || /* PUBLICKEYBLOB (0x06) */
  1534. header [1] != 0x02 || /* Version (0x02) */
  1535. header [2] != 0x00 || /* Reserved (word) */
  1536. header [3] != 0x00 ||
  1537. (guint)(read32 (header + 8)) != 0x31415352) /* DWORD magic = RSA1 */
  1538. return FALSE;
  1539. /* Based on this length, we _should_ be able to know if the length is right */
  1540. bitlen = read32 (header + 12) >> 3;
  1541. if ((bitlen + 16 + 4) != pkeylen)
  1542. return FALSE;
  1543. /* parsing is OK and the public key itself is not requested back */
  1544. if (!pubkey)
  1545. return TRUE;
  1546. /* Encode the size of the blob */
  1547. offset = 0;
  1548. if (keylen <= 127) {
  1549. arr = g_malloc (keylen + 1);
  1550. arr [offset++] = keylen;
  1551. } else {
  1552. arr = g_malloc (keylen + 2);
  1553. arr [offset++] = 0x80; /* 10bs */
  1554. arr [offset++] = keylen;
  1555. }
  1556. for (i = offset, j = 0; i < keylen + offset; i++) {
  1557. arr [i] = g_ascii_xdigit_value (key [j++]) << 4;
  1558. arr [i] |= g_ascii_xdigit_value (key [j++]);
  1559. }
  1560. *pubkey = arr;
  1561. return TRUE;
  1562. }
  1563. static gboolean
  1564. build_assembly_name (const char *name, const char *version, const char *culture, const char *token, const char *key, guint32 flags, guint32 arch, MonoAssemblyName *aname, gboolean save_public_key)
  1565. {
  1566. gint major, minor, build, revision;
  1567. gint len;
  1568. gint version_parts;
  1569. gchar *pkey, *pkeyptr, *encoded, tok [8];
  1570. memset (aname, 0, sizeof (MonoAssemblyName));
  1571. if (version) {
  1572. version_parts = sscanf (version, "%u.%u.%u.%u", &major, &minor, &build, &revision);
  1573. if (version_parts < 2 || version_parts > 4)
  1574. return FALSE;
  1575. /* FIXME: we should set build & revision to -1 (instead of 0)
  1576. if these are not set in the version string. That way, later on,
  1577. we can still determine if these were specified. */
  1578. aname->major = major;
  1579. aname->minor = minor;
  1580. if (version_parts >= 3)
  1581. aname->build = build;
  1582. else
  1583. aname->build = 0;
  1584. if (version_parts == 4)
  1585. aname->revision = revision;
  1586. else
  1587. aname->revision = 0;
  1588. }
  1589. aname->flags = flags;
  1590. aname->arch = arch;
  1591. aname->name = g_strdup (name);
  1592. if (culture) {
  1593. if (g_ascii_strcasecmp (culture, "neutral") == 0)
  1594. aname->culture = g_strdup ("");
  1595. else
  1596. aname->culture = g_strdup (culture);
  1597. }
  1598. if (token && strncmp (token, "null", 4) != 0) {
  1599. char *lower;
  1600. /* the constant includes the ending NULL, hence the -1 */
  1601. if (strlen (token) != (MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)) {
  1602. mono_assembly_name_free (aname);
  1603. return FALSE;
  1604. }
  1605. lower

Large files files are truncated, but you can click here to view the full file