dl.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. {$IFNDEF FPC_DOTTEDUNITS}
  12. unit dl;
  13. {$ENDIF FPC_DOTTEDUNITS}
  14. interface
  15. const
  16. {$ifdef BSD} // dlopen is in libc on FreeBSD.
  17. LibDL = 'c';
  18. {$else}
  19. {$ifdef HAIKU}
  20. LibDL = 'root';
  21. {$else}
  22. LibDL = 'dl';
  23. {$endif}
  24. {$endif}
  25. {$if defined(linux) or defined(freebsd) or defined(openbsd) or defined(dragonfly)}
  26. {$define ELF} // ELF symbol versioning.
  27. {$endif}
  28. {$if defined(linux)}
  29. { if libc is not linked explicitly, FPC might chose the wrong startup code, as
  30. libdl depends on libc on linux, this does not hurt }
  31. {$linklib c}
  32. {$endif}
  33. {$ifdef aix}
  34. RTLD_LAZY = $004;
  35. RTLD_NOW = $002;
  36. RTLD_BINDING_MASK = $006;
  37. RTLD_LOCAL = 0;
  38. RTLD_GLOBAL = $10000;
  39. RTLD_MEMBER = $40000;
  40. RTLD_NEXT = pointer(-3);
  41. RTLD_DEFAULT = pointer(-1);
  42. {$else}
  43. RTLD_LAZY = $001;
  44. RTLD_NOW = $002;
  45. RTLD_BINDING_MASK = $003;
  46. {$ifdef DARWIN}
  47. RTLD_LOCAL = $004;
  48. RTLD_GLOBAL = $008;
  49. RTLD_NOLOAD = $010;
  50. RTLD_NODELETE = $080;
  51. RTLD_FIRST = $100;
  52. RTLD_NEXT = pointer(-1);
  53. RTLD_DEFAULT = pointer(-2);
  54. RTLD_SELF = pointer(-3);
  55. RTLD_MAIN_ONLY = pointer(-5);
  56. {$else}
  57. RTLD_LOCAL = 0;
  58. RTLD_GLOBAL = $100;
  59. RTLD_NEXT = pointer(-1);
  60. {$ifdef LINUX}
  61. RTLD_DEFAULT = nil;
  62. RTLD_NOLOAD = $00004; // GLIBC 2.2 and above
  63. RTLD_DI_LINKMAP = 2;
  64. {$endif}
  65. {$ifdef BSD}
  66. RTLD_DEFAULT = pointer(-2);
  67. RTLD_MODEMASK = RTLD_BINDING_MASK;
  68. {$endif}
  69. {$endif} // DARWIN
  70. {$endif}
  71. type
  72. Pdl_info = ^dl_info;
  73. dl_info = record
  74. dli_fname : PAnsiChar;
  75. dli_fbase : pointer;
  76. dli_sname : PAnsiChar;
  77. dli_saddr : pointer;
  78. end;
  79. function dlopen(Name : PAnsiChar; Flags : longint) : Pointer; cdecl; external libdl;
  80. function dlsym(Lib : Pointer; Name : PAnsiChar) : Pointer; cdecl; external Libdl;
  81. {$ifdef ELF}
  82. function dlvsym(Lib : Pointer; Name : PAnsiChar; Version: PAnsiChar) : Pointer; cdecl; external Libdl;
  83. {$endif}
  84. function dlclose(Lib : Pointer) : Longint; cdecl; external libdl;
  85. function dlerror() : PAnsiChar; cdecl; external libdl;
  86. { overloaded for compatibility with hmodule }
  87. function dlsym(Lib : PtrInt; Name : PAnsiChar) : Pointer; cdecl; external Libdl;
  88. function dlclose(Lib : PtrInt) : Longint; cdecl; external libdl;
  89. function dladdr(Lib: pointer; info: Pdl_info): Longint; cdecl; {$if not defined(aix) and not defined(android)} external; {$endif}
  90. type
  91. plink_map = ^link_map;
  92. link_map = record
  93. l_addr:pointer; { Difference between the address in the ELF file and the address in memory }
  94. l_name:PAnsiChar; { Absolute pathname where object was found }
  95. l_ld:pointer; { Dynamic section of the shared object }
  96. l_next, l_prev:^link_map; { Chain of loaded objects }
  97. {Plus additional fields private to the implementation }
  98. end;
  99. {$if defined(BSD) or defined(LINUX)}
  100. function dlinfo(Lib:pointer;request:longint;info:pointer):longint;cdecl;external Libdl;
  101. {$else}
  102. { Fortunately investigating the sources of open source projects brought the understanding, that
  103. `handle` is just a `struct link_map*` that contains full library name.}
  104. {$endif}
  105. implementation
  106. {$IFDEF FPC_DOTTEDUNITS}
  107. uses
  108. System.CTypes;
  109. {$ELSE FPC_DOTTEDUNITS}
  110. uses
  111. ctypes;
  112. {$ENDIF FPC_DOTTEDUNITS}
  113. function PosLastSlash(const s : string) : longint;
  114. var
  115. i : longint;
  116. begin
  117. PosLastSlash:=0;
  118. for i:=1 to length(s) do
  119. if s[i]='/' then
  120. PosLastSlash:=i;
  121. end;
  122. function SimpleExtractFilename(const s : string) : string;
  123. begin
  124. SimpleExtractFilename:=Copy(s,PosLastSlash(s)+1,Length(s)-PosLastSlash(s));
  125. end;
  126. procedure UnixGetModuleByAddr(addr: pointer; var baseaddr: pointer; var filename: ansistring);
  127. var
  128. dlinfo: dl_info;
  129. begin
  130. baseaddr:=nil;
  131. FillChar(dlinfo, sizeof(dlinfo), 0);
  132. dladdr(addr, @dlinfo);
  133. baseaddr:=dlinfo.dli_fbase;
  134. filename:=String(dlinfo.dli_fname);
  135. end;
  136. {$ifdef aix}
  137. {$i dlaix.inc}
  138. {$endif}
  139. {$ifdef android}
  140. {$i dlandroid.inc}
  141. {$endif}
  142. begin
  143. UnixGetModuleByAddrHook:=@UnixGetModuleByAddr;
  144. end.