FIRCLSMachO.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <mach-o/arch.h>
  16. #include <mach-o/loader.h>
  17. #include <sys/types.h>
  18. #include <CoreFoundation/CoreFoundation.h>
  19. struct FIRCLSMachOFile {
  20. int fd;
  21. size_t mappedSize;
  22. void* mappedFile;
  23. };
  24. typedef struct FIRCLSMachOFile* FIRCLSMachOFileRef;
  25. struct FIRCLSMachOSlice {
  26. const void* startAddress;
  27. cpu_type_t cputype;
  28. cpu_subtype_t cpusubtype;
  29. };
  30. typedef struct FIRCLSMachOSlice* FIRCLSMachOSliceRef;
  31. typedef struct {
  32. uint32_t major;
  33. uint32_t minor;
  34. uint32_t bugfix;
  35. } FIRCLSMachOVersion;
  36. typedef struct {
  37. uint64_t addr;
  38. uint64_t size;
  39. uint32_t offset;
  40. } FIRCLSMachOSection;
  41. typedef struct {
  42. char segname[16];
  43. uint64_t vmaddr;
  44. uint64_t vmsize;
  45. } FIRCLSMachOSegmentCommand;
  46. typedef void (^FIRCLSMachOSliceIterator)(FIRCLSMachOSliceRef slice);
  47. typedef void (^FIRCLSMachOLoadCommandIterator)(uint32_t type,
  48. uint32_t size,
  49. const struct load_command* cmd);
  50. typedef void (*FIRCLSMachOLoadCommandIteratorFunc)(uint32_t type,
  51. uint32_t size,
  52. const struct load_command* cmd,
  53. void* context);
  54. __BEGIN_DECLS
  55. bool FIRCLSMachOFileInitWithPath(FIRCLSMachOFileRef file, const char* path);
  56. bool FIRCLSMachOFileInitWithCurrent(FIRCLSMachOFileRef file);
  57. void FIRCLSMachOFileDestroy(FIRCLSMachOFileRef file);
  58. void FIRCLSMachOFileEnumerateSlices(FIRCLSMachOFileRef file, FIRCLSMachOSliceIterator block);
  59. struct FIRCLSMachOSlice FIRCLSMachOFileSliceWithArchitectureName(FIRCLSMachOFileRef file,
  60. const char* name);
  61. void FIRCLSMachOEnumerateSlicesAtAddress(void* executableData, FIRCLSMachOSliceIterator block);
  62. void FIRCLSMachOSliceEnumerateLoadCommands(FIRCLSMachOSliceRef slice,
  63. FIRCLSMachOLoadCommandIterator block);
  64. void FIRCLSMachOSliceEnumerateLoadCommands_f(FIRCLSMachOSliceRef slice,
  65. void* context,
  66. FIRCLSMachOLoadCommandIteratorFunc function);
  67. struct FIRCLSMachOSlice FIRCLSMachOSliceGetCurrent(void);
  68. struct FIRCLSMachOSlice FIRCLSMachOSliceWithHeader(void* machHeader);
  69. const char* FIRCLSMachOSliceGetExecutablePath(FIRCLSMachOSliceRef slice);
  70. const char* FIRCLSMachOSliceGetArchitectureName(FIRCLSMachOSliceRef slice);
  71. bool FIRCLSMachOSliceIs64Bit(FIRCLSMachOSliceRef slice);
  72. bool FIRCLSMachOSliceGetSectionByName(FIRCLSMachOSliceRef slice,
  73. const char* segName,
  74. const char* sectionName,
  75. const void** ptr);
  76. bool FIRCLSMachOSliceInitSectionByName(FIRCLSMachOSliceRef slice,
  77. const char* segName,
  78. const char* sectionName,
  79. FIRCLSMachOSection* section);
  80. void FIRCLSMachOSliceGetUnwindInformation(FIRCLSMachOSliceRef slice,
  81. const void** ehFrame,
  82. const void** unwindInfo);
  83. // load-command-specific calls for convenience
  84. // returns a pointer to the 16-byte UUID
  85. uint8_t const* FIRCLSMachOGetUUID(const struct load_command* cmd);
  86. const char* FIRCLSMachOGetDylibPath(const struct load_command* cmd);
  87. // return true if the header indicates the binary is encrypted
  88. bool FIRCLSMachOGetEncrypted(const struct load_command* cmd);
  89. // SDK minimums
  90. FIRCLSMachOVersion FIRCLSMachOGetMinimumOSVersion(const struct load_command* cmd);
  91. FIRCLSMachOVersion FIRCLSMachOGetLinkedSDKVersion(const struct load_command* cmd);
  92. // Helpers
  93. FIRCLSMachOSegmentCommand FIRCLSMachOGetSegmentCommand(const struct load_command* cmd);
  94. #ifdef __OBJC__
  95. NSString* FIRCLSMachONormalizeUUID(CFUUIDBytes* uuidBytes);
  96. NSString* FIRCLSMachOFormatVersion(FIRCLSMachOVersion* version);
  97. #endif
  98. __END_DECLS