fix-mingw.diff 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. diff --git a/src/win/memory.c b/src/win/memory.c
  2. index 07a4024..ab3868c 100644
  3. --- a/src/win/memory.c
  4. +++ b/src/win/memory.c
  5. @@ -50,8 +50,13 @@ LM_ReadMemoryEx(const lm_process_t *process,
  6. if (!hproc)
  7. return 0;
  8. + #if defined(__MINGW32__) || defined(__clang__)
  9. + if (!ReadProcessMemory(hproc, (LPCVOID)source, dest, size, &bytes_read))
  10. + bytes_read = 0;
  11. + #else
  12. if (!ReadProcessMemory(hproc, source, dest, size, &bytes_read))
  13. bytes_read = 0;
  14. + #endif
  15. close_handle(hproc);
  16. return (lm_size_t)bytes_read;
  17. @@ -75,8 +80,13 @@ LM_WriteMemoryEx(const lm_process_t *process,
  18. if (!hproc)
  19. return 0;
  20. + #if defined(__MINGW32__) || defined(__clang__)
  21. + if (!WriteProcessMemory(hproc, (LPVOID)dest, source, size, &bytes_written))
  22. + bytes_written = 0;
  23. + #else
  24. if (!WriteProcessMemory(hproc, dest, source, size, &bytes_written))
  25. bytes_written = 0;
  26. + #endif
  27. close_handle(hproc);
  28. return (lm_size_t)bytes_written;
  29. @@ -100,8 +110,13 @@ LM_ProtMemory(lm_address_t address,
  30. size = get_page_size();
  31. osprot = get_os_prot(prot);
  32. + #if defined(__MINGW32__) || defined(__clang__)
  33. + if (!VirtualProtect((LPVOID)address, size, osprot, &old_osprot))
  34. + return LM_FALSE;
  35. + #else
  36. if (!VirtualProtect(address, size, osprot, &old_osprot))
  37. return LM_FALSE;
  38. + #endif
  39. if (oldprot_out)
  40. *oldprot_out = get_prot(old_osprot);
  41. @@ -134,8 +149,13 @@ LM_ProtMemoryEx(const lm_process_t *process,
  42. return result;
  43. osprot = get_os_prot(prot);
  44. + #if defined(__MINGW32__) || defined(__clang__)
  45. + if (!VirtualProtectEx(hproc, (LPVOID)address, size, osprot, &old_osprot))
  46. + goto CLOSE_EXIT;
  47. + #else
  48. if (!VirtualProtectEx(hproc, address, size, osprot, &old_osprot))
  49. goto CLOSE_EXIT;
  50. + #endif
  51. if (oldprot_out)
  52. *oldprot_out = get_prot(old_osprot);
  53. @@ -212,7 +232,11 @@ LM_FreeMemory(lm_address_t alloc,
  54. */
  55. size = 0;
  56. + #if defined(__MINGW32__) || defined(__clang__)
  57. + return VirtualFree((LPVOID)alloc, size, MEM_RELEASE) ? LM_TRUE : LM_FALSE;
  58. + #else
  59. return VirtualFree(alloc, size, MEM_RELEASE) ? LM_TRUE : LM_FALSE;
  60. + #endif
  61. }
  62. /********************************/
  63. @@ -233,7 +257,11 @@ LM_FreeMemoryEx(const lm_process_t *process,
  64. return LM_FALSE;
  65. size = 0;
  66. + #if defined(__MINGW32__) || defined(__clang__)
  67. + ret = VirtualFreeEx(hproc, (LPVOID)alloc, size, MEM_RELEASE);
  68. + #else
  69. ret = VirtualFreeEx(hproc, alloc, size, MEM_RELEASE);
  70. + #endif
  71. close_handle(hproc);
  72. diff --git a/src/win/module.c b/src/win/module.c
  73. index 013b03d..07ba91b 100644
  74. --- a/src/win/module.c
  75. +++ b/src/win/module.c
  76. @@ -123,14 +123,23 @@ LM_LoadModuleEx(const lm_process_t *process,
  77. if (modpath_addr == LM_ADDRESS_BAD)
  78. return result;
  79. + #if defined(__MINGW32__) || defined(__clang__)
  80. + if (!LM_WriteMemoryEx(process, modpath_addr, (lm_bytearray_t)wpath, sizeof(wpath)))
  81. + goto FREE_EXIT;
  82. + #else
  83. if (!LM_WriteMemoryEx(process, modpath_addr, wpath, sizeof(wpath)))
  84. goto FREE_EXIT;
  85. + #endif
  86. hproc = open_process(process->pid, PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ);
  87. if (!hproc)
  88. goto FREE_EXIT;
  89. + #if defined(__MINGW32__) || defined(__clang__)
  90. + hthread = (HANDLE)CreateRemoteThread(hproc, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, (LPVOID)modpath_addr, 0, NULL);
  91. + #else
  92. hthread = (HANDLE)CreateRemoteThread(hproc, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, modpath_addr, 0, NULL);
  93. + #endif
  94. close_handle(&hproc);
  95. @@ -194,7 +203,11 @@ LM_UnloadModuleEx(const lm_process_t *process,
  96. if (!hproc)
  97. return LM_FALSE;
  98. + #if defined(__MINGW32__) || defined(__clang__)
  99. + hthread = (HANDLE)CreateRemoteThread(hproc, NULL, 0, (LPTHREAD_START_ROUTINE)FreeLibrary, (LPVOID)module->base, 0, NULL);
  100. + #else
  101. hthread = (HANDLE)CreateRemoteThread(hproc, NULL, 0, (LPTHREAD_START_ROUTINE)FreeLibrary, module->base, 0, NULL);
  102. + #endif
  103. close_handle(&hproc);
  104. diff --git a/src/win/segment.c b/src/win/segment.c
  105. index bed38e0..fc9af97 100644
  106. --- a/src/win/segment.c
  107. +++ b/src/win/segment.c
  108. @@ -39,7 +39,11 @@ LM_EnumSegments(lm_bool_t (LM_CALL *callback)(lm_segment_t *segment,
  109. return result;
  110. for (address = 0;
  111. - VirtualQuery(address, &meminfo, sizeof(meminfo)) > 0;
  112. + #if defined(__MINGW32__) || defined(__clang__)
  113. + VirtualQuery((LPCVOID)address, &meminfo, sizeof(meminfo)) > 0;
  114. + #else
  115. + VirtualQuery(address, &meminfo, sizeof(meminfo)) > 0;
  116. + #endif
  117. address += meminfo.RegionSize) {
  118. /* Skip unallocated regions */
  119. if (meminfo.State == MEM_FREE)
  120. @@ -82,7 +86,11 @@ LM_EnumSegmentsEx(const lm_process_t *process,
  121. /* TODO: Add fix for 32 bit processes enumerating 64 bit target processes (avoid address overflow) */
  122. for (address = 0;
  123. + #if defined(__MINGW32__) || defined(__clang__)
  124. + VirtualQueryEx(hproc, (LPCVOID)address, &meminfo, sizeof(meminfo)) > 0;
  125. + #else
  126. VirtualQueryEx(hproc, address, &meminfo, sizeof(meminfo)) > 0;
  127. + #endif
  128. address += meminfo.RegionSize) {
  129. /* Skip unallocated regions */
  130. if (meminfo.State == MEM_FREE)