dl.pp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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)}
  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_GLOBAL = $10000;
  35. RTLD_MEMBER = $40000;
  36. RTLD_NEXT = pointer(-3);
  37. RTLD_DEFAULT = pointer(-1);
  38. {$else}
  39. RTLD_LAZY = $001;
  40. RTLD_NOW = $002;
  41. RTLD_BINDING_MASK = $003;
  42. RTLD_GLOBAL = $100;
  43. RTLD_NEXT = pointer(-1);
  44. {$ifdef LINUX}
  45. RTLD_DEFAULT = nil;
  46. {$endif}
  47. {$ifdef BSD}
  48. RTLD_DEFAULT = pointer(-2);
  49. RTLD_MODEMASK = RTLD_BINDING_MASK;
  50. {$endif}
  51. {$endif}
  52. type
  53. Pdl_info = ^dl_info;
  54. dl_info = record
  55. dli_fname : Pchar;
  56. dli_fbase : pointer;
  57. dli_sname : Pchar;
  58. dli_saddr : pointer;
  59. end;
  60. function dlopen(Name : PChar; Flags : longint) : Pointer; cdecl; external libdl;
  61. function dlsym(Lib : Pointer; Name : Pchar) : Pointer; cdecl; external Libdl;
  62. {$ifdef ELF}
  63. function dlvsym(Lib : Pointer; Name : Pchar; Version: Pchar) : Pointer; cdecl; external Libdl;
  64. {$endif}
  65. function dlclose(Lib : Pointer) : Longint; cdecl; external libdl;
  66. function dlerror() : Pchar; cdecl; external libdl;
  67. { overloaded for compatibility with hmodule }
  68. function dlsym(Lib : PtrInt; Name : Pchar) : Pointer; cdecl; external Libdl;
  69. function dlclose(Lib : PtrInt) : Longint; cdecl; external libdl;
  70. function dladdr(Lib: pointer; info: Pdl_info): Longint; cdecl; {$ifndef aix}external;{$endif}
  71. implementation
  72. uses
  73. ctypes;
  74. function PosLastSlash(const s : string) : longint;
  75. var
  76. i : longint;
  77. begin
  78. PosLastSlash:=0;
  79. for i:=1 to length(s) do
  80. if s[i]='/' then
  81. PosLastSlash:=i;
  82. end;
  83. function SimpleExtractFilename(const s : string) : string;
  84. begin
  85. SimpleExtractFilename:=Copy(s,PosLastSlash(s)+1,Length(s)-PosLastSlash(s));
  86. end;
  87. procedure UnixGetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: openstring);
  88. var
  89. dlinfo: dl_info;
  90. begin
  91. baseaddr:=nil;
  92. FillChar(dlinfo, sizeof(dlinfo), 0);
  93. dladdr(addr, @dlinfo);
  94. baseaddr:=dlinfo.dli_fbase;
  95. filename:=String(dlinfo.dli_fname);
  96. if SimpleExtractFilename(filename)=SimpleExtractFilename(ParamStr(0)) then
  97. baseaddr:=nil;
  98. end;
  99. {$ifdef aix}
  100. {$i dlaix.inc}
  101. {$endif}
  102. begin
  103. UnixGetModuleByAddrHook:=@UnixGetModuleByAddr;
  104. end.