FIRCLSMachO.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. __BEGIN_DECLS
  51. bool FIRCLSMachOFileInitWithPath(FIRCLSMachOFileRef file, const char* path);
  52. bool FIRCLSMachOFileInitWithCurrent(FIRCLSMachOFileRef file);
  53. void FIRCLSMachOFileDestroy(FIRCLSMachOFileRef file);
  54. void FIRCLSMachOFileEnumerateSlices(FIRCLSMachOFileRef file, FIRCLSMachOSliceIterator block);
  55. struct FIRCLSMachOSlice FIRCLSMachOFileSliceWithArchitectureName(FIRCLSMachOFileRef file,
  56. const char* name);
  57. void FIRCLSMachOEnumerateSlicesAtAddress(void* executableData, FIRCLSMachOSliceIterator block);
  58. void FIRCLSMachOSliceEnumerateLoadCommands(FIRCLSMachOSliceRef slice,
  59. FIRCLSMachOLoadCommandIterator block);
  60. struct FIRCLSMachOSlice FIRCLSMachOSliceGetCurrent(void);
  61. struct FIRCLSMachOSlice FIRCLSMachOSliceWithHeader(void* machHeader);
  62. const char* FIRCLSMachOSliceGetExecutablePath(FIRCLSMachOSliceRef slice);
  63. const char* FIRCLSMachOSliceGetArchitectureName(FIRCLSMachOSliceRef slice);
  64. bool FIRCLSMachOSliceIs64Bit(FIRCLSMachOSliceRef slice);
  65. bool FIRCLSMachOSliceGetSectionByName(FIRCLSMachOSliceRef slice,
  66. const char* segName,
  67. const char* sectionName,
  68. const void** ptr);
  69. bool FIRCLSMachOSliceInitSectionByName(FIRCLSMachOSliceRef slice,
  70. const char* segName,
  71. const char* sectionName,
  72. FIRCLSMachOSection* section);
  73. void FIRCLSMachOSliceGetUnwindInformation(FIRCLSMachOSliceRef slice,
  74. const void** ehFrame,
  75. const void** unwindInfo);
  76. // load-command-specific calls for convenience
  77. // returns a pointer to the 16-byte UUID
  78. uint8_t const* FIRCLSMachOGetUUID(const struct load_command* cmd);
  79. const char* FIRCLSMachOGetDylibPath(const struct load_command* cmd);
  80. // return true if the header indicates the binary is encrypted
  81. bool FIRCLSMachOGetEncrypted(const struct load_command* cmd);
  82. // SDK minimums
  83. FIRCLSMachOVersion FIRCLSMachOGetMinimumOSVersion(const struct load_command* cmd);
  84. FIRCLSMachOVersion FIRCLSMachOGetLinkedSDKVersion(const struct load_command* cmd);
  85. // Helpers
  86. FIRCLSMachOSegmentCommand FIRCLSMachOGetSegmentCommand(const struct load_command* cmd);
  87. #ifdef __OBJC__
  88. NSString* FIRCLSMachONormalizeUUID(CFUUIDBytes* uuidBytes);
  89. NSString* FIRCLSMachOFormatVersion(FIRCLSMachOVersion* version);
  90. #endif
  91. __END_DECLS