fishhook.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 rebindings_entry {
  48. struct rebinding *rebindings;
  49. size_t rebindings_nel;
  50. struct rebindings_entry *next;
  51. };
  52. static struct rebindings_entry *_rebindings_head;
  53. static int prepend_rebindings(struct rebindings_entry **rebindings_head,
  54. struct rebinding rebindings[],
  55. size_t nel) {
  56. struct rebindings_entry *new_entry = (struct rebindings_entry *) malloc(sizeof(struct rebindings_entry));
  57. if (!new_entry) {
  58. return -1;
  59. }
  60. new_entry->rebindings = (struct rebinding *) malloc(sizeof(struct rebinding) * nel);
  61. if (!new_entry->rebindings) {
  62. free(new_entry);
  63. return -1;
  64. }
  65. memcpy(new_entry->rebindings, rebindings, sizeof(struct 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 perform_rebinding_with_section(struct 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. if (strnlen(symbol_name, 2) < 2) {
  88. continue;
  89. }
  90. struct rebindings_entry *cur = rebindings;
  91. while (cur) {
  92. for (uint j = 0; j < cur->rebindings_nel; j++) {
  93. if (strcmp(&symbol_name[1], cur->rebindings[j].name) == 0) {
  94. if (cur->rebindings[j].replaced != NULL &&
  95. indirect_symbol_bindings[i] != cur->rebindings[j].replacement) {
  96. *(cur->rebindings[j].replaced) = indirect_symbol_bindings[i];
  97. }
  98. indirect_symbol_bindings[i] = cur->rebindings[j].replacement;
  99. goto symbol_loop;
  100. }
  101. }
  102. cur = cur->next;
  103. }
  104. symbol_loop:;
  105. }
  106. }
  107. static void rebind_symbols_for_image(struct rebindings_entry *rebindings,
  108. const struct mach_header *header,
  109. intptr_t slide) {
  110. Dl_info info;
  111. if (dladdr(header, &info) == 0) {
  112. return;
  113. }
  114. segment_command_t *cur_seg_cmd;
  115. segment_command_t *linkedit_segment = NULL;
  116. struct symtab_command* symtab_cmd = NULL;
  117. struct dysymtab_command* dysymtab_cmd = NULL;
  118. uintptr_t cur = (uintptr_t)header + sizeof(mach_header_t);
  119. for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) {
  120. cur_seg_cmd = (segment_command_t *)cur;
  121. if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) {
  122. if (strcmp(cur_seg_cmd->segname, SEG_LINKEDIT) == 0) {
  123. linkedit_segment = cur_seg_cmd;
  124. }
  125. } else if (cur_seg_cmd->cmd == LC_SYMTAB) {
  126. symtab_cmd = (struct symtab_command*)cur_seg_cmd;
  127. } else if (cur_seg_cmd->cmd == LC_DYSYMTAB) {
  128. dysymtab_cmd = (struct dysymtab_command*)cur_seg_cmd;
  129. }
  130. }
  131. if (!symtab_cmd || !dysymtab_cmd || !linkedit_segment ||
  132. !dysymtab_cmd->nindirectsyms) {
  133. return;
  134. }
  135. // Find base symbol/string table addresses
  136. uintptr_t linkedit_base = (uintptr_t)slide + linkedit_segment->vmaddr - linkedit_segment->fileoff;
  137. nlist_t *symtab = (nlist_t *)(linkedit_base + symtab_cmd->symoff);
  138. char *strtab = (char *)(linkedit_base + symtab_cmd->stroff);
  139. // Get indirect symbol table (array of uint32_t indices into symbol table)
  140. uint32_t *indirect_symtab = (uint32_t *)(linkedit_base + dysymtab_cmd->indirectsymoff);
  141. cur = (uintptr_t)header + sizeof(mach_header_t);
  142. for (uint i = 0; i < header->ncmds; i++, cur += cur_seg_cmd->cmdsize) {
  143. cur_seg_cmd = (segment_command_t *)cur;
  144. if (cur_seg_cmd->cmd == LC_SEGMENT_ARCH_DEPENDENT) {
  145. if (strcmp(cur_seg_cmd->segname, SEG_DATA) != 0 &&
  146. strcmp(cur_seg_cmd->segname, SEG_DATA_CONST) != 0) {
  147. continue;
  148. }
  149. for (uint j = 0; j < cur_seg_cmd->nsects; j++) {
  150. section_t *sect =
  151. (section_t *)(cur + sizeof(segment_command_t)) + j;
  152. if ((sect->flags & SECTION_TYPE) == S_LAZY_SYMBOL_POINTERS) {
  153. perform_rebinding_with_section(rebindings, sect, slide, symtab, strtab, indirect_symtab);
  154. }
  155. if ((sect->flags & SECTION_TYPE) == S_NON_LAZY_SYMBOL_POINTERS) {
  156. perform_rebinding_with_section(rebindings, sect, slide, symtab, strtab, indirect_symtab);
  157. }
  158. }
  159. }
  160. }
  161. }
  162. static void _rebind_symbols_for_image(const struct mach_header *header,
  163. intptr_t slide) {
  164. rebind_symbols_for_image(_rebindings_head, header, slide);
  165. }
  166. int rebind_symbols_image(void *header,
  167. intptr_t slide,
  168. struct rebinding rebindings[],
  169. size_t rebindings_nel) {
  170. struct rebindings_entry *rebindings_head = NULL;
  171. int retval = prepend_rebindings(&rebindings_head, rebindings, rebindings_nel);
  172. rebind_symbols_for_image(rebindings_head, (const struct mach_header *) header, slide);
  173. free(rebindings_head);
  174. return retval;
  175. }
  176. int rebind_symbols(struct rebinding rebindings[], size_t rebindings_nel) {
  177. int retval = prepend_rebindings(&_rebindings_head, rebindings, rebindings_nel);
  178. if (retval < 0) {
  179. return retval;
  180. }
  181. // If this was the first call, register callback for image additions (which is also invoked for
  182. // existing images, otherwise, just run on existing images
  183. if (!_rebindings_head->next) {
  184. _dyld_register_func_for_add_image(_rebind_symbols_for_image);
  185. } else {
  186. uint32_t c = _dyld_image_count();
  187. for (uint32_t i = 0; i < c; i++) {
  188. _rebind_symbols_for_image(_dyld_get_image_header(i), _dyld_get_image_vmaddr_slide(i));
  189. }
  190. }
  191. return retval;
  192. }