Image.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "il2cpp-config.h"
  2. #include "utils/Memory.h"
  3. #if IL2CPP_TARGET_WINDOWS
  4. #include "os/Image.h"
  5. #include "WindowsHeaders.h"
  6. #include <libloaderapi.h>
  7. #include <cstdio>
  8. EXTERN_C IMAGE_DOS_HEADER __ImageBase;
  9. namespace il2cpp
  10. {
  11. namespace os
  12. {
  13. namespace Image
  14. {
  15. static void InitializeManagedSection()
  16. {
  17. PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)(((char*)&__ImageBase) + __ImageBase.e_lfanew);
  18. PIMAGE_SECTION_HEADER sectionHeader = (PIMAGE_SECTION_HEADER)((char*)&ntHeaders->OptionalHeader + ntHeaders->FileHeader.SizeOfOptionalHeader);
  19. for (int i = 0; i < ntHeaders->FileHeader.NumberOfSections; i++)
  20. {
  21. if (strncmp(IL2CPP_BINARY_SECTION_NAME, (char*)sectionHeader->Name, IMAGE_SIZEOF_SHORT_NAME) == 0)
  22. {
  23. void* start = (char*)&__ImageBase + sectionHeader->VirtualAddress;
  24. void* end = (char*)start + sectionHeader->Misc.VirtualSize;
  25. SetManagedSectionStartAndEnd(start, end);
  26. }
  27. sectionHeader++;
  28. }
  29. }
  30. void Initialize()
  31. {
  32. InitializeManagedSection();
  33. }
  34. void* GetImageBase()
  35. {
  36. return &__ImageBase;
  37. }
  38. #if IL2CPP_ENABLE_NATIVE_INSTRUCTION_POINTER_EMISSION
  39. struct PdbInfo
  40. {
  41. DWORD Signature;
  42. GUID Guid;
  43. DWORD Age;
  44. char PdbFileName[1];
  45. };
  46. char* GetImageUUID()
  47. {
  48. uintptr_t base_pointer = (uintptr_t)((char*)&__ImageBase);
  49. PIMAGE_NT_HEADERS ntHeaders = (PIMAGE_NT_HEADERS)(base_pointer + __ImageBase.e_lfanew);
  50. IMAGE_DATA_DIRECTORY* dir = &ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
  51. IMAGE_DEBUG_DIRECTORY* dbg_dir = (IMAGE_DEBUG_DIRECTORY*)(base_pointer + dir->VirtualAddress);
  52. if (IMAGE_DEBUG_TYPE_CODEVIEW == dbg_dir->Type)
  53. {
  54. PdbInfo* pdb_info = (PdbInfo*)(base_pointer + dbg_dir->AddressOfRawData);
  55. char* uuid = static_cast<char*>(IL2CPP_MALLOC(41));
  56. // Crash reporting expects the GUID without dashes (the format used by symbol servers)
  57. _snprintf_s(uuid, 41, 40, "%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%X",
  58. (unsigned char)(pdb_info->Guid.Data1 >> 24),
  59. (unsigned char)(pdb_info->Guid.Data1 >> 16),
  60. (unsigned char)(pdb_info->Guid.Data1 >> 8),
  61. (unsigned char)pdb_info->Guid.Data1,
  62. (unsigned char)(pdb_info->Guid.Data2 >> 8),
  63. (unsigned char)pdb_info->Guid.Data2,
  64. (unsigned char)(pdb_info->Guid.Data3 >> 8),
  65. (unsigned char)pdb_info->Guid.Data3,
  66. pdb_info->Guid.Data4[0], pdb_info->Guid.Data4[1], pdb_info->Guid.Data4[2], pdb_info->Guid.Data4[3],
  67. pdb_info->Guid.Data4[4], pdb_info->Guid.Data4[5], pdb_info->Guid.Data4[6], pdb_info->Guid.Data4[7],
  68. pdb_info->Age);
  69. return uuid;
  70. }
  71. return NULL;
  72. }
  73. char* GetImageName()
  74. {
  75. char* name = static_cast<char*>(IL2CPP_MALLOC(MAX_PATH + 1));
  76. GetModuleFileNameA((HMODULE)&__ImageBase, name, MAX_PATH + 1);
  77. return name;
  78. }
  79. #endif
  80. }
  81. }
  82. }
  83. #endif