dl.pp 2.6 KB

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