load_add_on.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* loader-load_add_on.c -- dynamic linking for BeOS
  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 load_add_on_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_exit (lt_user_data loader_data);
  38. static lt_module vm_open (lt_user_data loader_data, const char *filename,
  39. lt_dladvise advise);
  40. static int vm_close (lt_user_data loader_data, lt_module module);
  41. static void * vm_sym (lt_user_data loader_data, lt_module module,
  42. const char *symbolname);
  43. static lt_dlvtable *vtable = 0;
  44. /* Return the vtable for this loader, only the name and sym_prefix
  45. attributes (plus the virtual function implementations, obviously)
  46. change between loaders. */
  47. lt_dlvtable *
  48. get_vtable (lt_user_data loader_data)
  49. {
  50. if (!vtable)
  51. {
  52. vtable = lt__zalloc (sizeof *vtable);
  53. }
  54. if (vtable && !vtable->name)
  55. {
  56. vtable->name = "lt_load_add_on";
  57. vtable->module_open = vm_open;
  58. vtable->module_close = vm_close;
  59. vtable->find_sym = vm_sym;
  60. vtable->dlloader_exit = vl_exit;
  61. vtable->dlloader_data = loader_data;
  62. vtable->priority = LT_DLLOADER_APPEND;
  63. }
  64. if (vtable && (vtable->dlloader_data != loader_data))
  65. {
  66. LT__SETERROR (INIT_LOADER);
  67. return 0;
  68. }
  69. return vtable;
  70. }
  71. /* --- IMPLEMENTATION --- */
  72. #include <kernel/image.h>
  73. /* A function called through the vtable when this loader is no
  74. longer needed by the application. */
  75. static int
  76. vl_exit (lt_user_data loader_data LT__UNUSED)
  77. {
  78. vtable = NULL;
  79. return 0;
  80. }
  81. /* A function called through the vtable to open a module with this
  82. loader. Returns an opaque representation of the newly opened
  83. module for processing with this loader's other vtable functions. */
  84. static lt_module
  85. vm_open (lt_user_data loader_data LT__UNUSED, const char *filename,
  86. lt_dladvise advise LT__UNUSED)
  87. {
  88. image_id image = 0;
  89. if (filename)
  90. {
  91. image = load_add_on (filename);
  92. }
  93. else
  94. {
  95. image_info info;
  96. int32 cookie = 0;
  97. if (get_next_image_info (0, &cookie, &info) == B_OK)
  98. image = load_add_on (info.name);
  99. }
  100. if (image <= 0)
  101. {
  102. LT__SETERROR (CANNOT_OPEN);
  103. image = 0;
  104. }
  105. return (lt_module) image;
  106. }
  107. /* A function called through the vtable when a particular module
  108. should be unloaded. */
  109. static int
  110. vm_close (lt_user_data loader_data LT__UNUSED, lt_module module)
  111. {
  112. int errors = 0;
  113. if (unload_add_on ((image_id) module) != B_OK)
  114. {
  115. LT__SETERROR (CANNOT_CLOSE);
  116. ++errors;
  117. }
  118. return errors;
  119. }
  120. /* A function called through the vtable to get the address of
  121. a symbol loaded from a particular module. */
  122. static void *
  123. vm_sym (lt_user_data loader_data LT__UNUSED, lt_module module, const char *name)
  124. {
  125. void *address = 0;
  126. image_id image = (image_id) module;
  127. if (get_image_symbol (image, name, B_SYMBOL_TYPE_ANY, address) != B_OK)
  128. {
  129. LT__SETERROR (SYMBOL_NOT_FOUND);
  130. address = 0;
  131. }
  132. return address;
  133. }