dlfcn.h 5.6 KB

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