preopen.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* loader-preopen.c -- emulate dynamic linking using preloaded_symbols
  2. Copyright (C) 1998-2000, 2004, 2006-2008, 2011-2015 Free Software
  3. Foundation, Inc.
  4. Written by Thomas Tanner, 1998
  5. NOTE: The canonical source of this file is maintained with the
  6. GNU Libtool package. Report bugs to bug-libtool@gnu.org.
  7. GNU Libltdl is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11. As a special exception to the GNU Lesser General Public License,
  12. if you distribute this file as part of a program or library that
  13. is built using GNU Libtool, you may include this file under the
  14. same distribution terms that you use for the rest of that program.
  15. GNU Libltdl is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Lesser General Public License for more details.
  19. You should have received a copy of the GNU Lesser General Public
  20. License along with GNU Libltdl; see the file COPYING.LIB. If not, a
  21. copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
  22. or obtained by writing to the Free Software Foundation, Inc.,
  23. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. #include "lt__private.h"
  26. #include "lt_dlloader.h"
  27. /* Use the preprocessor to rename non-static symbols to avoid namespace
  28. collisions when the loader code is statically linked into libltdl.
  29. Use the "<module_name>_LTX_" prefix so that the symbol addresses can
  30. be fetched from the preloaded symbol list by lt_dlsym(): */
  31. #define get_vtable preopen_LTX_get_vtable
  32. LT_BEGIN_C_DECLS
  33. LT_SCOPE lt_dlvtable *get_vtable (lt_user_data loader_data);
  34. LT_END_C_DECLS
  35. /* Boilerplate code to set up the vtable for hooking this loader into
  36. libltdl's loader list: */
  37. static int vl_init (lt_user_data loader_data);
  38. static int vl_exit (lt_user_data loader_data);
  39. static lt_module vm_open (lt_user_data loader_data, const char *filename,
  40. lt_dladvise advise);
  41. static int vm_close (lt_user_data loader_data, lt_module module);
  42. static void * vm_sym (lt_user_data loader_data, lt_module module,
  43. const char *symbolname);
  44. static lt_dlvtable *vtable = 0;
  45. /* Return the vtable for this loader, only the name and sym_prefix
  46. attributes (plus the virtual function implementations, obviously)
  47. change between loaders. */
  48. lt_dlvtable *
  49. get_vtable (lt_user_data loader_data)
  50. {
  51. if (!vtable)
  52. {
  53. vtable = (lt_dlvtable *) lt__zalloc (sizeof *vtable);
  54. }
  55. if (vtable && !vtable->name)
  56. {
  57. vtable->name = "lt_preopen";
  58. vtable->sym_prefix = 0;
  59. vtable->module_open = vm_open;
  60. vtable->module_close = vm_close;
  61. vtable->find_sym = vm_sym;
  62. vtable->dlloader_init = vl_init;
  63. vtable->dlloader_exit = vl_exit;
  64. vtable->dlloader_data = loader_data;
  65. vtable->priority = LT_DLLOADER_PREPEND;
  66. }
  67. if (vtable && (vtable->dlloader_data != loader_data))
  68. {
  69. LT__SETERROR (INIT_LOADER);
  70. return 0;
  71. }
  72. return vtable;
  73. }
  74. /* --- IMPLEMENTATION --- */
  75. /* Wrapper type to chain together symbol lists of various origins. */
  76. typedef struct symlist_chain
  77. {
  78. struct symlist_chain *next;
  79. const lt_dlsymlist *symlist;
  80. } symlist_chain;
  81. static int add_symlist (const lt_dlsymlist *symlist);
  82. static int free_symlists (void);
  83. /* The start of the symbol lists chain. */
  84. static symlist_chain *preloaded_symlists = 0;
  85. /* A symbol list preloaded before lt_init() was called. */
  86. static const lt_dlsymlist *default_preloaded_symbols = 0;
  87. /* A function called through the vtable to initialise this loader. */
  88. static int
  89. vl_init (lt_user_data loader_data LT__UNUSED)
  90. {
  91. int errors = 0;
  92. preloaded_symlists = 0;
  93. if (default_preloaded_symbols)
  94. {
  95. errors = lt_dlpreload (default_preloaded_symbols);
  96. }
  97. return errors;
  98. }
  99. /* A function called through the vtable when this loader is no
  100. longer needed by the application. */
  101. static int
  102. vl_exit (lt_user_data loader_data LT__UNUSED)
  103. {
  104. vtable = NULL;
  105. free_symlists ();
  106. return 0;
  107. }
  108. /* A function called through the vtable to open a module with this
  109. loader. Returns an opaque representation of the newly opened
  110. module for processing with this loader's other vtable functions. */
  111. static lt_module
  112. vm_open (lt_user_data loader_data LT__UNUSED, const char *filename,
  113. lt_dladvise advise LT__UNUSED)
  114. {
  115. symlist_chain *lists;
  116. lt_module module = 0;
  117. if (!preloaded_symlists)
  118. {
  119. LT__SETERROR (NO_SYMBOLS);
  120. goto done;
  121. }
  122. /* Can't use NULL as the reflective symbol header, as NULL is
  123. used to mark the end of the entire symbol list. Self-dlpreopened
  124. symbols follow this magic number, chosen to be an unlikely
  125. clash with a real module name. */
  126. if (!filename)
  127. {
  128. filename = "@PROGRAM@";
  129. }
  130. for (lists = preloaded_symlists; lists; lists = lists->next)
  131. {
  132. const lt_dlsymlist *symbol;
  133. for (symbol= lists->symlist; symbol->name; ++symbol)
  134. {
  135. if (!symbol->address && STREQ (symbol->name, filename))
  136. {
  137. /* If the next symbol's name and address is 0, it means
  138. the module just contains the originator and no symbols.
  139. In this case we pretend that we never saw the module and
  140. hope that some other loader will be able to load the module
  141. and have access to its symbols */
  142. const lt_dlsymlist *next_symbol = symbol +1;
  143. if (next_symbol->address && next_symbol->name)
  144. {
  145. module = (lt_module) lists->symlist;
  146. goto done;
  147. }
  148. }
  149. }
  150. }
  151. LT__SETERROR (FILE_NOT_FOUND);
  152. done:
  153. return module;
  154. }
  155. /* A function called through the vtable when a particular module
  156. should be unloaded. */
  157. static int
  158. vm_close (lt_user_data loader_data LT__UNUSED, lt_module module LT__UNUSED)
  159. {
  160. /* Just to silence gcc -Wall */
  161. module = 0;
  162. return 0;
  163. }
  164. /* A function called through the vtable to get the address of
  165. a symbol loaded from a particular module. */
  166. static void *
  167. vm_sym (lt_user_data loader_data LT__UNUSED, lt_module module, const char *name)
  168. {
  169. lt_dlsymlist *symbol = (lt_dlsymlist*) module;
  170. if (symbol[1].name && STREQ (symbol[1].name, "@INIT@"))
  171. {
  172. symbol++; /* Skip optional init entry. */
  173. }
  174. symbol +=2; /* Skip header (originator then libname). */
  175. while (symbol->name)
  176. {
  177. if (STREQ (symbol->name, name))
  178. {
  179. return symbol->address;
  180. }
  181. ++symbol;
  182. }
  183. LT__SETERROR (SYMBOL_NOT_FOUND);
  184. return 0;
  185. }
  186. /* --- HELPER FUNCTIONS --- */
  187. /* The symbol lists themselves are not allocated from the heap, but
  188. we can unhook them and free up the chain of links between them. */
  189. static int
  190. free_symlists (void)
  191. {
  192. symlist_chain *lists;
  193. lists = preloaded_symlists;
  194. while (lists)
  195. {
  196. symlist_chain *next = lists->next;
  197. FREE (lists);
  198. lists = next;
  199. }
  200. preloaded_symlists = 0;
  201. return 0;
  202. }
  203. /* Add a new symbol list to the global chain. */
  204. static int
  205. add_symlist (const lt_dlsymlist *symlist)
  206. {
  207. symlist_chain *lists;
  208. int errors = 0;
  209. /* Search for duplicate entries: */
  210. for (lists = preloaded_symlists;
  211. lists && lists->symlist != symlist; lists = lists->next)
  212. /*NOWORK*/;
  213. /* Don't add the same list twice: */
  214. if (!lists)
  215. {
  216. symlist_chain *tmp = (symlist_chain *) lt__zalloc (sizeof *tmp);
  217. if (tmp)
  218. {
  219. tmp->symlist = symlist;
  220. tmp->next = preloaded_symlists;
  221. preloaded_symlists = tmp;
  222. if (symlist[1].name && STREQ (symlist[1].name, "@INIT@"))
  223. {
  224. void (*init_symlist)(void);
  225. *(void **)(&init_symlist) = symlist[1].address;
  226. (*init_symlist)();
  227. }
  228. }
  229. else
  230. {
  231. ++errors;
  232. }
  233. }
  234. return errors;
  235. }
  236. /* --- PRELOADING API CALL IMPLEMENTATIONS --- */
  237. /* Save a default symbol list for later. */
  238. int
  239. lt_dlpreload_default (const lt_dlsymlist *preloaded)
  240. {
  241. default_preloaded_symbols = preloaded;
  242. return 0;
  243. }
  244. /* Add a symbol list to the global chain, or with a NULL argument,
  245. revert to just the default list. */
  246. int
  247. lt_dlpreload (const lt_dlsymlist *preloaded)
  248. {
  249. int errors = 0;
  250. if (preloaded)
  251. {
  252. errors = add_symlist (preloaded);
  253. }
  254. else
  255. {
  256. free_symlists();
  257. if (default_preloaded_symbols)
  258. {
  259. errors = lt_dlpreload (default_preloaded_symbols);
  260. }
  261. }
  262. return errors;
  263. }
  264. /* Open all the preloaded modules from the named originator, executing
  265. a callback for each one. If ORIGINATOR is NULL, then call FUNC for
  266. each preloaded module from the program itself. */
  267. int
  268. lt_dlpreload_open (const char *originator, lt_dlpreload_callback_func *func)
  269. {
  270. symlist_chain *list;
  271. int errors = 0;
  272. int found = 0;
  273. /* For each symlist in the chain... */
  274. for (list = preloaded_symlists; list; list = list->next)
  275. {
  276. /* ...that was preloaded by the requesting ORIGINATOR... */
  277. if ((originator && STREQ (list->symlist->name, originator))
  278. || (!originator && STREQ (list->symlist->name, "@PROGRAM@")))
  279. {
  280. const lt_dlsymlist *symbol;
  281. unsigned int idx = 0;
  282. ++found;
  283. /* ...load the symbols per source compilation unit:
  284. (we preincrement the index to skip over the originator entry) */
  285. while ((symbol = &list->symlist[++idx])->name != 0)
  286. {
  287. if ((symbol->address == 0)
  288. && (STRNEQ (symbol->name, "@PROGRAM@")))
  289. {
  290. lt_dlhandle handle = lt_dlopen (symbol->name);
  291. if (handle == 0)
  292. {
  293. ++errors;
  294. }
  295. else
  296. {
  297. errors += (*func) (handle);
  298. }
  299. }
  300. }
  301. }
  302. }
  303. if (!found)
  304. {
  305. LT__SETERROR(CANNOT_OPEN);
  306. ++errors;
  307. }
  308. return errors;
  309. }