this repo has no description
1
fork

Configure Feed

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

dyld now working, added support for LC_MAIN

+131 -77
+2
CMakeLists.txt
··· 5 5 cmake_policy(SET CMP0003 NEW) 6 6 endif(COMMAND cmake_policy) 7 7 8 + ADD_DEFINITIONS(-ggdb) 9 + 8 10 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) 9 11 include_directories(${CMAKE_CURRENT_SOURCE_DIR}) 10 12 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/xnu)
+1
config.h.in
··· 2 2 #define CONFIG_H 3 3 4 4 #define LIB_PATH "${CMAKE_INSTALL_PREFIX}/lib/darling" 5 + #define INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}" 5 6 6 7 /* 7 8 * Credit:
+20 -11
src/dyld/MachOLoader.cpp
··· 164 164 165 165 // __darwin_dlopen checks if already loaded 166 166 // automatically adds a reference if so 167 + 167 168 if (!__darwin_dlopen(dylib.c_str(), DARWIN_RTLD_GLOBAL)) 168 169 throw std::runtime_error("Cannot load " + dylib + "!"); 169 170 } ··· 528 529 mach.close(); 529 530 530 531 runPendingInitFuncs(argc, argv, envp, apple); 531 - 532 - LOG << "booting from " << (void*)mach.entry() << "..." << std::endl; 533 532 534 533 fflush(stdout); 535 534 assert(argc > 0); 536 - 537 - boot(mach.entry(), argc, argv, envp); 538 - 539 - /* 540 - int (*fp)(int, char**, char**) = 541 - (int(*)(int, char**, char**))mach.entry(); 542 - int ret = fp(argc, argv, envp); 543 - exit(ret); 544 - */ 535 + 536 + if (mach.entry()) 537 + { 538 + LOG << "booting from " << (void*)mach.entry() << "..." << std::endl; 539 + LOG << "==========\n"; 540 + boot(mach.entry(), argc, argv, envp); 541 + } 542 + else if (mach.main()) 543 + { 544 + LOG << "running main at " << (void*) mach.main() << "...\n"; 545 + LOG << "==========\n"; 546 + 547 + int (*pMain)(int, char**, char**, char**) = reinterpret_cast<int (*)(int, char**, char**, char**)>(mach.main()); 548 + 549 + int rv = pMain(argc, argv, envp, apple); 550 + exit(rv); 551 + } 552 + else 553 + throw std::runtime_error("No entry point found"); 545 554 } 546 555 547 556 void MachOLoader::pushTrampolineCode(unsigned int c)
+12
src/dyld/dyld.cpp
··· 66 66 return 1; 67 67 } 68 68 } 69 + 70 + extern "C" const char* dyld_getDarwinExecutablePath() 71 + { 72 + return g_darwin_executable_path; 73 + } 74 + 75 + extern "C" const char* dyld_getLoaderPath() 76 + { 77 + return g_loader_path; 78 + } 79 + 80 +
+35 -5
src/dyld/ld.cpp
··· 12 12 #include <map> 13 13 #include <string> 14 14 #include <cstring> 15 + #include <sys/types.h> 16 + #include <sys/stat.h> 15 17 #include <limits.h> 16 18 17 19 static Darling::Mutex g_ldMutex; ··· 53 55 flag = translateFlags(flag); 54 56 55 57 std::string path; 58 + start_search: 56 59 if (*filename == '/') 57 60 { 58 61 path = std::string(filename) + ".so"; 62 + LOG << "Trying " << path << std::endl; 59 63 if (::access(path.c_str(), R_OK) == 0) 60 64 RET_IF( attemptDlopen(path.c_str(), flag) ); 61 65 ··· 69 73 path = std::string(LIB_PATH) + filename; 70 74 if (::access(path.c_str(), R_OK) == 0) 71 75 RET_IF( attemptDlopen(path.c_str(), flag) ); 76 + 77 + if (strcmp(INSTALL_PREFIX, "/usr") != 0) 78 + { 79 + // We need to change the prefix in filename if present 80 + if (strncmp(filename, "/usr", 4) == 0 && strncmp(filename, INSTALL_PREFIX, strlen(INSTALL_PREFIX)) != 0) 81 + { 82 + char* name = reinterpret_cast<char*>(alloca( strlen(INSTALL_PREFIX) + strlen(filename) + 1 )); 83 + strcpy(name, INSTALL_PREFIX); 84 + strcat(name, filename+4); 85 + filename = name; 86 + LOG << "Remapping prefix, loading " << name << " instead\n"; 87 + goto start_search; 88 + } 89 + } 72 90 } 73 91 else if (strncmp(filename, "@executable_path", 16) == 0) 74 92 { ··· 127 145 return native_flags; 128 146 } 129 147 148 + static bool isSymlink(const char* path) 149 + { 150 + struct stat st; 151 + if (::stat(path, &st) == -1) 152 + return false; 153 + return S_ISLNK(st.st_mode); 154 + } 155 + 130 156 void* attemptDlopen(const char* filename, int flag) 131 157 { 132 158 char name[2048]; 159 + 160 + TRACE2(filename,flag); 133 161 134 162 // Resolve symlinks so that we don't load the same library multiple times 135 - if (::readlink(filename, name, sizeof name) == -1) 163 + if (isSymlink(filename) && ::readlink(filename, name, sizeof name) == -1) 136 164 { 137 - if (errno == EINVAL) 138 - strcpy(name, filename); 139 - else 140 - return 0; 165 + LOG << "Invalid symlink found: " << filename << std::endl; 166 + return 0; 141 167 } 168 + else 169 + strcpy(name, filename); 142 170 143 171 std::map<std::string,LoadedLibrary*>::iterator it = g_ldLibraries.find(name); 144 172 if (it != g_ldLibraries.end()) ··· 160 188 // we followed a link, so we need to check for .so., too 161 189 if ((p && name+strlen(name)-p == 3) || strstr(name, ".so.")) // endsWith() 162 190 { 191 + LOG << "Loading a native library " << name << std::endl; 163 192 // We're loading a native library 164 193 // TODO: flags 165 194 void* d = ::dlopen(name, RTLD_NOW); ··· 177 206 } 178 207 else 179 208 { 209 + LOG << "Library failed to load: " << ::dlerror() << std::endl; 180 210 strcpy(g_ldError, ::dlerror()); 181 211 return 0; 182 212 }
+1 -1
src/libSystem/CMakeLists.txt
··· 73 73 74 74 add_library(System.dylib SHARED ${libc_SRCS} ${bsdkern_SRCS} ${machkern_SRCS}) 75 75 # -luuid to make uuid_ functions available for Darwin apps 76 - target_link_libraries(System.dylib -ldl -lpthread -luuid -lmach-o) 76 + target_link_libraries(System.dylib -ldl -lpthread -luuid -lmach-o -l:libobjc.so.4 -lrt -lssl) 77 77 78 78 install(TARGETS System.dylib DESTINATION lib) 79 79
-30
src/libSystem/libc/directmap.asm
··· 15 15 _ZNSt13basic_filebufIcSt11char_traitsIcEE7seekoffExSt12_Ios_SeekdirSt13_Ios_Openmode: 16 16 jmp _ZNSt13basic_filebufIcSt11char_traitsIcEE7seekoffElSt12_Ios_SeekdirSt13_Ios_Openmode WRT ..plt 17 17 18 - global _ZNSt12__basic_fileIcE7seekoffExSt12_Ios_SeekdirSt13_Ios_Openmode 19 - extern _ZNSt12__basic_fileIcE7seekoffElSt12_Ios_SeekdirSt13_Ios_Openmode 20 - _ZNSt12__basic_fileIcE7seekoffExSt12_Ios_SeekdirSt13_Ios_Openmode: 21 - jmp _ZNSt12__basic_fileIcE7seekoffElSt12_Ios_SeekdirSt13_Ios_Openmode WRT ..plt 22 - 23 18 global _ZNSi5seekgExSt12_Ios_Seekdir 24 19 extern _ZNSi5seekgElSt12_Ios_Seekdir 25 20 _ZNSi5seekgExSt12_Ios_Seekdir: ··· 80 75 opendir$INODE64: 81 76 jmp opendir WRT ..plt 82 77 83 - global readdir$INODE64 84 - extern __darwin_readdir64 85 - readdir$INODE64: 86 - jmp __darwin_readdir64 WRT ..plt 87 - 88 - global stat$INODE64 89 - extern __darwin_stat64 90 - stat$INODE64: 91 - jmp __darwin_stat64 WRT ..plt 92 - 93 - global fstat$INODE64 94 - extern __darwin_fstat64 95 - fstat$INODE64: 96 - jmp __darwin_fstat64 WRT ..plt 97 - 98 - global lstat$INODE64 99 - extern __darwin_lstat64 100 - lstat$INODE64: 101 - jmp __darwin_lstat64 WRT ..plt 102 - 103 78 global CC_MD5_Init 104 79 extern MD5_Init 105 80 CC_MD5_Init: ··· 124 99 extern __cxa_atexit 125 100 __darwin_atexit: 126 101 jmp __cxa_atexit WRT ..plt 127 - 128 - global _ZNSt12__basic_fileIcE8sys_openEP7__sFILESt13_Ios_Openmode 129 - extern _ZNSt12__basic_fileIcE8sys_openEP8_IO_FILESt13_Ios_Openmode 130 - _ZNSt12__basic_fileIcE8sys_openEP7__sFILESt13_Ios_Openmode: 131 - jmp _ZNSt12__basic_fileIcE8sys_openEP8_IO_FILESt13_Ios_Openmode WRT ..plt 132 102 133 103 global __darwin_select$1050 134 104 extern __darwin_select
+1 -8
src/libSystem/libc/directmap.lst
··· 1 1 __error;__errno_location 2 2 _ZNSt13basic_filebufIwSt11char_traitsIwEE7seekoffExSt12_Ios_SeekdirSt13_Ios_Openmode;_ZNSt13basic_filebufIwSt11char_traitsIwEE7seekoffElSt12_Ios_SeekdirSt13_Ios_Openmode 3 3 _ZNSt13basic_filebufIcSt11char_traitsIcEE7seekoffExSt12_Ios_SeekdirSt13_Ios_Openmode;_ZNSt13basic_filebufIcSt11char_traitsIcEE7seekoffElSt12_Ios_SeekdirSt13_Ios_Openmode 4 - _ZNSt12__basic_fileIcE7seekoffExSt12_Ios_SeekdirSt13_Ios_Openmode;_ZNSt12__basic_fileIcE7seekoffElSt12_Ios_SeekdirSt13_Ios_Openmode 5 4 _ZNSi5seekgExSt12_Ios_Seekdir;_ZNSi5seekgElSt12_Ios_Seekdir 6 5 _ZNSt13basic_istreamIwSt11char_traitsIwEE5seekgExSt12_Ios_Seekdir;_ZNSt13basic_istreamIwSt11char_traitsIwEE5seekgElSt12_Ios_Seekdir 7 6 _ZNSo5seekpExSt12_Ios_Seekdir;_ZNSo5seekpElSt12_Ios_Seekdir ··· 14 13 _ZNSt15basic_streambufIwSt11char_traitsIwEE10pubseekoffExSt12_Ios_SeekdirSt13_Ios_Openmode;_ZNSt15basic_streambufIwSt11char_traitsIwEE10pubseekoffElSt12_Ios_SeekdirSt13_Ios_Openmode 15 14 _ZNSt12strstreambuf7seekoffExSt12_Ios_SeekdirSt13_Ios_Openmode;_ZNSt12strstreambuf7seekoffElSt12_Ios_SeekdirSt13_Ios_Openmode 16 15 opendir$INODE64;opendir 17 - readdir$INODE64;__darwin_readdir64 18 - stat$INODE64;__darwin_stat64 19 - fstat$INODE64;__darwin_fstat64 20 - lstat$INODE64;__darwin_lstat64 21 16 CC_MD5_Init;MD5_Init 22 17 CC_MD5_Update;MD5_Update 23 18 CC_MD5_Final;MD5_Final 24 19 CC_MD5;MD5 25 20 __darwin_atexit;__cxa_atexit 26 - _ZNSt12__basic_fileIcE8sys_openEP7__sFILESt13_Ios_Openmode;_ZNSt12__basic_fileIcE8sys_openEP8_IO_FILESt13_Ios_Openmode 27 21 __darwin_select$1050;__darwin_select 28 - __darwin_select;select; TEMPORARY, needs to be implemented! 29 22 __darwin_warn;__darwin__warn 30 23 __darwin_err;__darwin__err 31 24 32 - ;; dir.cpp 25 + ;;; dir.cpp 33 26 __darwin_opendir$INODE64;__darwin_opendir 34 27 __darwin_telldir$INODE64;telldir
+12 -12
src/libSystem/libc/err.c
··· 184 184 { 185 185 va_list ap; 186 186 va_start(ap, fmt); 187 - verrc(eval, errno, fmt, ap); 187 + __darwin_verrc(eval, errno, fmt, ap); 188 188 va_end(ap); 189 189 } 190 190 ··· 194 194 const char *fmt; 195 195 va_list ap; 196 196 { 197 - verrc(eval, errno, fmt, ap); 197 + __darwin_verrc(eval, errno, fmt, ap); 198 198 } 199 199 200 200 void ··· 202 202 { 203 203 va_list ap; 204 204 va_start(ap, fmt); 205 - verrc(eval, code, fmt, ap); 205 + __darwin_verrc(eval, code, fmt, ap); 206 206 va_end(ap); 207 207 } 208 208 ··· 211 211 { 212 212 code = errnoDarwinToLinux(code); 213 213 if (_e_err_file == 0) 214 - err_set_file((FILE *)0); 214 + __darwin_err_set_file((FILE *)0); 215 215 fprintf(_e_err_file, "%s: ", program_invocation_name); 216 216 if (fmt != NULL) { 217 217 _e_visprintf(_e_err_file, fmt, ap); ··· 233 233 { 234 234 va_list ap; 235 235 va_start(ap, fmt); 236 - verrx(eval, fmt, ap); 236 + __darwin_verrx(eval, fmt, ap); 237 237 va_end(ap); 238 238 } 239 239 ··· 241 241 __darwin_verrx(int eval, const char *fmt, va_list ap) 242 242 { 243 243 if (_e_err_file == 0) 244 - err_set_file((FILE *)0); 244 + __darwin_err_set_file((FILE *)0); 245 245 fprintf(_e_err_file, "%s: ", program_invocation_name); 246 246 if (fmt != NULL) 247 247 _e_visprintf(_e_err_file, fmt, ap); ··· 263 263 { 264 264 va_list ap; 265 265 va_start(ap, fmt); 266 - vwarnc(errno, fmt, ap); 266 + __darwin_vwarnc(errno, fmt, ap); 267 267 va_end(ap); 268 268 } 269 269 270 270 void 271 271 __darwin_vwarn(const char *fmt, va_list ap) 272 272 { 273 - vwarnc(errno, fmt, ap); 273 + __darwin_vwarnc(errno, fmt, ap); 274 274 } 275 275 276 276 void ··· 278 278 { 279 279 va_list ap; 280 280 va_start(ap, fmt); 281 - vwarnc(code, fmt, ap); 281 + __darwin_vwarnc(code, fmt, ap); 282 282 va_end(ap); 283 283 } 284 284 ··· 287 287 { 288 288 code = errnoDarwinToLinux(code); 289 289 if (_e_err_file == 0) 290 - err_set_file((FILE *)0); 290 + __darwin_err_set_file((FILE *)0); 291 291 fprintf(_e_err_file, "%s: ", program_invocation_name); 292 292 if (fmt != NULL) { 293 293 _e_visprintf(_e_err_file, fmt, ap); ··· 301 301 { 302 302 va_list ap; 303 303 va_start(ap, fmt); 304 - vwarnx(fmt, ap); 304 + __darwin_vwarnx(fmt, ap); 305 305 va_end(ap); 306 306 } 307 307 ··· 309 309 __darwin_vwarnx(const char *fmt, va_list ap) 310 310 { 311 311 if (_e_err_file == 0) 312 - err_set_file((FILE *)0); 312 + __darwin_err_set_file((FILE *)0); 313 313 fprintf(_e_err_file, "%s: ", program_invocation_name); 314 314 if (fmt != NULL) 315 315 _e_visprintf(_e_err_file, fmt, ap);
+5 -4
src/libSystem/libc/errno.h
··· 14 14 char* __darwin_strerror(int errnum); 15 15 int __darwin_strerror_r(int errnum, char *strerrbuf, size_t buflen); 16 16 17 + int errnoDarwinToLinux(int err); 18 + int errnoLinuxToDarwin(int err); 19 + void errnoOut(); 20 + void errnoIn(); 21 + 17 22 #ifdef __cplusplus 18 23 } 19 24 #endif 20 25 21 - int errnoDarwinToLinux(int err); 22 - int errnoLinuxToDarwin(int err); 23 - void errnoOut(); 24 - void errnoIn(); 25 26 26 27 #endif
+10 -2
src/libSystem/libc/mac.c
··· 169 169 return mmap(addr, length, prot, flags, fd, offset); 170 170 } 171 171 172 + extern char* dyld_getDarwinExecutablePath(); 173 + extern char* dyld_getLoaderPath(); 172 174 173 - extern char __darwin_executable_path[PATH_MAX]; 174 - char __loader_path[PATH_MAX]; 175 + char* __darwin_executable_path = 0; 176 + char* __loader_path = 0; 177 + 178 + __attribute__((constructor)) void getLoaderGlobals() 179 + { 180 + __darwin_executable_path = dyld_getDarwinExecutablePath(); 181 + __loader_path = dyld_getLoaderPath(); 182 + } 175 183 176 184 int _NSGetExecutablePath(char* buf, unsigned int* size) { 177 185 strcpy(buf, __darwin_executable_path);
+3 -3
src/libSystem/libc/stdio.cpp
··· 9 9 #include <stdio_ext.h> 10 10 #include "log.h" 11 11 12 - extern "C" __darwin_FILE* __stdinp; 13 - extern "C" __darwin_FILE* __stdoutp; 14 - extern "C" __darwin_FILE* __stderrp; 12 + extern "C" __darwin_FILE* __stdinp = 0; 13 + extern "C" __darwin_FILE* __stdoutp = 0; 14 + extern "C" __darwin_FILE* __stderrp = 0; 15 15 16 16 static __darwin_FILE* InitDarwinFILE(FILE* linux_fp) 17 17 {
+3 -1
src/libmach-o/MachO.h
··· 42 42 public: 43 43 __attribute__ ((visibility ("default"))) 44 44 static MachO* readFile(std::string path, const char* arch, bool need_exports = true); 45 + __attribute__ ((visibility ("default"))) 45 46 static bool isMachO(const char* path); 46 47 47 48 virtual ~MachO() {} ··· 100 101 const char* base() const { return m_base; } 101 102 102 103 uint64_t entry() const { return m_entry; } 104 + uint64_t main() const { return m_main; } 103 105 104 106 const std::vector<uint64_t>& init_funcs() const { return m_init_funcs; } 105 107 const std::vector<uint64_t>& exit_funcs() const { return m_exit_funcs; } ··· 121 123 std::vector<Export*> m_exports; 122 124 std::vector<Symbol> m_symbols; 123 125 const char* m_base; 124 - uint64_t m_entry; 126 + uint64_t m_entry, m_main; 125 127 std::vector<uint64_t> m_init_funcs; 126 128 std::vector<uint64_t> m_exit_funcs; 127 129 uint64_t m_dyld_data;
+13
src/libmach-o/MachOImpl.cpp
··· 88 88 89 89 section* sections = reinterpret_cast<section*>(cmds_ptr + sizeof(segment_command)); 90 90 91 + if (!strcmp(segment->segname, "__TEXT")) 92 + m_text_offset = (intptr_t) segment->vmaddr; // needed for LC_MAIN 93 + 91 94 for (uint32_t j = 0; j < segment->nsects; j++) 92 95 { 93 96 const section& sec = sections[j]; ··· 231 234 CHECK(fd); 232 235 m_fd = fd; 233 236 m_offset = offset; 237 + m_text_offset = 0; 238 + m_main = 0; 234 239 235 240 if (!m_mapped_size) 236 241 m_mapped_size = ::lseek(m_fd, 0, SEEK_END); ··· 474 479 m_entry = reinterpret_cast<uint32_t*>(cmds_ptr)[14]; 475 480 476 481 LOGF("entry=%llx\n", (ull)m_entry); 482 + break; 483 + } 484 + 485 + case LC_MAIN: 486 + { 487 + entry_point_command* cmd = reinterpret_cast<entry_point_command*>(cmds_ptr); 488 + LOGF("MAIN: entry offset: %x\n", cmd->entryoff); 489 + m_main = reinterpret_cast<uint64_t>(m_text_offset + cmd->entryoff); 477 490 break; 478 491 } 479 492
+9
src/libmach-o/MachOImpl.h
··· 64 64 char* m_mapped; 65 65 size_t m_mapped_size; 66 66 bool m_need_exports; 67 + intptr_t m_text_offset; 67 68 68 69 struct sym 69 70 { ··· 88 89 uint16_t n_desc; 89 90 uint64_t n_value; 90 91 }; 92 + }; 93 + 94 + #define LC_MAIN (0x28|LC_REQ_DYLD) 95 + struct entry_point_command { 96 + uint32_t cmd; /* LC_MAIN only used in MH_EXECUTE filetypes */ 97 + uint32_t cmdsize; /* 24 */ 98 + uint64_t entryoff; /* file (__TEXT) offset of main() */ 99 + uint64_t stacksize;/* if not zero, initial stack size */ 91 100 }; 92 101 93 102 #endif
+4
tools/gendirectmapassembly
··· 7 7 echo 8 8 9 9 while read line; do 10 + if [[ $line == ";;*" ]]; then 11 + continue 12 + fi 13 + 10 14 arrIN=(${line//;/ }) 11 15 12 16 if [ ${#arrIN[@]} -ne 2 ]; then