dl.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2008 by the Free Pascal development team
  4. This file implements dyn. lib calls calls for Unix
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit dl;
  12. interface
  13. const
  14. {$ifdef BSD} // dlopen is in libc on FreeBSD.
  15. LibDL = 'c';
  16. {$else}
  17. {$ifdef HAIKU}
  18. LibDL = 'root';
  19. {$else}
  20. LibDL = 'dl';
  21. {$endif}
  22. {$endif}
  23. {$if defined(linux) or defined(freebsd) or defined(openbsd) or defined(dragonfly)}
  24. {$define ELF} // ELF symbol versioning.
  25. {$endif}
  26. {$if defined(linux)}
  27. { if libc is not linked explicitly, FPC might chose the wrong startup code, as
  28. libdl depends on libc on linux, this does not hurt }
  29. {$linklib c}
  30. {$endif}
  31. {$ifdef aix}
  32. RTLD_LAZY = $004;
  33. RTLD_NOW = $002;
  34. RTLD_BINDING_MASK = $006;
  35. RTLD_LOCAL = 0;
  36. RTLD_GLOBAL = $10000;
  37. RTLD_MEMBER = $40000;
  38. RTLD_NEXT = pointer(-3);
  39. RTLD_DEFAULT = pointer(-1);
  40. {$else}
  41. RTLD_LAZY = $001;
  42. RTLD_NOW = $002;
  43. RTLD_BINDING_MASK = $003;
  44. {$ifdef DARWIN}
  45. RTLD_LOCAL = $004;
  46. RTLD_GLOBAL = $008;
  47. RTLD_NOLOAD = $010;
  48. RTLD_NODELETE = $080;
  49. RTLD_FIRST = $100;
  50. RTLD_NEXT = pointer(-1);
  51. RTLD_DEFAULT = pointer(-2);
  52. RTLD_SELF = pointer(-3);
  53. RTLD_MAIN_ONLY = pointer(-5);
  54. {$else}
  55. RTLD_LOCAL = 0;
  56. RTLD_GLOBAL = $100;
  57. RTLD_NEXT = pointer(-1);
  58. {$ifdef LINUX}
  59. RTLD_DEFAULT = nil;
  60. {$endif}
  61. {$ifdef BSD}
  62. RTLD_DEFAULT = pointer(-2);
  63. RTLD_MODEMASK = RTLD_BINDING_MASK;
  64. {$endif}
  65. {$endif} // DARWIN
  66. {$endif}
  67. type
  68. Pdl_info = ^dl_info;
  69. dl_info = record
  70. dli_fname : Pchar;
  71. dli_fbase : pointer;
  72. dli_sname : Pchar;
  73. dli_saddr : pointer;
  74. end;
  75. function dlopen(Name : PChar; Flags : longint) : Pointer; cdecl; external libdl;
  76. function dlsym(Lib : Pointer; Name : Pchar) : Pointer; cdecl; external Libdl;
  77. {$ifdef ELF}
  78. function dlvsym(Lib : Pointer; Name : Pchar; Version: Pchar) : Pointer; cdecl; external Libdl;
  79. {$endif}
  80. function dlclose(Lib : Pointer) : Longint; cdecl; external libdl;
  81. function dlerror() : Pchar; cdecl; external libdl;
  82. { overloaded for compatibility with hmodule }
  83. function dlsym(Lib : PtrInt; Name : Pchar) : Pointer; cdecl; external Libdl;
  84. function dlclose(Lib : PtrInt) : Longint; cdecl; external libdl;
  85. function dladdr(Lib: pointer; info: Pdl_info): Longint; cdecl; {$if not defined(aix) and not defined(android)} external; {$endif}
  86. implementation
  87. uses
  88. ctypes;
  89. function PosLastSlash(const s : string) : longint;
  90. var
  91. i : longint;
  92. begin
  93. PosLastSlash:=0;
  94. for i:=1 to length(s) do
  95. if s[i]='/' then
  96. PosLastSlash:=i;
  97. end;
  98. function SimpleExtractFilename(const s : string) : string;
  99. begin
  100. SimpleExtractFilename:=Copy(s,PosLastSlash(s)+1,Length(s)-PosLastSlash(s));
  101. end;
  102. procedure UnixGetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: openstring);
  103. var
  104. dlinfo: dl_info;
  105. begin
  106. baseaddr:=nil;
  107. FillChar(dlinfo, sizeof(dlinfo), 0);
  108. dladdr(addr, @dlinfo);
  109. baseaddr:=dlinfo.dli_fbase;
  110. filename:=String(dlinfo.dli_fname);
  111. {$ifdef darwin}
  112. if SimpleExtractFilename(filename)=SimpleExtractFilename(ParamStr(0)) then
  113. baseaddr:=nil;
  114. {$endif darwin}
  115. end;
  116. {$ifdef aix}
  117. {$i dlaix.inc}
  118. {$endif}
  119. {$ifdef android}
  120. {$i dlandroid.inc}
  121. {$endif}
  122. begin
  123. UnixGetModuleByAddrHook:=@UnixGetModuleByAddr;
  124. end.