fishhook.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2013, Facebook, Inc.
  2. // All rights reserved.
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are met:
  5. // * Redistributions of source code must retain the above copyright notice,
  6. // this list of conditions and the following disclaimer.
  7. // * Redistributions in binary form must reproduce the above copyright notice,
  8. // this list of conditions and the following disclaimer in the documentation
  9. // and/or other materials provided with the distribution.
  10. // * Neither the name Facebook nor the names of its contributors may be used to
  11. // endorse or promote products derived from this software without specific
  12. // prior written permission.
  13. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  17. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. #import "fishhook.h"
  24. #import <dlfcn.h>
  25. #import <stdlib.h>
  26. #import <string.h>
  27. #import <sys/types.h>
  28. #import <mach-o/dyld.h>
  29. #import <mach-o/loader.h>
  30. #import <mach-o/nlist.h>
  31. #ifdef __LP64__
  32. typedef struct mach_header_64 mach_header_t;
  33. typedef struct segment_command_64 segment_command_t;
  34. typedef struct section_64 section_t;
  35. typedef struct nlist_64 nlist_t;
  36. #define LC_SEGMENT_ARCH_DEPENDENT LC_SEGMENT_64
  37. #else
  38. typedef struct mach_header mach_header_t;
  39. typedef struct segment_command segment_command_t;
  40. typedef struct section section_t;
  41. typedef struct nlist nlist_t;
  42. #define LC_SEGMENT_ARCH_DEPENDENT LC_SEGMENT
  43. #endif
  44. #ifndef SEG_DATA_CONST
  45. #define SEG_DATA_CONST "__DATA_CONST"
  46. #endif
  47. struct rcd_rebindings_entry {
  48. struct rcd_rebinding *rebindings;
  49. size_t rebindings_nel;
  50. struct rcd_rebindings_entry *next;
  51. };
  52. static struct rcd_rebindings_entry *_rebindings_head;
  53. static int rcd_prepend_rebindings(struct rcd_rebindings_entry **rebindings_head,
  54. struct rcd_rebinding rebindings[],
  55. size_t nel) {
  56. struct rcd_rebindings_entry *new_entry = malloc(sizeof(struct rcd_rebindings_entry));
  57. if (!new_entry) {
  58. return -1;
  59. }
  60. new_entry->rebindings = malloc(sizeof(struct rcd_rebinding) * nel);
  61. if (!new_entry->rebindings) {
  62. free(new_entry);
  63. return -1;
  64. }
  65. memcpy(new_entry->rebindings, rebindings, sizeof(struct rcd_rebinding) * nel);
  66. new_entry->rebindings_nel = nel;
  67. new_entry->next = *rebindings_head;
  68. *rebindings_head = new_entry;
  69. return 0;
  70. }
  71. static void rcd_perform_rebinding_with_section(struct rcd_rebindings_entry *rebindings,
  72. section_t *section,
  73. intptr_t slide,
  74. nlist_t *symtab,
  75. char *strtab,
  76. uint32_t *indirect_symtab) {
  77. uint32_t *indirect_symbol_indices = indirect_symtab + section->reserved1;
  78. void **indirect_symbol_bindings = (void **)((uintptr_t)slide + section->addr);
  79. for (uint i = 0; i < section->size / sizeof(void *); i++) {
  80. uint32_t symtab_index = indirect_symbol_indices[i];
  81. if (symtab_index == INDIRECT_SYMBOL_ABS || symtab_index == INDIRECT_SYMBOL_LOCAL ||
  82. symtab_index == (INDIRECT_SYMBOL_LOCAL | INDIRECT_SYMBOL_ABS)) {
  83. continue;
  84. }
  85. uint32_t strtab_offset = symtab[symtab_index].n_un.n_strx;
  86. char *symbol_name = strtab + strtab_offset;
  87. struct rcd_rebindings_entry *cur = rebindings;
  88. while (cur) {
  89. for (uint j = 0; j < cur->rebindings_nel; j++) {
  90. if (strlen(symbol_name) > 1 &&
  91. strcmp(&symbol_name[1], cur->rebindings[j].name) == 0) {
  92. if (cur->rebindings[j].replaced != NULL &&
  93. indirect_symbol_bindings[i] != cur->rebindings[j].replacement) {
  94. *(cur->rebindings[j].replaced) = indirect_symbol_bindings[i];
  95. }
  96. indirect_symbol_bindings[i] = cur->rebindings[j].replacement;
  97. goto symbol_loop;
  98. }
  99. }
  100. cur = cur->next;
  101. }
  102. symbol_loop:;
  103. }
  104. }
  105. static void rebind_symbols_for_image(struct rcd_rebindings_entry *rebindings,
  106. const struct mach_header *header,
  107. intptr_t slide) {
  108. Dl_info info;
  109. if (dladdr(header, &info) == 0) {
  110. return;
  111. }
  112. segment_command_t *cur_seg_cmd;
  113. segment_command_t *linkedit_segment = NULL;
  114. struct symtab_command* symtab_cmd = NULL;
  115. struct dysymtab_command* dysymtab_cmd = NULL;
  116. uintptr_t cur = (uintptr_t)header + sizeof(mach_header_t);
  117. for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) {
  118. cur_seg_cmd = (segment_command_t *)cur;
  119. if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) {
  120. if (strcmp(cur_seg_cmd->segname, SEG_LINKEDIT) == 0) {
  121. linkedit_segment = cur_seg_cmd;
  122. }
  123. } else if (cur_seg_cmd->cmd == LC_SYMTAB) {
  124. symtab_cmd = (struct symtab_command*)cur_seg_cmd;
  125. } else if (cur_seg_cmd->cmd == LC_DYSYMTAB) {
  126. dysymtab_cmd = (struct dysymtab_command*)cur_seg_cmd;
  127. }
  128. }
  129. if (!symtab_cmd || !dysymtab_cmd || !linkedit_segment ||
  130. !dysymtab_cmd->nindirectsyms) {
  131. return;
  132. }
  133. // Find base symbol/string table addresses
  134. uintptr_t linkedit_base = (uintptr_t)slide + linkedit_segment->vmaddr - linkedit_segment->fileoff;
  135. nlist_t *symtab = (nlist_t *)(linkedit_base + symtab_cmd->symoff);
  136. char *strtab = (char *)(linkedit_base + symtab_cmd->stroff);
  137. // Get indirect symbol table (array of uint32_t indices into symbol table)
  138. uint32_t *indirect_symtab = (uint32_t *)(linkedit_base + dysymtab_cmd->indirectsymoff);
  139. cur = (uintptr_t)header + sizeof(mach_header_t);
  140. for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) {
  141. cur_seg_cmd = (segment_command_t *)cur;
  142. if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) {
  143. if (strcmp(cur_seg_cmd->segname, SEG_DATA) != 0 &&
  144. strcmp(cur_seg_cmd->segname, SEG_DATA_CONST) != 0) {
  145. continue;
  146. }
  147. for (uint j = 0; j < cur_seg_cmd->nsects; j++) {
  148. section_t *sect =
  149. (section_t *)(cur + sizeof(segment_command_t)) + j;
  150. if ((sect->flags & SECTION_TYPE) == S_LAZY_SYMBOL_POINTERS) {
  151. rcd_perform_rebinding_with_section(rebindings, sect, slide, symtab, strtab, indirect_symtab);
  152. }
  153. if ((sect->flags & SECTION_TYPE) == S_NON_LAZY_SYMBOL_POINTERS) {
  154. rcd_perform_rebinding_with_section(rebindings, sect, slide, symtab, strtab, indirect_symtab);
  155. }
  156. }
  157. }
  158. }
  159. }
  160. static void _rebind_symbols_for_image(const struct mach_header *header,
  161. intptr_t slide) {
  162. rebind_symbols_for_image(_rebindings_head, header, slide);
  163. }
  164. int rcd_rebind_symbols_image(void *header,
  165. intptr_t slide,
  166. struct rcd_rebinding rebindings[],
  167. size_t rebindings_nel) {
  168. struct rcd_rebindings_entry *rebindings_head = NULL;
  169. int retval = rcd_prepend_rebindings(&rebindings_head, rebindings, rebindings_nel);
  170. rebind_symbols_for_image(rebindings_head, header, slide);
  171. free(rebindings_head);
  172. return retval;
  173. }
  174. int rcd_rebind_symbols(struct rcd_rebinding rebindings[], size_t rebindings_nel) {
  175. int retval = rcd_prepend_rebindings(&_rebindings_head, rebindings, rebindings_nel);
  176. if (retval < 0) {
  177. return retval;
  178. }
  179. // If this was the first call, register callback for image additions (which is also invoked for
  180. // existing images, otherwise, just run on existing images
  181. if (!_rebindings_head->next) {
  182. _dyld_register_func_for_add_image(_rebind_symbols_for_image);
  183. } else {
  184. uint32_t c = _dyld_image_count();
  185. for (uint32_t i = 0; i < c; i++) {
  186. _rebind_symbols_for_image(_dyld_get_image_header(i), _dyld_get_image_vmaddr_slide(i));
  187. }
  188. }
  189. return retval;
  190. }