/src/rt/image.d

http://github.com/AlexeyProkhin/druntime · D · 150 lines · 105 code · 34 blank · 11 comment · 13 complexity · a075df7bd46a6984f617c53e47288a08 MD5 · raw file

  1. /**
  2. * Copyright: Copyright Digital Mars 2010.
  3. * License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
  4. * Authors: Jacob Carlborg
  5. * Version: Initial created: Feb 23, 2010
  6. */
  7. /* Copyright Digital Mars 2010.
  8. * Distributed under the Boost Software License, Version 1.0.
  9. * (See accompanying file LICENSE or copy at
  10. * http://www.boost.org/LICENSE_1_0.txt)
  11. */
  12. module rt.image;
  13. version (OSX):
  14. import core.sys.osx.mach.dyld;
  15. import core.sys.osx.mach.loader;
  16. import core.sys.osx.mach.getsect;
  17. struct Image
  18. {
  19. private mach_header* header_;
  20. private uint index_;
  21. this (uint index)
  22. {
  23. header_ = _dyld_get_image_header(index);
  24. index_ = index;
  25. }
  26. static uint numberOfImages ()
  27. {
  28. return _dyld_image_count;
  29. }
  30. static int opApply (int delegate(ref Image) dg)
  31. {
  32. int result;
  33. for (uint i = 0; i < numberOfImages; i++)
  34. {
  35. auto image = Image(i);
  36. result = dg(image);
  37. if (result)
  38. break;
  39. }
  40. return result;
  41. }
  42. static int opApplyReverse (int delegate(ref Image) dg)
  43. {
  44. int result;
  45. for (int i = numberOfImages - 1; i >= 0; i--)
  46. {
  47. auto image = Image(i);
  48. result = dg(image);
  49. if (result)
  50. break;
  51. }
  52. return result;
  53. }
  54. mach_header* header ()
  55. {
  56. return header_;
  57. }
  58. mach_header_64* header64 ()
  59. {
  60. return cast(mach_header_64*) header_;
  61. }
  62. CPU cpu ()
  63. {
  64. return CPU(header_);
  65. }
  66. }
  67. struct CPU
  68. {
  69. private mach_header* header;
  70. static CPU opCall (mach_header* header)
  71. {
  72. CPU cpu;
  73. cpu.header = header;
  74. return cpu;
  75. }
  76. bool is32bit ()
  77. {
  78. return (header.magic == MH_MAGIC);
  79. }
  80. bool is64bit ()
  81. {
  82. return (header.magic == MH_MAGIC_64);
  83. }
  84. }
  85. T[] getSectionData (T, string segmentName, string sectionName) ()
  86. {
  87. T[] array;
  88. const c_segmentName = segmentName.ptr;
  89. const c_sectionName = sectionName.ptr;
  90. void* start;
  91. void* end;
  92. foreach_reverse (image ; Image)
  93. {
  94. if (image.cpu.is32bit)
  95. {
  96. auto header = image.header;
  97. section* sect = getsectbynamefromheader(header, c_segmentName, c_sectionName);
  98. if (sect is null || sect.size == 0)
  99. continue;
  100. start = cast(void*) (cast(byte*) header + sect.offset);
  101. end = cast(void*) (cast(byte*) start + sect.size);
  102. }
  103. else
  104. {
  105. auto header = image.header64;
  106. section_64* sect = getsectbynamefromheader_64(header, c_segmentName, c_sectionName);
  107. if (sect is null || sect.size == 0)
  108. continue;
  109. start = cast(void*) (cast(byte*) header + sect.offset);
  110. end = cast(void*) (cast(byte*) start + sect.size);
  111. }
  112. size_t len = cast(T*)end - cast(T*)start;
  113. array ~= (cast(T*)start)[0 .. len];
  114. }
  115. return array;
  116. }