dl.pp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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) and defined(cpuarm)}
  27. { arm-linux seems to require this }
  28. {$linklib c}
  29. {$endif}
  30. {$ifdef aix}
  31. RTLD_LAZY = $004;
  32. RTLD_NOW = $002;
  33. RTLD_BINDING_MASK = $006;
  34. RTLD_LOCAL = 0;
  35. RTLD_GLOBAL = $10000;
  36. RTLD_MEMBER = $40000;
  37. RTLD_NEXT = pointer(-3);
  38. RTLD_DEFAULT = pointer(-1);
  39. {$else}
  40. RTLD_LAZY = $001;
  41. RTLD_NOW = $002;
  42. RTLD_BINDING_MASK = $003;
  43. {$ifdef DARWIN}
  44. RTLD_LOCAL = $004;
  45. RTLD_GLOBAL = $008;
  46. RTLD_NOLOAD = $010;
  47. RTLD_NODELETE = $080;
  48. RTLD_FIRST = $100;
  49. RTLD_NEXT = pointer(-1);
  50. RTLD_DEFAULT = pointer(-2);
  51. RTLD_SELF = pointer(-3);
  52. RTLD_MAIN_ONLY = pointer(-5);
  53. {$else}
  54. RTLD_LOCAL = 0;
  55. RTLD_GLOBAL = $100;
  56. RTLD_NEXT = pointer(-1);
  57. {$ifdef LINUX}
  58. RTLD_DEFAULT = nil;
  59. {$endif}
  60. {$ifdef BSD}
  61. RTLD_DEFAULT = pointer(-2);
  62. RTLD_MODEMASK = RTLD_BINDING_MASK;
  63. {$endif}
  64. {$endif} // DARWIN
  65. {$endif}
  66. type
  67. Pdl_info = ^dl_info;
  68. dl_info = record
  69. dli_fname : Pchar;
  70. dli_fbase : pointer;
  71. dli_sname : Pchar;
  72. dli_saddr : pointer;
  73. end;
  74. function dlopen(Name : PChar; Flags : longint) : Pointer; cdecl; external libdl;
  75. function dlsym(Lib : Pointer; Name : Pchar) : Pointer; cdecl; external Libdl;
  76. {$ifdef ELF}
  77. function dlvsym(Lib : Pointer; Name : Pchar; Version: Pchar) : Pointer; cdecl; external Libdl;
  78. {$endif}
  79. function dlclose(Lib : Pointer) : Longint; cdecl; external libdl;
  80. function dlerror() : Pchar; cdecl; external libdl;
  81. { overloaded for compatibility with hmodule }
  82. function dlsym(Lib : PtrInt; Name : Pchar) : Pointer; cdecl; external Libdl;
  83. function dlclose(Lib : PtrInt) : Longint; cdecl; external libdl;
  84. function dladdr(Lib: pointer; info: Pdl_info): Longint; cdecl; {$if not defined(aix) and not defined(android)} external; {$endif}
  85. implementation
  86. uses
  87. ctypes;
  88. function PosLastSlash(const s : string) : longint;
  89. var
  90. i : longint;
  91. begin
  92. PosLastSlash:=0;
  93. for i:=1 to length(s) do
  94. if s[i]='/' then
  95. PosLastSlash:=i;
  96. end;
  97. function SimpleExtractFilename(const s : string) : string;
  98. begin
  99. SimpleExtractFilename:=Copy(s,PosLastSlash(s)+1,Length(s)-PosLastSlash(s));
  100. end;
  101. procedure UnixGetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: openstring);
  102. var
  103. dlinfo: dl_info;
  104. begin
  105. baseaddr:=nil;
  106. FillChar(dlinfo, sizeof(dlinfo), 0);
  107. dladdr(addr, @dlinfo);
  108. baseaddr:=dlinfo.dli_fbase;
  109. filename:=String(dlinfo.dli_fname);
  110. if SimpleExtractFilename(filename)=SimpleExtractFilename(ParamStr(0)) then
  111. baseaddr:=nil;
  112. end;
  113. {$ifdef aix}
  114. {$i dlaix.inc}
  115. {$endif}
  116. {$ifdef android}
  117. {$i dlandroid.inc}
  118. {$endif}
  119. begin
  120. UnixGetModuleByAddrHook:=@UnixGetModuleByAddr;
  121. end.