Unix.HostLibs.Mod 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (**
  2. AUTHOR: Alexey Morozov
  3. PURPOSE: Dynamic library tools for Unix platforms
  4. *)
  5. MODULE HostLibs;
  6. IMPORT
  7. SYSTEM, Unix;
  8. TYPE
  9. LibHandle* = ADDRESS; (** dynamic library handle type *)
  10. CONST
  11. NilLibHandle* = NIL; (** invalid library handle *)
  12. (*
  13. dlopen flags
  14. #define RTLD_LAZY 0x0001
  15. #define RTLD_NOW 0x0002
  16. #define RTLD_GLOBAL 0x0100
  17. #define RTLD_LOCAL 0x0000
  18. #define RTLD_NOSHARE 0x1000
  19. #define RTLD_EXE 0x2000
  20. #define RTLD_SCRIPT 0x4000
  21. *)
  22. (**
  23. Load a dynamic library
  24. fileName: library file name
  25. lib: returned loaded library handle; NilLibHandle in case of an error
  26. Return: TRUE in case of success
  27. *)
  28. PROCEDURE LoadLibrary*(CONST fileName: ARRAY OF CHAR; VAR lib: LibHandle): BOOLEAN;
  29. BEGIN
  30. lib := Unix.Dlopen(fileName, 0x0001); (* RTLD_LAZY: use lazy binding - resolve symbols only at the user request *)
  31. RETURN (lib # NilLibHandle);
  32. FINALLY
  33. RETURN FALSE;
  34. END LoadLibrary;
  35. (**
  36. Free a previously loaded dynamic library
  37. lib: library handle
  38. Return: TRUE in case of success
  39. *)
  40. PROCEDURE FreeLibrary*(CONST lib: LibHandle): BOOLEAN;
  41. BEGIN
  42. IF lib # NIL THEN
  43. Unix.Dlclose(lib);
  44. RETURN TRUE;
  45. END;
  46. FINALLY
  47. RETURN FALSE;
  48. END FreeLibrary;
  49. (**
  50. Get a procedure from a loaded dynamic library
  51. lib: library handle
  52. name: name of the procedure
  53. procAddr: address of the destination procedure pointer (e.g. ADDRESSOF(procedureVariable))
  54. Return: TRUE in case of success
  55. *)
  56. PROCEDURE GetProcedure*(CONST lib: LibHandle; CONST name: ARRAY OF CHAR; CONST procAddr: ADDRESS): BOOLEAN;
  57. VAR addr: ADDRESS;
  58. BEGIN
  59. ASSERT(procAddr # NIL);
  60. Unix.Dlsym(lib,name,procAddr);
  61. SYSTEM.GET(procAddr,addr);
  62. RETURN addr # NIL;
  63. FINALLY
  64. RETURN FALSE;
  65. END GetProcedure;
  66. END HostLibs.