loader.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright 1995-1998 by Metro Link, Inc.
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that
  7. * copyright notice and this permission notice appear in supporting
  8. * documentation, and that the name of Metro Link, Inc. not be used in
  9. * advertising or publicity pertaining to distribution of the software without
  10. * specific, written prior permission. Metro Link, Inc. makes no
  11. * representations about the suitability of this software for any purpose.
  12. * It is provided "as is" without express or implied warranty.
  13. *
  14. * METRO LINK, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL METRO LINK, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20. * PERFORMANCE OF THIS SOFTWARE.
  21. */
  22. /*
  23. * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
  24. *
  25. * Permission is hereby granted, free of charge, to any person obtaining a
  26. * copy of this software and associated documentation files (the "Software"),
  27. * to deal in the Software without restriction, including without limitation
  28. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  29. * and/or sell copies of the Software, and to permit persons to whom the
  30. * Software is furnished to do so, subject to the following conditions:
  31. *
  32. * The above copyright notice and this permission notice shall be included in
  33. * all copies or substantial portions of the Software.
  34. *
  35. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  36. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  37. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  38. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  39. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  40. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  41. * OTHER DEALINGS IN THE SOFTWARE.
  42. *
  43. * Except as contained in this notice, the name of the copyright holder(s)
  44. * and author(s) shall not be used in advertising or otherwise to promote
  45. * the sale, use or other dealings in this Software without prior written
  46. * authorization from the copyright holder(s) and author(s).
  47. */
  48. #ifdef HAVE_XORG_CONFIG_H
  49. #include <xorg-config.h>
  50. #endif
  51. #include <errno.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <sys/types.h>
  55. #include <unistd.h>
  56. #include <sys/stat.h>
  57. #include <fcntl.h>
  58. #include <string.h>
  59. #include <stdarg.h>
  60. #include "os.h"
  61. #include "loader.h"
  62. #include "loaderProcs.h"
  63. #include "xf86.h"
  64. #include "xf86Priv.h"
  65. #include "compiler.h"
  66. #ifdef HAVE_DLFCN_H
  67. #include <dlfcn.h>
  68. #include <X11/Xos.h>
  69. #if defined(DL_LAZY)
  70. #define DLOPEN_LAZY DL_LAZY
  71. #elif defined(RTLD_LAZY)
  72. #define DLOPEN_LAZY RTLD_LAZY
  73. #elif defined(__FreeBSD__)
  74. #define DLOPEN_LAZY 1
  75. #else
  76. #define DLOPEN_LAZY 0
  77. #endif
  78. #if defined(LD_GLOBAL)
  79. #define DLOPEN_GLOBAL LD_GLOBAL
  80. #elif defined(RTLD_GLOBAL)
  81. #define DLOPEN_GLOBAL RTLD_GLOBAL
  82. #else
  83. #define DLOPEN_GLOBAL 0
  84. #endif
  85. #else
  86. #error i have no dynamic linker and i must scream
  87. #endif
  88. extern void *xorg_symbols[];
  89. void
  90. LoaderInit(void)
  91. {
  92. xf86MsgVerb(X_INFO, 2, "Loader magic: %p\n", (void *) xorg_symbols);
  93. xf86MsgVerb(X_INFO, 2, "Module ABI versions:\n");
  94. xf86ErrorFVerb(2, "\t%s: %d.%d\n", ABI_CLASS_ANSIC,
  95. GET_ABI_MAJOR(LoaderVersionInfo.ansicVersion),
  96. GET_ABI_MINOR(LoaderVersionInfo.ansicVersion));
  97. xf86ErrorFVerb(2, "\t%s: %d.%d\n", ABI_CLASS_VIDEODRV,
  98. GET_ABI_MAJOR(LoaderVersionInfo.videodrvVersion),
  99. GET_ABI_MINOR(LoaderVersionInfo.videodrvVersion));
  100. xf86ErrorFVerb(2, "\t%s : %d.%d\n", ABI_CLASS_XINPUT,
  101. GET_ABI_MAJOR(LoaderVersionInfo.xinputVersion),
  102. GET_ABI_MINOR(LoaderVersionInfo.xinputVersion));
  103. xf86ErrorFVerb(2, "\t%s : %d.%d\n", ABI_CLASS_EXTENSION,
  104. GET_ABI_MAJOR(LoaderVersionInfo.extensionVersion),
  105. GET_ABI_MINOR(LoaderVersionInfo.extensionVersion));
  106. }
  107. /* Public Interface to the loader. */
  108. void *
  109. LoaderOpen(const char *module, int *errmaj, int *errmin)
  110. {
  111. void *ret;
  112. #if defined(DEBUG)
  113. ErrorF("LoaderOpen(%s)\n", module);
  114. #endif
  115. xf86Msg(X_INFO, "Loading %s\n", module);
  116. if (!(ret = dlopen(module, DLOPEN_LAZY | DLOPEN_GLOBAL))) {
  117. xf86Msg(X_ERROR, "Failed to load %s: %s\n", module, dlerror());
  118. if (errmaj)
  119. *errmaj = LDR_NOLOAD;
  120. if (errmin)
  121. *errmin = LDR_NOLOAD;
  122. return NULL;
  123. }
  124. return ret;
  125. }
  126. void *
  127. LoaderSymbol(const char *name)
  128. {
  129. static void *global_scope = NULL;
  130. void *p;
  131. p = dlsym(RTLD_DEFAULT, name);
  132. if (p != NULL)
  133. return p;
  134. if (!global_scope)
  135. global_scope = dlopen(NULL, DLOPEN_LAZY | DLOPEN_GLOBAL);
  136. if (global_scope)
  137. return dlsym(global_scope, name);
  138. return NULL;
  139. }
  140. void *
  141. LoaderSymbolFromModule(void *handle, const char *name)
  142. {
  143. return dlsym(handle, name);
  144. }
  145. void
  146. LoaderUnload(const char *name, void *handle)
  147. {
  148. LogMessageVerbSigSafe(X_INFO, 1, "Unloading %s\n", name);
  149. if (handle)
  150. dlclose(handle);
  151. }
  152. unsigned long LoaderOptions = 0;
  153. void
  154. LoaderSetOptions(unsigned long opts)
  155. {
  156. LoaderOptions |= opts;
  157. }
  158. Bool
  159. LoaderShouldIgnoreABI(void)
  160. {
  161. return (LoaderOptions & LDR_OPT_ABI_MISMATCH_NONFATAL) != 0;
  162. }
  163. int
  164. LoaderGetABIVersion(const char *abiclass)
  165. {
  166. struct {
  167. const char *name;
  168. int version;
  169. } classes[] = {
  170. {ABI_CLASS_ANSIC, LoaderVersionInfo.ansicVersion},
  171. {ABI_CLASS_VIDEODRV, LoaderVersionInfo.videodrvVersion},
  172. {ABI_CLASS_XINPUT, LoaderVersionInfo.xinputVersion},
  173. {ABI_CLASS_EXTENSION, LoaderVersionInfo.extensionVersion},
  174. {ABI_CLASS_FONT, LoaderVersionInfo.fontVersion},
  175. {NULL, 0}
  176. };
  177. int i;
  178. for (i = 0; classes[i].name; i++) {
  179. if (!strcmp(classes[i].name, abiclass)) {
  180. return classes[i].version;
  181. }
  182. }
  183. return 0;
  184. }