dl.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. {$ifndef SYMOBI}
  55. function dladdr(Lib: pointer; info: Pdl_info): Longint; cdecl; external;
  56. {$endif}
  57. implementation
  58. function PosLastSlash(const s : string) : longint;
  59. var
  60. i : longint;
  61. begin
  62. PosLastSlash:=0;
  63. for i:=1 to length(s) do
  64. if s[i]='/' then
  65. PosLastSlash:=i;
  66. end;
  67. function SimpleExtractFilename(const s : string) : string;
  68. begin
  69. SimpleExtractFilename:=Copy(s,PosLastSlash(s)+1,Length(s)-PosLastSlash(s));
  70. end;
  71. {$ifndef SYMOBI}
  72. procedure UnixGetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: openstring);
  73. var
  74. dlinfo: dl_info;
  75. begin
  76. baseaddr:=nil;
  77. FillChar(dlinfo, sizeof(dlinfo), 0);
  78. dladdr(addr, @dlinfo);
  79. baseaddr:=dlinfo.dli_fbase;
  80. filename:=String(dlinfo.dli_fname);
  81. if SimpleExtractFilename(filename)=SimpleExtractFilename(ParamStr(0)) then
  82. baseaddr:=nil;
  83. end;
  84. {$endif}
  85. begin
  86. {$ifndef SYMOBI}
  87. UnixGetModuleByAddrHook:=@UnixGetModuleByAddr;
  88. {$endif}
  89. end.