this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add support code needed for gdb-darling

+94 -2
+6
platform-include/mach-o/dyld_images.h
··· 25 25 26 26 #include <stdbool.h> 27 27 #include <unistd.h> 28 + #ifndef MLDR_BUILD 28 29 #include <mach/mach.h> 30 + #else 31 + #include <stdint.h> 32 + #endif 29 33 30 34 #ifdef __cplusplus 31 35 extern "C" { ··· 73 77 /* then file has been modified since dyld loaded it */ 74 78 }; 75 79 80 + #ifndef MLDR_BUILD 76 81 struct dyld_uuid_info { 77 82 const struct mach_header* imageLoadAddress; /* base address image is mapped into */ 78 83 uuid_t imageUUID; /* UUID of image */ 79 84 }; 85 + #endif 80 86 81 87 typedef void (*dyld_image_notifier)(enum dyld_image_mode mode, uint32_t infoCount, const struct dyld_image_info info[]); 82 88
+3 -2
src/dyld/CMakeLists.txt
··· 9 9 10 10 enable_language(C ASM) 11 11 12 - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11") 13 - add_definitions(-DINSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}" -D_GNU_SOURCE) 12 + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11 -ggdb") 13 + add_definitions(-DINSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}" -D_GNU_SOURCE -DMLDR_BUILD) 14 14 15 15 add_executable(darling darling.c) 16 16 ··· 19 19 set(mldr_sources 20 20 mldr.c 21 21 threads.c 22 + gdb.c 22 23 ) 23 24 add_executable(mldr ${mldr_sources}) 24 25 target_link_libraries(mldr -lpthread -ldl)
+58
src/dyld/gdb.c
··· 1 + #include "gdb.h" 2 + #include <string.h> 3 + #include <stdio.h> 4 + 5 + __attribute__ ((noinline)) 6 + static void gdb_notifier(enum dyld_image_mode mode, uint32_t infoCount, const struct dyld_image_info info[]); 7 + static void dyld_notification_wrapper(enum dyld_image_mode mode, uint32_t infoCount, const struct dyld_image_info info[]); 8 + 9 + static struct dyld_all_image_infos* orig_dyld_all_image_infos; 10 + 11 + // This is the symbol GDB looks for 12 + struct dyld_all_image_infos _dyld_all_image_infos = { 13 + .version = 15, 14 + .infoArrayCount = 0, 15 + .infoArray = NULL, 16 + .notification = &gdb_notifier, 17 + }; 18 + 19 + void gdb_notifier(enum dyld_image_mode mode, uint32_t infoCount, const struct dyld_image_info info[]) 20 + { 21 + // GDB sets a breakpoint in this function 22 + } 23 + 24 + struct jump 25 + { 26 + uint16_t mov; 27 + void* addr; 28 + uint16_t jump; 29 + } __attribute__ ((packed)); 30 + 31 + void setup_gdb_notifications(uint64_t slide, uint64_t addr) 32 + { 33 + orig_dyld_all_image_infos = (struct dyld_all_image_infos*)(addr + slide); 34 + 35 + // dyld will later rebase the address in notification, 36 + // but at this point we must add slide manually. 37 + struct jump* jump = (struct jump*)(((uint64_t)orig_dyld_all_image_infos->notification) + slide); 38 + 39 + // Rewrite instructions in the notification function to redirect the call to us. 40 + #ifdef __x86_64__ 41 + jump->mov = 0xb948; // movabs imm,%rcx 42 + jump->addr = (void*) &dyld_notification_wrapper; // immediate for preceding movabs 43 + jump->jump = 0xe1ff; // jmpq *%ecx 44 + #else 45 + # error TODO: Unsupported platform 46 + #endif 47 + } 48 + 49 + void dyld_notification_wrapper(enum dyld_image_mode mode, uint32_t infoCount, const struct dyld_image_info info[]) 50 + { 51 + // Copy over all data from dyld's copy of the structure to the one GDB can find (ours) 52 + memcpy(&_dyld_all_image_infos, orig_dyld_all_image_infos, sizeof(_dyld_all_image_infos)); 53 + _dyld_all_image_infos.notification = &gdb_notifier; 54 + 55 + printf("Got notification from dyld\n"); 56 + gdb_notifier(mode, infoCount, info); 57 + } 58 +
+9
src/dyld/gdb.h
··· 1 + #ifndef _GDB_H_ 2 + #define _GDB_H_ 3 + #include <mach-o/dyld_images.h> 4 + #include <stdint.h> 5 + 6 + void setup_gdb_notifications(uint64_t slide, uint64_t addr); 7 + 8 + #endif 9 +
+18
src/dyld/mldr.c
··· 14 14 #include <dlfcn.h> 15 15 #include "elfcalls.h" 16 16 #include "threads.h" 17 + #include "gdb.h" 17 18 18 19 #ifndef PAGE_SIZE 19 20 # define PAGE_SIZE 4096 ··· 232 233 233 234 if (seg->fileoff == 0) 234 235 mappedHeader = (struct mach_header_64*) (seg->vmaddr + slide); 236 + } 237 + 238 + if (strcmp(SEG_DATA, seg->segname) == 0) 239 + { 240 + // Look for section named __all_image_info for GDB integration 241 + struct section_64* sect = (struct section_64*) (seg+1); 242 + struct section_64* end = (struct section_64*) (&cmds[p + lc->cmdsize]); 243 + 244 + while (sect < end) 245 + { 246 + if (strncmp(sect->sectname, "__all_image_info", 16) == 0) 247 + { 248 + setup_gdb_notifications(slide, sect->addr); 249 + break; 250 + } 251 + sect++; 252 + } 235 253 } 236 254 break; 237 255 }