Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 10.27 KB | None | 0 0
  1. Index: src/rt/memory.d
  2. ===================================================================
  3. --- src/rt/memory.d (revision 423)
  4. +++ src/rt/memory.d (working copy)
  5. @@ -12,7 +12,6 @@
  6.   */
  7.  module rt.memory;
  8.  
  9. -
  10.  private
  11.  {
  12.      version( linux )
  13. @@ -174,10 +173,6 @@
  14.              alias __data_start  Data_Start;
  15.              alias _end          Data_End;
  16.      }
  17. -    else version( OSX )
  18. -    {
  19. -        extern (C) void _d_osx_image_init();
  20. -    }
  21.      else version( FreeBSD )
  22.      {
  23.          extern (C)
  24. @@ -230,3 +225,114 @@
  25.          static assert( false, "Operating system not supported." );
  26.      }
  27.  }
  28. +
  29. +version (OSX):
  30. +
  31. +import core.sys.osx.mach.dyld;
  32. +import core.sys.osx.mach.getsect;
  33. +import core.sys.osx.mach.loader;
  34. +import rt.deh2;
  35. +
  36. +struct SegmentSection
  37. +{
  38. +    const char* segment;
  39. +    const char* section;
  40. +}
  41. +
  42. +SegmentSection[5] dataSegments = [SegmentSection(SEG_DATA, SECT_DATA),
  43. +                                 SegmentSection(SEG_DATA, SECT_BSS),
  44. +                                 SegmentSection(SEG_DATA, SECT_COMMON),
  45. +                                 SegmentSection(SEG_DATA, "__tls_data".ptr),
  46. +                                 SegmentSection(SEG_DATA, "__tlscoal_nt".ptr)];
  47. +
  48. +extern (C) extern __gshared ModuleInfo*[] _moduleinfo_array;
  49. +extern (C) extern __gshared FuncTable[] _eh_tables;
  50. +
  51. +bool isSectionValid (const section* sect)
  52. +{
  53. +   return sect !is null && sect.size > 0;
  54. +}
  55. +
  56. +const(section*) getSection (const mach_header* header, in char* segment, in char* section)
  57. +{
  58. +   version (D_LP64)
  59. +       return getsectbynamefromheader_64(header, segment, section);
  60. +      
  61. +   else
  62. +       return getsectbynamefromheader(header, segment, section);
  63. +}
  64. +
  65. +void getSectionData (string segmentName, string sectionName, T) (const mach_header* header, ref T[] array)
  66. +{
  67. +    const c_segmentName = segmentName.ptr;
  68. +    const c_sectionName = sectionName.ptr;
  69. +
  70. +   const sect = getSection(header, c_segmentName, c_sectionName);
  71. +
  72. +   if (!isSectionValid(sect))
  73. +       return;
  74. +
  75. +   void* start = cast(void*) (cast(byte*) header + sect.offset);
  76. +    void* end = cast(void*) (cast(byte*) start + sect.size);
  77. +
  78. +    size_t len = cast(T*)end - cast(T*)start;
  79. +   array ~= (cast(T*)start)[0 .. len];
  80. +}
  81. +                                  
  82. +extern (C) void onAddImage (const mach_header* header, ptrdiff_t slide)
  83. +{
  84. +   foreach (s ;  dataSegments)
  85. +   {
  86. +       const sect = getSection(header, s.segment, s.section);
  87. +          
  88. +       if (!isSectionValid(sect))
  89. +           continue;
  90. +          
  91. +       gc_addRange(cast(void*) sect.addr + slide, sect.size);
  92. +   }
  93. +  
  94. +   addModuleInfo(header);
  95. +   addEHTables(header);
  96. +}
  97. +
  98. +extern (C) void onRemoveImage (const mach_header* header, ptrdiff_t slide)
  99. +{
  100. +   foreach (s ;  dataSegments)
  101. +   {
  102. +       const sect = getSection(header, s.segment, s.section);
  103. +          
  104. +       if (!isSectionValid(sect))
  105. +           continue;
  106. +          
  107. +       gc_removeRange(cast(void*) sect.addr + slide);
  108. +   }
  109. +  
  110. +   removeModuleInfo(header);
  111. +   removeEHTables(header);
  112. +}
  113. +
  114. +void addModuleInfo (const mach_header* header)
  115. +{
  116. +   getSectionData!("__DATA", "__minfodata")(header, _moduleinfo_array);
  117. +}
  118. +
  119. +void removeModuleInfo (const mach_header* header)
  120. +{
  121. +  
  122. +}
  123. +
  124. +void addEHTables (const mach_header* header)
  125. +{
  126. +   getSectionData!("__DATA", "__deh_eh")(header, _eh_tables);
  127. +}
  128. +
  129. +void removeEHTables (const mach_header* header)
  130. +{
  131. +  
  132. +}
  133. +
  134. +void _d_osx_image_init ()
  135. +{
  136. +    _dyld_register_func_for_add_image(&onAddImage);
  137. +   _dyld_register_func_for_remove_image(&onRemoveImage);
  138. +}
  139. \ No newline at end of file
  140. Index: src/rt/deh2.d
  141. ===================================================================
  142. --- src/rt/deh2.d   (revision 423)
  143. +++ src/rt/deh2.d   (working copy)
  144. @@ -15,11 +15,19 @@
  145.  //debug=1;
  146.  
  147.  extern (C)
  148. -{
  149. +{ 
  150. +   version (OSX)
  151. +       __gshared FuncTable[] _eh_tables;
  152. +      
  153.      extern __gshared
  154.      {
  155. -        void* _deh_beg;
  156. -        void* _deh_end;
  157. +       version (OSX) {}
  158. +  
  159. +       else
  160. +       {
  161. +           void* _deh_beg;
  162. +           void* _deh_end;
  163. +       }
  164.      }
  165.      
  166.      Throwable.TraceInfo _d_traceContext(void* ptr = null);
  167. @@ -94,25 +102,37 @@
  168.  
  169.  DHandlerTable *__eh_finddata(void *address)
  170.  {
  171. -    debug printf("FuncTable.sizeof = %p\n", FuncTable.sizeof);
  172. -    debug printf("__eh_finddata(address = %p)\n", address);
  173. -    debug printf("_deh_beg = %p, _deh_end = %p\n", &_deh_beg, &_deh_end);
  174. -    for (auto ft = cast(FuncTable *)&_deh_beg;
  175. -         ft < cast(FuncTable *)&_deh_end;
  176. -         ft++)
  177. -    {
  178. -      debug printf("\tft = %p, fptr = %p, fsize = x%03x, handlertable = %p\n",
  179. -              ft, ft.fptr, ft.fsize, ft.handlertable);
  180. +   version (OSX)
  181. +   {
  182. +        foreach (ft ; _eh_tables)
  183. +            if (ft.fptr <= address && address < cast(void *)(cast(char *)ft.fptr + ft.fsize))
  184. +                return ft.handlertable;
  185.  
  186. -        if (ft.fptr <= address &&
  187. -            address < cast(void *)(cast(char *)ft.fptr + ft.fsize))
  188. -        {
  189. -          debug printf("\tfound handler table\n");
  190. -            return ft.handlertable;
  191. -        }
  192. -    }
  193. -    debug printf("\tnot found\n");
  194. -    return null;
  195. +        return null;
  196. +   }
  197. +  
  198. +   else
  199. +   {
  200. +       debug printf("FuncTable.sizeof = %p\n", FuncTable.sizeof);
  201. +       debug printf("__eh_finddata(address = %p)\n", address);
  202. +       debug printf("_deh_beg = %p, _deh_end = %p\n", &_deh_beg, &_deh_end);
  203. +       for (auto ft = cast(FuncTable *)&_deh_beg;
  204. +            ft < cast(FuncTable *)&_deh_end;
  205. +            ft++)
  206. +       {
  207. +         debug printf("\tft = %p, fptr = %p, fsize = x%03x, handlertable = %p\n",
  208. +                 ft, ft.fptr, ft.fsize, ft.handlertable);
  209. +
  210. +           if (ft.fptr <= address &&
  211. +               address < cast(void *)(cast(char *)ft.fptr + ft.fsize))
  212. +           {
  213. +             debug printf("\tfound handler table\n");
  214. +               return ft.handlertable;
  215. +           }
  216. +       }
  217. +       debug printf("\tnot found\n");
  218. +       return null;
  219. +   }
  220.  }
  221.  
  222.  
  223. Index: src/object_.d
  224. ===================================================================
  225. --- src/object_.d   (revision 423)
  226. +++ src/object_.d   (working copy)
  227. @@ -1731,15 +1731,6 @@
  228.      extern (C) __gshared ModuleReference* _Dmodule_ref;   // start of linked list
  229.  }
  230.  
  231. -version (OSX)
  232. -{
  233. -    extern (C)
  234. -    {
  235. -        extern __gshared void* _minfo_beg;
  236. -        extern __gshared void* _minfo_end;
  237. -    }
  238. -}
  239. -
  240.  __gshared ModuleInfo*[] _moduleinfo_dtors;
  241.  __gshared uint          _moduleinfo_dtors_i;
  242.  
  243. @@ -1801,7 +1792,8 @@
  244.          }
  245.      }
  246.  
  247. -    version (OSX)
  248. +   // this is now handled in memory.d
  249. +    /+version (OSX)
  250.      {
  251.          /* The ModuleInfo references are stored in the special segment
  252.           * __minfodata, which is bracketed by the segments __minfo_beg
  253. @@ -1809,6 +1801,7 @@
  254.           * are of zero size and are in the two bracketing segments,
  255.           * respectively.
  256.           */
  257. +      
  258.           size_t length = cast(ModuleInfo**)&_minfo_end - cast(ModuleInfo**)&_minfo_beg;
  259.           _moduleinfo_array = (cast(ModuleInfo**)&_minfo_beg)[0 .. length];
  260.           debug printf("moduleinfo: ptr = %p, length = %d\n", _moduleinfo_array.ptr, _moduleinfo_array.length);
  261. @@ -1818,7 +1811,7 @@
  262.               //printf("\t%p\n", m);
  263.               printf("\t%.*s\n", m.name);
  264.           }
  265. -    }    
  266. +    }+/    
  267.  
  268.      version (Windows)
  269.      {
  270. Index: src/core/sys/osx/mach/getsect.d
  271. ===================================================================
  272. --- src/core/sys/osx/mach/getsect.d (revision 423)
  273. +++ src/core/sys/osx/mach/getsect.d (working copy)
  274. @@ -17,6 +17,8 @@
  275.  
  276.  extern (C):
  277.  
  278. -section* getsectbynamefromheader (in mach_header* mhp, in char* segname, in char* sectname);
  279. -section_64* getsectbynamefromheader_64 (mach_header_64* mhp, in char* segname, in char* sectname);
  280. -
  281. +version (D_LP64)
  282. +   const(section*) getsectbynamefromheader_64 (in mach_header* mhp, in char* segname, in char* sectname);
  283. +  
  284. +else
  285. +   const(section*) getsectbynamefromheader (in mach_header* mhp, in char* segname, in char* sectname);
  286. \ No newline at end of file
  287. Index: src/core/sys/osx/mach/loader.d
  288. ===================================================================
  289. --- src/core/sys/osx/mach/loader.d  (revision 423)
  290. +++ src/core/sys/osx/mach/loader.d  (working copy)
  291. @@ -13,6 +13,11 @@
  292.  
  293.  version (OSX):
  294.  
  295. +enum immutable(char)* SEG_DATA = "__DATA".ptr;
  296. +enum immutable(char)* SECT_DATA = "__data".ptr;
  297. +enum immutable(char)* SECT_BSS = "__bss".ptr;
  298. +enum immutable(char)* SECT_COMMON = "__common".ptr;
  299. +
  300.  struct mach_header
  301.  {
  302.     uint magic;
  303. @@ -22,20 +27,11 @@
  304.     uint ncmds;
  305.     uint sizeofcmds;
  306.     uint flags;
  307. +  
  308. +   version (D_LP64)
  309. +       uint reserved;
  310.  }
  311.  
  312. -struct mach_header_64
  313. -{
  314. -   uint magic;
  315. -   int cputype;
  316. -   int cpusubtype;
  317. -   uint filetype;
  318. -   uint ncmds;
  319. -   uint sizeofcmds;
  320. -   uint flags;
  321. -   uint reserved;
  322. -}
  323. -
  324.  enum : uint
  325.  {
  326.     MH_MAGIC = 0xfeedface,
  327. @@ -57,21 +53,7 @@
  328.     uint flags;
  329.     uint reserved1;
  330.     uint reserved2;
  331. -}
  332. -
  333. -struct section_64
  334. -{
  335. -   char[16] sectname;
  336. -   char[16] segname;
  337. -   long addr;
  338. -   long size;
  339. -   uint offset;
  340. -   uint align_;
  341. -   uint reloff;
  342. -   uint nreloc;
  343. -   uint flags;
  344. -   uint reserved1;
  345. -   uint reserved2;
  346. -   uint reserved3;
  347. -}
  348. -
  349. +  
  350. +   version (D_LP64)
  351. +       uint reserved3;
  352. +}
  353. \ No newline at end of file
  354. Index: src/core/sys/osx/mach/dyld.d
  355. ===================================================================
  356. --- src/core/sys/osx/mach/dyld.d    (revision 423)
  357. +++ src/core/sys/osx/mach/dyld.d    (working copy)
  358. @@ -17,7 +17,9 @@
  359.  
  360.  extern (C):
  361.  
  362. +alias void function (mach_header* mh, ptrdiff_t vmaddr_slide) DyldFuncPointer;
  363. +
  364.  uint _dyld_image_count ();
  365.  mach_header* _dyld_get_image_header (uint image_index);
  366. -
  367. -
  368. +void _dyld_register_func_for_add_image (DyldFuncPointer);
  369. +void _dyld_register_func_for_remove_image (DyldFuncPointer);
  370. \ No newline at end of file
  371. Index: posix.mak
  372. ===================================================================
  373. --- posix.mak   (revision 423)
  374. +++ posix.mak   (working copy)
  375. @@ -153,14 +153,12 @@
  376.     src/rt/deh2.d \
  377.     src/rt/dmain2.d \
  378.     src/rt/dylib_fixes.c \
  379. -   src/rt/image.d \
  380.     src/rt/invariant.d \
  381.     src/rt/invariant_.d \
  382.     src/rt/lifetime.d \
  383.     src/rt/llmath.d \
  384.     src/rt/mars.h \
  385.     src/rt/memory.d \
  386. -   src/rt/memory_osx.c \
  387.     src/rt/memset.d \
  388.     src/rt/minit.asm \
  389.     src/rt/monitor.c \
  390. @@ -339,7 +337,7 @@
  391.  #       minit.asm is not used by dmd for Linux
  392.  
  393.  OBJS= $(OBJDIR)/errno_c.o $(OBJDIR)/threadasm.o $(OBJDIR)/complex.o    \
  394. -$(OBJDIR)/critical.o $(OBJDIR)/memory_osx.o $(OBJDIR)/monitor.o
  395. +$(OBJDIR)/critical.o $(OBJDIR)/monitor.o
  396.  
  397.  DOCS=\
  398.     $(DOCDIR)/object.html \
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement