KSDynamicLinker.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // KSDynamicLinker.h
  3. //
  4. // Created by Karl Stenerud on 2013-10-02.
  5. //
  6. // Copyright (c) 2012 Karl Stenerud. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall remain in place
  16. // in this source code.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #ifndef HDR_KSDynamicLinker_h
  27. #define HDR_KSDynamicLinker_h
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include <dlfcn.h>
  32. #include <stdbool.h>
  33. #include <stdint.h>
  34. typedef struct
  35. {
  36. uint64_t address;
  37. uint64_t vmAddress;
  38. uint64_t size;
  39. const char* name;
  40. const uint8_t* uuid;
  41. int cpuType;
  42. int cpuSubType;
  43. uint64_t majorVersion;
  44. uint64_t minorVersion;
  45. uint64_t revisionVersion;
  46. const char* crashInfoMessage;
  47. const char* crashInfoMessage2;
  48. } KSBinaryImage;
  49. /** Get the number of loaded binary images.
  50. */
  51. int ksdl_imageCount(void);
  52. /** Get information about a binary image.
  53. *
  54. * @param index The binary index.
  55. *
  56. * @param buffer A structure to hold the information.
  57. *
  58. * @return True if the image was successfully queried.
  59. */
  60. bool ksdl_getBinaryImage(int index, KSBinaryImage* buffer);
  61. /** Get information about a binary image based on mach_header.
  62. *
  63. * @param header_ptr The pointer to mach_header of the image.
  64. *
  65. * @param image_name The name of the image.
  66. *
  67. * @param buffer A structure to hold the information.
  68. *
  69. * @return True if the image was successfully queried.
  70. */
  71. bool ksdl_getBinaryImageForHeader(const void* const header_ptr, const char* const image_name, KSBinaryImage* buffer);
  72. /** Find a loaded binary image with the specified name.
  73. *
  74. * @param imageName The image name to look for.
  75. *
  76. * @param exactMatch If true, look for an exact match instead of a partial one.
  77. *
  78. * @return the index of the matched image, or UINT32_MAX if not found.
  79. */
  80. uint32_t ksdl_imageNamed(const char* const imageName, bool exactMatch);
  81. /** Get the UUID of a loaded binary image with the specified name.
  82. *
  83. * @param imageName The image name to look for.
  84. *
  85. * @param exactMatch If true, look for an exact match instead of a partial one.
  86. *
  87. * @return A pointer to the binary (16 byte) UUID of the image, or NULL if it
  88. * wasn't found.
  89. */
  90. const uint8_t* ksdl_imageUUID(const char* const imageName, bool exactMatch);
  91. /** async-safe version of dladdr.
  92. *
  93. * This method searches the dynamic loader for information about any image
  94. * containing the specified address. It may not be entirely successful in
  95. * finding information, in which case any fields it could not find will be set
  96. * to NULL.
  97. *
  98. * Unlike dladdr(), this method does not make use of locks, and does not call
  99. * async-unsafe functions.
  100. *
  101. * @param address The address to search for.
  102. * @param info Gets filled out by this function.
  103. * @return true if at least some information was found.
  104. */
  105. bool ksdl_dladdr(const uintptr_t address, Dl_info* const info);
  106. #ifdef __cplusplus
  107. }
  108. #endif
  109. #endif // HDR_KSDynamicLinker_h