dl.pp 4.4 KB

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