dl.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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) and defined(cpuarm)}
  24. { arm-linux seems to require this }
  25. {$linklib c}
  26. {$endif}
  27. RTLD_LAZY = $001;
  28. RTLD_NOW = $002;
  29. RTLD_BINDING_MASK = $003;
  30. RTLD_GLOBAL = $100;
  31. RTLD_NEXT = pointer(-1);
  32. {$ifdef LINUX}
  33. RTLD_DEFAULT = nil;
  34. {$endif}
  35. {$ifdef BSD}
  36. RTLD_DEFAULT = pointer(-2);
  37. RTLD_MODEMASK = RTLD_BINDING_MASK;
  38. {$endif}
  39. type
  40. Pdl_info = ^dl_info;
  41. dl_info = record
  42. dli_fname : Pchar;
  43. dli_fbase : pointer;
  44. dli_sname : Pchar;
  45. dli_saddr : pointer;
  46. end;
  47. function dlopen(Name : PChar; Flags : longint) : Pointer; cdecl; external libdl;
  48. function dlsym(Lib : Pointer; Name : Pchar) : Pointer; cdecl; external Libdl;
  49. function dlclose(Lib : Pointer) : Longint; cdecl; external libdl;
  50. function dlerror() : Pchar; cdecl; external libdl;
  51. { overloaded for compatibility with hmodule }
  52. function dlsym(Lib : PtrInt; Name : Pchar) : Pointer; cdecl; external Libdl;
  53. function dlclose(Lib : PtrInt) : Longint; cdecl; external libdl;
  54. function dladdr(Lib: pointer; info: Pdl_info): Longint; cdecl; external;
  55. implementation
  56. function PosLastSlash(const s : string) : longint;
  57. var
  58. i : longint;
  59. begin
  60. PosLastSlash:=0;
  61. for i:=1 to length(s) do
  62. if s[i]='/' then
  63. PosLastSlash:=i;
  64. end;
  65. function SimpleExtractFilename(const s : string) : string;
  66. begin
  67. SimpleExtractFilename:=Copy(s,PosLastSlash(s)+1,Length(s)-PosLastSlash(s));
  68. end;
  69. procedure UnixGetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: openstring);
  70. var
  71. dlinfo: dl_info;
  72. begin
  73. baseaddr:=nil;
  74. FillChar(dlinfo, sizeof(dlinfo), 0);
  75. dladdr(addr, @dlinfo);
  76. baseaddr:=dlinfo.dli_fbase;
  77. filename:=String(dlinfo.dli_fname);
  78. if SimpleExtractFilename(filename)=SimpleExtractFilename(ParamStr(0)) then
  79. baseaddr:=nil;
  80. end;
  81. begin
  82. UnixGetModuleByAddrHook:=@UnixGetModuleByAddr;
  83. end.