1
0

dlfcn.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. dlfcn.h
  8. Abstract:
  9. This header contains definitions for loading dynamic libraries at runtime.
  10. Author:
  11. Evan Green 17-Oct-2013
  12. --*/
  13. #ifndef _DLFCN_H
  14. #define _DLFCN_H
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <libcbase.h>
  19. //
  20. // ---------------------------------------------------------------- Definitions
  21. //
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. //
  26. // Define flags that can be passed to the dlopen function.
  27. //
  28. //
  29. // Set this flag to have relocations performed on an as-needed basis.
  30. //
  31. #define RTLD_LAZY 0x00000000
  32. //
  33. // Set this flag to have relocations performed when an object is loaded.
  34. //
  35. #define RTLD_NOW 0x00000080
  36. //
  37. // Set this flag to have all symbols be available to other modules for
  38. // dynamic linking.
  39. //
  40. #define RTLD_GLOBAL 0x00000100
  41. //
  42. // Set this flag to prevent symbols from being available to other modules for
  43. // dynamic linking.
  44. //
  45. #define RTLD_LOCAL 0x00000000
  46. //
  47. // Provide this handle to search for symbols in the executing program's global
  48. // scope.
  49. //
  50. #define RTLD_DEFAULT ((void *)0)
  51. //
  52. // Provide this handle to search for symbols in the executable after the
  53. // currently executing program. "Next" is defined in terms of load order.
  54. //
  55. #define RTLD_NEXT ((void *)-1)
  56. //
  57. // ------------------------------------------------------ Data Type Definitions
  58. //
  59. /*++
  60. Structure Description:
  61. This structure defines dynamic library information for an address.
  62. Members:
  63. dli_fname - Stores the path name of the library that stores the address.
  64. dli_fbase - Stores the base address at which the library is loaded.
  65. dli_sname - Stores the name of the symbol that contains the address.
  66. dli_saddr - Stores the address of the symbol. This may differ from the
  67. address used to look up the symbol.
  68. --*/
  69. typedef struct {
  70. const char *dli_fname;
  71. void *dli_fbase;
  72. const char *dli_sname;
  73. void *dli_saddr;
  74. } Dl_info;
  75. //
  76. // -------------------------------------------------------------------- Globals
  77. //
  78. //
  79. // -------------------------------------------------------- Function Prototypes
  80. //
  81. LIBC_API
  82. void *
  83. dlopen (
  84. const char *Library,
  85. int Flags
  86. );
  87. /*++
  88. Routine Description:
  89. This routine opens and loads a dynamic library object with the given name.
  90. Only one instance of a given binary will be loaded per process.
  91. Arguments:
  92. Library - Supplies a pointer to the null terminated string of the library
  93. to open. Supply NULL to open a handle to a global symbol table.
  94. Flags - Supplies a bitfield of flags governing the behavior of the library.
  95. See RTLD_* definitions.
  96. Return Value:
  97. Returns an opaque handle to the library that can be used in calls to dlsym.
  98. NULL on failure, and more information can be retrieved via the dlerror
  99. function.
  100. --*/
  101. LIBC_API
  102. int
  103. dlclose (
  104. void *Handle
  105. );
  106. /*++
  107. Routine Description:
  108. This routine closes a previously opened dynamic library. This may or may
  109. not result in the library being unloaded, depending on what else has
  110. references out on it. Either way, callers should assume the handle is
  111. not valid for any future calls to the dlsym function.
  112. Arguments:
  113. Handle - Supplies the opaque handle returned when the library was opened.
  114. Return Value:
  115. 0 on success.
  116. Non-zero on failure, and dlerror will be set to contain more information.
  117. --*/
  118. LIBC_API
  119. char *
  120. dlerror (
  121. void
  122. );
  123. /*++
  124. Routine Description:
  125. This routine returns a null terminated string (with no trailing newline)
  126. that describes the last error that occurred during dynamic linking
  127. processing. If no errors have occurred since the last invocation, NULL is
  128. returned. Invoking this routine a second time immediately following a prior
  129. invocation will return NULL. This routine is neither thread-safe nor
  130. reentrant.
  131. Arguments:
  132. None.
  133. Return Value:
  134. Returns a pointer to a string describing the last error on success. The
  135. caller is not responsible for freeing this memory.
  136. NULL if nothing went wrong since the last invocation.
  137. --*/
  138. __HIDDEN
  139. void *
  140. dlsym (
  141. void *Handle,
  142. const char *SymbolName
  143. );
  144. /*++
  145. Routine Description:
  146. This routine returns the address of a symbol defined within an object made
  147. accessible through a call to dlopen. This routine searches both this object
  148. and any objects loaded as a result of this one.
  149. Arguments:
  150. Handle - Supplies a pointer to the opaque handle returned by the dlopen
  151. routine. Additionally, supply RTLD_DEFAULT to search through the
  152. executable (global) scope. Supply RTLD_NEXT to search for the next
  153. instance of the symbol after instance defined in the module that
  154. called dlsym.
  155. SymbolName - Supplies a pointer to a null-terminated string containing the
  156. name of the symbol whose address should be retrieved.
  157. Return Value:
  158. Returns the address of the symbol on success.
  159. NULL if the handle was not valid or the symbol could not be found. More
  160. information can be retrieved via the dlerror function.
  161. --*/
  162. LIBC_API
  163. void *
  164. __dlsym (
  165. void *Handle,
  166. const char *SymbolName,
  167. void *CallerAddress
  168. );
  169. /*++
  170. Routine Description:
  171. This routine returns the address of a symbol defined within an object made
  172. accessible through a call to dlopen. This is an internal routine that
  173. should not be called directly by users.
  174. Arguments:
  175. Handle - Supplies a pointer to the opaque handle returned by the dlopen
  176. routine.
  177. SymbolName - Supplies a pointer to a null-terminated string containing the
  178. name of the symbol whose address should be retrieved.
  179. CallerAddress - Supplies an address within the dynamic object of the
  180. calling executable. This routine will use this address to determine
  181. which object to start from and skip if RTLD_NEXT is provided as the
  182. handle.
  183. Return Value:
  184. Returns the address of the symbol on success.
  185. NULL if the handle was not valid or the symbol could not be found. More
  186. information can be retrieved via the dlerror function.
  187. --*/
  188. LIBC_API
  189. int
  190. dladdr (
  191. void *Address,
  192. Dl_info *Information
  193. );
  194. /*++
  195. Routine Description:
  196. This routine resolves an address into the symbol and dynamic library
  197. information.
  198. Arguments:
  199. Address - Supplies the address being resolved.
  200. Information - Supplies a pointer that recevies that dynamic library
  201. information for the given address.
  202. Return Value:
  203. Non-zero on success.
  204. 0 on failure, but dlerror will not be set to contain more information.
  205. --*/
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif