dlandroid.inc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // On Android the dladdr() function does not return full path to modules.
  2. // Emulate dladdr() by reading the /proc/self/maps to get full path to modules.
  3. threadvar
  4. _ModuleName: ansistring;
  5. function dladdr(Lib: pointer; info: Pdl_info): Longint; cdecl;
  6. var
  7. F: Text;
  8. s, ss, curnode: ansistring;
  9. a1, a2, curbase: ptruint;
  10. i: longint;
  11. p, pp: PAnsiChar;
  12. begin
  13. {$PUSH}
  14. {$I-}
  15. dladdr:=0;
  16. _ModuleName:='';
  17. if info = nil then
  18. exit;
  19. curbase:=0;
  20. curnode:='';
  21. Assign(F, '/proc/self/maps');
  22. Reset(F);
  23. if IoResult <> 0 then
  24. exit;
  25. while not Eof(F) do
  26. begin
  27. // Read the address range info
  28. ReadLn(F, ss);
  29. p:=PAnsiChar(ss);
  30. // Starting address
  31. pp:=p;
  32. while not (p^ in ['-', #0]) do
  33. Inc(p);
  34. SetString(s, pp, p - pp);
  35. Val('$' + s, a1, i);
  36. if i = 0 then
  37. begin
  38. // Ending address
  39. Inc(p);
  40. pp:=p;
  41. while p^ > ' ' do
  42. Inc(p);
  43. SetString(s, pp, p - pp);
  44. Val('$' + s, a2, i);
  45. if i = 0 then
  46. begin
  47. while p^ <= ' ' do Inc(p); // Whitespace
  48. while p^ > ' ' do Inc(p); // Skip perms
  49. while p^ <= ' ' do Inc(p); // Whitespace
  50. while p^ > ' ' do Inc(p); // Skip offset
  51. while p^ <= ' ' do Inc(p); // Whitespace
  52. while p^ > ' ' do Inc(p); // Skip dev
  53. while p^ <= ' ' do Inc(p); // Whitespace
  54. // inode
  55. pp:=p;
  56. while p^ > ' ' do
  57. Inc(p);
  58. SetString(s, pp, p - pp);
  59. if s <> '0' then
  60. begin
  61. if s <> curnode then
  62. begin
  63. curnode:=s;
  64. curbase:=a1;
  65. end;
  66. if (ptruint(Lib) >= a1) and (ptruint(Lib) < a2) then
  67. begin
  68. while p^ <= ' ' do Inc(p); // Whitespace
  69. // File name
  70. if p^ = '/' then
  71. begin
  72. _ModuleName:=p;
  73. info^.dli_fname:=PAnsiChar(_ModuleName);
  74. info^.dli_fbase:=pointer(curbase);
  75. info^.dli_sname:=nil;
  76. info^.dli_saddr:=nil;
  77. dladdr:=1;
  78. end;
  79. break;
  80. end;
  81. end;
  82. end;
  83. end;
  84. end;
  85. Close(F);
  86. {$POP}
  87. end;