this repo has no description
1
fork

Configure Feed

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

Hello world now runs

+5500 -55
+1
cmake/use_ld64.cmake
··· 18 18 -Wl,-dylib_file,/usr/lib/system/libsystem_coreservices.dylib:${CMAKE_BINARY_DIR}/src/libsystem_coreservices/libsystem_coreservices.dylib \ 19 19 -Wl,-dylib_file,/usr/lib/system/libsystem_notify.dylib:${CMAKE_BINARY_DIR}/src/libnotify/libsystem_notify.dylib \ 20 20 -Wl,-dylib_file,/usr/lib/system/libdispatch.dylib:${CMAKE_BINARY_DIR}/src/external/libdispatch/src/libdispatch.dylib \ 21 + -Wl,-dylib_file,/usr/lib/system/libmacho.dylib:${CMAKE_BINARY_DIR}/src/libmacho/libmacho.dylib \ 21 22 -Wl,-dylib_file,/usr/lib/system/libsystem_malloc.dylib:${CMAKE_BINARY_DIR}/src/libmalloc/libsystem_malloc.dylib ") 22 23 23 24 add_dependencies(${target} x86_64-apple-darwin11-ld)
+23 -6
platform-include/mach-o/fat.h
··· 1 - // Modified by Lubos Dolezel for Darling build 2 1 /* 3 - * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 2 + * Copyright (c) 2016 Apple, Inc. All rights reserved. 4 3 * 5 4 * @APPLE_LICENSE_HEADER_START@ 6 5 * ··· 43 42 * and contains the constants for the possible values of these types. 44 43 */ 45 44 #include <stdint.h> 46 - 47 - typedef int cpu_type_t; 48 - typedef int cpu_subtype_t; 45 + #include <mach/machine.h> 46 + #include <architecture/byte_order.h> 49 47 50 48 #define FAT_MAGIC 0xcafebabe 51 49 #define FAT_CIGAM 0xbebafeca /* NXSwapLong(FAT_MAGIC) */ 52 50 53 51 struct fat_header { 54 - uint32_t magic; /* FAT_MAGIC */ 52 + uint32_t magic; /* FAT_MAGIC or FAT_MAGIC_64 */ 55 53 uint32_t nfat_arch; /* number of structs that follow */ 56 54 }; 57 55 ··· 61 59 uint32_t offset; /* file offset to this object file */ 62 60 uint32_t size; /* size of this object file */ 63 61 uint32_t align; /* alignment as a power of 2 */ 62 + }; 63 + 64 + /* 65 + * The support for the 64-bit fat file format described here is a work in 66 + * progress and not yet fully supported in all the Apple Developer Tools. 67 + * 68 + * When a slice is greater than 4mb or an offset to a slice is greater than 4mb 69 + * then the 64-bit fat file format is used. 70 + */ 71 + #define FAT_MAGIC_64 0xcafebabf 72 + #define FAT_CIGAM_64 0xbfbafeca /* NXSwapLong(FAT_MAGIC_64) */ 73 + 74 + struct fat_arch_64 { 75 + cpu_type_t cputype; /* cpu specifier (int) */ 76 + cpu_subtype_t cpusubtype; /* machine specifier (int) */ 77 + uint64_t offset; /* file offset to this object file */ 78 + uint64_t size; /* size of this object file */ 79 + uint32_t align; /* alignment as a power of 2 */ 80 + uint32_t reserved; /* reserved */ 64 81 }; 65 82 66 83 #endif /* _MACH_O_FAT_H_ */
+26 -3
platform-include/mach-o/ranlib.h
··· 1 1 /* 2 - * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 2 + * Copyright (c) 2016 Apple, Inc. All rights reserved. 3 3 * 4 4 * @APPLE_LICENSE_HEADER_START@ 5 5 * ··· 49 49 50 50 /* 51 51 * Structure of the __.SYMDEF table of contents for an archive. 52 - * __.SYMDEF begins with a long giving the size in bytes of the ranlib 52 + * __.SYMDEF begins with a uint32_t giving the size in bytes of the ranlib 53 53 * structures which immediately follow, and then continues with a string 54 - * table consisting of a long giving the number of bytes of strings which 54 + * table consisting of a uint32_t giving the number of bytes of strings which 55 55 * follow and then the strings themselves. The ran_strx fields index the 56 56 * string table whose first byte is numbered 0. 57 57 */ ··· 63 63 #endif 64 64 } ran_un; 65 65 uint32_t ran_off; /* library member at this offset */ 66 + }; 67 + 68 + #define SYMDEF_64 "__.SYMDEF_64" 69 + #define SYMDEF_64_SORTED "__.SYMDEF_64 SORTED" 70 + 71 + /* 72 + * The support for the 64-bit table of contents described here is a work in 73 + * progress and not yet fully supported in all the Apple Developer Tools. 74 + * 75 + * When an archive offset to a library member is more than 32-bits then this is 76 + * the structure of the __.SYMDEF_64 table of contents for an archive. 77 + * __.SYMDEF_64 begins with a uint64_t giving the size in bytes of the ranlib 78 + * structures which immediately follow, and then continues with a string 79 + * table consisting of a uint64_t giving the number of bytes of strings which 80 + * follow and then the strings themselves. The ran_strx fields index the 81 + * string table whose first byte is numbered 0. 82 + */ 83 + 84 + struct ranlib_64 { 85 + union { 86 + uint64_t ran_strx; /* string table index of */ 87 + } ran_un; 88 + uint64_t ran_off; /* library member at this offset */ 66 89 }; 67 90 #endif /* _MACH_O_RANLIB_H_ */
+1
src/CMakeLists.txt
··· 55 55 add_subdirectory(kernel) 56 56 add_subdirectory(libc) 57 57 add_subdirectory(libm) 58 + add_subdirectory(libmacho) 58 59 add_subdirectory(libgcc) 59 60 add_subdirectory(copyfile) 60 61 add_subdirectory(libinfo)
+1 -1
src/duct/src/CMakeLists.txt
··· 8 8 add_definitions(-D__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1080) 9 9 10 10 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdinc -D__DARWIN_UNIX03 -fPIC -w") 11 - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib -Wl,-flat_namespace -Wl,-undefined,suppress") 11 + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib -Wl,-bind_at_load -Wl,-flat_namespace -Wl,-undefined,suppress") 12 12 13 13 SET(duct_sources 14 14 commpage.c libnotify.c numcpus.c
+7
src/duct/src/acl.c
··· 8 8 return 0; 9 9 } 10 10 11 + void _simple_asl_msg_set() 12 + { 13 + } 14 + 15 + void _simple_asl_send() 16 + { 17 + }
+1 -1
src/dyld-apple/CMakeLists.txt
··· 11 11 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdinc") 12 12 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdinc -std=c++11 -include ${CMAKE_SOURCE_DIR}/src/duct/include/CrashReporterClient.h") 13 13 set(CMAKE_SHARED_LINKER_FLAGS_SAVED "${CMAKE_SHARED_LINKER_FLAGS}") 14 - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib -Wl,-flat_namespace -Wl,-undefined,suppress") 14 + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib -Wl,-bind_at_load -Wl,-flat_namespace -Wl,-undefined,suppress") 15 15 16 16 add_definitions(-DTARGET_OS_MAC=1) 17 17 add_definitions(-DHAVE_STDINT_H=1)
+7
src/dyld-apple/src/dyldAPIs.cpp
··· 112 112 #if DEPRECATED_APIS_SUPPORTED 113 113 #endif 114 114 115 + #ifdef DARLING 116 + extern "C" int mach_driver_get_fd(void); 117 + #endif 118 + 115 119 static void unimplemented() 116 120 { 117 121 dyld::halt("unimplemented dyld function\n"); ··· 206 210 {"__dyld_NSGetSectionDataInObjectFileImage", (void*)NSGetSectionDataInObjectFileImage }, 207 211 #if OLD_LIBSYSTEM_SUPPORT 208 212 {"__dyld_link_module", (void*)_dyld_link_module }, 213 + #endif 214 + #ifdef DARLING 215 + {"__dyld_get_mach_driver_fd", (void*)mach_driver_get_fd }, 209 216 #endif 210 217 #endif //DEPRECATED_APIS_SUPPORTED 211 218
+1 -1
src/dyld-apple/src/dyldLibSystemGlue.c
··· 68 68 // dylib1.o is used, it overrides this, otherwise this implementation is used. 69 69 __attribute__((weak)) 70 70 #endif 71 - __attribute__((visibility("hidden"))) 71 + //__attribute__((visibility("hidden"))) 72 72 int _dyld_func_lookup(const char* dyld_func_name, void **address) 73 73 { 74 74 return (*myDyldSection.lookup)(dyld_func_name, address);
+1
src/dyld/CMakeLists.txt
··· 18 18 19 19 set(mldr_sources 20 20 mldr.c 21 + threads.c 21 22 ) 22 23 add_executable(mldr ${mldr_sources}) 23 24 target_link_libraries(mldr -lpthread -ldl)
+21
src/dyld/elfcalls.h
··· 1 + #ifndef _ELFCALLS_H_ 2 + #define _ELFCALLS_H_ 3 + #include <stdint.h> 4 + 5 + struct elf_calls 6 + { 7 + void* (*dlopen)(const char* name); 8 + int (*dlclose)(void* lib); 9 + void* (*dlsym)(void* lib, const char* name); 10 + char* (*dlerror)(void); 11 + void* (*darling_thread_create)(unsigned long stack_size, unsigned long pthobj_size, 12 + void* entry_point, uintptr_t arg3, 13 + uintptr_t arg4, uintptr_t arg5, uintptr_t arg6, 14 + int (*thread_self_trap)()); 15 + int (*darling_thread_terminate)(void* stackaddr, 16 + unsigned long freesize, unsigned long pthobj_size); 17 + void* (*darling_thread_get_stack)(void); 18 + }; 19 + 20 + #endif 21 +
+29 -2
src/dyld/mldr.c
··· 11 11 #include <stdbool.h> 12 12 #include <mach-o/loader.h> 13 13 #include <mach-o/fat.h> 14 + #include <dlfcn.h> 15 + #include "elfcalls.h" 16 + #include "threads.h" 14 17 15 18 #ifndef PAGE_SIZE 16 19 # define PAGE_SIZE 4096 ··· 30 33 static void load(const char* path, uint64_t* entryPoint_out, uint64_t* mh_out); 31 34 static int native_prot(int prot); 32 35 static void apply_root_path(char* path); 36 + static char* elfcalls_make(void); 33 37 34 38 int main(int argc, const char** argv) 35 39 { 36 40 uint64_t entryPoint, mh; 37 41 void** sp; 38 42 int pushCount = 0; 39 - const char* apple[2]; 43 + const char* apple[3]; 40 44 41 45 if (argc <= 1) 42 46 { ··· 60 64 #endif 61 65 62 66 apple[0] = argv[1]; 63 - apple[1] = NULL; 67 + apple[1] = elfcalls_make(); 68 + apple[2] = NULL; 64 69 65 70 GETSP(sp); 66 71 sp--; ··· 324 329 while (next != NULL); 325 330 } 326 331 332 + static void* dlopen_simple(const char* name) 333 + { 334 + return dlopen(name, RTLD_LAZY); 335 + } 336 + 337 + static char* elfcalls_make(void) 338 + { 339 + static char param[32]; 340 + static struct elf_calls calls; 341 + 342 + calls.dlopen = dlopen_simple; 343 + calls.dlclose = dlclose; 344 + calls.dlsym = dlsym; 345 + calls.dlerror = dlerror; 346 + calls.darling_thread_create = __darling_thread_create; 347 + calls.darling_thread_terminate = __darling_thread_terminate; 348 + calls.darling_thread_get_stack = __darling_thread_get_stack; 349 + 350 + sprintf(param, "elf_calls=%p", &calls); 351 + return param; 352 + } 353 +
+258
src/dyld/threads.c
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2015 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #include "threads.h" 21 + #include <pthread.h> 22 + #include <sys/mman.h> 23 + #include <semaphore.h> 24 + #include <string.h> 25 + #include <stdbool.h> 26 + #include <stdlib.h> 27 + 28 + // The point of this file is build macOS threads on top of native libc's threads, 29 + // otherwise it would not be possible to make native calls from these threads. 30 + 31 + typedef void (*thread_ep)(void**, int, ...); 32 + struct arg_struct 33 + { 34 + thread_ep entry_point; 35 + uintptr_t arg3; 36 + uintptr_t arg4; 37 + uintptr_t arg5; 38 + uintptr_t arg6; 39 + union 40 + { 41 + int (*thread_self_trap)(); 42 + int port; 43 + }; 44 + unsigned long pth_obj_size; 45 + void* pth; 46 + }; 47 + struct reaper_item 48 + { 49 + struct reaper_item* next; 50 + pthread_t thread; 51 + void* stack; 52 + size_t stacksize; 53 + }; 54 + 55 + static void* darling_thread_entry(void* p); 56 + static void start_reaper(); 57 + 58 + static sem_t reaper_sem; 59 + static pthread_mutex_t reaper_mutex = PTHREAD_MUTEX_INITIALIZER; 60 + static struct reaper_item *reaper_items_front = NULL, *reaper_items_end = NULL; 61 + static void reaper_item_push(struct reaper_item* item); 62 + static struct reaper_item* reaper_item_pop(void); 63 + 64 + #ifndef PTHREAD_STACK_MIN 65 + # define PTHREAD_STACK_MIN 16384 66 + #endif 67 + 68 + void* __darling_thread_create(unsigned long stack_size, unsigned long pth_obj_size, 69 + void* entry_point, uintptr_t arg3, 70 + uintptr_t arg4, uintptr_t arg5, uintptr_t arg6, 71 + int (*thread_self_trap)()) 72 + { 73 + static pthread_once_t reaper_once = PTHREAD_ONCE_INIT; 74 + 75 + struct arg_struct args = { (thread_ep) entry_point, arg3, 76 + arg4, arg5, arg6, thread_self_trap, pth_obj_size, NULL }; 77 + pthread_attr_t attr; 78 + pthread_t nativeLibcThread; 79 + void* pth; 80 + 81 + pthread_once(&reaper_once, start_reaper); 82 + 83 + pthread_attr_init(&attr); 84 + //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 85 + // pthread_attr_setstacksize(&attr, stack_size); 86 + 87 + pth = mmap(NULL, stack_size + pth_obj_size + 0x1000, PROT_READ | PROT_WRITE, 88 + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 89 + pthread_attr_setstack(&attr, ((char*)pth) + pth_obj_size, stack_size - pth_obj_size - 0x1000); 90 + 91 + // std::cout << "Allocated stack at " << pth << ", size " << stack_size << std::endl; 92 + pth = ((char*) pth) + stack_size + 0x1000; 93 + 94 + args.pth = pth; 95 + pthread_create(&nativeLibcThread, &attr, darling_thread_entry, &args); 96 + pthread_attr_destroy(&attr); 97 + 98 + while (args.pth != NULL) 99 + sched_yield(); 100 + 101 + return pth; 102 + } 103 + 104 + static void* darling_thread_entry(void* p) 105 + { 106 + struct arg_struct* in_args = (struct arg_struct*) p; 107 + struct arg_struct args; 108 + 109 + memcpy(&args, in_args, sizeof(args)); 110 + 111 + args.port = args.thread_self_trap(); 112 + in_args->pth = NULL; 113 + 114 + #ifdef __x86_64__ 115 + __asm__ __volatile__ ( 116 + "movq %1, %%rdi\n" 117 + "movq 40(%0), %%rsi\n" 118 + "movq 8(%0), %%rdx\n" 119 + "testq %%rdx, %%rdx\n" 120 + "jnz 1f\n" 121 + "movq %%rsp, %%rdx\n" // wqthread hack: if 3rd arg is null, we pass sp 122 + "1:\n" 123 + "movq 16(%0), %%rcx\n" 124 + "movq 24(%0), %%r8\n" 125 + "movq 32(%0), %%r9\n" 126 + "movq %%rdi, 56(%0)\n" 127 + "movq (%0), %%rax\n" 128 + "andq $-0x10, %%rsp\n" 129 + "pushq $0\n" 130 + "pushq $0\n" 131 + "jmpq *%%rax\n" 132 + :: "a" (&args), "di" (args.pth)); 133 + #elif defined(__i386__) // args in eax, ebx, ecx, edx, edi, esi 134 + __asm__ __volatile__ ( 135 + "movl (%0), %%eax\n" 136 + "pushl %%eax\n" // address to be jumped to 137 + "movl %1, 28(%0)\n" 138 + "movl %1, %%eax\n" // 1st arg 139 + "movl 20(%0), %%ebx\n" // 2nd arg 140 + "movl 8(%0), %%edx\n" // 4th arg 141 + "movl 12(%0), %%edi\n" // 5th arg 142 + "movl 16(%0), %%esi\n" // 6th arg 143 + "movl 4(%0), %%ecx\n" // 3rd arg 144 + "testl %%ecx, %%ecx\n" // FIXME: clobbered ecx! 145 + "jnz 1f\n" 146 + "movl %%esp, %%ecx\n" 147 + "1:\n" 148 + "ret\n" // Jump to the address pushed at the beginning 149 + :: "c" (&args), "d" (args.pth)); 150 + #endif 151 + return NULL; 152 + } 153 + 154 + int __darling_thread_terminate(void* stackaddr, 155 + unsigned long freesize, unsigned long pthobj_size) 156 + { 157 + struct reaper_item* item = (struct reaper_item*) malloc(sizeof(struct reaper_item)); 158 + item->thread = pthread_self(); 159 + item->stack = stackaddr; 160 + item->stacksize = freesize; 161 + reaper_item_push(item); 162 + 163 + sem_post(&reaper_sem); 164 + 165 + pthread_exit(NULL); 166 + 167 + __builtin_unreachable(); 168 + } 169 + 170 + void* __darling_thread_get_stack(void) 171 + { 172 + pthread_attr_t attr; 173 + void* stackaddr; 174 + size_t stacksize; 175 + 176 + pthread_getattr_np(pthread_self(), &attr); 177 + pthread_attr_getstack(&attr, &stackaddr, &stacksize); 178 + 179 + return ((char*)stackaddr) + stacksize - 0x2000; 180 + } 181 + 182 + static void* reaper_entry(void* unused) 183 + { 184 + while (true) 185 + { 186 + struct reaper_item* item; 187 + 188 + sem_wait(&reaper_sem); 189 + 190 + item = reaper_item_pop(); 191 + if (!item) 192 + continue; // Should not happen! 193 + 194 + // std::cout << "Reaping thread " << (void*)item.thread << "; Free stack at " << item.stack << ", " << item.stacksize << " bytes\n"; 195 + 196 + // Wait for thread to terminate 197 + pthread_join(item->thread, NULL); 198 + 199 + // Free its stack in the extended range requested by Darwin's libc 200 + munmap(item->stack, item->stacksize); 201 + 202 + free(item); 203 + } 204 + } 205 + 206 + static void start_reaper() 207 + { 208 + pthread_attr_t attr; 209 + pthread_t thread; 210 + 211 + pthread_attr_init(&attr); 212 + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 213 + 214 + sem_init(&reaper_sem, 0, 0); 215 + pthread_create(&thread, &attr, reaper_entry, NULL); 216 + pthread_attr_destroy(&attr); 217 + } 218 + 219 + static void reaper_item_push(struct reaper_item* item) 220 + { 221 + pthread_mutex_lock(&reaper_mutex); 222 + 223 + item->next = NULL; 224 + if (reaper_items_end != NULL) 225 + { 226 + reaper_items_end->next = item; 227 + reaper_items_end = item; 228 + } 229 + else 230 + { 231 + reaper_items_front = reaper_items_end = item; 232 + } 233 + 234 + pthread_mutex_unlock(&reaper_mutex); 235 + } 236 + 237 + static struct reaper_item* reaper_item_pop(void) 238 + { 239 + struct reaper_item* e; 240 + pthread_mutex_lock(&reaper_mutex); 241 + 242 + if (reaper_items_front != NULL) 243 + { 244 + e = reaper_items_front; 245 + 246 + if (reaper_items_front == reaper_items_end) 247 + reaper_items_front = reaper_items_end = NULL; // The list is now empty 248 + else 249 + reaper_items_front = e->next; 250 + } 251 + else 252 + e = NULL; 253 + 254 + pthread_mutex_unlock(&reaper_mutex); 255 + 256 + return e; 257 + } 258 +
+41
src/dyld/threads.h
··· 1 + /* 2 + This file is part of Darling. 3 + 4 + Copyright (C) 2015 Lubos Dolezel 5 + 6 + Darling is free software: you can redistribute it and/or modify 7 + it under the terms of the GNU General Public License as published by 8 + the Free Software Foundation, either version 3 of the License, or 9 + (at your option) any later version. 10 + 11 + Darling is distributed in the hope that it will be useful, 12 + but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + GNU General Public License for more details. 15 + 16 + You should have received a copy of the GNU General Public License 17 + along with Darling. If not, see <http://www.gnu.org/licenses/>. 18 + */ 19 + 20 + #ifndef DYLD_THREADS_H 21 + #define DYLD_THREADS_H 22 + #include <stdint.h> 23 + 24 + #ifdef __cplusplus 25 + extern "C" { 26 + #endif 27 + 28 + void* __darling_thread_create(unsigned long stack_size, unsigned long pthobj_size, 29 + void* entry_point, uintptr_t arg3, 30 + uintptr_t arg4, uintptr_t arg5, uintptr_t arg6, 31 + int (*thread_self_trap)()); 32 + int __darling_thread_terminate(void* stackaddr, 33 + unsigned long freesize, unsigned long pthobj_size); 34 + void* __darling_thread_get_stack(void); 35 + 36 + #ifdef __cplusplus 37 + } 38 + #endif 39 + 40 + #endif 41 +
+1 -1
src/kernel/CMakeLists.txt
··· 45 45 #$<TARGET_OBJECTS:kqueue> 46 46 $<TARGET_OBJECTS:emulation_dyld> 47 47 ${extra_objs} 48 - $<TARGET_OBJECTS:mach_server_client>) 48 + $<TARGET_OBJECTS:mach_server_client_dyld>) 49 49 50 50 install(TARGETS system_kernel DESTINATION libexec/darling/usr/lib/system) 51 51
+8 -1
src/kernel/emulation/linux/CMakeLists.txt
··· 2 2 3 3 cmake_minimum_required(VERSION 2.4.0) 4 4 5 + cmake_policy(SET CMP0005 NEW) 5 6 enable_language(ASM) 6 7 7 8 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -fvisibility=hidden -fPIC -ggdb -Wno-int-conversion") 8 9 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib") 9 10 10 - add_definitions(-DBSDTHREAD_WRAP_LINUX_PTHREAD) 11 + add_definitions(-DBSDTHREAD_WRAP_LINUX_PTHREAD 12 + -DEMULATED_SYSNAME="Darwin" 13 + -DEMULATED_RELEASE="12.0.0" 14 + -DEMULATED_VERSION="Darwin Kernel Version 12.0.0") 15 + 16 + include_directories(${CMAKE_SOURCE_DIR}/src/dyld) 11 17 12 18 set(emulation_sources 19 + elfcalls_wrapper.c 13 20 base.c 14 21 syscalls.c 15 22 simple.c
+45
src/kernel/emulation/linux/elfcalls_wrapper.c
··· 1 + #include "elfcalls_wrapper.h" 2 + #include <elfcalls.h> 3 + 4 + extern struct elf_calls* _elfcalls; 5 + 6 + void* __darling_thread_create(unsigned long stack_size, unsigned long pthobj_size, 7 + void* entry_point, uintptr_t arg3, 8 + uintptr_t arg4, uintptr_t arg5, uintptr_t arg6, 9 + int (*thread_self_trap)()) 10 + { 11 + return _elfcalls->darling_thread_create(stack_size, pthobj_size, entry_point, 12 + arg3, arg4, arg5, arg6, thread_self_trap); 13 + } 14 + 15 + int __darling_thread_terminate(void* stackaddr, 16 + unsigned long freesize, unsigned long pthobj_size) 17 + { 18 + return _elfcalls->darling_thread_terminate(stackaddr, freesize, pthobj_size); 19 + } 20 + 21 + void* __darling_thread_get_stack(void) 22 + { 23 + return _elfcalls->darling_thread_get_stack(); 24 + } 25 + 26 + void* native_dlopen(const char* path) 27 + { 28 + return _elfcalls->dlopen(path); 29 + } 30 + 31 + char* native_dlerror(void) 32 + { 33 + return _elfcalls->dlerror(); 34 + } 35 + 36 + void* native_dlsym(void* module, const char* name) 37 + { 38 + return _elfcalls->dlsym(module, name); 39 + } 40 + 41 + int native_dlclose(void* module) 42 + { 43 + return _elfcalls->dlclose(module); 44 + } 45 +
+31
src/kernel/emulation/linux/elfcalls_wrapper.h
··· 1 + #ifndef _ELFCALLS_WRAPPER_H 2 + #define _ELFCALLS_WRAPPER_H 3 + #include <stdint.h> 4 + 5 + #ifdef __cplusplus 6 + extern "C" { 7 + #endif 8 + 9 + // Native library loading 10 + void* native_dlopen(const char* path); 11 + char* native_dlerror(void); 12 + void* native_dlsym(void* module, const char* name); 13 + int native_dlclose(void* module); 14 + 15 + // Native thread wrapping 16 + void* __darling_thread_create(unsigned long stack_size, unsigned long pthobj_size, 17 + void* entry_point, uintptr_t arg3, 18 + uintptr_t arg4, uintptr_t arg5, uintptr_t arg6, 19 + int (*thread_self_trap)()); 20 + 21 + int __darling_thread_terminate(void* stackaddr, 22 + unsigned long freesize, unsigned long pthobj_size); 23 + 24 + void* __darling_thread_get_stack(void); 25 + 26 + #ifdef __cplusplus 27 + } 28 + #endif 29 + 30 + #endif 31 +
+1
src/kernel/emulation/linux/linux-syscall.S
··· 1 1 .text 2 2 .globl _linux_syscall 3 + .private_extern _linux_syscall 3 4 4 5 _linux_syscall: 5 6
+3 -27
src/kernel/emulation/linux/misc/sysctl.c
··· 14 14 #include "../ext/syslog.h" 15 15 #include "getrlimit.h" 16 16 #include "darling-config.h" 17 - #include <util/IniConfig.h> 18 17 19 18 static long sysctl_name_to_oid(const char* name, int* oid_name, 20 19 unsigned long* oid_len); ··· 40 39 }; 41 40 42 41 static struct linux_utsname lu; 43 - static iniconfig_t version_conf = NULL; 44 - static inivalmap_t version_conf_sect = NULL; 45 42 static void copyout_string(const char* str, char* out, unsigned long* out_len); 46 43 static void need_uname(void); 47 44 ··· 207 204 { 208 205 case KERN_OSTYPE: 209 206 { 210 - const char* s = NULL; 211 - #ifndef VARIANT_DYLD 212 - if (version_conf_sect != NULL) 213 - s = iniconfig_valmap_get(version_conf_sect, "sysname"); 214 - #endif 215 - if (s == NULL) 216 - s = lu.sysname; 207 + const char* s = EMULATED_SYSNAME; 217 208 218 209 copyout_string(s, (char*) old, oldlen); 219 210 return 0; ··· 223 214 return 0; 224 215 case KERN_OSRELEASE: 225 216 { 226 - const char* s = NULL; 227 - #ifndef VARIANT_DYLD 228 - if (version_conf_sect != NULL) 229 - s = iniconfig_valmap_get(version_conf_sect, "release"); 230 - #endif 231 - if (s == NULL) 232 - s = lu.release; 217 + const char* s = EMULATED_RELEASE; 233 218 234 219 copyout_string(s, (char*) old, oldlen); 235 220 return 0; 236 221 } 237 222 case KERN_VERSION: 238 223 { 239 - const char* s = NULL; 240 - #ifndef VARIANT_DYLD 241 - if (version_conf_sect != NULL) 242 - s = iniconfig_valmap_get(version_conf_sect, "version"); 243 - #endif 244 - if (s == NULL) 245 - s = lu.version; 224 + const char* s = EMULATED_VERSION; 246 225 247 226 copyout_string(s, (char*) old, oldlen); 248 227 return 0; ··· 263 242 if (!lu.sysname[0]) 264 243 { 265 244 __linux_uname(&lu); 266 - version_conf = iniconfig_load(ETC_DARLING_PATH "/version.conf"); 267 - if (version_conf != NULL) 268 - version_conf_sect = iniconfig_getsection(version_conf, "uname"); 269 245 } 270 246 #endif 271 247 }
+1
src/kernel/emulation/linux/simple.c
··· 108 108 *buf = 0; 109 109 } 110 110 111 + __attribute__ ((visibility ("default"))) 111 112 void __simple_printf(const char* format, ...) 112 113 { 113 114 char buffer[512];
+1
src/kernel/emulation/linux/syscalls-table.S
··· 1 1 .text 2 2 .globl __darling_bsd_syscall 3 + .private_extern __darling_bsd_syscall 3 4 4 5 #if defined(__x86_64__) 5 6
+2 -1
src/kernel/libsyscall/CMakeLists.txt
··· 9 9 include_directories("${CMAKE_CURRENT_SOURCE_DIR}/mach") 10 10 include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../iokit") 11 11 include_directories("${CMAKE_CURRENT_SOURCE_DIR}/os") 12 - include_directories("${DARLING_TOP_DIRECTORY}/platform-include") 12 + include_directories("${CMAKE_SOURCE_DIR}/platform-include") 13 + include_directories("${CMAKE_SOURCE_DIR}/src/dyld") # for elfcalls.h 13 14 include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../libc/include") 14 15 include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../../duct/include") 15 16 include_directories("${CMAKE_CURRENT_SOURCE_DIR}/wrappers/libproc")
+17 -1
src/kernel/libsyscall/wrappers/_libkernel_init.c
··· 28 28 */ 29 29 30 30 #include "_libkernel_init.h" 31 + #include <elfcalls.h> 31 32 32 33 extern int mach_init(void); 33 34 extern void mach_driver_init(void); 34 35 35 36 /* dlsym() funcptr is for legacy support in exc_catcher */ 36 37 void* (*_dlsym)(void*, const char*) __attribute__((visibility("hidden"))); 38 + extern int strncmp(const char *s1, const char *s2, __SIZE_TYPE__ n); 39 + extern unsigned long long __simple_atoi16(const char* str, const char** endp); 37 40 38 41 __attribute__((visibility("hidden"))) 39 42 _libkernel_functions_t _libkernel_functions; 40 43 44 + __attribute__((visibility("hidden"))) 45 + struct elf_calls* _elfcalls; 46 + 41 47 void 42 48 __libkernel_init(_libkernel_functions_t fns, 43 49 const char *envp[] __attribute__((unused)), 44 - const char *apple[] __attribute__((unused)), 50 + const char *apple[], 45 51 const struct ProgramVars *vars __attribute__((unused))) 46 52 { 53 + int i; 47 54 _libkernel_functions = fns; 48 55 if (fns->dlsym) { 49 56 _dlsym = fns->dlsym; 57 + } 58 + 59 + for (i = 0; apple[i] != NULL; i++) 60 + { 61 + if (strncmp(apple[i], "elf_calls=", 10) == 0) 62 + { 63 + uintptr_t table = (uintptr_t) __simple_atoi16(apple[i] + 10, NULL); 64 + _elfcalls = (struct elf_calls*) table; 65 + } 50 66 } 51 67 52 68 mach_init();
+1
src/kernel/libsyscall/wrappers/_libkernel_init.h
··· 65 65 void (*pthread_clear_qos_tsd)(mach_port_t); 66 66 67 67 /* Subsequent versions must only add pointers! */ 68 + void (*dyld_func_lookup)(const char*,void**); 68 69 } *_libkernel_functions_t; 69 70 70 71 typedef const struct _libkernel_voucher_functions {
+2
src/kernel/mach_server/client/CMakeLists.txt
··· 12 12 ) 13 13 14 14 add_library(mach_server_client OBJECT ${mach_server_client_sources}) 15 + add_library(mach_server_client_dyld OBJECT ${mach_server_client_sources}) 16 + set_target_properties(mach_server_client_dyld PROPERTIES COMPILE_FLAGS "-DVARIANT_DYLD")
+20
src/kernel/mach_server/client/lkm.c
··· 2 2 #include "../../lkm/api.h" 3 3 #include <fcntl.h> 4 4 #include <unistd.h> 5 + #include "../../libsyscall/wrappers/_libkernel_init.h" 5 6 6 7 int driver_fd = -1; 7 8 8 9 extern int __real_ioctl(int fd, int cmd, void* arg); 10 + extern _libkernel_functions_t _libkernel_functions; 9 11 10 12 void mach_driver_init(void) 11 13 { 12 14 if (driver_fd != -1) 13 15 close(driver_fd); 16 + #ifndef VARIANT_DYLD 17 + else 18 + { 19 + // Ask for fd already set up by dyld 20 + int (*p)(void); 21 + _libkernel_functions->dyld_func_lookup("__dyld_get_mach_driver_fd", &p); 22 + 23 + driver_fd = (*p)(); 24 + 25 + if (driver_fd != -1) 26 + return; 27 + } 28 + #endif 14 29 15 30 driver_fd = open("/dev/mach", O_RDWR | O_CLOEXEC); 16 31 if (driver_fd == -1) ··· 34 49 { 35 50 return __real_ioctl(driver_fd, call_nr, arg); 36 51 } 52 + 53 + int mach_driver_get_fd(void) 54 + { 55 + return driver_fd; 56 + }
+2 -2
src/libc/CMakeLists.txt
··· 35 35 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdinc -fPIC -w ") 36 36 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -include ${CMAKE_CURRENT_SOURCE_DIR}/weak_reference.h") 37 37 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb -O0") # development flags 38 - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib -Wl,-flat_namespace -Wl,-undefined,suppress") 38 + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib -Wl,-bind_at_load -Wl,-flat_namespace -Wl,-undefined,suppress") 39 39 40 40 include_directories("${CMAKE_CURRENT_SOURCE_DIR}/fbsdcompat") 41 41 include_directories("${CMAKE_CURRENT_SOURCE_DIR}/pthreads") ··· 140 140 $<TARGET_OBJECTS:libc-darwin> 141 141 $<TARGET_OBJECTS:libc-platform> 142 142 ${extra_objs}) 143 - target_link_libraries(system_c system_kernel system_m) 143 + target_link_libraries(system_c system_kernel system_m macho) 144 144 145 145 install(TARGETS system_c DESTINATION libexec/darling/usr/lib/system) 146 146
+3 -2
src/libc/os/alloc_once_impl.c
··· 1 1 #include "alloc_once_impl.h" 2 2 #include <pthread.h> 3 + #include <sys/mman.h> 3 4 4 5 static pthread_mutex_t mutexes[OS_ALLOC_ONCE_KEY_MAX]; 5 6 static pthread_once_t mutexes_once = PTHREAD_ONCE_INIT; ··· 20 21 21 22 if (blocks[key] == NULL) 22 23 { 23 - blocks[key] = malloc(size); 24 - memset(blocks[key], 0, size); 24 + int npages = (size+4095)/4096; 25 + blocks[key] = mmap(NULL, npages*4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); 25 26 if (ctor != NULL) 26 27 ctor(blocks[key]); 27 28 }
+2
src/libc/sys/__libc_init.c
··· 39 39 extern void __pthread_pfz_setup(const char *apple[]); 40 40 extern void __guard_setup(const char *apple[]); 41 41 extern void __malloc_entropy_setup(const char *apple[]); 42 + extern void __atexit_init(void); 42 43 extern int usenew_impl; 43 44 44 45 __private_extern__ uintptr_t commpage_pfz_base; ··· 58 59 _program_vars_init(vars); 59 60 _libc_fork_init(atfork_prepare, atfork_parent, atfork_child); 60 61 LOCK_INIT(_malloc_lock); 62 + __atexit_init(); 61 63 _init_clock_port(); 62 64 __xlocale_init(); 63 65 __guard_setup(apple);
+33
src/libmacho/CMakeLists.txt
··· 1 + project(libmacho) 2 + 3 + cmake_minimum_required(VERSION 2.4.0) 4 + 5 + add_definitions(-DTARGET_OS_MAC=1) 6 + add_definitions(-DHAVE_STDINT_H=1) 7 + add_definitions(-D__APPLE__ -D__DYNAMIC__) 8 + add_definitions(-D__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1080) 9 + 10 + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdinc -D__DARWIN_UNIX03 -fPIC -w") 11 + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib -Wl,-bind_at_load -Wl,-flat_namespace -Wl,-undefined,suppress") 12 + 13 + SET(libmacho_sources 14 + arch.c 15 + get_end.c 16 + getsecbyname.c 17 + getsegbyname.c 18 + #hppa_swap.c 19 + i386_swap.c 20 + #i860_swap.c 21 + #m68k_swap.c 22 + #m88k_swap.c 23 + #ppc_swap.c 24 + slot_name.c 25 + #sparc_swap.c 26 + swap.c 27 + ) 28 + 29 + set(DYLIB_INSTALL_NAME "/usr/lib/system/libmacho.dylib") 30 + add_darling_library(macho SHARED ${libmacho_sources}) 31 + target_link_libraries(macho system_kernel) 32 + 33 + install(TARGETS macho DESTINATION libexec/darling/usr/lib/system)
+1222
src/libmacho/arch.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + /* 24 + * Copyright (c) 1993 NeXT Computer, Inc. 25 + * 26 + * Architecture computing functions. 27 + * 28 + * HISTORY 29 + * 30 + * 11 April 1997 31 + * Update m98k to ppc and removed the never supported architectures (mips, 32 + * and vax). Apple Computer, Inc. 33 + * 34 + * 4 February 1993 Lennart Lovstrand <lennart@next.com> 35 + * Redesigned to use NXArchInfo based names and signatures. 36 + * 37 + * Originally written at NeXT, Inc. 38 + * 39 + */ 40 + #ifndef RLD 41 + #include <stdint.h> 42 + #include <stdio.h> 43 + #include <stdlib.h> 44 + #include <string.h> 45 + #include <limits.h> 46 + 47 + #include "mach/machine.h" 48 + #include "mach/mach.h" 49 + #include "stuff/openstep_mach.h" 50 + #include <mach-o/fat.h> 51 + #include <mach-o/arch.h> 52 + 53 + /* The array of all currently know architecture flags (terminated with an entry 54 + * with all zeros). Pointer to this returned with NXGetAllArchInfos(). 55 + */ 56 + static const NXArchInfo ArchInfoTable[] = { 57 + /* architecture families */ 58 + {"hppa", CPU_TYPE_HPPA, CPU_SUBTYPE_HPPA_ALL, NX_BigEndian, 59 + "HP-PA"}, 60 + {"i386", CPU_TYPE_I386, CPU_SUBTYPE_I386_ALL, NX_LittleEndian, 61 + "Intel 80x86"}, 62 + { "x86_64", CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL, NX_LittleEndian, 63 + "Intel x86-64" }, 64 + { "x86_64h", CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_H, NX_LittleEndian, 65 + "Intel x86-64h Haswell" }, 66 + {"i860", CPU_TYPE_I860, CPU_SUBTYPE_I860_ALL, NX_BigEndian, 67 + "Intel 860"}, 68 + {"m68k", CPU_TYPE_MC680x0, CPU_SUBTYPE_MC680x0_ALL, NX_BigEndian, 69 + "Motorola 68K"}, 70 + {"m88k", CPU_TYPE_MC88000, CPU_SUBTYPE_MC88000_ALL, NX_BigEndian, 71 + "Motorola 88K"}, 72 + {"ppc", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL, NX_BigEndian, 73 + "PowerPC"}, 74 + {"ppc64", CPU_TYPE_POWERPC64, CPU_SUBTYPE_POWERPC_ALL, NX_BigEndian, 75 + "PowerPC 64-bit"}, 76 + {"sparc", CPU_TYPE_SPARC, CPU_SUBTYPE_SPARC_ALL, NX_BigEndian, 77 + "SPARC"}, 78 + {"arm", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_ALL, NX_LittleEndian, 79 + "ARM"}, 80 + {"arm64", CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL, NX_LittleEndian, 81 + "ARM64"}, 82 + {"any", CPU_TYPE_ANY, CPU_SUBTYPE_MULTIPLE, NX_UnknownByteOrder, 83 + "Architecture Independent"}, 84 + #ifndef DARLING 85 + {"veo", CPU_TYPE_VEO, CPU_SUBTYPE_VEO_ALL, NX_BigEndian, 86 + "veo"}, 87 + #endif 88 + /* specific architecture implementations */ 89 + {"hppa7100LC", CPU_TYPE_HPPA, CPU_SUBTYPE_HPPA_7100LC, NX_BigEndian, 90 + "HP-PA 7100LC"}, 91 + {"m68030", CPU_TYPE_MC680x0, CPU_SUBTYPE_MC68030_ONLY, NX_BigEndian, 92 + "Motorola 68030"}, 93 + {"m68040", CPU_TYPE_MC680x0, CPU_SUBTYPE_MC68040, NX_BigEndian, 94 + "Motorola 68040"}, 95 + {"i486", CPU_TYPE_I386, CPU_SUBTYPE_486, NX_LittleEndian, 96 + "Intel 80486"}, 97 + {"i486SX", CPU_TYPE_I386, CPU_SUBTYPE_486SX, NX_LittleEndian, 98 + "Intel 80486SX"}, 99 + {"pentium",CPU_TYPE_I386, CPU_SUBTYPE_PENT, NX_LittleEndian, 100 + "Intel Pentium"}, /* same as 586 */ 101 + {"i586", CPU_TYPE_I386, CPU_SUBTYPE_586, NX_LittleEndian, 102 + "Intel 80586"}, 103 + {"pentpro", CPU_TYPE_I386, CPU_SUBTYPE_PENTPRO, NX_LittleEndian, 104 + "Intel Pentium Pro"}, /* same as 686 */ 105 + {"i686", CPU_TYPE_I386, CPU_SUBTYPE_PENTPRO, NX_LittleEndian, 106 + "Intel Pentium Pro"}, 107 + {"pentIIm3", CPU_TYPE_I386, CPU_SUBTYPE_PENTII_M3, NX_LittleEndian, 108 + "Intel Pentium II Model 3" }, 109 + {"pentIIm5", CPU_TYPE_I386, CPU_SUBTYPE_PENTII_M5, NX_LittleEndian, 110 + "Intel Pentium II Model 5" }, 111 + {"pentium4", CPU_TYPE_I386, CPU_SUBTYPE_PENTIUM_4, NX_LittleEndian, 112 + "Intel Pentium 4" }, 113 + { "x86_64h", CPU_TYPE_I386, CPU_SUBTYPE_X86_64_H, NX_LittleEndian, 114 + "Intel x86-64h Haswell" }, 115 + {"ppc601", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_601, NX_BigEndian, 116 + "PowerPC 601" }, 117 + {"ppc603", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_603, NX_BigEndian, 118 + "PowerPC 603" }, 119 + {"ppc603e",CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_603e, NX_BigEndian, 120 + "PowerPC 603e" }, 121 + {"ppc603ev",CPU_TYPE_POWERPC,CPU_SUBTYPE_POWERPC_603ev,NX_BigEndian, 122 + "PowerPC 603ev" }, 123 + {"ppc604", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_604, NX_BigEndian, 124 + "PowerPC 604" }, 125 + {"ppc604e",CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_604e, NX_BigEndian, 126 + "PowerPC 604e" }, 127 + {"ppc750", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_750, NX_BigEndian, 128 + "PowerPC 750" }, 129 + {"ppc7400",CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_7400, NX_BigEndian, 130 + "PowerPC 7400" }, 131 + {"ppc7450",CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_7450, NX_BigEndian, 132 + "PowerPC 7450" }, 133 + {"ppc970", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_970, NX_BigEndian, 134 + "PowerPC 970" }, 135 + {"ppc970-64", CPU_TYPE_POWERPC64, CPU_SUBTYPE_POWERPC_970, NX_BigEndian, 136 + "PowerPC 970 64-bit"}, 137 + {"armv4t", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V4T, NX_LittleEndian, 138 + "arm v4t"}, 139 + {"armv5", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V5TEJ, NX_LittleEndian, 140 + "arm v5"}, 141 + {"xscale", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_XSCALE, NX_LittleEndian, 142 + "arm xscale"}, 143 + {"armv6", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6, NX_LittleEndian, 144 + "arm v6"}, 145 + {"armv6m", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6M, NX_LittleEndian, 146 + "arm v6m"}, 147 + {"armv7", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7, NX_LittleEndian, 148 + "arm v7"}, 149 + {"armv7f", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7F, NX_LittleEndian, 150 + "arm v7f"}, 151 + {"armv7s", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7S, NX_LittleEndian, 152 + "arm v7s"}, 153 + {"armv7k", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7K, NX_LittleEndian, 154 + "arm v7k"}, 155 + {"armv7m", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7M, NX_LittleEndian, 156 + "arm v7m"}, 157 + {"armv7em",CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7EM, NX_LittleEndian, 158 + "arm v7em"}, 159 + {"armv8", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V8, NX_LittleEndian, 160 + "arm v8"}, 161 + {"arm64",CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_V8, NX_LittleEndian, 162 + "arm64 v8"}, 163 + {"little", CPU_TYPE_ANY, CPU_SUBTYPE_LITTLE_ENDIAN, NX_LittleEndian, 164 + "Little Endian"}, 165 + {"big", CPU_TYPE_ANY, CPU_SUBTYPE_BIG_ENDIAN, NX_BigEndian, 166 + "Big Endian"}, 167 + #ifndef DARLING 168 + {"veo1",CPU_TYPE_VEO, CPU_SUBTYPE_VEO_1, NX_BigEndian, 169 + "veo 1" }, 170 + {"veo2",CPU_TYPE_VEO, CPU_SUBTYPE_VEO_2, NX_BigEndian, 171 + "veo 2" }, 172 + #endif 173 + {NULL, 0, 0, 0, 174 + NULL} 175 + }; 176 + 177 + /* 178 + * NXGetAllArchInfos() returns a pointer to an array of all currently know 179 + * architecture flags (terminated with an entry with all zeros). 180 + */ 181 + const 182 + NXArchInfo * 183 + NXGetAllArchInfos(void) 184 + { 185 + return(ArchInfoTable); 186 + } 187 + 188 + /* 189 + * NXGetLocalArchInfo() returns the NXArchInfo matching the cputype and 190 + * cpusubtype of the local host. NULL is returned if there is no matching 191 + * entry in the ArchInfoTable. 192 + */ 193 + const 194 + NXArchInfo * 195 + NXGetLocalArchInfo(void) 196 + { 197 + struct host_basic_info hbi; 198 + kern_return_t ret; 199 + unsigned int count; 200 + mach_port_t my_mach_host_self; 201 + 202 + count = HOST_BASIC_INFO_COUNT; 203 + my_mach_host_self = mach_host_self(); 204 + ret = host_info(my_mach_host_self, HOST_BASIC_INFO, (host_info_t)&hbi, 205 + &count); 206 + mach_port_deallocate(mach_task_self(), my_mach_host_self); 207 + if(ret != KERN_SUCCESS) 208 + return(NULL); 209 + 210 + /* 211 + * There is a "bug" in the kernel for compatiblity that on 212 + * an 030 machine host_info() returns cpusubtype 213 + * CPU_SUBTYPE_MC680x0_ALL and not CPU_SUBTYPE_MC68030_ONLY. 214 + */ 215 + if(hbi.cpu_type == CPU_TYPE_MC680x0 && 216 + hbi.cpu_subtype == CPU_SUBTYPE_MC680x0_ALL) 217 + hbi.cpu_subtype = CPU_SUBTYPE_MC68030_ONLY; 218 + 219 + return(NXGetArchInfoFromCpuType(hbi.cpu_type, hbi.cpu_subtype)); 220 + } 221 + 222 + /* 223 + * NXGetArchInfoFromName() is passed an architecture name (like "m68k") 224 + * and returns the matching NXArchInfo struct, or NULL if none is found. 225 + */ 226 + const 227 + NXArchInfo * 228 + NXGetArchInfoFromName( 229 + const char *name) 230 + { 231 + const NXArchInfo *ai; 232 + 233 + for(ai = ArchInfoTable; ai->name != NULL; ai++) 234 + if(strcmp(ai->name, name) == 0) 235 + return(ai); 236 + 237 + return(NULL); 238 + } 239 + 240 + /* 241 + * NXGetArchInfoFromName() is passed a cputype and cpusubtype and returns 242 + * the matching NXArchInfo struct, or NULL if none is found. If the 243 + * cpusubtype is given as CPU_SUBTYPE_MULTIPLE, the first entry that 244 + * matches the given cputype is returned. This is the NXArchInfo struct 245 + * describing the CPU "family". 246 + */ 247 + const 248 + NXArchInfo * 249 + NXGetArchInfoFromCpuType( 250 + cpu_type_t cputype, 251 + cpu_subtype_t cpusubtype) 252 + { 253 + const NXArchInfo *ai; 254 + NXArchInfo *q; 255 + 256 + for(ai = ArchInfoTable; ai->name != NULL; ai++) 257 + if(ai->cputype == cputype && 258 + (cpusubtype == CPU_SUBTYPE_MULTIPLE || 259 + ((ai->cpusubtype & ~CPU_SUBTYPE_MASK) == 260 + (cpusubtype & ~CPU_SUBTYPE_MASK)))) 261 + return(ai); 262 + 263 + if(cputype == CPU_TYPE_I386){ 264 + q = malloc(sizeof(NXArchInfo)); 265 + for(ai = ArchInfoTable; ai->name != NULL; ai++){ 266 + if(ai->cputype == cputype){ 267 + *q = *ai; 268 + break; 269 + } 270 + } 271 + q->cpusubtype = cpusubtype; 272 + q->description = malloc(sizeof("Intel family model ") + 2 + 8); 273 + if(q->description == NULL){ 274 + free(q); 275 + return(NULL); 276 + } 277 + sprintf((char *)q->description, "Intel family %u model %u", 278 + CPU_SUBTYPE_INTEL_FAMILY(cpusubtype & ~CPU_SUBTYPE_MASK), 279 + CPU_SUBTYPE_INTEL_MODEL(cpusubtype & ~CPU_SUBTYPE_MASK)); 280 + return((const NXArchInfo *)q); 281 + } 282 + else if(cputype == CPU_TYPE_POWERPC){ 283 + q = malloc(sizeof(NXArchInfo)); 284 + for(ai = ArchInfoTable; ai->name != NULL; ai++){ 285 + if(ai->cputype == cputype){ 286 + *q = *ai; 287 + break; 288 + } 289 + } 290 + q->cpusubtype = cpusubtype; 291 + q->description = malloc(sizeof("PowerPC cpusubtype ") + 10); 292 + if(q->description == NULL){ 293 + free(q); 294 + return(NULL); 295 + } 296 + sprintf((char *)q->description, "PowerPC cpusubtype %u", cpusubtype); 297 + return((const NXArchInfo *)q); 298 + } 299 + 300 + return(NULL); 301 + } 302 + 303 + /* 304 + * internal_NXFindBestFatArch() is passed a cputype and cpusubtype and a 305 + * either set of fat_arch structs or fat_arch_64 structs and selects the best 306 + * one that matches (if any) and returns an index to the array of structs or 307 + * -1 if none works for the cputype and cpusubtype. The fat_arch structs or 308 + * fat_arch_64 structs must be in the host byte sex and correct such that the 309 + * fat_archs really points to enough memory for nfat_arch structs. It is 310 + * possible that this routine could fail if new cputypes or cpusubtypes are 311 + * added and an old version of this routine is used. But if there is an exact 312 + * match between the cputype and cpusubtype and one of the structs this routine 313 + * will always succeed. 314 + */ 315 + int32_t 316 + internal_NXFindBestFatArch( 317 + cpu_type_t cputype, 318 + cpu_subtype_t cpusubtype, 319 + struct fat_arch *fat_archs, 320 + struct fat_arch_64 *fat_archs64, 321 + uint32_t nfat_archs) 322 + { 323 + uint32_t i; 324 + int32_t lowest_family, lowest_model, lowest_index; 325 + cpu_type_t fat_cputype; 326 + cpu_subtype_t fat_cpusubtype; 327 + 328 + /* 329 + * Look for the first exact match. 330 + */ 331 + for(i = 0; i < nfat_archs; i++){ 332 + if(fat_archs64 != NULL){ 333 + fat_cputype = fat_archs64[i].cputype; 334 + fat_cpusubtype = fat_archs64[i].cpusubtype; 335 + } 336 + else{ 337 + fat_cputype = fat_archs[i].cputype; 338 + fat_cpusubtype = fat_archs[i].cpusubtype; 339 + } 340 + if(fat_cputype == cputype && 341 + (fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 342 + (cpusubtype & ~CPU_SUBTYPE_MASK)) 343 + return(i); 344 + } 345 + 346 + /* 347 + * An exact match was not found so find the next best match which is 348 + * cputype dependent. 349 + */ 350 + switch(cputype){ 351 + case CPU_TYPE_I386: 352 + switch(cpusubtype & ~CPU_SUBTYPE_MASK){ 353 + default: 354 + /* 355 + * Intel cpusubtypes after the pentium (same as 586) are handled 356 + * such that they require an exact match or they can use the 357 + * pentium. If that is not found call into the loop for the 358 + * earilier subtypes. 359 + */ 360 + for(i = 0; i < nfat_archs; i++){ 361 + if(fat_archs64 != NULL){ 362 + fat_cputype = fat_archs64[i].cputype; 363 + fat_cpusubtype = fat_archs64[i].cpusubtype; 364 + } 365 + else{ 366 + fat_cputype = fat_archs[i].cputype; 367 + fat_cpusubtype = fat_archs[i].cpusubtype; 368 + } 369 + if(fat_cputype != cputype) 370 + continue; 371 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 372 + CPU_SUBTYPE_PENT) 373 + return(i); 374 + } 375 + case CPU_SUBTYPE_PENT: 376 + case CPU_SUBTYPE_486SX: 377 + /* 378 + * Since an exact match as not found look for the i486 else 379 + * break into the loop to look for the i386_ALL. 380 + */ 381 + for(i = 0; i < nfat_archs; i++){ 382 + if(fat_archs64 != NULL){ 383 + fat_cputype = fat_archs64[i].cputype; 384 + fat_cpusubtype = fat_archs64[i].cpusubtype; 385 + } 386 + else{ 387 + fat_cputype = fat_archs[i].cputype; 388 + fat_cpusubtype = fat_archs[i].cpusubtype; 389 + } 390 + if(fat_cputype != cputype) 391 + continue; 392 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 393 + CPU_SUBTYPE_486) 394 + return(i); 395 + } 396 + break; 397 + case CPU_SUBTYPE_I386_ALL: 398 + /* case CPU_SUBTYPE_I386: same as above */ 399 + case CPU_SUBTYPE_486: 400 + break; 401 + } 402 + for(i = 0; i < nfat_archs; i++){ 403 + if(fat_archs64 != NULL){ 404 + fat_cputype = fat_archs64[i].cputype; 405 + fat_cpusubtype = fat_archs64[i].cpusubtype; 406 + } 407 + else{ 408 + fat_cputype = fat_archs[i].cputype; 409 + fat_cpusubtype = fat_archs[i].cpusubtype; 410 + } 411 + if(fat_cputype != cputype) 412 + continue; 413 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 414 + CPU_SUBTYPE_I386_ALL) 415 + return(i); 416 + } 417 + 418 + /* 419 + * A match failed, promote as little as possible. 420 + */ 421 + for(i = 0; i < nfat_archs; i++){ 422 + if(fat_archs64 != NULL){ 423 + fat_cputype = fat_archs64[i].cputype; 424 + fat_cpusubtype = fat_archs64[i].cpusubtype; 425 + } 426 + else{ 427 + fat_cputype = fat_archs[i].cputype; 428 + fat_cpusubtype = fat_archs[i].cpusubtype; 429 + } 430 + if(fat_cputype != cputype) 431 + continue; 432 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 433 + CPU_SUBTYPE_486) 434 + return(i); 435 + } 436 + for(i = 0; i < nfat_archs; i++){ 437 + if(fat_archs64 != NULL){ 438 + fat_cputype = fat_archs64[i].cputype; 439 + fat_cpusubtype = fat_archs64[i].cpusubtype; 440 + } 441 + else{ 442 + fat_cputype = fat_archs[i].cputype; 443 + fat_cpusubtype = fat_archs[i].cpusubtype; 444 + } 445 + if(fat_cputype != cputype) 446 + continue; 447 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 448 + CPU_SUBTYPE_486SX) 449 + return(i); 450 + } 451 + for(i = 0; i < nfat_archs; i++){ 452 + if(fat_archs64 != NULL){ 453 + fat_cputype = fat_archs64[i].cputype; 454 + fat_cpusubtype = fat_archs64[i].cpusubtype; 455 + } 456 + else{ 457 + fat_cputype = fat_archs[i].cputype; 458 + fat_cpusubtype = fat_archs[i].cpusubtype; 459 + } 460 + if(fat_cputype != cputype) 461 + continue; 462 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 463 + CPU_SUBTYPE_586) 464 + return(i); 465 + } 466 + /* 467 + * Now look for the lowest family and in that the lowest model. 468 + */ 469 + lowest_family = CPU_SUBTYPE_INTEL_FAMILY_MAX + 1; 470 + for(i = 0; i < nfat_archs; i++){ 471 + if(fat_archs64 != NULL){ 472 + fat_cputype = fat_archs64[i].cputype; 473 + fat_cpusubtype = fat_archs64[i].cpusubtype; 474 + } 475 + else{ 476 + fat_cputype = fat_archs[i].cputype; 477 + fat_cpusubtype = fat_archs[i].cpusubtype; 478 + } 479 + if(fat_cputype != cputype) 480 + continue; 481 + if(CPU_SUBTYPE_INTEL_FAMILY(fat_cpusubtype & 482 + ~CPU_SUBTYPE_MASK) < lowest_family) 483 + lowest_family = CPU_SUBTYPE_INTEL_FAMILY( 484 + fat_cpusubtype & ~CPU_SUBTYPE_MASK); 485 + } 486 + /* if no intel cputypes found return NULL */ 487 + if(lowest_family == CPU_SUBTYPE_INTEL_FAMILY_MAX + 1) 488 + return(-1); 489 + lowest_model = INT_MAX; 490 + lowest_index = -1; 491 + for(i = 0; i < nfat_archs; i++){ 492 + if(fat_archs64 != NULL){ 493 + fat_cputype = fat_archs64[i].cputype; 494 + fat_cpusubtype = fat_archs64[i].cpusubtype; 495 + } 496 + else{ 497 + fat_cputype = fat_archs[i].cputype; 498 + fat_cpusubtype = fat_archs[i].cpusubtype; 499 + } 500 + if(fat_cputype != cputype) 501 + continue; 502 + if(CPU_SUBTYPE_INTEL_FAMILY(fat_cpusubtype & 503 + ~CPU_SUBTYPE_MASK) == lowest_family){ 504 + if(CPU_SUBTYPE_INTEL_MODEL(fat_cpusubtype & 505 + ~CPU_SUBTYPE_MASK) < lowest_model){ 506 + lowest_model = CPU_SUBTYPE_INTEL_MODEL( 507 + fat_cpusubtype & 508 + ~CPU_SUBTYPE_MASK); 509 + lowest_index = i; 510 + } 511 + } 512 + } 513 + return(lowest_index); 514 + case CPU_TYPE_X86_64: 515 + for(i = 0; i < nfat_archs; i++){ 516 + if(fat_archs64 != NULL){ 517 + fat_cputype = fat_archs64[i].cputype; 518 + fat_cpusubtype = fat_archs64[i].cpusubtype; 519 + } 520 + else{ 521 + fat_cputype = fat_archs[i].cputype; 522 + fat_cpusubtype = fat_archs[i].cpusubtype; 523 + } 524 + if(fat_cputype != cputype) 525 + continue; 526 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 527 + CPU_SUBTYPE_X86_64_ALL) 528 + return(i); 529 + } 530 + break; 531 + case CPU_TYPE_MC680x0: 532 + for(i = 0; i < nfat_archs; i++){ 533 + if(fat_archs64 != NULL){ 534 + fat_cputype = fat_archs64[i].cputype; 535 + fat_cpusubtype = fat_archs64[i].cpusubtype; 536 + } 537 + else{ 538 + fat_cputype = fat_archs[i].cputype; 539 + fat_cpusubtype = fat_archs[i].cpusubtype; 540 + } 541 + if(fat_cputype != cputype) 542 + continue; 543 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 544 + CPU_SUBTYPE_MC680x0_ALL) 545 + return(i); 546 + } 547 + /* 548 + * Try to promote if starting from CPU_SUBTYPE_MC680x0_ALL and 549 + * favor the CPU_SUBTYPE_MC68040 over the CPU_SUBTYPE_MC68030_ONLY. 550 + */ 551 + if((cpusubtype & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC680x0_ALL){ 552 + for(i = 0; i < nfat_archs; i++){ 553 + if(fat_archs64 != NULL){ 554 + fat_cputype = fat_archs64[i].cputype; 555 + fat_cpusubtype = fat_archs64[i].cpusubtype; 556 + } 557 + else{ 558 + fat_cputype = fat_archs[i].cputype; 559 + fat_cpusubtype = fat_archs[i].cpusubtype; 560 + } 561 + if(fat_cputype != cputype) 562 + continue; 563 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 564 + CPU_SUBTYPE_MC68040) 565 + return(i); 566 + } 567 + for(i = 0; i < nfat_archs; i++){ 568 + if(fat_archs64 != NULL){ 569 + fat_cputype = fat_archs64[i].cputype; 570 + fat_cpusubtype = fat_archs64[i].cpusubtype; 571 + } 572 + else{ 573 + fat_cputype = fat_archs[i].cputype; 574 + fat_cpusubtype = fat_archs[i].cpusubtype; 575 + } 576 + if(fat_cputype != cputype) 577 + continue; 578 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 579 + CPU_SUBTYPE_MC68030_ONLY) 580 + return(i); 581 + } 582 + } 583 + break; 584 + case CPU_TYPE_POWERPC: 585 + /* 586 + * An exact match as not found. So for all the PowerPC subtypes 587 + * pick the subtype from the following order starting from a subtype 588 + * that will work (contains 64-bit instructions or altivec if 589 + * needed): 590 + * 970, 7450, 7400, 750, 604e, 604, 603ev, 603e, 603, ALL 591 + * Note the 601 is NOT in the list above. It is only picked via 592 + * an exact match. For an unknown subtype pick only the ALL type if 593 + * it exists. 594 + */ 595 + switch(cpusubtype & ~CPU_SUBTYPE_MASK){ 596 + case CPU_SUBTYPE_POWERPC_ALL: 597 + /* 598 + * The CPU_SUBTYPE_POWERPC_ALL is only used by the development 599 + * environment tools when building a generic ALL type binary. 600 + * In the case of a non-exact match we pick the most current 601 + * processor. 602 + */ 603 + case CPU_SUBTYPE_POWERPC_970: 604 + for(i = 0; i < nfat_archs; i++){ 605 + if(fat_archs64 != NULL){ 606 + fat_cputype = fat_archs64[i].cputype; 607 + fat_cpusubtype = fat_archs64[i].cpusubtype; 608 + } 609 + else{ 610 + fat_cputype = fat_archs[i].cputype; 611 + fat_cpusubtype = fat_archs[i].cpusubtype; 612 + } 613 + if(fat_cputype != cputype) 614 + continue; 615 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 616 + CPU_SUBTYPE_POWERPC_970) 617 + return(i); 618 + } 619 + case CPU_SUBTYPE_POWERPC_7450: 620 + case CPU_SUBTYPE_POWERPC_7400: 621 + for(i = 0; i < nfat_archs; i++){ 622 + if(fat_archs64 != NULL){ 623 + fat_cputype = fat_archs64[i].cputype; 624 + fat_cpusubtype = fat_archs64[i].cpusubtype; 625 + } 626 + else{ 627 + fat_cputype = fat_archs[i].cputype; 628 + fat_cpusubtype = fat_archs[i].cpusubtype; 629 + } 630 + if(fat_cputype != cputype) 631 + continue; 632 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 633 + CPU_SUBTYPE_POWERPC_7450) 634 + return(i); 635 + } 636 + for(i = 0; i < nfat_archs; i++){ 637 + if(fat_archs64 != NULL){ 638 + fat_cputype = fat_archs64[i].cputype; 639 + fat_cpusubtype = fat_archs64[i].cpusubtype; 640 + } 641 + else{ 642 + fat_cputype = fat_archs[i].cputype; 643 + fat_cpusubtype = fat_archs[i].cpusubtype; 644 + } 645 + if(fat_cputype != cputype) 646 + continue; 647 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 648 + CPU_SUBTYPE_POWERPC_7400) 649 + return(i); 650 + } 651 + case CPU_SUBTYPE_POWERPC_750: 652 + case CPU_SUBTYPE_POWERPC_604e: 653 + case CPU_SUBTYPE_POWERPC_604: 654 + case CPU_SUBTYPE_POWERPC_603ev: 655 + case CPU_SUBTYPE_POWERPC_603e: 656 + case CPU_SUBTYPE_POWERPC_603: 657 + for(i = 0; i < nfat_archs; i++){ 658 + if(fat_archs64 != NULL){ 659 + fat_cputype = fat_archs64[i].cputype; 660 + fat_cpusubtype = fat_archs64[i].cpusubtype; 661 + } 662 + else{ 663 + fat_cputype = fat_archs[i].cputype; 664 + fat_cpusubtype = fat_archs[i].cpusubtype; 665 + } 666 + if(fat_cputype != cputype) 667 + continue; 668 + if((fat_cpusubtype & CPU_SUBTYPE_MASK) == 669 + CPU_SUBTYPE_POWERPC_750) 670 + return(i); 671 + } 672 + for(i = 0; i < nfat_archs; i++){ 673 + if(fat_archs64 != NULL){ 674 + fat_cputype = fat_archs64[i].cputype; 675 + fat_cpusubtype = fat_archs64[i].cpusubtype; 676 + } 677 + else{ 678 + fat_cputype = fat_archs[i].cputype; 679 + fat_cpusubtype = fat_archs[i].cpusubtype; 680 + } 681 + if(fat_cputype != cputype) 682 + continue; 683 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 684 + CPU_SUBTYPE_POWERPC_604e) 685 + return(i); 686 + } 687 + for(i = 0; i < nfat_archs; i++){ 688 + if(fat_archs64 != NULL){ 689 + fat_cputype = fat_archs64[i].cputype; 690 + fat_cpusubtype = fat_archs64[i].cpusubtype; 691 + } 692 + else{ 693 + fat_cputype = fat_archs[i].cputype; 694 + fat_cpusubtype = fat_archs[i].cpusubtype; 695 + } 696 + if(fat_cputype != cputype) 697 + continue; 698 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 699 + CPU_SUBTYPE_POWERPC_604) 700 + return(i); 701 + } 702 + for(i = 0; i < nfat_archs; i++){ 703 + if(fat_archs64 != NULL){ 704 + fat_cputype = fat_archs64[i].cputype; 705 + fat_cpusubtype = fat_archs64[i].cpusubtype; 706 + } 707 + else{ 708 + fat_cputype = fat_archs[i].cputype; 709 + fat_cpusubtype = fat_archs[i].cpusubtype; 710 + } 711 + if((fat_cputype & ~CPU_SUBTYPE_MASK) != cputype) 712 + continue; 713 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 714 + CPU_SUBTYPE_POWERPC_603ev) 715 + return(i); 716 + } 717 + for(i = 0; i < nfat_archs; i++){ 718 + if(fat_archs64 != NULL){ 719 + fat_cputype = fat_archs64[i].cputype; 720 + fat_cpusubtype = fat_archs64[i].cpusubtype; 721 + } 722 + else{ 723 + fat_cputype = fat_archs[i].cputype; 724 + fat_cpusubtype = fat_archs[i].cpusubtype; 725 + } 726 + if(fat_cputype != cputype) 727 + continue; 728 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 729 + CPU_SUBTYPE_POWERPC_603e) 730 + return(i); 731 + } 732 + for(i = 0; i < nfat_archs; i++){ 733 + if(fat_archs64 != NULL){ 734 + fat_cputype = fat_archs64[i].cputype; 735 + fat_cpusubtype = fat_archs64[i].cpusubtype; 736 + } 737 + else{ 738 + fat_cputype = fat_archs[i].cputype; 739 + fat_cpusubtype = fat_archs[i].cpusubtype; 740 + } 741 + if(fat_cputype != cputype) 742 + continue; 743 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 744 + CPU_SUBTYPE_POWERPC_603) 745 + return(i); 746 + } 747 + default: 748 + for(i = 0; i < nfat_archs; i++){ 749 + if(fat_archs64 != NULL){ 750 + fat_cputype = fat_archs64[i].cputype; 751 + fat_cpusubtype = fat_archs64[i].cpusubtype; 752 + } 753 + else{ 754 + fat_cputype = fat_archs[i].cputype; 755 + fat_cpusubtype = fat_archs[i].cpusubtype; 756 + } 757 + if(fat_cputype != cputype) 758 + continue; 759 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 760 + CPU_SUBTYPE_POWERPC_ALL) 761 + return(i); 762 + } 763 + } 764 + break; 765 + case CPU_TYPE_POWERPC64: 766 + /* 767 + * An exact match as not found. So for all the PowerPC64 subtypes 768 + * pick the subtype from the following order starting from a subtype 769 + * that will work (contains 64-bit instructions or altivec if 770 + * needed): 771 + * 970 (currently only the one 64-bit subtype) 772 + * For an unknown subtype pick only the ALL type if it exists. 773 + */ 774 + switch(cpusubtype & ~CPU_SUBTYPE_MASK){ 775 + case CPU_SUBTYPE_POWERPC_ALL: 776 + /* 777 + * The CPU_SUBTYPE_POWERPC_ALL is only used by the development 778 + * environment tools when building a generic ALL type binary. 779 + * In the case of a non-exact match we pick the most current 780 + * processor. 781 + */ 782 + case CPU_SUBTYPE_POWERPC_970: 783 + for(i = 0; i < nfat_archs; i++){ 784 + if(fat_archs64 != NULL){ 785 + fat_cputype = fat_archs64[i].cputype; 786 + fat_cpusubtype = fat_archs64[i].cpusubtype; 787 + } 788 + else{ 789 + fat_cputype = fat_archs[i].cputype; 790 + fat_cpusubtype = fat_archs[i].cpusubtype; 791 + } 792 + if(fat_cputype != cputype) 793 + continue; 794 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 795 + CPU_SUBTYPE_POWERPC_970) 796 + return(i); 797 + } 798 + default: 799 + for(i = 0; i < nfat_archs; i++){ 800 + if(fat_archs64 != NULL){ 801 + fat_cputype = fat_archs64[i].cputype; 802 + fat_cpusubtype = fat_archs64[i].cpusubtype; 803 + } 804 + else{ 805 + fat_cputype = fat_archs[i].cputype; 806 + fat_cpusubtype = fat_archs[i].cpusubtype; 807 + } 808 + if(fat_cputype != cputype) 809 + continue; 810 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 811 + CPU_SUBTYPE_POWERPC_ALL) 812 + return(i); 813 + } 814 + } 815 + break; 816 + case CPU_TYPE_MC88000: 817 + for(i = 0; i < nfat_archs; i++){ 818 + if(fat_archs64 != NULL){ 819 + fat_cputype = fat_archs64[i].cputype; 820 + fat_cpusubtype = fat_archs64[i].cpusubtype; 821 + } 822 + else{ 823 + fat_cputype = fat_archs[i].cputype; 824 + fat_cpusubtype = fat_archs[i].cpusubtype; 825 + } 826 + if(fat_cputype != cputype) 827 + continue; 828 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 829 + CPU_SUBTYPE_MC88000_ALL) 830 + return(i); 831 + } 832 + break; 833 + case CPU_TYPE_I860: 834 + for(i = 0; i < nfat_archs; i++){ 835 + if(fat_archs64 != NULL){ 836 + fat_cputype = fat_archs64[i].cputype; 837 + fat_cpusubtype = fat_archs64[i].cpusubtype; 838 + } 839 + else{ 840 + fat_cputype = fat_archs[i].cputype; 841 + fat_cpusubtype = fat_archs[i].cpusubtype; 842 + } 843 + if(fat_cputype != cputype) 844 + continue; 845 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 846 + CPU_SUBTYPE_I860_ALL) 847 + return(i); 848 + } 849 + break; 850 + case CPU_TYPE_HPPA: 851 + for(i = 0; i < nfat_archs; i++){ 852 + if(fat_archs64 != NULL){ 853 + fat_cputype = fat_archs64[i].cputype; 854 + fat_cpusubtype = fat_archs64[i].cpusubtype; 855 + } 856 + else{ 857 + fat_cputype = fat_archs[i].cputype; 858 + fat_cpusubtype = fat_archs[i].cpusubtype; 859 + } 860 + if(fat_cputype != cputype) 861 + continue; 862 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 863 + CPU_SUBTYPE_HPPA_ALL) 864 + return(i); 865 + } 866 + break; 867 + case CPU_TYPE_SPARC: 868 + for(i = 0; i < nfat_archs; i++){ 869 + if(fat_archs64 != NULL){ 870 + fat_cputype = fat_archs64[i].cputype; 871 + fat_cpusubtype = fat_archs64[i].cpusubtype; 872 + } 873 + else{ 874 + fat_cputype = fat_archs[i].cputype; 875 + fat_cpusubtype = fat_archs[i].cpusubtype; 876 + } 877 + if(fat_cputype != cputype) 878 + continue; 879 + if((fat_cpusubtype & ~CPU_SUBTYPE_MASK) == 880 + CPU_SUBTYPE_SPARC_ALL) 881 + return(i); 882 + } 883 + break; 884 + case CPU_TYPE_ARM: 885 + case CPU_TYPE_ARM64: 886 + { 887 + /* 888 + * ARM is straightforward, since each architecture is backward 889 + * compatible with previous architectures. So, we just take the 890 + * highest that is less than our target. 891 + */ 892 + int fat_match_found = 0; 893 + uint32_t best_fat_arch = 0; 894 + for(i = 0; i < nfat_archs; i++){ 895 + if(fat_archs64 != NULL){ 896 + fat_cputype = fat_archs64[i].cputype; 897 + fat_cpusubtype = fat_archs64[i].cpusubtype; 898 + } 899 + else{ 900 + fat_cputype = fat_archs[i].cputype; 901 + fat_cpusubtype = fat_archs[i].cpusubtype; 902 + } 903 + if(fat_cputype != cputype) 904 + continue; 905 + if(fat_cpusubtype > cpusubtype) 906 + continue; 907 + if(!fat_match_found){ 908 + fat_match_found = 1; 909 + best_fat_arch = i; 910 + continue; 911 + } 912 + if(fat_archs64 != NULL){ 913 + if(fat_cpusubtype > 914 + fat_archs64[best_fat_arch].cpusubtype) 915 + best_fat_arch = i; 916 + } 917 + else{ 918 + if(fat_cpusubtype > 919 + fat_archs[best_fat_arch].cpusubtype) 920 + best_fat_arch = i; 921 + } 922 + } 923 + if(fat_match_found) 924 + return(best_fat_arch); 925 + /* 926 + * For CPU_TYPE_ARM64, we will fall back to a CPU_TYPE_ARM 927 + * with the highest subtype. 928 + */ 929 + if(cputype == CPU_TYPE_ARM64){ 930 + int fat_match_found = 0; 931 + uint32_t best_fat_arch = 0; 932 + for(i = 0; i < nfat_archs; i++){ 933 + if(fat_archs64 != NULL){ 934 + fat_cputype = fat_archs64[i].cputype; 935 + fat_cpusubtype = fat_archs64[i].cpusubtype; 936 + } 937 + else{ 938 + fat_cputype = fat_archs[i].cputype; 939 + fat_cpusubtype = fat_archs[i].cpusubtype; 940 + } 941 + if(fat_cputype != CPU_TYPE_ARM) 942 + continue; 943 + if(!fat_match_found){ 944 + fat_match_found = 1; 945 + best_fat_arch = i; 946 + continue; 947 + } 948 + if(fat_archs64 != NULL){ 949 + if(fat_cpusubtype > 950 + fat_archs64[best_fat_arch].cpusubtype) 951 + best_fat_arch = i; 952 + } 953 + else{ 954 + if(fat_cpusubtype > 955 + fat_archs[best_fat_arch].cpusubtype) 956 + best_fat_arch = i; 957 + } 958 + } 959 + if(fat_match_found) 960 + return(best_fat_arch); 961 + } 962 + } 963 + break; 964 + default: 965 + return(-1); 966 + } 967 + return(-1); 968 + } 969 + 970 + /* 971 + * NXFindBestFatArch() is passed a cputype and cpusubtype and a set of 972 + * fat_arch structs and selects the best one that matches (if any) and returns 973 + * a pointer to that fat_arch struct (or NULL). The fat_arch structs must be 974 + * in the host byte order and correct such that the fat_archs really points to 975 + * enough memory for nfat_arch structs. It is possible that this routine could 976 + * fail if new cputypes or cpusubtypes are added and an old version of this 977 + * routine is used. But if there is an exact match between the cputype and 978 + * cpusubtype and one of the fat_arch structs this routine will always succeed. 979 + */ 980 + struct fat_arch * 981 + NXFindBestFatArch( 982 + cpu_type_t cputype, 983 + cpu_subtype_t cpusubtype, 984 + struct fat_arch *fat_archs, 985 + uint32_t nfat_archs) 986 + { 987 + int32_t i; 988 + 989 + i = internal_NXFindBestFatArch(cputype, cpusubtype, fat_archs, NULL, 990 + nfat_archs); 991 + if(i == -1) 992 + return(NULL); 993 + return(fat_archs + i); 994 + } 995 + 996 + /* NXFindBestFatArch_64() is passed a cputype and cpusubtype and a set of 997 + * fat_arch_64 structs and selects the best one that matches (if any) and 998 + * returns a pointer to that fat_arch_64 struct (or NULL). The fat_arch_64 999 + * structs must be in the host byte order and correct such that the fat_archs64 1000 + * really points to enough memory for nfat_arch structs. It is possible that 1001 + * this routine could fail if new cputypes or cpusubtypes are added and an old 1002 + * version of this routine is used. But if there is an exact match between the 1003 + * cputype and cpusubtype and one of the fat_arch_64 structs this routine will 1004 + * always succeed. 1005 + */ 1006 + struct fat_arch_64 * 1007 + NXFindBestFatArch_64( 1008 + cpu_type_t cputype, 1009 + cpu_subtype_t cpusubtype, 1010 + struct fat_arch_64 *fat_archs64, 1011 + uint32_t nfat_archs) 1012 + { 1013 + int32_t i; 1014 + 1015 + i = internal_NXFindBestFatArch(cputype, cpusubtype, NULL, 1016 + fat_archs64, nfat_archs); 1017 + if(i == -1) 1018 + return(NULL); 1019 + return(fat_archs64 + i); 1020 + } 1021 + 1022 + /* 1023 + * NXCombineCpuSubtypes() returns the resulting cpusubtype when combining two 1024 + * different cpusubtypes for the specified cputype. If the two cpusubtypes 1025 + * can't be combined (the specific subtypes are mutually exclusive) -1 is 1026 + * returned indicating it is an error to combine them. This can also fail and 1027 + * return -1 if new cputypes or cpusubtypes are added and an old version of 1028 + * this routine is used. But if the cpusubtypes are the same they can always 1029 + * be combined and this routine will return the cpusubtype pass in. 1030 + */ 1031 + cpu_subtype_t 1032 + NXCombineCpuSubtypes( 1033 + cpu_type_t cputype, 1034 + cpu_subtype_t cpusubtype1, 1035 + cpu_subtype_t cpusubtype2) 1036 + { 1037 + /* 1038 + * If this is an x86_64 cputype and either subtype is the 1039 + * "Haswell and compatible" it does not combine with anything else. 1040 + */ 1041 + if(cputype == CPU_TYPE_X86_64 && 1042 + (cpusubtype1 == CPU_SUBTYPE_X86_64_H || 1043 + cpusubtype2 == CPU_SUBTYPE_X86_64_H)) 1044 + return((cpu_subtype_t)-1); 1045 + 1046 + /* 1047 + * We now combine any i386 or x86-64 subtype to the ALL subtype. 1048 + */ 1049 + if(cputype == CPU_TYPE_I386) 1050 + return(CPU_SUBTYPE_I386_ALL); 1051 + 1052 + if(cputype == CPU_TYPE_X86_64) 1053 + return(CPU_SUBTYPE_X86_64_ALL); 1054 + 1055 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == 1056 + (cpusubtype2 & ~CPU_SUBTYPE_MASK)) 1057 + return(cpusubtype1); 1058 + 1059 + switch(cputype){ 1060 + case CPU_TYPE_MC680x0: 1061 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC680x0_ALL && 1062 + (cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC68030_ONLY && 1063 + (cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC68040) 1064 + return((cpu_subtype_t)-1); 1065 + if((cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC680x0_ALL && 1066 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC68030_ONLY && 1067 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC68040) 1068 + return((cpu_subtype_t)-1); 1069 + 1070 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC68030_ONLY && 1071 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC68040) 1072 + return((cpu_subtype_t)-1); 1073 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC68040 && 1074 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC68030_ONLY) 1075 + return((cpu_subtype_t)-1); 1076 + 1077 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC68030_ONLY || 1078 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC68030_ONLY) 1079 + return(CPU_SUBTYPE_MC68030_ONLY); 1080 + 1081 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC68040 || 1082 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC68040) 1083 + return(CPU_SUBTYPE_MC68040); 1084 + break; /* logically can't get here */ 1085 + 1086 + case CPU_TYPE_POWERPC: 1087 + /* 1088 + * Combining with the ALL type becomes the other type. Combining 1089 + * anything with the 601 becomes 601. All other non exact matches 1090 + * combine to the higher value subtype. 1091 + */ 1092 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_ALL) 1093 + return(cpusubtype2); 1094 + if((cpusubtype2 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_ALL) 1095 + return(cpusubtype1); 1096 + 1097 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_601 || 1098 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_POWERPC_601) 1099 + return(CPU_SUBTYPE_POWERPC_601); 1100 + 1101 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) > 1102 + (cpusubtype2 & ~CPU_SUBTYPE_MASK)) 1103 + return(cpusubtype1); 1104 + else 1105 + return(cpusubtype2); 1106 + break; /* logically can't get here */ 1107 + 1108 + case CPU_TYPE_MC88000: 1109 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC88000_ALL && 1110 + (cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC88110) 1111 + return((cpu_subtype_t)-1); 1112 + if((cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC88000_ALL && 1113 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_MC88110) 1114 + return((cpu_subtype_t)-1); 1115 + 1116 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC88110 || 1117 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_MC88110) 1118 + return(CPU_SUBTYPE_MC88110); 1119 + 1120 + break; /* logically can't get here */ 1121 + 1122 + case CPU_TYPE_I860: 1123 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_I860_ALL && 1124 + (cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_I860_860) 1125 + return((cpu_subtype_t)-1); 1126 + if((cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_I860_ALL && 1127 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_I860_860) 1128 + return((cpu_subtype_t)-1); 1129 + 1130 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_I860_860 || 1131 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_I860_860) 1132 + return(CPU_SUBTYPE_I860_860); 1133 + break; /* logically can't get here */ 1134 + 1135 + case CPU_TYPE_HPPA: 1136 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_HPPA_ALL && 1137 + (cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_HPPA_7100LC) 1138 + return((cpu_subtype_t)-1); 1139 + if((cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_HPPA_ALL && 1140 + (cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_HPPA_7100LC) 1141 + return((cpu_subtype_t)-1); 1142 + 1143 + return(CPU_SUBTYPE_HPPA_7100LC); 1144 + break; /* logically can't get here */ 1145 + 1146 + case CPU_TYPE_SPARC: 1147 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_SPARC_ALL) 1148 + return((cpu_subtype_t)-1); 1149 + if((cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_SPARC_ALL) 1150 + return((cpu_subtype_t)-1); 1151 + break; /* logically can't get here */ 1152 + 1153 + case CPU_TYPE_ARM: 1154 + /* 1155 + * Combinability matrix for ARM: 1156 + * V4T V5 XSCALE V6 V7 ALL 1157 + * ~~~ ~~ ~~~~~~ ~~ ~~ ~~~ 1158 + * V4T V4T V5 XSCALE V6 V7 ALL 1159 + * V5 V5 V5 -- V6 V7 ALL 1160 + * XSCALE XSCALE -- XSCALE -- -- ALL 1161 + * V6 V6 V6 -- V6 V7 ALL 1162 + * V7 V7 V7 -- V7 V7 ALL 1163 + * ALL ALL ALL ALL ALL ALL ALL 1164 + */ 1165 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_ALL) 1166 + return(cpusubtype2); 1167 + if((cpusubtype2 & ~CPU_SUBTYPE_MASK) == CPU_SUBTYPE_ARM_ALL) 1168 + return(cpusubtype1); 1169 + switch((cpusubtype1 & ~CPU_SUBTYPE_MASK)){ 1170 + case CPU_SUBTYPE_ARM_V7: 1171 + switch((cpusubtype2 & ~CPU_SUBTYPE_MASK)){ 1172 + case CPU_SUBTYPE_ARM_XSCALE: 1173 + return((cpu_subtype_t)-1); 1174 + default: 1175 + return(CPU_SUBTYPE_ARM_V7); 1176 + } 1177 + case CPU_SUBTYPE_ARM_V6: 1178 + switch((cpusubtype2 & ~CPU_SUBTYPE_MASK)){ 1179 + case CPU_SUBTYPE_ARM_XSCALE: 1180 + return((cpu_subtype_t)-1); 1181 + default: 1182 + return(CPU_SUBTYPE_ARM_V6); 1183 + } 1184 + case CPU_SUBTYPE_ARM_XSCALE: 1185 + switch((cpusubtype2 & ~CPU_SUBTYPE_MASK)){ 1186 + case CPU_SUBTYPE_ARM_V7: 1187 + case CPU_SUBTYPE_ARM_V6: 1188 + case CPU_SUBTYPE_ARM_V5TEJ: 1189 + return((cpu_subtype_t)-1); 1190 + default: 1191 + return(CPU_SUBTYPE_ARM_XSCALE); 1192 + } 1193 + case CPU_SUBTYPE_ARM_V5TEJ: 1194 + switch((cpusubtype2 & ~CPU_SUBTYPE_MASK)){ 1195 + case CPU_SUBTYPE_ARM_XSCALE: 1196 + return((cpu_subtype_t)-1); 1197 + case CPU_SUBTYPE_ARM_V7: 1198 + return(CPU_SUBTYPE_ARM_V7); 1199 + case CPU_SUBTYPE_ARM_V6: 1200 + return(CPU_SUBTYPE_ARM_V6); 1201 + default: 1202 + return(CPU_SUBTYPE_ARM_V5TEJ); 1203 + } 1204 + case CPU_SUBTYPE_ARM_V4T: 1205 + return((cpusubtype2 & ~CPU_SUBTYPE_MASK)); 1206 + default: 1207 + return((cpu_subtype_t)-1); 1208 + } 1209 + 1210 + case CPU_TYPE_ARM64: 1211 + if((cpusubtype1 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_ARM64_ALL) 1212 + return((cpu_subtype_t)-1); 1213 + if((cpusubtype2 & ~CPU_SUBTYPE_MASK) != CPU_SUBTYPE_ARM64_ALL) 1214 + return((cpu_subtype_t)-1); 1215 + break; /* logically can't get here */ 1216 + 1217 + default: 1218 + return((cpu_subtype_t)-1); 1219 + } 1220 + return((cpu_subtype_t)-1); /* logically can't get here */ 1221 + } 1222 + #endif /* !defined(RLD) */
+9
src/libmacho/dylib.ofileList
··· 1 + arch.o 2 + getsecbyname.o 3 + getsegbyname.o 4 + get_end.o 5 + swap.o 6 + i386_swap.o 7 + m68k_swap.o 8 + sparc_swap.o 9 + ppc_swap.o
+152
src/libmacho/get_end.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + #include <string.h> 25 + #include <mach-o/ldsyms.h> 26 + #include <mach-o/getsect.h> 27 + #ifndef __OPENSTEP__ 28 + #include <crt_externs.h> 29 + #else /* defined(__OPENSTEP__) */ 30 + #ifdef __DYNAMIC__ 31 + #include "mach-o/dyld.h" /* defines _dyld_lookup_and_bind() */ 32 + #endif 33 + 34 + #if !defined(__DYNAMIC__) 35 + #define DECLARE_VAR(var, type) \ 36 + extern type var 37 + #define SETUP_VAR(var) 38 + #define USE_VAR(var) var 39 + #else 40 + #define STRINGIFY(a) # a 41 + #define DECLARE_VAR(var, type) \ 42 + static type * var ## _pointer = 0 43 + #define SETUP_VAR(var) \ 44 + if ( var ## _pointer == 0) { \ 45 + _dyld_lookup_and_bind( STRINGIFY(_ ## var), \ 46 + (uint32_t *) & var ## _pointer, 0); \ 47 + } 48 + #define USE_VAR(var) (* var ## _pointer) 49 + #endif 50 + #endif /* __OPENSTEP__ */ 51 + 52 + /* 53 + * This routine returns the highest address of the segments in the program (NOT 54 + * the shared libraries). It is intended to be used as a stop gap for programs 55 + * that make UNIX style assumptions about how memory is allocated. Typicly the 56 + * asumptions under which this is used is that memory is contiguously allocated 57 + * by the program's text and data from address 0 with no gaps. The value of 58 + * this differs from the value of &_end in a UNIX program in that this routine 59 + * returns the address of the end of the segment not the end of the last section 60 + * in that segment as would be the value of the symbol &_end. 61 + */ 62 + unsigned long 63 + get_end(void) 64 + { 65 + #ifndef __LP64__ 66 + 67 + struct segment_command *sgp; 68 + unsigned long _end; 69 + uint32_t i; 70 + #ifndef __OPENSTEP__ 71 + struct mach_header *mhp = _NSGetMachExecuteHeader(); 72 + #else /* defined(__OPENSTEP__) */ 73 + static struct mach_header *mhp = NULL; 74 + DECLARE_VAR(_mh_execute_header, struct mach_header); 75 + SETUP_VAR(_mh_execute_header); 76 + 77 + mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header)); 78 + #endif /* __OPENSTEP__ */ 79 + _end = 0; 80 + sgp = (struct segment_command *) 81 + ((char *)mhp + sizeof(struct mach_header)); 82 + for(i = 0; i < mhp->ncmds; i++){ 83 + if(sgp->cmd == LC_SEGMENT) 84 + if(sgp->vmaddr + sgp->vmsize > _end) 85 + _end = sgp->vmaddr + sgp->vmsize; 86 + sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); 87 + } 88 + return(_end); 89 + 90 + #else /* defined(__LP64__) */ 91 + 92 + struct mach_header_64 *mhp = _NSGetMachExecuteHeader(); 93 + struct segment_command_64 *sgp; 94 + unsigned long _end; 95 + uint32_t i; 96 + 97 + _end = 0; 98 + sgp = (struct segment_command_64 *) 99 + ((char *)mhp + sizeof(struct mach_header_64)); 100 + for(i = 0; i < mhp->ncmds; i++){ 101 + if(sgp->cmd == LC_SEGMENT_64) 102 + if(sgp->vmaddr + sgp->vmsize > _end) 103 + _end = sgp->vmaddr + sgp->vmsize; 104 + sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize); 105 + } 106 + return(_end); 107 + 108 + #endif /* defined(__LP64__) */ 109 + } 110 + 111 + /* 112 + * This returns what was the value of the UNIX link editor defined symbol 113 + * _etext (the first address after the text section). Note this my or may not 114 + * be the only section in the __TEXT segment. 115 + */ 116 + unsigned long 117 + get_etext(void) 118 + { 119 + #ifndef __LP64__ 120 + const struct section *sp; 121 + #else /* defined(__LP64__) */ 122 + const struct section_64 *sp; 123 + #endif /* defined(__LP64__) */ 124 + 125 + sp = getsectbyname(SEG_TEXT, SECT_TEXT); 126 + if(sp) 127 + return(sp->addr + sp->size); 128 + else 129 + return(0); 130 + } 131 + 132 + /* 133 + * This returns what was the value of the UNIX link editor defined symbol 134 + * _edata (the first address after the data section). Note this my or may not 135 + * be the last non-zero fill section in the __DATA segment. 136 + */ 137 + unsigned long 138 + get_edata(void) 139 + { 140 + #ifndef __LP64__ 141 + const struct section *sp; 142 + #else /* defined(__LP64__) */ 143 + const struct section_64 *sp; 144 + #endif /* defined(__LP64__) */ 145 + 146 + sp = getsectbyname(SEG_DATA, SECT_DATA); 147 + if(sp) 148 + return(sp->addr + sp->size); 149 + else 150 + return(0); 151 + } 152 + #endif /* !defined(RLD) */
+569
src/libmacho/getsecbyname.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + #include <mach-o/ldsyms.h> 25 + #include <mach-o/swap.h> 26 + #include <string.h> 27 + #ifdef __DYNAMIC__ 28 + #include <mach-o/dyld.h> /* defines _dyld_lookup_and_bind() */ 29 + #endif /* defined(__DYNAMIC__) */ 30 + #ifndef __OPENSTEP__ 31 + #include <crt_externs.h> 32 + #else /* defined(__OPENSTEP__) */ 33 + 34 + #if !defined(__DYNAMIC__) 35 + #define DECLARE_VAR(var, type) \ 36 + extern type var 37 + #define SETUP_VAR(var) 38 + #define USE_VAR(var) var 39 + #else 40 + #define STRINGIFY(a) # a 41 + #define DECLARE_VAR(var, type) \ 42 + static type * var ## _pointer = NULL 43 + #define SETUP_VAR(var) \ 44 + if ( var ## _pointer == NULL) { \ 45 + _dyld_lookup_and_bind( STRINGIFY(_ ## var), \ 46 + (uint32_t *) & var ## _pointer, NULL); \ 47 + } 48 + #define USE_VAR(var) (* var ## _pointer) 49 + #endif 50 + #endif /* __OPENSTEP__ */ 51 + 52 + /* 53 + * This routine returns the section structure for the named section in the 54 + * named segment for the mach_header pointer passed to it if it exist. 55 + * Otherwise it returns zero. 56 + */ 57 + const struct section * 58 + getsectbynamefromheader( 59 + struct mach_header *mhp, 60 + const char *segname, 61 + const char *sectname) 62 + { 63 + struct segment_command *sgp; 64 + struct section *sp; 65 + uint32_t i, j; 66 + 67 + sgp = (struct segment_command *) 68 + ((char *)mhp + sizeof(struct mach_header)); 69 + for(i = 0; i < mhp->ncmds; i++){ 70 + if(sgp->cmd == LC_SEGMENT) 71 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 || 72 + mhp->filetype == MH_OBJECT){ 73 + sp = (struct section *)((char *)sgp + 74 + sizeof(struct segment_command)); 75 + for(j = 0; j < sgp->nsects; j++){ 76 + if(strncmp(sp->sectname, sectname, 77 + sizeof(sp->sectname)) == 0 && 78 + strncmp(sp->segname, segname, 79 + sizeof(sp->segname)) == 0) 80 + return(sp); 81 + sp = (struct section *)((char *)sp + 82 + sizeof(struct section)); 83 + } 84 + } 85 + sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); 86 + } 87 + return((struct section *)0); 88 + } 89 + 90 + /* 91 + * This routine returns the section structure for the named section in the 92 + * named segment for the mach_header_64 pointer passed to it if it exist. 93 + * Otherwise it returns zero. 94 + */ 95 + const struct section_64 * 96 + getsectbynamefromheader_64( 97 + struct mach_header_64 *mhp, 98 + const char *segname, 99 + const char *sectname) 100 + { 101 + struct segment_command_64 *sgp; 102 + struct section_64 *sp; 103 + uint32_t i, j; 104 + 105 + sgp = (struct segment_command_64 *) 106 + ((char *)mhp + sizeof(struct mach_header_64)); 107 + for(i = 0; i < mhp->ncmds; i++){ 108 + if(sgp->cmd == LC_SEGMENT_64) 109 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 || 110 + mhp->filetype == MH_OBJECT){ 111 + sp = (struct section_64 *)((char *)sgp + 112 + sizeof(struct segment_command_64)); 113 + for(j = 0; j < sgp->nsects; j++){ 114 + if(strncmp(sp->sectname, sectname, 115 + sizeof(sp->sectname)) == 0 && 116 + strncmp(sp->segname, segname, 117 + sizeof(sp->segname)) == 0) 118 + return(sp); 119 + sp = (struct section_64 *)((char *)sp + 120 + sizeof(struct section_64)); 121 + } 122 + } 123 + sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize); 124 + } 125 + return((struct section_64 *)0); 126 + } 127 + 128 + /* 129 + * This routine returns the section structure for the named section in the 130 + * named segment for the mach_header pointer passed to it if it exist. 131 + * Otherwise it returns zero. If fSwap == YES (the mach header has been 132 + * swapped to the endiannes of the current machine, but the segments and 133 + * sections are different) then the segment and sections are swapped. 134 + */ 135 + const struct section * 136 + getsectbynamefromheaderwithswap( 137 + struct mach_header *mhp, 138 + const char *segname, 139 + const char *sectname, 140 + int fSwap) 141 + { 142 + struct segment_command *sgp; 143 + struct section *sp; 144 + uint32_t i, j; 145 + 146 + sgp = (struct segment_command *) 147 + ((char *)mhp + sizeof(struct mach_header)); 148 + for(i = 0; i < mhp->ncmds; i++){ 149 + if(sgp->cmd == (fSwap ? OSSwapInt32(LC_SEGMENT) : LC_SEGMENT)) { 150 + 151 + if (fSwap) { 152 + #ifdef __LITTLE_ENDIAN__ 153 + swap_segment_command(sgp, NX_BigEndian); 154 + #else 155 + swap_segment_command(sgp, NX_LittleEndian); 156 + #endif /* __LITTLE_ENDIAN__ */ 157 + } 158 + 159 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 || 160 + mhp->filetype == MH_OBJECT){ 161 + sp = (struct section *)((char *)sgp + 162 + sizeof(struct segment_command)); 163 + 164 + if (fSwap) { 165 + #ifdef __LITTLE_ENDIAN__ 166 + swap_section(sp, sgp->nsects, NX_BigEndian); 167 + #else 168 + swap_section(sp, sgp->nsects, NX_LittleEndian); 169 + #endif /* __LITTLE_ENDIAN__ */ 170 + } 171 + 172 + for(j = 0; j < sgp->nsects; j++){ 173 + if(strncmp(sp->sectname, sectname, 174 + sizeof(sp->sectname)) == 0 && 175 + strncmp(sp->segname, segname, 176 + sizeof(sp->segname)) == 0) 177 + return(sp); 178 + sp = (struct section *)((char *)sp + 179 + sizeof(struct section)); 180 + } 181 + } 182 + sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); 183 + } else { 184 + sgp = (struct segment_command *)((char *)sgp + 185 + (fSwap ? OSSwapInt32(sgp->cmdsize) : sgp->cmdsize)); 186 + } 187 + } 188 + return((struct section *)0); 189 + } 190 + 191 + /* 192 + * This routine returns the section_64 structure for the named section in the 193 + * named segment for the mach_header_64 pointer passed to it if it exist. 194 + * Otherwise it returns zero. If fSwap == YES (the mach header has been 195 + * swapped to the endiannes of the current machine, but the segments and 196 + * sections are different) then the segment and sections are swapped. 197 + */ 198 + const struct section_64 * 199 + getsectbynamefromheaderwithswap_64( 200 + struct mach_header_64 *mhp, 201 + const char *segname, 202 + const char *sectname, 203 + int fSwap) 204 + { 205 + struct segment_command_64 *sgp; 206 + struct section_64 *sp; 207 + uint32_t i, j; 208 + 209 + sgp = (struct segment_command_64 *) 210 + ((char *)mhp + sizeof(struct mach_header_64)); 211 + for(i = 0; i < mhp->ncmds; i++){ 212 + if(sgp->cmd == (fSwap ? OSSwapInt32(LC_SEGMENT) : LC_SEGMENT)) { 213 + 214 + if (fSwap) { 215 + #ifdef __LITTLE_ENDIAN__ 216 + swap_segment_command_64(sgp, NX_BigEndian); 217 + #else 218 + swap_segment_command_64(sgp, NX_LittleEndian); 219 + #endif /* __LITTLE_ENDIAN__ */ 220 + } 221 + 222 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 || 223 + mhp->filetype == MH_OBJECT){ 224 + sp = (struct section_64 *)((char *)sgp + 225 + sizeof(struct segment_command_64)); 226 + 227 + if (fSwap) { 228 + #ifdef __LITTLE_ENDIAN__ 229 + swap_section_64(sp, sgp->nsects, NX_BigEndian); 230 + #else 231 + swap_section_64(sp, sgp->nsects, NX_LittleEndian); 232 + #endif /* __LITTLE_ENDIAN__ */ 233 + } 234 + 235 + for(j = 0; j < sgp->nsects; j++){ 236 + if(strncmp(sp->sectname, sectname, 237 + sizeof(sp->sectname)) == 0 && 238 + strncmp(sp->segname, segname, 239 + sizeof(sp->segname)) == 0) 240 + return(sp); 241 + sp = (struct section_64 *)((char *)sp + 242 + sizeof(struct section_64)); 243 + } 244 + } 245 + sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize); 246 + } else { 247 + sgp = (struct segment_command_64 *)((char *)sgp + 248 + (fSwap ? OSSwapInt32(sgp->cmdsize) : sgp->cmdsize)); 249 + } 250 + } 251 + return((struct section_64 *)0); 252 + } 253 + 254 + /* 255 + * This routine returns the a pointer the section structure of the named 256 + * section in the named segment if it exist in the mach executable it is 257 + * linked into. Otherwise it returns zero. 258 + */ 259 + #ifndef __LP64__ 260 + 261 + const struct section * 262 + getsectbyname( 263 + const char *segname, 264 + const char *sectname) 265 + { 266 + #ifndef __OPENSTEP__ 267 + struct mach_header *mhp = _NSGetMachExecuteHeader(); 268 + #else /* defined(__OPENSTEP__) */ 269 + static struct mach_header *mhp = NULL; 270 + DECLARE_VAR(_mh_execute_header, struct mach_header); 271 + SETUP_VAR(_mh_execute_header); 272 + mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header)); 273 + #endif /* __OPENSTEP__ */ 274 + return(getsectbynamefromheader(mhp, segname, sectname)); 275 + } 276 + 277 + #else /* defined(__LP64__) */ 278 + 279 + const struct section_64 * 280 + getsectbyname( 281 + const char *segname, 282 + const char *sectname) 283 + { 284 + struct mach_header_64 *mhp = _NSGetMachExecuteHeader(); 285 + 286 + return(getsectbynamefromheader_64(mhp, segname, sectname)); 287 + } 288 + 289 + #endif /* defined(__LP64__) */ 290 + 291 + /* 292 + * This routine returns the a pointer to the data for the named section in the 293 + * named segment if it exist in the mach executable it is linked into. Also 294 + * it returns the size of the section data indirectly through the pointer size. 295 + * Otherwise it returns zero for the pointer and the size. 296 + */ 297 + char * 298 + getsectdata( 299 + const char *segname, 300 + const char *sectname, 301 + unsigned long *size) 302 + { 303 + #ifndef __LP64__ 304 + const struct section *sp; 305 + #else /* defined(__LP64__) */ 306 + const struct section_64 *sp; 307 + #endif /* defined(__LP64__) */ 308 + 309 + sp = getsectbyname(segname, sectname); 310 + if(sp == NULL){ 311 + *size = 0; 312 + return(NULL); 313 + } 314 + *size = sp->size; 315 + return((char *)(sp->addr)); 316 + } 317 + 318 + /* 319 + * This routine returns the a pointer to the section contents of the named 320 + * section in the named segment if it exists in the image pointed to by the 321 + * mach header. Otherwise it returns zero. 322 + */ 323 + #ifndef __LP64__ 324 + 325 + uint8_t * 326 + getsectiondata( 327 + const struct mach_header *mhp, 328 + const char *segname, 329 + const char *sectname, 330 + unsigned long *size) 331 + { 332 + struct segment_command *sgp; 333 + struct section *sp; 334 + uint32_t i, j; 335 + intptr_t slide; 336 + 337 + slide = 0; 338 + sp = 0; 339 + sgp = (struct segment_command *) 340 + ((char *)mhp + sizeof(struct mach_header)); 341 + for(i = 0; i < mhp->ncmds; i++){ 342 + if(sgp->cmd == LC_SEGMENT){ 343 + if(strcmp(sgp->segname, "__TEXT") == 0){ 344 + slide = (uintptr_t)mhp - sgp->vmaddr; 345 + } 346 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){ 347 + sp = (struct section *)((char *)sgp + 348 + sizeof(struct segment_command)); 349 + for(j = 0; j < sgp->nsects; j++){ 350 + if(strncmp(sp->sectname, sectname, 351 + sizeof(sp->sectname)) == 0 && 352 + strncmp(sp->segname, segname, 353 + sizeof(sp->segname)) == 0){ 354 + *size = sp->size; 355 + return((uint8_t *)(sp->addr) + slide); 356 + } 357 + sp = (struct section *)((char *)sp + 358 + sizeof(struct section)); 359 + } 360 + } 361 + } 362 + sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); 363 + } 364 + return(0); 365 + } 366 + 367 + uint8_t * 368 + getsegmentdata( 369 + const struct mach_header *mhp, 370 + const char *segname, 371 + unsigned long *size) 372 + { 373 + struct segment_command *sgp; 374 + intptr_t slide; 375 + uint32_t i; 376 + 377 + slide = 0; 378 + sgp = (struct segment_command *) 379 + ((char *)mhp + sizeof(struct mach_header)); 380 + for(i = 0; i < mhp->ncmds; i++){ 381 + if(sgp->cmd == LC_SEGMENT){ 382 + if(strcmp(sgp->segname, "__TEXT") == 0){ 383 + slide = (uintptr_t)mhp - sgp->vmaddr; 384 + } 385 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){ 386 + *size = sgp->vmsize; 387 + return((uint8_t *)(sgp->vmaddr + slide)); 388 + } 389 + } 390 + sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); 391 + } 392 + return(0); 393 + } 394 + 395 + #else /* defined(__LP64__) */ 396 + 397 + uint8_t * 398 + getsectiondata( 399 + const struct mach_header_64 *mhp, 400 + const char *segname, 401 + const char *sectname, 402 + unsigned long *size) 403 + { 404 + struct segment_command_64 *sgp; 405 + struct section_64 *sp; 406 + uint32_t i, j; 407 + intptr_t slide; 408 + 409 + slide = 0; 410 + sp = 0; 411 + sgp = (struct segment_command_64 *) 412 + ((char *)mhp + sizeof(struct mach_header_64)); 413 + for(i = 0; i < mhp->ncmds; i++){ 414 + if(sgp->cmd == LC_SEGMENT_64){ 415 + if(strcmp(sgp->segname, "__TEXT") == 0){ 416 + slide = (uintptr_t)mhp - sgp->vmaddr; 417 + } 418 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){ 419 + sp = (struct section_64 *)((char *)sgp + 420 + sizeof(struct segment_command_64)); 421 + for(j = 0; j < sgp->nsects; j++){ 422 + if(strncmp(sp->sectname, sectname, 423 + sizeof(sp->sectname)) == 0 && 424 + strncmp(sp->segname, segname, 425 + sizeof(sp->segname)) == 0){ 426 + *size = sp->size; 427 + return((uint8_t *)(sp->addr) + slide); 428 + } 429 + sp = (struct section_64 *)((char *)sp + 430 + sizeof(struct section_64)); 431 + } 432 + } 433 + } 434 + sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize); 435 + } 436 + return(0); 437 + } 438 + 439 + uint8_t * 440 + getsegmentdata( 441 + const struct mach_header_64 *mhp, 442 + const char *segname, 443 + unsigned long *size) 444 + { 445 + struct segment_command_64 *sgp; 446 + intptr_t slide; 447 + uint32_t i; 448 + 449 + slide = 0; 450 + sgp = (struct segment_command_64 *) 451 + ((char *)mhp + sizeof(struct mach_header_64)); 452 + for(i = 0; i < mhp->ncmds; i++){ 453 + if(sgp->cmd == LC_SEGMENT_64){ 454 + if(strcmp(sgp->segname, "__TEXT") == 0){ 455 + slide = (uintptr_t)mhp - sgp->vmaddr; 456 + } 457 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){ 458 + *size = sgp->vmsize; 459 + return((uint8_t *)(sgp->vmaddr + slide)); 460 + } 461 + } 462 + sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize); 463 + } 464 + return(0); 465 + } 466 + 467 + #endif /* defined(__LP64__) */ 468 + 469 + /* 470 + * This routine returns the a pointer to the data for the named section in the 471 + * named segment if it exist in the mach header passed to it. Also it returns 472 + * the size of the section data indirectly through the pointer size. Otherwise 473 + * it returns zero for the pointer and the size. 474 + */ 475 + char * 476 + getsectdatafromheader( 477 + struct mach_header *mhp, 478 + const char *segname, 479 + const char *sectname, 480 + uint32_t *size) 481 + { 482 + const struct section *sp; 483 + 484 + sp = getsectbynamefromheader(mhp, segname, sectname); 485 + if(sp == NULL){ 486 + *size = 0; 487 + return(NULL); 488 + } 489 + *size = sp->size; 490 + return((char *)((uintptr_t)(sp->addr))); 491 + } 492 + 493 + /* 494 + * This routine returns the a pointer to the data for the named section in the 495 + * named segment if it exist in the 64-bit mach header passed to it. Also it 496 + * returns the size of the section data indirectly through the pointer size. 497 + * Otherwise it returns zero for the pointer and the size. 498 + */ 499 + char * 500 + getsectdatafromheader_64( 501 + struct mach_header_64 *mhp, 502 + const char *segname, 503 + const char *sectname, 504 + uint64_t *size) 505 + { 506 + const struct section_64 *sp; 507 + 508 + sp = getsectbynamefromheader_64(mhp, segname, sectname); 509 + if(sp == NULL){ 510 + *size = 0; 511 + return(NULL); 512 + } 513 + *size = sp->size; 514 + return((char *)((uintptr_t)(sp->addr))); 515 + } 516 + 517 + #ifdef __DYNAMIC__ 518 + /* 519 + * This routine returns the a pointer to the data for the named section in the 520 + * named segment if it exist in the named Framework. Also it returns the size 521 + * of the section data indirectly through the pointer size. Otherwise it 522 + * returns zero for the pointer and the size. The last component of the path 523 + * of the Framework is passed as FrameworkName. 524 + */ 525 + void * 526 + getsectdatafromFramework( 527 + const char *FrameworkName, 528 + const char *segname, 529 + const char *sectname, 530 + unsigned long *size) 531 + { 532 + uint32_t i, n; 533 + uintptr_t vmaddr_slide; 534 + #ifndef __LP64__ 535 + struct mach_header *mh; 536 + const struct section *s; 537 + #else /* defined(__LP64__) */ 538 + struct mach_header_64 *mh; 539 + const struct section_64 *s; 540 + #endif /* defined(__LP64__) */ 541 + char *name, *p; 542 + 543 + n = _dyld_image_count(); 544 + for(i = 0; i < n ; i++){ 545 + name = _dyld_get_image_name(i); 546 + p = strrchr(name, '/'); 547 + if(p != NULL && p[1] != '\0') 548 + name = p + 1; 549 + if(strcmp(name, FrameworkName) != 0) 550 + continue; 551 + mh = _dyld_get_image_header(i); 552 + vmaddr_slide = _dyld_get_image_vmaddr_slide(i); 553 + #ifndef __LP64__ 554 + s = getsectbynamefromheader(mh, segname, sectname); 555 + #else /* defined(__LP64__) */ 556 + s = getsectbynamefromheader_64(mh, segname, sectname); 557 + #endif /* defined(__LP64__) */ 558 + if(s == NULL){ 559 + *size = 0; 560 + return(NULL); 561 + } 562 + *size = s->size; 563 + return((void *)(s->addr + vmaddr_slide)); 564 + } 565 + *size = 0; 566 + return(NULL); 567 + } 568 + #endif /* __DYNAMIC__ */ 569 + #endif /* !defined(RLD) */
+119
src/libmacho/getsegbyname.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #include <mach-o/ldsyms.h> 24 + #include <string.h> 25 + #ifndef __OPENSTEP__ 26 + 27 + #ifndef RLD 28 + #include <crt_externs.h> 29 + #endif /* !defined(RLD) */ 30 + 31 + #else /* defined(__OPENSTEP__) */ 32 + #ifdef __DYNAMIC__ 33 + #include "mach-o/dyld.h" /* defines _dyld_lookup_and_bind() */ 34 + #endif 35 + 36 + #if !defined(__DYNAMIC__) 37 + #define DECLARE_VAR(var, type) \ 38 + extern type var 39 + #define SETUP_VAR(var) 40 + #define USE_VAR(var) var 41 + #else 42 + #define STRINGIFY(a) # a 43 + #define DECLARE_VAR(var, type) \ 44 + static type * var ## _pointer = 0 45 + #define SETUP_VAR(var) \ 46 + if ( var ## _pointer == 0) { \ 47 + _dyld_lookup_and_bind( STRINGIFY(_ ## var), \ 48 + (uint32_t *) & var ## _pointer, 0); \ 49 + } 50 + #define USE_VAR(var) (* var ## _pointer) 51 + #endif 52 + #endif /* __OPENSTEP__ */ 53 + 54 + /* 55 + * This routine returns the segment_command structure for the named segment if 56 + * it exist in the mach executible it is linked into. Otherwise it returns 57 + * zero. It uses the link editor defined symbol _mh_execute_header and just 58 + * looks through the load commands. Since these are mapped into the text 59 + * segment they are read only and thus const. 60 + */ 61 + #ifndef __LP64__ 62 + 63 + const struct segment_command * 64 + getsegbyname( 65 + char *segname) 66 + { 67 + struct segment_command *sgp; 68 + uint32_t i; 69 + #ifndef RLD 70 + #ifndef __OPENSTEP__ 71 + struct mach_header *mhp = _NSGetMachExecuteHeader(); 72 + #else /* defined(__OPENSTEP__) */ 73 + static struct mach_header *mhp = NULL; 74 + DECLARE_VAR(_mh_execute_header, struct mach_header); 75 + SETUP_VAR(_mh_execute_header); 76 + mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header)); 77 + #endif /* __OPENSTEP__ */ 78 + #else /* defined(RLD) */ 79 + mhp = (struct mach_header *)(&_mh_execute_header); 80 + #endif /* defined(RLD) */ 81 + 82 + sgp = (struct segment_command *) 83 + ((char *)mhp + sizeof(struct mach_header)); 84 + for(i = 0; i < mhp->ncmds; i++){ 85 + if(sgp->cmd == LC_SEGMENT) 86 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) 87 + return(sgp); 88 + sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize); 89 + } 90 + return(NULL); 91 + } 92 + 93 + #else /* defined(__LP64__) */ 94 + 95 + const struct segment_command_64 * 96 + getsegbyname( 97 + char *segname) 98 + { 99 + struct mach_header_64 *mhp = NULL; 100 + struct segment_command_64 *sgp; 101 + uint32_t i; 102 + 103 + #ifndef RLD 104 + mhp = _NSGetMachExecuteHeader(); 105 + #else /* defined(RLD) */ 106 + mhp = (struct mach_header_64 *)(&_mh_execute_header); 107 + #endif /* defined(RLD) */ 108 + 109 + sgp = (struct segment_command_64 *) 110 + ((char *)mhp + sizeof(struct mach_header_64)); 111 + for(i = 0; i < mhp->ncmds; i++){ 112 + if(sgp->cmd == LC_SEGMENT_64) 113 + if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) 114 + return(sgp); 115 + sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize); 116 + } 117 + return(NULL); 118 + } 119 + #endif /* defined(__LP64__) */
+121
src/libmacho/hppa_swap.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + #import <mach-o/hppa/swap.h> 25 + 26 + void 27 + swap_hppa_integer_thread_state( 28 + struct hp_pa_integer_thread_state *regs, 29 + enum NXByteOrder target_byte_order) 30 + { 31 + regs->ts_gr1 = OSSwapInt32(regs->ts_gr1); 32 + regs->ts_gr2 = OSSwapInt32(regs->ts_gr2); 33 + regs->ts_gr3 = OSSwapInt32(regs->ts_gr3); 34 + regs->ts_gr4 = OSSwapInt32(regs->ts_gr4); 35 + regs->ts_gr5 = OSSwapInt32(regs->ts_gr5); 36 + regs->ts_gr6 = OSSwapInt32(regs->ts_gr6); 37 + regs->ts_gr7 = OSSwapInt32(regs->ts_gr7); 38 + regs->ts_gr8 = OSSwapInt32(regs->ts_gr8); 39 + regs->ts_gr9 = OSSwapInt32(regs->ts_gr9); 40 + regs->ts_gr10 = OSSwapInt32(regs->ts_gr10); 41 + regs->ts_gr11 = OSSwapInt32(regs->ts_gr11); 42 + regs->ts_gr12 = OSSwapInt32(regs->ts_gr12); 43 + regs->ts_gr13 = OSSwapInt32(regs->ts_gr13); 44 + regs->ts_gr14 = OSSwapInt32(regs->ts_gr14); 45 + regs->ts_gr15 = OSSwapInt32(regs->ts_gr15); 46 + regs->ts_gr16 = OSSwapInt32(regs->ts_gr16); 47 + regs->ts_gr17 = OSSwapInt32(regs->ts_gr17); 48 + regs->ts_gr18 = OSSwapInt32(regs->ts_gr18); 49 + regs->ts_gr19 = OSSwapInt32(regs->ts_gr19); 50 + regs->ts_gr20 = OSSwapInt32(regs->ts_gr20); 51 + regs->ts_gr21 = OSSwapInt32(regs->ts_gr21); 52 + regs->ts_gr22 = OSSwapInt32(regs->ts_gr22); 53 + regs->ts_gr23 = OSSwapInt32(regs->ts_gr23); 54 + regs->ts_gr24 = OSSwapInt32(regs->ts_gr24); 55 + regs->ts_gr25 = OSSwapInt32(regs->ts_gr25); 56 + regs->ts_gr26 = OSSwapInt32(regs->ts_gr26); 57 + regs->ts_gr27 = OSSwapInt32(regs->ts_gr27); 58 + regs->ts_gr28 = OSSwapInt32(regs->ts_gr28); 59 + regs->ts_gr29 = OSSwapInt32(regs->ts_gr29); 60 + regs->ts_gr30 = OSSwapInt32(regs->ts_gr30); 61 + regs->ts_gr31 = OSSwapInt32(regs->ts_gr31); 62 + regs->ts_sr0 = OSSwapInt32(regs->ts_sr0); 63 + regs->ts_sr1 = OSSwapInt32(regs->ts_sr1); 64 + regs->ts_sr2 = OSSwapInt32(regs->ts_sr2); 65 + regs->ts_sr3 = OSSwapInt32(regs->ts_sr3); 66 + regs->ts_sar = OSSwapInt32(regs->ts_sar); 67 + } 68 + 69 + void swap_hppa_frame_thread_state( 70 + struct hp_pa_frame_thread_state *frame, 71 + enum NXByteOrder target_byte_order) 72 + { 73 + frame->ts_pcsq_front = OSSwapInt32(frame->ts_pcsq_front); 74 + frame->ts_pcsq_back = OSSwapInt32(frame->ts_pcsq_back); 75 + frame->ts_pcoq_front = OSSwapInt32(frame->ts_pcoq_front); 76 + frame->ts_pcoq_back = OSSwapInt32(frame->ts_pcoq_back); 77 + frame->ts_psw = OSSwapInt32(frame->ts_psw); 78 + frame->ts_unaligned_faults = OSSwapInt32(frame->ts_unaligned_faults); 79 + frame->ts_fault_address = OSSwapInt32(frame->ts_fault_address); 80 + frame->ts_step_range_start = OSSwapInt32(frame->ts_step_range_start); 81 + frame->ts_step_range_stop = OSSwapInt32(frame->ts_step_range_stop); 82 + } 83 + 84 + void swap_hppa_fp_thread_state( 85 + struct hp_pa_fp_thread_state *fp, 86 + enum NXByteOrder target_byte_order) 87 + { 88 + fp->ts_fp0 = OSSwapInt64(fp->ts_fp0); 89 + fp->ts_fp1 = OSSwapInt64(fp->ts_fp1); 90 + fp->ts_fp2 = OSSwapInt64(fp->ts_fp2); 91 + fp->ts_fp3 = OSSwapInt64(fp->ts_fp3); 92 + fp->ts_fp4 = OSSwapInt64(fp->ts_fp4); 93 + fp->ts_fp5 = OSSwapInt64(fp->ts_fp5); 94 + fp->ts_fp6 = OSSwapInt64(fp->ts_fp6); 95 + fp->ts_fp7 = OSSwapInt64(fp->ts_fp7); 96 + fp->ts_fp8 = OSSwapInt64(fp->ts_fp8); 97 + fp->ts_fp9 = OSSwapInt64(fp->ts_fp9); 98 + fp->ts_fp10 = OSSwapInt64(fp->ts_fp10); 99 + fp->ts_fp11 = OSSwapInt64(fp->ts_fp11); 100 + fp->ts_fp12 = OSSwapInt64(fp->ts_fp12); 101 + fp->ts_fp13 = OSSwapInt64(fp->ts_fp13); 102 + fp->ts_fp14 = OSSwapInt64(fp->ts_fp14); 103 + fp->ts_fp15 = OSSwapInt64(fp->ts_fp15); 104 + fp->ts_fp16 = OSSwapInt64(fp->ts_fp16); 105 + fp->ts_fp17 = OSSwapInt64(fp->ts_fp17); 106 + fp->ts_fp18 = OSSwapInt64(fp->ts_fp18); 107 + fp->ts_fp19 = OSSwapInt64(fp->ts_fp19); 108 + fp->ts_fp20 = OSSwapInt64(fp->ts_fp20); 109 + fp->ts_fp21 = OSSwapInt64(fp->ts_fp21); 110 + fp->ts_fp22 = OSSwapInt64(fp->ts_fp22); 111 + fp->ts_fp23 = OSSwapInt64(fp->ts_fp23); 112 + fp->ts_fp24 = OSSwapInt64(fp->ts_fp24); 113 + fp->ts_fp25 = OSSwapInt64(fp->ts_fp25); 114 + fp->ts_fp26 = OSSwapInt64(fp->ts_fp26); 115 + fp->ts_fp27 = OSSwapInt64(fp->ts_fp27); 116 + fp->ts_fp28 = OSSwapInt64(fp->ts_fp28); 117 + fp->ts_fp29 = OSSwapInt64(fp->ts_fp29); 118 + fp->ts_fp30 = OSSwapInt64(fp->ts_fp30); 119 + fp->ts_fp31 = OSSwapInt64(fp->ts_fp31); 120 + } 121 + #endif /* !defined(RLD) */
+922
src/libmacho/i386_swap.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + #ifdef SHLIB 25 + #undef environ 26 + #endif 27 + 28 + #define __busy busy 29 + #define __c0 c0 30 + #define __c1 c1 31 + #define __c2 c2 32 + #define __c3 c3 33 + #define __cs cs 34 + #define __darwin_fp_control fp_control 35 + #define __darwin_fp_status fp_status 36 + #define __darwin_i386_exception_state i386_exception_state 37 + #define __darwin_i386_float_state i386_float_state 38 + #define __darwin_i386_thread_state i386_thread_state 39 + #define __darwin_mmst_reg mmst_reg 40 + #define __darwin_xmm_reg xmm_reg 41 + #define __denorm denorm 42 + #define __ds ds 43 + #define __eax eax 44 + #define __ebp ebp 45 + #define __ebx ebx 46 + #define __ecx ecx 47 + #define __edi edi 48 + #define __edx edx 49 + #define __eflags eflags 50 + #define __eip eip 51 + #define __err err 52 + #define __errsumm errsumm 53 + #define __es es 54 + #define __esi esi 55 + #define __esp esp 56 + #define __faultvaddr faultvaddr 57 + #define __fpu_cs fpu_cs 58 + #define __fpu_dp fpu_dp 59 + #define __fpu_ds fpu_ds 60 + #define __fpu_fcw fpu_fcw 61 + #define __fpu_fop fpu_fop 62 + #define __fpu_fsw fpu_fsw 63 + #define __fpu_ftw fpu_ftw 64 + #define __fpu_ip fpu_ip 65 + #define __fpu_mxcsr fpu_mxcsr 66 + #define __fpu_mxcsrmask fpu_mxcsrmask 67 + #define __fpu_reserved fpu_reserved 68 + #define __fpu_reserved1 fpu_reserved1 69 + #define __fpu_rsrv1 fpu_rsrv1 70 + #define __fpu_rsrv2 fpu_rsrv2 71 + #define __fpu_rsrv3 fpu_rsrv3 72 + #define __fpu_rsrv4 fpu_rsrv4 73 + #define __fpu_stmm0 fpu_stmm0 74 + #define __fpu_stmm1 fpu_stmm1 75 + #define __fpu_stmm2 fpu_stmm2 76 + #define __fpu_stmm3 fpu_stmm3 77 + #define __fpu_stmm4 fpu_stmm4 78 + #define __fpu_stmm5 fpu_stmm5 79 + #define __fpu_stmm6 fpu_stmm6 80 + #define __fpu_stmm7 fpu_stmm7 81 + #define __fpu_xmm0 fpu_xmm0 82 + #define __fpu_xmm1 fpu_xmm1 83 + #define __fpu_xmm2 fpu_xmm2 84 + #define __fpu_xmm3 fpu_xmm3 85 + #define __fpu_xmm4 fpu_xmm4 86 + #define __fpu_xmm5 fpu_xmm5 87 + #define __fpu_xmm6 fpu_xmm6 88 + #define __fpu_xmm7 fpu_xmm7 89 + #define __fs fs 90 + #define __gs gs 91 + #define __invalid invalid 92 + #define __mmst_reg mmst_reg 93 + #define __mmst_rsrv mmst_rsrv 94 + #define __ovrfl ovrfl 95 + #define __pc pc 96 + #define __precis precis 97 + #define __rc rc 98 + #define __ss ss 99 + #define __stkflt stkflt 100 + #define __tos tos 101 + #define __trapno trapno 102 + #define __undfl undfl 103 + #define __xmm_reg xmm_reg 104 + #define __zdiv zdiv 105 + 106 + #define __rax rax 107 + #define __rbx rbx 108 + #define __rcx rcx 109 + #define __rdx rdx 110 + #define __rdi rdi 111 + #define __rsi rsi 112 + #define __rbp rbp 113 + #define __rsp rsp 114 + #define __r8 r8 115 + #define __r9 r9 116 + #define __r10 r10 117 + #define __r11 r11 118 + #define __r12 r12 119 + #define __r13 r13 120 + #define __r14 r14 121 + #define __r15 r15 122 + #define __rip rip 123 + #define __rflags rflags 124 + 125 + #define __dr0 dr0 126 + #define __dr1 dr1 127 + #define __dr2 dr2 128 + #define __dr3 dr3 129 + #define __dr4 dr4 130 + #define __dr5 dr5 131 + #define __dr6 dr6 132 + #define __dr7 dr7 133 + 134 + #include <string.h> 135 + #include <libkern/OSByteOrder.h> 136 + #import <mach-o/i386/swap.h> 137 + 138 + void 139 + swap_i386_thread_state( 140 + i386_thread_state_t *cpu, 141 + enum NXByteOrder target_byte_sex) 142 + { 143 + cpu->eax = OSSwapInt32(cpu->eax); 144 + cpu->ebx = OSSwapInt32(cpu->ebx); 145 + cpu->ecx = OSSwapInt32(cpu->ecx); 146 + cpu->edx = OSSwapInt32(cpu->edx); 147 + cpu->edi = OSSwapInt32(cpu->edi); 148 + cpu->esi = OSSwapInt32(cpu->esi); 149 + cpu->ebp = OSSwapInt32(cpu->ebp); 150 + cpu->esp = OSSwapInt32(cpu->esp); 151 + cpu->ss = OSSwapInt32(cpu->ss); 152 + cpu->eflags = OSSwapInt32(cpu->eflags); 153 + cpu->eip = OSSwapInt32(cpu->eip); 154 + cpu->cs = OSSwapInt32(cpu->cs); 155 + cpu->ds = OSSwapInt32(cpu->ds); 156 + cpu->es = OSSwapInt32(cpu->es); 157 + cpu->fs = OSSwapInt32(cpu->fs); 158 + cpu->gs = OSSwapInt32(cpu->gs); 159 + } 160 + 161 + #ifdef x86_THREAD_STATE64 162 + void 163 + swap_x86_thread_state64( 164 + x86_thread_state64_t *cpu, 165 + enum NXByteOrder target_byte_sex) 166 + { 167 + cpu->rax = OSSwapInt64(cpu->rax); 168 + cpu->rbx = OSSwapInt64(cpu->rbx); 169 + cpu->rcx = OSSwapInt64(cpu->rcx); 170 + cpu->rdx = OSSwapInt64(cpu->rdx); 171 + cpu->rdi = OSSwapInt64(cpu->rdi); 172 + cpu->rsi = OSSwapInt64(cpu->rsi); 173 + cpu->rbp = OSSwapInt64(cpu->rbp); 174 + cpu->rsp = OSSwapInt64(cpu->rsp); 175 + cpu->rflags = OSSwapInt64(cpu->rflags); 176 + cpu->rip = OSSwapInt64(cpu->rip); 177 + cpu->r8 = OSSwapInt64(cpu->r8); 178 + cpu->r9 = OSSwapInt64(cpu->r9); 179 + cpu->r10 = OSSwapInt64(cpu->r10); 180 + cpu->r11 = OSSwapInt64(cpu->r11); 181 + cpu->r12 = OSSwapInt64(cpu->r12); 182 + cpu->r13 = OSSwapInt64(cpu->r13); 183 + cpu->r14 = OSSwapInt64(cpu->r14); 184 + cpu->r15 = OSSwapInt64(cpu->r15); 185 + cpu->cs = OSSwapInt64(cpu->cs); 186 + cpu->fs = OSSwapInt64(cpu->fs); 187 + cpu->gs = OSSwapInt64(cpu->gs); 188 + } 189 + 190 + void 191 + swap_x86_state_hdr( 192 + x86_state_hdr_t *hdr, 193 + enum NXByteOrder target_byte_sex) 194 + { 195 + hdr->flavor = OSSwapInt32(hdr->flavor); 196 + hdr->count = OSSwapInt32(hdr->count); 197 + } 198 + 199 + void 200 + swap_x86_float_state64( 201 + x86_float_state64_t *fpu, 202 + enum NXByteOrder target_byte_sex) 203 + { 204 + struct swapped_fp_control { 205 + union { 206 + struct { 207 + unsigned short 208 + :3, 209 + /*inf*/ :1, 210 + rc :2, 211 + pc :2, 212 + :2, 213 + precis :1, 214 + undfl :1, 215 + ovrfl :1, 216 + zdiv :1, 217 + denorm :1, 218 + invalid :1; 219 + } fields; 220 + unsigned short half; 221 + } u; 222 + } sfpc; 223 + 224 + struct swapped_fp_status { 225 + union { 226 + struct { 227 + unsigned short 228 + busy :1, 229 + c3 :1, 230 + tos :3, 231 + c2 :1, 232 + c1 :1, 233 + c0 :1, 234 + errsumm :1, 235 + stkflt :1, 236 + precis :1, 237 + undfl :1, 238 + ovrfl :1, 239 + zdiv :1, 240 + denorm :1, 241 + invalid :1; 242 + } fields; 243 + unsigned short half; 244 + } u; 245 + } sfps; 246 + 247 + enum NXByteOrder host_byte_sex; 248 + 249 + host_byte_sex = NXHostByteOrder(); 250 + 251 + fpu->fpu_reserved[0] = OSSwapInt32(fpu->fpu_reserved[0]); 252 + fpu->fpu_reserved[1] = OSSwapInt32(fpu->fpu_reserved[1]); 253 + 254 + if(target_byte_sex == host_byte_sex){ 255 + memcpy(&sfpc, &(fpu->fpu_fcw), 256 + sizeof(struct swapped_fp_control)); 257 + sfpc.u.half = OSSwapInt16(sfpc.u.half); 258 + fpu->fpu_fcw.rc = sfpc.u.fields.rc; 259 + fpu->fpu_fcw.pc = sfpc.u.fields.pc; 260 + fpu->fpu_fcw.precis = sfpc.u.fields.precis; 261 + fpu->fpu_fcw.undfl = sfpc.u.fields.undfl; 262 + fpu->fpu_fcw.ovrfl = sfpc.u.fields.ovrfl; 263 + fpu->fpu_fcw.zdiv = sfpc.u.fields.zdiv; 264 + fpu->fpu_fcw.denorm = sfpc.u.fields.denorm; 265 + fpu->fpu_fcw.invalid = sfpc.u.fields.invalid; 266 + 267 + memcpy(&sfps, &(fpu->fpu_fsw), 268 + sizeof(struct swapped_fp_status)); 269 + sfps.u.half = OSSwapInt16(sfps.u.half); 270 + fpu->fpu_fsw.busy = sfps.u.fields.busy; 271 + fpu->fpu_fsw.c3 = sfps.u.fields.c3; 272 + fpu->fpu_fsw.tos = sfps.u.fields.tos; 273 + fpu->fpu_fsw.c2 = sfps.u.fields.c2; 274 + fpu->fpu_fsw.c1 = sfps.u.fields.c1; 275 + fpu->fpu_fsw.c0 = sfps.u.fields.c0; 276 + fpu->fpu_fsw.errsumm = sfps.u.fields.errsumm; 277 + fpu->fpu_fsw.stkflt = sfps.u.fields.stkflt; 278 + fpu->fpu_fsw.precis = sfps.u.fields.precis; 279 + fpu->fpu_fsw.undfl = sfps.u.fields.undfl; 280 + fpu->fpu_fsw.ovrfl = sfps.u.fields.ovrfl; 281 + fpu->fpu_fsw.zdiv = sfps.u.fields.zdiv; 282 + fpu->fpu_fsw.denorm = sfps.u.fields.denorm; 283 + fpu->fpu_fsw.invalid = sfps.u.fields.invalid; 284 + } 285 + else{ 286 + sfpc.u.fields.rc = fpu->fpu_fcw.rc; 287 + sfpc.u.fields.pc = fpu->fpu_fcw.pc; 288 + sfpc.u.fields.precis = fpu->fpu_fcw.precis; 289 + sfpc.u.fields.undfl = fpu->fpu_fcw.undfl; 290 + sfpc.u.fields.ovrfl = fpu->fpu_fcw.ovrfl; 291 + sfpc.u.fields.zdiv = fpu->fpu_fcw.zdiv; 292 + sfpc.u.fields.denorm = fpu->fpu_fcw.denorm; 293 + sfpc.u.fields.invalid = fpu->fpu_fcw.invalid; 294 + sfpc.u.half = OSSwapInt16(sfpc.u.half); 295 + memcpy(&(fpu->fpu_fcw), &sfpc, 296 + sizeof(struct swapped_fp_control)); 297 + 298 + sfps.u.fields.busy = fpu->fpu_fsw.busy; 299 + sfps.u.fields.c3 = fpu->fpu_fsw.c3; 300 + sfps.u.fields.tos = fpu->fpu_fsw.tos; 301 + sfps.u.fields.c2 = fpu->fpu_fsw.c2; 302 + sfps.u.fields.c1 = fpu->fpu_fsw.c1; 303 + sfps.u.fields.c0 = fpu->fpu_fsw.c0; 304 + sfps.u.fields.errsumm = fpu->fpu_fsw.errsumm; 305 + sfps.u.fields.stkflt = fpu->fpu_fsw.stkflt; 306 + sfps.u.fields.precis = fpu->fpu_fsw.precis; 307 + sfps.u.fields.undfl = fpu->fpu_fsw.undfl; 308 + sfps.u.fields.ovrfl = fpu->fpu_fsw.ovrfl; 309 + sfps.u.fields.zdiv = fpu->fpu_fsw.zdiv; 310 + sfps.u.fields.denorm = fpu->fpu_fsw.denorm; 311 + sfps.u.fields.invalid = fpu->fpu_fsw.invalid; 312 + sfps.u.half = OSSwapInt16(sfps.u.half); 313 + memcpy(&(fpu->fpu_fsw), &sfps, 314 + sizeof(struct swapped_fp_status)); 315 + } 316 + fpu->fpu_fop = OSSwapInt16(fpu->fpu_fop); 317 + fpu->fpu_ip = OSSwapInt32(fpu->fpu_ip); 318 + fpu->fpu_cs = OSSwapInt16(fpu->fpu_cs); 319 + fpu->fpu_rsrv2 = OSSwapInt16(fpu->fpu_rsrv2); 320 + fpu->fpu_dp = OSSwapInt32(fpu->fpu_dp); 321 + fpu->fpu_ds = OSSwapInt16(fpu->fpu_ds); 322 + fpu->fpu_rsrv3 = OSSwapInt16(fpu->fpu_rsrv3); 323 + fpu->fpu_mxcsr = OSSwapInt32(fpu->fpu_mxcsr); 324 + fpu->fpu_mxcsrmask = OSSwapInt32(fpu->fpu_mxcsrmask); 325 + fpu->fpu_reserved1 = OSSwapInt32(fpu->fpu_reserved1); 326 + } 327 + 328 + void 329 + swap_x86_exception_state64( 330 + x86_exception_state64_t *exc, 331 + enum NXByteOrder target_byte_sex) 332 + { 333 + exc->trapno = OSSwapInt32(exc->trapno); 334 + exc->err = OSSwapInt32(exc->err); 335 + exc->faultvaddr = OSSwapInt64(exc->faultvaddr); 336 + } 337 + 338 + void 339 + swap_x86_thread_state( 340 + x86_thread_state_t *cpu, 341 + enum NXByteOrder target_byte_sex) 342 + { 343 + x86_state_hdr_t hdr; 344 + enum NXByteOrder host_byte_sex; 345 + 346 + host_byte_sex = NXHostByteOrder(); 347 + hdr = cpu->tsh; 348 + if(target_byte_sex == host_byte_sex) 349 + swap_x86_state_hdr(&hdr, host_byte_sex); 350 + 351 + swap_x86_state_hdr(&cpu->tsh, target_byte_sex); 352 + 353 + if(hdr.flavor == x86_THREAD_STATE32) 354 + swap_i386_thread_state(&cpu->uts.ts32, target_byte_sex); 355 + else if(hdr.flavor == x86_THREAD_STATE64) 356 + swap_x86_thread_state64(&cpu->uts.ts64, target_byte_sex); 357 + } 358 + 359 + void 360 + swap_x86_float_state( 361 + x86_float_state_t *fpu, 362 + enum NXByteOrder target_byte_sex) 363 + { 364 + x86_state_hdr_t hdr; 365 + enum NXByteOrder host_byte_sex; 366 + 367 + host_byte_sex = NXHostByteOrder(); 368 + hdr = fpu->fsh; 369 + if(target_byte_sex == host_byte_sex) 370 + swap_x86_state_hdr(&hdr, host_byte_sex); 371 + 372 + swap_x86_state_hdr(&fpu->fsh, target_byte_sex); 373 + 374 + /* current i386 thread states */ 375 + #if i386_THREAD_STATE == 1 376 + if(hdr.flavor == x86_FLOAT_STATE32) 377 + swap_i386_float_state(&fpu->ufs.fs32, target_byte_sex); 378 + else 379 + #endif 380 + if(hdr.flavor == x86_FLOAT_STATE64) 381 + swap_x86_float_state64(&fpu->ufs.fs64, target_byte_sex); 382 + } 383 + 384 + void 385 + swap_x86_exception_state( 386 + x86_exception_state_t *exc, 387 + enum NXByteOrder target_byte_sex) 388 + { 389 + x86_state_hdr_t hdr; 390 + enum NXByteOrder host_byte_sex; 391 + 392 + host_byte_sex = NXHostByteOrder(); 393 + hdr = exc->esh; 394 + if(target_byte_sex == host_byte_sex) 395 + swap_x86_state_hdr(&hdr, host_byte_sex); 396 + 397 + swap_x86_state_hdr(&exc->esh, target_byte_sex); 398 + 399 + /* current i386 thread states */ 400 + #if i386_THREAD_STATE == 1 401 + if(hdr.flavor == x86_EXCEPTION_STATE32) 402 + swap_i386_exception_state(&exc->ues.es32, target_byte_sex); 403 + else 404 + #endif 405 + if(hdr.flavor == x86_EXCEPTION_STATE64) 406 + swap_x86_exception_state64(&exc->ues.es64, target_byte_sex); 407 + } 408 + 409 + void 410 + swap_x86_debug_state32( 411 + x86_debug_state32_t *debug, 412 + enum NXByteOrder target_byte_sex) 413 + { 414 + debug->dr0 = OSSwapInt32(debug->dr0); 415 + debug->dr1 = OSSwapInt32(debug->dr1); 416 + debug->dr2 = OSSwapInt32(debug->dr2); 417 + debug->dr3 = OSSwapInt32(debug->dr3); 418 + debug->dr4 = OSSwapInt32(debug->dr4); 419 + debug->dr5 = OSSwapInt32(debug->dr5); 420 + debug->dr6 = OSSwapInt32(debug->dr6); 421 + debug->dr7 = OSSwapInt32(debug->dr7); 422 + } 423 + 424 + void 425 + swap_x86_debug_state64( 426 + x86_debug_state64_t *debug, 427 + enum NXByteOrder target_byte_sex) 428 + { 429 + debug->dr0 = OSSwapInt64(debug->dr0); 430 + debug->dr1 = OSSwapInt64(debug->dr1); 431 + debug->dr2 = OSSwapInt64(debug->dr2); 432 + debug->dr3 = OSSwapInt64(debug->dr3); 433 + debug->dr4 = OSSwapInt64(debug->dr4); 434 + debug->dr5 = OSSwapInt64(debug->dr5); 435 + debug->dr6 = OSSwapInt64(debug->dr6); 436 + debug->dr7 = OSSwapInt64(debug->dr7); 437 + } 438 + 439 + void 440 + swap_x86_debug_state( 441 + x86_debug_state_t *debug, 442 + enum NXByteOrder target_byte_sex) 443 + { 444 + x86_state_hdr_t hdr; 445 + enum NXByteOrder host_byte_sex; 446 + 447 + host_byte_sex = NXHostByteOrder(); 448 + hdr = debug->dsh; 449 + if(target_byte_sex == host_byte_sex) 450 + swap_x86_state_hdr(&hdr, host_byte_sex); 451 + 452 + swap_x86_state_hdr(&debug->dsh, target_byte_sex); 453 + 454 + if(hdr.flavor == x86_DEBUG_STATE32) 455 + swap_x86_debug_state32(&debug->uds.ds32, target_byte_sex); 456 + else if(hdr.flavor == x86_DEBUG_STATE64) 457 + swap_x86_debug_state64(&debug->uds.ds64, target_byte_sex); 458 + } 459 + #endif /* x86_THREAD_STATE64 */ 460 + 461 + /* current i386 thread states */ 462 + #if i386_THREAD_STATE == 1 463 + void 464 + swap_i386_float_state( 465 + struct i386_float_state *fpu, 466 + enum NXByteOrder target_byte_sex) 467 + { 468 + #ifndef i386_EXCEPTION_STATE_COUNT 469 + /* this routine does nothing as their are currently no non-byte fields */ 470 + #else /* !defined(i386_EXCEPTION_STATE_COUNT) */ 471 + struct swapped_fp_control { 472 + union { 473 + struct { 474 + unsigned short 475 + :3, 476 + /*inf*/ :1, 477 + rc :2, 478 + pc :2, 479 + :2, 480 + precis :1, 481 + undfl :1, 482 + ovrfl :1, 483 + zdiv :1, 484 + denorm :1, 485 + invalid :1; 486 + } fields; 487 + unsigned short half; 488 + } u; 489 + } sfpc; 490 + 491 + struct swapped_fp_status { 492 + union { 493 + struct { 494 + unsigned short 495 + busy :1, 496 + c3 :1, 497 + tos :3, 498 + c2 :1, 499 + c1 :1, 500 + c0 :1, 501 + errsumm :1, 502 + stkflt :1, 503 + precis :1, 504 + undfl :1, 505 + ovrfl :1, 506 + zdiv :1, 507 + denorm :1, 508 + invalid :1; 509 + } fields; 510 + unsigned short half; 511 + } u; 512 + } sfps; 513 + 514 + enum NXByteOrder host_byte_sex; 515 + 516 + host_byte_sex = NXHostByteOrder(); 517 + 518 + fpu->fpu_reserved[0] = OSSwapInt32(fpu->fpu_reserved[0]); 519 + fpu->fpu_reserved[1] = OSSwapInt32(fpu->fpu_reserved[1]); 520 + 521 + if(target_byte_sex == host_byte_sex){ 522 + memcpy(&sfpc, &(fpu->fpu_fcw), 523 + sizeof(struct swapped_fp_control)); 524 + sfpc.u.half = OSSwapInt16(sfpc.u.half); 525 + fpu->fpu_fcw.rc = sfpc.u.fields.rc; 526 + fpu->fpu_fcw.pc = sfpc.u.fields.pc; 527 + fpu->fpu_fcw.precis = sfpc.u.fields.precis; 528 + fpu->fpu_fcw.undfl = sfpc.u.fields.undfl; 529 + fpu->fpu_fcw.ovrfl = sfpc.u.fields.ovrfl; 530 + fpu->fpu_fcw.zdiv = sfpc.u.fields.zdiv; 531 + fpu->fpu_fcw.denorm = sfpc.u.fields.denorm; 532 + fpu->fpu_fcw.invalid = sfpc.u.fields.invalid; 533 + 534 + memcpy(&sfps, &(fpu->fpu_fsw), 535 + sizeof(struct swapped_fp_status)); 536 + sfps.u.half = OSSwapInt16(sfps.u.half); 537 + fpu->fpu_fsw.busy = sfps.u.fields.busy; 538 + fpu->fpu_fsw.c3 = sfps.u.fields.c3; 539 + fpu->fpu_fsw.tos = sfps.u.fields.tos; 540 + fpu->fpu_fsw.c2 = sfps.u.fields.c2; 541 + fpu->fpu_fsw.c1 = sfps.u.fields.c1; 542 + fpu->fpu_fsw.c0 = sfps.u.fields.c0; 543 + fpu->fpu_fsw.errsumm = sfps.u.fields.errsumm; 544 + fpu->fpu_fsw.stkflt = sfps.u.fields.stkflt; 545 + fpu->fpu_fsw.precis = sfps.u.fields.precis; 546 + fpu->fpu_fsw.undfl = sfps.u.fields.undfl; 547 + fpu->fpu_fsw.ovrfl = sfps.u.fields.ovrfl; 548 + fpu->fpu_fsw.zdiv = sfps.u.fields.zdiv; 549 + fpu->fpu_fsw.denorm = sfps.u.fields.denorm; 550 + fpu->fpu_fsw.invalid = sfps.u.fields.invalid; 551 + } 552 + else{ 553 + sfpc.u.fields.rc = fpu->fpu_fcw.rc; 554 + sfpc.u.fields.pc = fpu->fpu_fcw.pc; 555 + sfpc.u.fields.precis = fpu->fpu_fcw.precis; 556 + sfpc.u.fields.undfl = fpu->fpu_fcw.undfl; 557 + sfpc.u.fields.ovrfl = fpu->fpu_fcw.ovrfl; 558 + sfpc.u.fields.zdiv = fpu->fpu_fcw.zdiv; 559 + sfpc.u.fields.denorm = fpu->fpu_fcw.denorm; 560 + sfpc.u.fields.invalid = fpu->fpu_fcw.invalid; 561 + sfpc.u.half = OSSwapInt16(sfpc.u.half); 562 + memcpy(&(fpu->fpu_fcw), &sfpc, 563 + sizeof(struct swapped_fp_control)); 564 + 565 + sfps.u.fields.busy = fpu->fpu_fsw.busy; 566 + sfps.u.fields.c3 = fpu->fpu_fsw.c3; 567 + sfps.u.fields.tos = fpu->fpu_fsw.tos; 568 + sfps.u.fields.c2 = fpu->fpu_fsw.c2; 569 + sfps.u.fields.c1 = fpu->fpu_fsw.c1; 570 + sfps.u.fields.c0 = fpu->fpu_fsw.c0; 571 + sfps.u.fields.errsumm = fpu->fpu_fsw.errsumm; 572 + sfps.u.fields.stkflt = fpu->fpu_fsw.stkflt; 573 + sfps.u.fields.precis = fpu->fpu_fsw.precis; 574 + sfps.u.fields.undfl = fpu->fpu_fsw.undfl; 575 + sfps.u.fields.ovrfl = fpu->fpu_fsw.ovrfl; 576 + sfps.u.fields.zdiv = fpu->fpu_fsw.zdiv; 577 + sfps.u.fields.denorm = fpu->fpu_fsw.denorm; 578 + sfps.u.fields.invalid = fpu->fpu_fsw.invalid; 579 + sfps.u.half = OSSwapInt16(sfps.u.half); 580 + memcpy(&(fpu->fpu_fsw), &sfps, 581 + sizeof(struct swapped_fp_status)); 582 + } 583 + fpu->fpu_fop = OSSwapInt16(fpu->fpu_fop); 584 + fpu->fpu_ip = OSSwapInt32(fpu->fpu_ip); 585 + fpu->fpu_cs = OSSwapInt16(fpu->fpu_cs); 586 + fpu->fpu_rsrv2 = OSSwapInt16(fpu->fpu_rsrv2); 587 + fpu->fpu_dp = OSSwapInt32(fpu->fpu_dp); 588 + fpu->fpu_ds = OSSwapInt16(fpu->fpu_ds); 589 + fpu->fpu_rsrv3 = OSSwapInt16(fpu->fpu_rsrv3); 590 + fpu->fpu_mxcsr = OSSwapInt32(fpu->fpu_mxcsr); 591 + fpu->fpu_mxcsrmask = OSSwapInt32(fpu->fpu_mxcsrmask); 592 + fpu->fpu_reserved1 = OSSwapInt32(fpu->fpu_reserved1); 593 + 594 + #endif /* !defined(i386_EXCEPTION_STATE_COUNT) */ 595 + } 596 + 597 + void 598 + swap_i386_exception_state( 599 + i386_exception_state_t *exc, 600 + enum NXByteOrder target_byte_sex) 601 + { 602 + exc->trapno = OSSwapInt32(exc->trapno); 603 + exc->err = OSSwapInt32(exc->err); 604 + exc->faultvaddr = OSSwapInt32(exc->faultvaddr); 605 + } 606 + #endif /* i386_THREAD_STATE == 1 */ 607 + 608 + /* i386 thread states on older releases */ 609 + #if i386_THREAD_STATE == -1 610 + void 611 + swap_i386_thread_fpstate( 612 + i386_thread_fpstate_t *fpu, 613 + enum NXByteOrder target_byte_sex) 614 + { 615 + struct swapped_fp_control { 616 + union { 617 + struct { 618 + unsigned short 619 + :3, 620 + /*inf*/ :1, 621 + rc :2, 622 + pc :2, 623 + :2, 624 + precis :1, 625 + undfl :1, 626 + ovrfl :1, 627 + zdiv :1, 628 + denorm :1, 629 + invalid :1; 630 + } fields; 631 + unsigned short half; 632 + } u; 633 + } sfpc; 634 + 635 + struct swapped_fp_status { 636 + union { 637 + struct { 638 + unsigned short 639 + busy :1, 640 + c3 :1, 641 + tos :3, 642 + c2 :1, 643 + c1 :1, 644 + c0 :1, 645 + errsumm :1, 646 + stkflt :1, 647 + precis :1, 648 + undfl :1, 649 + ovrfl :1, 650 + zdiv :1, 651 + denorm :1, 652 + invalid :1; 653 + } fields; 654 + unsigned short half; 655 + } u; 656 + } sfps; 657 + 658 + struct swapped_fp_tag { 659 + union { 660 + struct { 661 + unsigned short 662 + tag7 :2, 663 + tag6 :2, 664 + tag5 :2, 665 + tag4 :2, 666 + tag3 :2, 667 + tag2 :2, 668 + tag1 :2, 669 + tag0 :2; 670 + } fields; 671 + unsigned short half; 672 + } u; 673 + } sfpt; 674 + 675 + struct swapped_fp_data_reg { 676 + unsigned short mant; 677 + unsigned short mant1 :16, 678 + mant2 :16, 679 + mant3 :16; 680 + union { 681 + struct { 682 + unsigned short sign :1, 683 + exp :15; 684 + } fields; 685 + unsigned short half; 686 + } u; 687 + } sfpd; 688 + 689 + struct swapped_sel { 690 + union { 691 + struct { 692 + unsigned short 693 + index :13, 694 + ti :1, 695 + rpl :2; 696 + } fields; 697 + unsigned short half; 698 + } u; 699 + } ss; 700 + 701 + enum NXByteOrder host_byte_sex; 702 + int i; 703 + 704 + host_byte_sex = NXHostByteOrder(); 705 + 706 + fpu->environ.ip = OSSwapInt32(fpu->environ.ip); 707 + fpu->environ.opcode = OSSwapInt16(fpu->environ.opcode); 708 + fpu->environ.dp = OSSwapInt32(fpu->environ.dp); 709 + 710 + if(target_byte_sex == host_byte_sex){ 711 + memcpy(&sfpc, &(fpu->environ.control), 712 + sizeof(struct swapped_fp_control)); 713 + sfpc.u.half = OSSwapInt16(sfpc.u.half); 714 + fpu->environ.control.rc = sfpc.u.fields.rc; 715 + fpu->environ.control.pc = sfpc.u.fields.pc; 716 + fpu->environ.control.precis = sfpc.u.fields.precis; 717 + fpu->environ.control.undfl = sfpc.u.fields.undfl; 718 + fpu->environ.control.ovrfl = sfpc.u.fields.ovrfl; 719 + fpu->environ.control.zdiv = sfpc.u.fields.zdiv; 720 + fpu->environ.control.denorm = sfpc.u.fields.denorm; 721 + fpu->environ.control.invalid = sfpc.u.fields.invalid; 722 + 723 + memcpy(&sfps, &(fpu->environ.status), 724 + sizeof(struct swapped_fp_status)); 725 + sfps.u.half = OSSwapInt16(sfps.u.half); 726 + fpu->environ.status.busy = sfps.u.fields.busy; 727 + fpu->environ.status.c3 = sfps.u.fields.c3; 728 + fpu->environ.status.tos = sfps.u.fields.tos; 729 + fpu->environ.status.c2 = sfps.u.fields.c2; 730 + fpu->environ.status.c1 = sfps.u.fields.c1; 731 + fpu->environ.status.c0 = sfps.u.fields.c0; 732 + fpu->environ.status.errsumm = sfps.u.fields.errsumm; 733 + fpu->environ.status.stkflt = sfps.u.fields.stkflt; 734 + fpu->environ.status.precis = sfps.u.fields.precis; 735 + fpu->environ.status.undfl = sfps.u.fields.undfl; 736 + fpu->environ.status.ovrfl = sfps.u.fields.ovrfl; 737 + fpu->environ.status.zdiv = sfps.u.fields.zdiv; 738 + fpu->environ.status.denorm = sfps.u.fields.denorm; 739 + fpu->environ.status.invalid = sfps.u.fields.invalid; 740 + 741 + memcpy(&sfpt, &(fpu->environ.tag), 742 + sizeof(struct swapped_fp_tag)); 743 + sfpt.u.half = OSSwapInt16(sfpt.u.half); 744 + fpu->environ.tag.tag7 = sfpt.u.fields.tag7; 745 + fpu->environ.tag.tag6 = sfpt.u.fields.tag6; 746 + fpu->environ.tag.tag5 = sfpt.u.fields.tag5; 747 + fpu->environ.tag.tag4 = sfpt.u.fields.tag4; 748 + fpu->environ.tag.tag3 = sfpt.u.fields.tag3; 749 + fpu->environ.tag.tag2 = sfpt.u.fields.tag2; 750 + fpu->environ.tag.tag1 = sfpt.u.fields.tag1; 751 + fpu->environ.tag.tag0 = sfpt.u.fields.tag0; 752 + 753 + memcpy(&ss, &(fpu->environ.cs), 754 + sizeof(struct swapped_sel)); 755 + ss.u.half = OSSwapInt16(ss.u.half); 756 + fpu->environ.cs.index = ss.u.fields.index; 757 + fpu->environ.cs.ti = ss.u.fields.ti; 758 + fpu->environ.cs.rpl = ss.u.fields.rpl; 759 + 760 + memcpy(&ss, &(fpu->environ.ds), 761 + sizeof(struct swapped_sel)); 762 + ss.u.half = OSSwapInt16(ss.u.half); 763 + fpu->environ.ds.index = ss.u.fields.index; 764 + fpu->environ.ds.ti = ss.u.fields.ti; 765 + fpu->environ.ds.rpl = ss.u.fields.rpl; 766 + 767 + for(i = 0; i < 8; i++){ 768 + memcpy(&sfpd, &(fpu->stack.ST[i]), 769 + sizeof(struct swapped_fp_data_reg)); 770 + fpu->stack.ST[i].mant = OSSwapInt16(sfpd.mant); 771 + fpu->stack.ST[i].mant1 = OSSwapInt16(sfpd.mant1); 772 + fpu->stack.ST[i].mant2 = OSSwapInt16(sfpd.mant2); 773 + fpu->stack.ST[i].mant3 = OSSwapInt16(sfpd.mant3); 774 + sfpd.u.half = OSSwapInt16(sfpd.u.half); 775 + fpu->stack.ST[i].exp = sfpd.u.fields.exp; 776 + fpu->stack.ST[i].sign = sfpd.u.fields.sign; 777 + } 778 + } 779 + else{ 780 + sfpc.u.fields.rc = fpu->environ.control.rc; 781 + sfpc.u.fields.pc = fpu->environ.control.pc; 782 + sfpc.u.fields.precis = fpu->environ.control.precis; 783 + sfpc.u.fields.undfl = fpu->environ.control.undfl; 784 + sfpc.u.fields.ovrfl = fpu->environ.control.ovrfl; 785 + sfpc.u.fields.zdiv = fpu->environ.control.zdiv; 786 + sfpc.u.fields.denorm = fpu->environ.control.denorm; 787 + sfpc.u.fields.invalid = fpu->environ.control.invalid; 788 + sfpc.u.half = OSSwapInt16(sfpc.u.half); 789 + memcpy(&(fpu->environ.control), &sfpc, 790 + sizeof(struct swapped_fp_control)); 791 + 792 + sfps.u.fields.busy = fpu->environ.status.busy; 793 + sfps.u.fields.c3 = fpu->environ.status.c3; 794 + sfps.u.fields.tos = fpu->environ.status.tos; 795 + sfps.u.fields.c2 = fpu->environ.status.c2; 796 + sfps.u.fields.c1 = fpu->environ.status.c1; 797 + sfps.u.fields.c0 = fpu->environ.status.c0; 798 + sfps.u.fields.errsumm = fpu->environ.status.errsumm; 799 + sfps.u.fields.stkflt = fpu->environ.status.stkflt; 800 + sfps.u.fields.precis = fpu->environ.status.precis; 801 + sfps.u.fields.undfl = fpu->environ.status.undfl; 802 + sfps.u.fields.ovrfl = fpu->environ.status.ovrfl; 803 + sfps.u.fields.zdiv = fpu->environ.status.zdiv; 804 + sfps.u.fields.denorm = fpu->environ.status.denorm; 805 + sfps.u.fields.invalid = fpu->environ.status.invalid; 806 + sfps.u.half = OSSwapInt16(sfps.u.half); 807 + memcpy(&(fpu->environ.status), &sfps, 808 + sizeof(struct swapped_fp_status)); 809 + 810 + sfpt.u.fields.tag7 = fpu->environ.tag.tag7; 811 + sfpt.u.fields.tag6 = fpu->environ.tag.tag6; 812 + sfpt.u.fields.tag5 = fpu->environ.tag.tag5; 813 + sfpt.u.fields.tag4 = fpu->environ.tag.tag4; 814 + sfpt.u.fields.tag3 = fpu->environ.tag.tag3; 815 + sfpt.u.fields.tag2 = fpu->environ.tag.tag2; 816 + sfpt.u.fields.tag1 = fpu->environ.tag.tag1; 817 + sfpt.u.fields.tag0 = fpu->environ.tag.tag0; 818 + sfpt.u.half = OSSwapInt16(sfpt.u.half); 819 + memcpy(&(fpu->environ.tag), &sfpt, 820 + sizeof(struct swapped_fp_tag)); 821 + 822 + ss.u.fields.index = fpu->environ.cs.index; 823 + ss.u.fields.ti = fpu->environ.cs.ti; 824 + ss.u.fields.rpl = fpu->environ.cs.rpl; 825 + ss.u.half = OSSwapInt16(ss.u.half); 826 + memcpy(&(fpu->environ.cs), &ss, 827 + sizeof(struct swapped_sel)); 828 + 829 + ss.u.fields.index = fpu->environ.ds.index; 830 + ss.u.fields.ti = fpu->environ.ds.ti; 831 + ss.u.fields.rpl = fpu->environ.ds.rpl; 832 + ss.u.half = OSSwapInt16(ss.u.half); 833 + memcpy(&(fpu->environ.cs), &ss, 834 + sizeof(struct swapped_sel)); 835 + 836 + for(i = 0; i < 8; i++){ 837 + sfpd.mant = OSSwapInt16(fpu->stack.ST[i].mant); 838 + sfpd.mant1 = OSSwapInt16(fpu->stack.ST[i].mant1); 839 + sfpd.mant2 = OSSwapInt16(fpu->stack.ST[i].mant2); 840 + sfpd.mant3 = OSSwapInt16(fpu->stack.ST[i].mant3); 841 + sfpd.u.fields.exp = fpu->stack.ST[i].exp; 842 + sfpd.u.fields.sign = fpu->stack.ST[i].sign; 843 + sfpd.u.half = OSSwapInt16(sfpd.u.half); 844 + memcpy(&(fpu->stack.ST[i]), &sfpd, 845 + sizeof(struct swapped_fp_data_reg)); 846 + } 847 + } 848 + } 849 + 850 + void 851 + swap_i386_thread_exceptstate( 852 + i386_thread_exceptstate_t *exc, 853 + enum NXByteOrder target_byte_sex) 854 + { 855 + struct swapped_err_code { 856 + union { 857 + struct err_code_normal { 858 + unsigned int :16, 859 + index :13, 860 + tbl :2, 861 + ext :1; 862 + } normal; 863 + struct err_code_pgfault { 864 + unsigned int :29, 865 + user :1, 866 + wrtflt :1, 867 + prot :1; 868 + } pgfault; 869 + uint32_t word; 870 + } u; 871 + } sec; 872 + uint32_t word; 873 + enum NXByteOrder host_byte_sex; 874 + 875 + host_byte_sex = NXHostByteOrder(); 876 + 877 + exc->trapno = OSSwapInt32(exc->trapno); 878 + if(exc->trapno == 14){ 879 + if(target_byte_sex == host_byte_sex){ 880 + memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code)); 881 + sec.u.word = OSSwapInt32(sec.u.word); 882 + exc->err.pgfault.user = sec.u.pgfault.user; 883 + exc->err.pgfault.wrtflt = sec.u.pgfault.wrtflt; 884 + exc->err.pgfault.prot = sec.u.pgfault.prot; 885 + } 886 + else{ 887 + sec.u.pgfault.prot = exc->err.pgfault.prot; 888 + sec.u.pgfault.wrtflt = exc->err.pgfault.wrtflt; 889 + sec.u.pgfault.user = exc->err.pgfault.user; 890 + sec.u.word = OSSwapInt32(sec.u.word); 891 + memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code)); 892 + } 893 + } 894 + else{ 895 + if(target_byte_sex == host_byte_sex){ 896 + memcpy(&sec, &(exc->err), sizeof(struct swapped_err_code)); 897 + sec.u.word = OSSwapInt32(sec.u.word); 898 + word = sec.u.normal.index; 899 + exc->err.normal.index = OSSwapInt32(word); 900 + exc->err.normal.tbl = sec.u.normal.tbl; 901 + exc->err.normal.ext = sec.u.normal.ext; 902 + } 903 + else{ 904 + sec.u.normal.ext = exc->err.normal.ext; 905 + sec.u.normal.tbl = exc->err.normal.tbl; 906 + word = exc->err.normal.index; 907 + sec.u.normal.index = OSSwapInt32(word); 908 + sec.u.word = OSSwapInt32(sec.u.word); 909 + memcpy(&(exc->err), &sec, sizeof(struct swapped_err_code)); 910 + } 911 + } 912 + } 913 + 914 + void 915 + swap_i386_thread_cthreadstate( 916 + i386_thread_cthreadstate_t *user, 917 + enum NXByteOrder target_byte_sex) 918 + { 919 + user->self = OSSwapInt32(user->self); 920 + } 921 + #endif /* i386_THREAD_STATE == -1 */ 922 + #endif /* !defined(RLD) */
+61
src/libmacho/i860_swap.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + #import <mach-o/i860/swap.h> 25 + 26 + void 27 + swap_i860_thread_state_regs( 28 + struct i860_thread_state_regs *cpu, 29 + enum NXByteOrder target_byte_sex) 30 + { 31 + int i; 32 + 33 + for(i = 0; i < 31; i++) 34 + cpu->ireg[i] = OSSwapInt32(cpu->ireg[i]); 35 + for(i = 0; i < 30; i++) 36 + cpu->freg[i] = OSSwapInt32(cpu->freg[i]); 37 + cpu->psr = OSSwapInt32(cpu->psr); 38 + cpu->epsr = OSSwapInt32(cpu->epsr); 39 + cpu->db = OSSwapInt32(cpu->db); 40 + cpu->pc = OSSwapInt32(cpu->pc); 41 + cpu->_padding_ = OSSwapInt32(cpu->_padding_); 42 + cpu->Mres3 = OSSwapInt64(cpu->Mres3); 43 + cpu->Ares3 = OSSwapInt64(cpu->Ares3); 44 + cpu->Mres2 = OSSwapInt64(cpu->Mres2); 45 + cpu->Ares2 = OSSwapInt64(cpu->Ares2); 46 + cpu->Mres1 = OSSwapInt64(cpu->Mres1); 47 + cpu->Ares1 = OSSwapInt64(cpu->Ares1); 48 + cpu->Ires1 = OSSwapInt64(cpu->Ires1); 49 + cpu->Lres3m = OSSwapInt64(cpu->Lres3m); 50 + cpu->Lres2m = OSSwapInt64(cpu->Lres2m); 51 + cpu->Lres1m = OSSwapInt64(cpu->Lres1m); 52 + cpu->KR = OSSwapInt64(cpu->KR); 53 + cpu->KI = OSSwapInt64(cpu->KI); 54 + cpu->T = OSSwapInt64(cpu->T); 55 + cpu->Fsr3 = OSSwapInt32(cpu->Fsr3); 56 + cpu->Fsr2 = OSSwapInt32(cpu->Fsr2); 57 + cpu->Fsr1 = OSSwapInt32(cpu->Fsr1); 58 + cpu->Mergelo32 = OSSwapInt32(cpu->Mergelo32); 59 + cpu->Mergehi32 = OSSwapInt32(cpu->Mergehi32); 60 + } 61 + #endif /* !defined(RLD) */
+68
src/libmacho/m68k_swap.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + #import <mach-o/m68k/swap.h> 25 + 26 + void 27 + swap_m68k_thread_state_regs( 28 + struct m68k_thread_state_regs *cpu, 29 + enum NXByteOrder target_byte_sex) 30 + { 31 + uint32_t i; 32 + 33 + for(i = 0; i < 8; i++) 34 + cpu->dreg[i] = OSSwapInt32(cpu->dreg[i]); 35 + for(i = 0; i < 8; i++) 36 + cpu->areg[i] = OSSwapInt32(cpu->areg[i]); 37 + cpu->pad0 = OSSwapInt16(cpu->pad0); 38 + cpu->sr = OSSwapInt16(cpu->sr); 39 + cpu->pc = OSSwapInt32(cpu->pc); 40 + } 41 + 42 + void 43 + swap_m68k_thread_state_68882( 44 + struct m68k_thread_state_68882 *fpu, 45 + enum NXByteOrder target_byte_sex) 46 + { 47 + uint32_t i, tmp; 48 + 49 + for(i = 0; i < 8; i++){ 50 + tmp = OSSwapInt32(fpu->regs[i].fp[0]); 51 + fpu->regs[i].fp[1] = OSSwapInt32(fpu->regs[i].fp[1]); 52 + fpu->regs[i].fp[0] = OSSwapInt32(fpu->regs[i].fp[2]); 53 + fpu->regs[i].fp[2] = tmp; 54 + } 55 + fpu->cr = OSSwapInt32(fpu->cr); 56 + fpu->sr = OSSwapInt32(fpu->sr); 57 + fpu->iar = OSSwapInt32(fpu->iar); 58 + fpu->state = OSSwapInt32(fpu->state); 59 + } 60 + 61 + void 62 + swap_m68k_thread_state_user_reg( 63 + struct m68k_thread_state_user_reg *user_reg, 64 + enum NXByteOrder target_byte_sex) 65 + { 66 + user_reg->user_reg = OSSwapInt32(user_reg->user_reg); 67 + } 68 + #endif /* !defined(RLD) */
+430
src/libmacho/m88k_swap.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + #include <string.h> 25 + #import <mach-o/m88k/swap.h> 26 + #import <architecture/nrw/reg_help.h> 27 + 28 + void 29 + swap_m88k_thread_state_grf_t( 30 + m88k_thread_state_grf_t *cpu, 31 + enum NXByteOrder target_byte_sex) 32 + { 33 + cpu->r1 = OSSwapInt32(cpu->r1); 34 + cpu->r2 = OSSwapInt32(cpu->r2); 35 + cpu->r3 = OSSwapInt32(cpu->r3); 36 + cpu->r4 = OSSwapInt32(cpu->r4); 37 + cpu->r5 = OSSwapInt32(cpu->r5); 38 + cpu->r6 = OSSwapInt32(cpu->r6); 39 + cpu->r7 = OSSwapInt32(cpu->r7); 40 + cpu->r8 = OSSwapInt32(cpu->r8); 41 + cpu->r9 = OSSwapInt32(cpu->r9); 42 + cpu->r10 = OSSwapInt32(cpu->r10); 43 + cpu->r11 = OSSwapInt32(cpu->r11); 44 + cpu->r12 = OSSwapInt32(cpu->r12); 45 + cpu->r13 = OSSwapInt32(cpu->r13); 46 + cpu->r14 = OSSwapInt32(cpu->r14); 47 + cpu->r15 = OSSwapInt32(cpu->r15); 48 + cpu->r16 = OSSwapInt32(cpu->r16); 49 + cpu->r17 = OSSwapInt32(cpu->r17); 50 + cpu->r18 = OSSwapInt32(cpu->r18); 51 + cpu->r19 = OSSwapInt32(cpu->r19); 52 + cpu->r20 = OSSwapInt32(cpu->r20); 53 + cpu->r21 = OSSwapInt32(cpu->r21); 54 + cpu->r22 = OSSwapInt32(cpu->r22); 55 + cpu->r23 = OSSwapInt32(cpu->r23); 56 + cpu->r24 = OSSwapInt32(cpu->r24); 57 + cpu->r25 = OSSwapInt32(cpu->r25); 58 + cpu->r26 = OSSwapInt32(cpu->r26); 59 + cpu->r27 = OSSwapInt32(cpu->r27); 60 + cpu->r28 = OSSwapInt32(cpu->r28); 61 + cpu->r29 = OSSwapInt32(cpu->r29); 62 + cpu->r30 = OSSwapInt32(cpu->r30); 63 + cpu->r31 = OSSwapInt32(cpu->r31); 64 + cpu->xip = OSSwapInt32(cpu->xip); 65 + cpu->xip_in_bd = OSSwapInt32(cpu->xip_in_bd); 66 + cpu->nip = OSSwapInt32(cpu->nip); 67 + } 68 + 69 + void 70 + swap_m88k_thread_state_xrf_t( 71 + m88k_thread_state_xrf_t *fpu, 72 + enum NXByteOrder target_byte_sex) 73 + { 74 + enum NXByteOrder host_byte_sex; 75 + 76 + struct swapped_m88k_fpsr { 77 + union { 78 + struct { 79 + unsigned afinx:BIT_WIDTH(0); 80 + unsigned afovf:BIT_WIDTH(1); 81 + unsigned afunf:BIT_WIDTH(2); 82 + unsigned afdvz:BIT_WIDTH(3); 83 + unsigned afinv:BIT_WIDTH(4); 84 + unsigned :BITS_WIDTH(15,5); 85 + unsigned xmod:BIT_WIDTH(16); 86 + unsigned :BITS_WIDTH(31,17); 87 + } fields; 88 + uint32_t word; 89 + } u; 90 + } ssr; 91 + struct swapped_m88k_fpcr { 92 + union { 93 + struct { 94 + unsigned efinx:BIT_WIDTH(0); 95 + unsigned efovf:BIT_WIDTH(1); 96 + unsigned efunf:BIT_WIDTH(2); 97 + unsigned efdvz:BIT_WIDTH(3); 98 + unsigned efinv:BIT_WIDTH(4); 99 + unsigned :BITS_WIDTH(13,5); 100 + m88k_fpcr_rm_t rm:BITS_WIDTH(15,14); 101 + unsigned :BITS_WIDTH(31,16); 102 + } fields; 103 + uint32_t word; 104 + } u; 105 + } scr; 106 + 107 + host_byte_sex = NXHostByteOrder(); 108 + 109 + fpu->x1.x[0] = OSSwapInt32(fpu->x1.x[0]); 110 + fpu->x1.x[1] = OSSwapInt32(fpu->x1.x[1]); 111 + fpu->x1.x[2] = OSSwapInt32(fpu->x1.x[2]); 112 + fpu->x1.x[3] = OSSwapInt32(fpu->x1.x[3]); 113 + fpu->x2.x[0] = OSSwapInt32(fpu->x2.x[0]); 114 + fpu->x2.x[1] = OSSwapInt32(fpu->x2.x[1]); 115 + fpu->x2.x[2] = OSSwapInt32(fpu->x2.x[2]); 116 + fpu->x2.x[3] = OSSwapInt32(fpu->x2.x[3]); 117 + fpu->x3.x[0] = OSSwapInt32(fpu->x3.x[0]); 118 + fpu->x3.x[1] = OSSwapInt32(fpu->x3.x[1]); 119 + fpu->x3.x[2] = OSSwapInt32(fpu->x3.x[2]); 120 + fpu->x3.x[3] = OSSwapInt32(fpu->x3.x[3]); 121 + fpu->x4.x[0] = OSSwapInt32(fpu->x4.x[0]); 122 + fpu->x4.x[1] = OSSwapInt32(fpu->x4.x[1]); 123 + fpu->x4.x[2] = OSSwapInt32(fpu->x4.x[2]); 124 + fpu->x4.x[3] = OSSwapInt32(fpu->x4.x[3]); 125 + fpu->x5.x[0] = OSSwapInt32(fpu->x5.x[0]); 126 + fpu->x5.x[1] = OSSwapInt32(fpu->x5.x[1]); 127 + fpu->x5.x[2] = OSSwapInt32(fpu->x5.x[2]); 128 + fpu->x5.x[3] = OSSwapInt32(fpu->x5.x[3]); 129 + fpu->x6.x[0] = OSSwapInt32(fpu->x6.x[0]); 130 + fpu->x6.x[1] = OSSwapInt32(fpu->x6.x[1]); 131 + fpu->x6.x[2] = OSSwapInt32(fpu->x6.x[2]); 132 + fpu->x6.x[3] = OSSwapInt32(fpu->x6.x[3]); 133 + fpu->x7.x[0] = OSSwapInt32(fpu->x7.x[0]); 134 + fpu->x7.x[1] = OSSwapInt32(fpu->x7.x[1]); 135 + fpu->x7.x[2] = OSSwapInt32(fpu->x7.x[2]); 136 + fpu->x7.x[3] = OSSwapInt32(fpu->x7.x[3]); 137 + fpu->x8.x[0] = OSSwapInt32(fpu->x8.x[0]); 138 + fpu->x8.x[1] = OSSwapInt32(fpu->x8.x[1]); 139 + fpu->x8.x[2] = OSSwapInt32(fpu->x8.x[2]); 140 + fpu->x8.x[3] = OSSwapInt32(fpu->x8.x[3]); 141 + fpu->x9.x[0] = OSSwapInt32(fpu->x9.x[0]); 142 + fpu->x9.x[1] = OSSwapInt32(fpu->x9.x[1]); 143 + fpu->x9.x[2] = OSSwapInt32(fpu->x9.x[2]); 144 + fpu->x9.x[3] = OSSwapInt32(fpu->x9.x[3]); 145 + fpu->x10.x[0] = OSSwapInt32(fpu->x10.x[0]); 146 + fpu->x10.x[1] = OSSwapInt32(fpu->x10.x[1]); 147 + fpu->x10.x[2] = OSSwapInt32(fpu->x10.x[2]); 148 + fpu->x10.x[3] = OSSwapInt32(fpu->x10.x[3]); 149 + fpu->x11.x[0] = OSSwapInt32(fpu->x11.x[0]); 150 + fpu->x11.x[1] = OSSwapInt32(fpu->x11.x[1]); 151 + fpu->x11.x[2] = OSSwapInt32(fpu->x11.x[2]); 152 + fpu->x11.x[3] = OSSwapInt32(fpu->x11.x[3]); 153 + fpu->x12.x[0] = OSSwapInt32(fpu->x12.x[0]); 154 + fpu->x12.x[1] = OSSwapInt32(fpu->x12.x[1]); 155 + fpu->x12.x[2] = OSSwapInt32(fpu->x12.x[2]); 156 + fpu->x12.x[3] = OSSwapInt32(fpu->x12.x[3]); 157 + fpu->x13.x[0] = OSSwapInt32(fpu->x13.x[0]); 158 + fpu->x13.x[1] = OSSwapInt32(fpu->x13.x[1]); 159 + fpu->x13.x[2] = OSSwapInt32(fpu->x13.x[2]); 160 + fpu->x13.x[3] = OSSwapInt32(fpu->x13.x[3]); 161 + fpu->x14.x[0] = OSSwapInt32(fpu->x14.x[0]); 162 + fpu->x14.x[1] = OSSwapInt32(fpu->x14.x[1]); 163 + fpu->x14.x[2] = OSSwapInt32(fpu->x14.x[2]); 164 + fpu->x14.x[3] = OSSwapInt32(fpu->x14.x[3]); 165 + fpu->x15.x[0] = OSSwapInt32(fpu->x15.x[0]); 166 + fpu->x15.x[1] = OSSwapInt32(fpu->x15.x[1]); 167 + fpu->x15.x[2] = OSSwapInt32(fpu->x15.x[2]); 168 + fpu->x15.x[3] = OSSwapInt32(fpu->x15.x[3]); 169 + fpu->x16.x[0] = OSSwapInt32(fpu->x16.x[0]); 170 + fpu->x16.x[1] = OSSwapInt32(fpu->x16.x[1]); 171 + fpu->x16.x[2] = OSSwapInt32(fpu->x16.x[2]); 172 + fpu->x16.x[3] = OSSwapInt32(fpu->x16.x[3]); 173 + fpu->x17.x[0] = OSSwapInt32(fpu->x17.x[0]); 174 + fpu->x17.x[1] = OSSwapInt32(fpu->x17.x[1]); 175 + fpu->x17.x[2] = OSSwapInt32(fpu->x17.x[2]); 176 + fpu->x17.x[3] = OSSwapInt32(fpu->x17.x[3]); 177 + fpu->x18.x[0] = OSSwapInt32(fpu->x18.x[0]); 178 + fpu->x18.x[1] = OSSwapInt32(fpu->x18.x[1]); 179 + fpu->x18.x[2] = OSSwapInt32(fpu->x18.x[2]); 180 + fpu->x18.x[3] = OSSwapInt32(fpu->x18.x[3]); 181 + fpu->x19.x[0] = OSSwapInt32(fpu->x19.x[0]); 182 + fpu->x19.x[1] = OSSwapInt32(fpu->x19.x[1]); 183 + fpu->x19.x[2] = OSSwapInt32(fpu->x19.x[2]); 184 + fpu->x19.x[3] = OSSwapInt32(fpu->x19.x[3]); 185 + fpu->x20.x[0] = OSSwapInt32(fpu->x20.x[0]); 186 + fpu->x20.x[1] = OSSwapInt32(fpu->x20.x[1]); 187 + fpu->x20.x[2] = OSSwapInt32(fpu->x20.x[2]); 188 + fpu->x20.x[3] = OSSwapInt32(fpu->x20.x[3]); 189 + fpu->x21.x[0] = OSSwapInt32(fpu->x21.x[0]); 190 + fpu->x21.x[1] = OSSwapInt32(fpu->x21.x[1]); 191 + fpu->x21.x[2] = OSSwapInt32(fpu->x21.x[2]); 192 + fpu->x21.x[3] = OSSwapInt32(fpu->x21.x[3]); 193 + fpu->x22.x[0] = OSSwapInt32(fpu->x22.x[0]); 194 + fpu->x22.x[1] = OSSwapInt32(fpu->x22.x[1]); 195 + fpu->x22.x[2] = OSSwapInt32(fpu->x22.x[2]); 196 + fpu->x22.x[3] = OSSwapInt32(fpu->x22.x[3]); 197 + fpu->x23.x[0] = OSSwapInt32(fpu->x23.x[0]); 198 + fpu->x23.x[1] = OSSwapInt32(fpu->x23.x[1]); 199 + fpu->x23.x[2] = OSSwapInt32(fpu->x23.x[2]); 200 + fpu->x23.x[3] = OSSwapInt32(fpu->x23.x[3]); 201 + fpu->x24.x[0] = OSSwapInt32(fpu->x24.x[0]); 202 + fpu->x24.x[1] = OSSwapInt32(fpu->x24.x[1]); 203 + fpu->x24.x[2] = OSSwapInt32(fpu->x24.x[2]); 204 + fpu->x24.x[3] = OSSwapInt32(fpu->x24.x[3]); 205 + fpu->x25.x[0] = OSSwapInt32(fpu->x25.x[0]); 206 + fpu->x25.x[1] = OSSwapInt32(fpu->x25.x[1]); 207 + fpu->x25.x[2] = OSSwapInt32(fpu->x25.x[2]); 208 + fpu->x25.x[3] = OSSwapInt32(fpu->x25.x[3]); 209 + fpu->x26.x[0] = OSSwapInt32(fpu->x26.x[0]); 210 + fpu->x26.x[1] = OSSwapInt32(fpu->x26.x[1]); 211 + fpu->x26.x[2] = OSSwapInt32(fpu->x26.x[2]); 212 + fpu->x26.x[3] = OSSwapInt32(fpu->x26.x[3]); 213 + fpu->x27.x[0] = OSSwapInt32(fpu->x27.x[0]); 214 + fpu->x27.x[1] = OSSwapInt32(fpu->x27.x[1]); 215 + fpu->x27.x[2] = OSSwapInt32(fpu->x27.x[2]); 216 + fpu->x27.x[3] = OSSwapInt32(fpu->x27.x[3]); 217 + fpu->x28.x[0] = OSSwapInt32(fpu->x28.x[0]); 218 + fpu->x28.x[1] = OSSwapInt32(fpu->x28.x[1]); 219 + fpu->x28.x[2] = OSSwapInt32(fpu->x28.x[2]); 220 + fpu->x28.x[3] = OSSwapInt32(fpu->x28.x[3]); 221 + fpu->x29.x[0] = OSSwapInt32(fpu->x29.x[0]); 222 + fpu->x29.x[1] = OSSwapInt32(fpu->x29.x[1]); 223 + fpu->x29.x[2] = OSSwapInt32(fpu->x29.x[2]); 224 + fpu->x29.x[3] = OSSwapInt32(fpu->x29.x[3]); 225 + fpu->x30.x[0] = OSSwapInt32(fpu->x30.x[0]); 226 + fpu->x30.x[1] = OSSwapInt32(fpu->x30.x[1]); 227 + fpu->x30.x[2] = OSSwapInt32(fpu->x30.x[2]); 228 + fpu->x30.x[3] = OSSwapInt32(fpu->x30.x[3]); 229 + fpu->x31.x[0] = OSSwapInt32(fpu->x31.x[0]); 230 + fpu->x31.x[1] = OSSwapInt32(fpu->x31.x[1]); 231 + fpu->x31.x[2] = OSSwapInt32(fpu->x31.x[2]); 232 + fpu->x31.x[3] = OSSwapInt32(fpu->x31.x[3]); 233 + 234 + if(target_byte_sex == host_byte_sex){ 235 + memcpy(&ssr, &(fpu->fpsr), sizeof(struct swapped_m88k_fpsr)); 236 + ssr.u.word = OSSwapInt32(ssr.u.word); 237 + fpu->fpsr.afinx = ssr.u.fields.afinx; 238 + fpu->fpsr.afovf = ssr.u.fields.afovf; 239 + fpu->fpsr.afunf = ssr.u.fields.afunf; 240 + fpu->fpsr.afdvz = ssr.u.fields.afdvz; 241 + fpu->fpsr.afinv = ssr.u.fields.afinv; 242 + fpu->fpsr.xmod = ssr.u.fields.xmod; 243 + 244 + memcpy(&scr, &(fpu->fpcr), sizeof(struct swapped_m88k_fpcr)); 245 + scr.u.word = OSSwapInt32(scr.u.word); 246 + fpu->fpcr.efinx = scr.u.fields.efinx; 247 + fpu->fpcr.efovf = scr.u.fields.efovf; 248 + fpu->fpcr.efunf = scr.u.fields.efunf; 249 + fpu->fpcr.efdvz = scr.u.fields.efdvz; 250 + fpu->fpcr.efinv = scr.u.fields.efinv; 251 + fpu->fpcr.rm = scr.u.fields.rm; 252 + } 253 + else{ 254 + ssr.u.fields.afinx = fpu->fpsr.afinx; 255 + ssr.u.fields.afovf = fpu->fpsr.afovf; 256 + ssr.u.fields.afunf = fpu->fpsr.afunf; 257 + ssr.u.fields.afdvz = fpu->fpsr.afdvz; 258 + ssr.u.fields.afinv = fpu->fpsr.afinv; 259 + ssr.u.fields.xmod = fpu->fpsr.xmod; 260 + ssr.u.word = OSSwapInt32(ssr.u.word); 261 + memcpy(&(fpu->fpsr), &ssr, sizeof(struct swapped_m88k_fpsr)); 262 + 263 + scr.u.fields.efinx = fpu->fpcr.efinx; 264 + scr.u.fields.efovf = fpu->fpcr.efovf; 265 + scr.u.fields.efunf = fpu->fpcr.efunf; 266 + scr.u.fields.efdvz = fpu->fpcr.efdvz; 267 + scr.u.fields.efinv = fpu->fpcr.efinv; 268 + scr.u.fields.rm = fpu->fpcr.rm; 269 + scr.u.word = OSSwapInt32(scr.u.word); 270 + memcpy(&(fpu->fpcr), &scr, sizeof(struct swapped_m88k_fpcr)); 271 + } 272 + } 273 + 274 + void 275 + swap_m88k_thread_state_user_t( 276 + m88k_thread_state_user_t *user, 277 + enum NXByteOrder target_byte_sex) 278 + { 279 + user->user = OSSwapInt32(user->user); 280 + } 281 + 282 + void 283 + swap_m88110_thread_state_impl_t( 284 + m88110_thread_state_impl_t *spu, 285 + enum NXByteOrder target_byte_sex) 286 + { 287 + uint32_t i; 288 + enum NXByteOrder host_byte_sex; 289 + 290 + struct swapped_m88110_bp_ctrl { 291 + union { 292 + struct { 293 + unsigned v:BIT_WIDTH(0); 294 + m88110_match_t addr_match:BITS_WIDTH(12,1); 295 + unsigned :BITS_WIDTH(26,13); 296 + unsigned rwm:BIT_WIDTH(27); 297 + unsigned rw:BIT_WIDTH(28); 298 + unsigned :BITS_WIDTH(31,29); 299 + } fields; 300 + uint32_t word; 301 + } u; 302 + } sbpc; 303 + 304 + struct swap_m88110_psr { 305 + union { 306 + struct { 307 + unsigned :BITS_WIDTH(1,0); 308 + unsigned mxm_dis:BIT_WIDTH(2); 309 + unsigned sfu1dis:BIT_WIDTH(3); 310 + unsigned :BITS_WIDTH(22,4); 311 + unsigned trace:BIT_WIDTH(23); 312 + unsigned :BIT_WIDTH(24); 313 + unsigned sm:BIT_WIDTH(25); 314 + unsigned sgn_imd:BIT_WIDTH(26); 315 + unsigned :BIT_WIDTH(27); 316 + unsigned c:BIT_WIDTH(28); 317 + unsigned se:BIT_WIDTH(29); 318 + unsigned le:BIT_WIDTH(30); 319 + unsigned supr:BIT_WIDTH(31); 320 + } fields; 321 + uint32_t word; 322 + } u; 323 + } spsr; 324 + 325 + struct swapped_m88110_fp_trap_status { 326 + union { 327 + struct { 328 + unsigned efinx:BIT_WIDTH(0); 329 + unsigned efovf:BIT_WIDTH(1); 330 + unsigned efunf:BIT_WIDTH(2); 331 + unsigned efdvz:BIT_WIDTH(3); 332 + unsigned efinv:BIT_WIDTH(4); 333 + unsigned priv:BIT_WIDTH(5); 334 + unsigned unimp:BIT_WIDTH(6); 335 + unsigned int:BIT_WIDTH(7); 336 + unsigned sfu1_disabled:BIT_WIDTH(8); 337 + unsigned :BITS_WIDTH(13,9); 338 + m88110_iresult_size_t iresult_size:BITS_WIDTH(15,14); 339 + unsigned :BITS_WIDTH(31,16); 340 + } fields; 341 + uint32_t word; 342 + } u; 343 + } sfps; 344 + 345 + host_byte_sex = NXHostByteOrder(); 346 + 347 + if(target_byte_sex == host_byte_sex){ 348 + for(i = 0; i < M88110_N_DATA_BP; i++){ 349 + spu->data_bp[i].addr = OSSwapInt32(spu->data_bp[i].addr); 350 + memcpy(&sbpc, &(spu->data_bp[i].ctrl), 351 + sizeof(struct swapped_m88110_bp_ctrl)); 352 + sbpc.u.word = OSSwapInt32(sbpc.u.word); 353 + spu->data_bp[i].ctrl.v = sbpc.u.fields.v; 354 + spu->data_bp[i].ctrl.addr_match = sbpc.u.fields.addr_match; 355 + spu->data_bp[i].ctrl.rwm = sbpc.u.fields.rwm; 356 + spu->data_bp[i].ctrl.rw = sbpc.u.fields.rw; 357 + } 358 + 359 + memcpy(&spsr, &(spu->psr), sizeof(struct swap_m88110_psr)); 360 + spsr.u.word = OSSwapInt32(spsr.u.word); 361 + spu->psr.mxm_dis = spsr.u.fields.mxm_dis; 362 + spu->psr.sfu1dis = spsr.u.fields.sfu1dis; 363 + spu->psr.trace = spsr.u.fields.trace; 364 + spu->psr.sm = spsr.u.fields.sm; 365 + spu->psr.sgn_imd = spsr.u.fields.sgn_imd; 366 + spu->psr.c = spsr.u.fields.c; 367 + spu->psr.se = spsr.u.fields.se; 368 + spu->psr.le = spsr.u.fields.le; 369 + spu->psr.supr = spsr.u.fields.supr; 370 + 371 + memcpy(&sfps, &(spu->fp_trap_status), 372 + sizeof(struct swapped_m88110_fp_trap_status)); 373 + sfps.u.word = OSSwapInt32(sfps.u.word); 374 + spu->fp_trap_status.efinx = sfps.u.fields.efinx; 375 + spu->fp_trap_status.efovf = sfps.u.fields.efovf; 376 + spu->fp_trap_status.efunf = sfps.u.fields.efunf; 377 + spu->fp_trap_status.efdvz = sfps.u.fields.efdvz; 378 + spu->fp_trap_status.efinv = sfps.u.fields.efinv; 379 + spu->fp_trap_status.priv = sfps.u.fields.priv; 380 + spu->fp_trap_status.unimp = sfps.u.fields.unimp; 381 + spu->fp_trap_status.sfu1_disabled = sfps.u.fields.sfu1_disabled; 382 + spu->fp_trap_status.iresult_size = sfps.u.fields.iresult_size; 383 + } 384 + else{ 385 + for(i = 0; i < M88110_N_DATA_BP; i++){ 386 + spu->data_bp[i].addr = OSSwapInt32(spu->data_bp[i].addr); 387 + sbpc.u.fields.v = spu->data_bp[i].ctrl.v; 388 + sbpc.u.fields.addr_match = spu->data_bp[i].ctrl.addr_match; 389 + sbpc.u.fields.rwm = spu->data_bp[i].ctrl.rwm; 390 + sbpc.u.fields.rw = spu->data_bp[i].ctrl.rw; 391 + sbpc.u.word = OSSwapInt32(sbpc.u.word); 392 + memcpy(&(spu->data_bp[i].ctrl), &sbpc, 393 + sizeof(struct swapped_m88110_bp_ctrl)); 394 + } 395 + 396 + spsr.u.fields.mxm_dis = spu->psr.mxm_dis; 397 + spsr.u.fields.sfu1dis = spu->psr.sfu1dis; 398 + spsr.u.fields.trace = spu->psr.trace; 399 + spsr.u.fields.sm = spu->psr.sm; 400 + spsr.u.fields.sgn_imd = spu->psr.sgn_imd; 401 + spsr.u.fields.c = spu->psr.c; 402 + spsr.u.fields.se = spu->psr.se; 403 + spsr.u.fields.le = spu->psr.le; 404 + spsr.u.fields.supr = spu->psr.supr; 405 + spsr.u.word = OSSwapInt32(spsr.u.word); 406 + memcpy(&(spu->psr), &spsr, sizeof(struct swap_m88110_psr)); 407 + 408 + sfps.u.fields.efinx = spu->fp_trap_status.efinx; 409 + sfps.u.fields.efovf = spu->fp_trap_status.efovf; 410 + sfps.u.fields.efunf = spu->fp_trap_status.efunf; 411 + sfps.u.fields.efdvz = spu->fp_trap_status.efdvz; 412 + sfps.u.fields.efinv = spu->fp_trap_status.efinv; 413 + sfps.u.fields.priv = spu->fp_trap_status.priv; 414 + sfps.u.fields.unimp = spu->fp_trap_status.unimp; 415 + sfps.u.fields.sfu1_disabled = spu->fp_trap_status.sfu1_disabled; 416 + sfps.u.fields.iresult_size = spu->fp_trap_status.iresult_size; 417 + sfps.u.word = OSSwapInt32(sfps.u.word); 418 + memcpy(&(spu->fp_trap_status), &sfps, 419 + sizeof(struct swapped_m88110_fp_trap_status)); 420 + } 421 + spu->intermediate_result.x[0] = 422 + OSSwapInt32(spu->intermediate_result.x[0]); 423 + spu->intermediate_result.x[1] = 424 + OSSwapInt32(spu->intermediate_result.x[1]); 425 + spu->intermediate_result.x[2] = 426 + OSSwapInt32(spu->intermediate_result.x[2]); 427 + spu->intermediate_result.x[3] = 428 + OSSwapInt32(spu->intermediate_result.x[3]); 429 + } 430 + #endif /* !defined(RLD) */
+154
src/libmacho/ppc_swap.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + 25 + #define __cr cr 26 + #define __ctr ctr 27 + #define __dar dar 28 + #define __dsisr dsisr 29 + #define __exception exception 30 + #define __fpregs fpregs 31 + #define __fpscr fpscr 32 + #define __fpscr_pad fpscr_pad 33 + #define __lr lr 34 + #define __mq mq 35 + #define __pad0 pad0 36 + #define __pad1 pad1 37 + #define __r0 r0 38 + #define __r1 r1 39 + #define __r10 r10 40 + #define __r11 r11 41 + #define __r12 r12 42 + #define __r13 r13 43 + #define __r14 r14 44 + #define __r15 r15 45 + #define __r16 r16 46 + #define __r17 r17 47 + #define __r18 r18 48 + #define __r19 r19 49 + #define __r2 r2 50 + #define __r20 r20 51 + #define __r21 r21 52 + #define __r22 r22 53 + #define __r23 r23 54 + #define __r24 r24 55 + #define __r25 r25 56 + #define __r26 r26 57 + #define __r27 r27 58 + #define __r28 r28 59 + #define __r29 r29 60 + #define __r3 r3 61 + #define __r30 r30 62 + #define __r31 r31 63 + #define __r4 r4 64 + #define __r5 r5 65 + #define __r6 r6 66 + #define __r7 r7 67 + #define __r8 r8 68 + #define __r9 r9 69 + #define __srr0 srr0 70 + #define __srr1 srr1 71 + #define __vrsave vrsave 72 + #define __xer xer 73 + 74 + #import <mach-o/ppc/swap.h> 75 + #import <architecture/nrw/reg_help.h> 76 + 77 + void 78 + swap_ppc_thread_state_t( 79 + ppc_thread_state_t *cpu, 80 + enum NXByteOrder target_byte_sex) 81 + { 82 + cpu->srr0 = OSSwapInt32(cpu->srr0); 83 + cpu->srr1 = OSSwapInt32(cpu->srr1); 84 + cpu->r0 = OSSwapInt32(cpu->r0); 85 + cpu->r1 = OSSwapInt32(cpu->r1); 86 + cpu->r2 = OSSwapInt32(cpu->r2); 87 + cpu->r3 = OSSwapInt32(cpu->r3); 88 + cpu->r4 = OSSwapInt32(cpu->r4); 89 + cpu->r5 = OSSwapInt32(cpu->r5); 90 + cpu->r6 = OSSwapInt32(cpu->r6); 91 + cpu->r7 = OSSwapInt32(cpu->r7); 92 + cpu->r8 = OSSwapInt32(cpu->r8); 93 + cpu->r9 = OSSwapInt32(cpu->r9); 94 + cpu->r10 = OSSwapInt32(cpu->r10); 95 + cpu->r11 = OSSwapInt32(cpu->r11); 96 + cpu->r12 = OSSwapInt32(cpu->r12); 97 + cpu->r13 = OSSwapInt32(cpu->r13); 98 + cpu->r14 = OSSwapInt32(cpu->r14); 99 + cpu->r15 = OSSwapInt32(cpu->r15); 100 + cpu->r16 = OSSwapInt32(cpu->r16); 101 + cpu->r17 = OSSwapInt32(cpu->r17); 102 + cpu->r18 = OSSwapInt32(cpu->r18); 103 + cpu->r19 = OSSwapInt32(cpu->r19); 104 + cpu->r20 = OSSwapInt32(cpu->r20); 105 + cpu->r21 = OSSwapInt32(cpu->r21); 106 + cpu->r22 = OSSwapInt32(cpu->r22); 107 + cpu->r23 = OSSwapInt32(cpu->r23); 108 + cpu->r24 = OSSwapInt32(cpu->r24); 109 + cpu->r25 = OSSwapInt32(cpu->r25); 110 + cpu->r26 = OSSwapInt32(cpu->r26); 111 + cpu->r27 = OSSwapInt32(cpu->r27); 112 + cpu->r28 = OSSwapInt32(cpu->r28); 113 + cpu->r29 = OSSwapInt32(cpu->r29); 114 + cpu->r30 = OSSwapInt32(cpu->r30); 115 + cpu->r31 = OSSwapInt32(cpu->r31); 116 + cpu->lr = OSSwapInt32(cpu->lr); 117 + cpu->cr = OSSwapInt32(cpu->cr); 118 + cpu->xer = OSSwapInt32(cpu->xer); 119 + cpu->ctr = OSSwapInt32(cpu->ctr); 120 + cpu->mq = OSSwapInt32(cpu->mq); 121 + cpu->vrsave = OSSwapInt32(cpu->vrsave); 122 + 123 + } 124 + 125 + void 126 + swap_ppc_float_state_t( 127 + ppc_float_state_t *fpu, 128 + enum NXByteOrder target_byte_sex) 129 + { 130 + uint32_t i; 131 + 132 + for(i = 0; i < 32; i++) 133 + fpu->fpregs[i] = OSSwapInt64(fpu->fpregs[i]); 134 + 135 + fpu->fpscr_pad = OSSwapInt32(fpu->fpscr_pad); 136 + fpu->fpscr = OSSwapInt32(fpu->fpscr); 137 + } 138 + 139 + void 140 + swap_ppc_exception_state_t( 141 + ppc_exception_state_t *state, 142 + enum NXByteOrder target_byte_sex) 143 + { 144 + uint32_t i; 145 + 146 + state->dar = OSSwapInt32(state->dar); 147 + state->dsisr = OSSwapInt32(state->dsisr); 148 + state->exception = OSSwapInt32(state->exception); 149 + state->pad0 = OSSwapInt32(state->pad0); 150 + 151 + for(i = 0; i < 4; i++) 152 + state->pad1[i] = OSSwapInt32(state->pad1[i]); 153 + } 154 + #endif /* !defined(RLD) */
+6
src/libmacho/shlib.ofileList
··· 1 + arch.o 2 + getmachhead.o 3 + getsecbyname.o 4 + getsegbyname.o 5 + get_end.o 6 + swap.o
+66
src/libmacho/slot_name.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. The rights granted to you under the License 10 + * may not be used to create, or enable the creation or redistribution of, 11 + * unlawful or unlicensed copies of an Apple operating system, or to 12 + * circumvent, violate, or enable the circumvention or violation of, any 13 + * terms of an Apple operating system software license agreement. 14 + * 15 + * Please obtain a copy of the License at 16 + * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 + * 18 + * The Original Code and all software distributed under the License are 19 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 + * Please see the License for the specific language governing rights and 24 + * limitations under the License. 25 + * 26 + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 + */ 28 + /* 29 + * File: slot_name.c 30 + * Author: Avadis Tevanian, Jr. 31 + * 32 + * Copyright (C) 1987, Avadis Tevanian, Jr. 33 + * 34 + * Convert machine slot values to human readable strings. 35 + * 36 + * HISTORY 37 + * 26-Jan-88 Mary Thompson (mrt) at Carnegie Mellon 38 + * added case for CUP_SUBTYPE_RT_APC 39 + * 40 + * 28-Feb-87 Avadis Tevanian (avie) at Carnegie-Mellon University 41 + * Created. 42 + * 43 + */ 44 + 45 + #include <mach-o/arch.h> 46 + #include <stddef.h> 47 + 48 + /* 49 + * Convert the specified cpu_type/cpu_subtype pair to their 50 + * human readable form. 51 + */ 52 + void slot_name(cpu_type, cpu_subtype, cpu_name, cpu_subname) 53 + cpu_type_t cpu_type; 54 + cpu_subtype_t cpu_subtype; 55 + char **cpu_name, **cpu_subname; 56 + { 57 + register char *name = "Unknown CPU"; 58 + register char *subname = ""; 59 + const NXArchInfo *ai = NXGetArchInfoFromCpuType(cpu_type, cpu_subtype); 60 + if (ai != NULL) { 61 + name = (char *)ai->name; 62 + subname = (char *)ai->description; 63 + } 64 + *cpu_name = name; 65 + *cpu_subname = subname; 66 + }
+178
src/libmacho/sparc_swap.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + #include <string.h> 25 + #import <mach-o/sparc/swap.h> 26 + #import <architecture/nrw/reg_help.h> 27 + 28 + void 29 + swap_sparc_thread_state_regs( 30 + struct sparc_thread_state_regs *cpu, 31 + enum NXByteOrder target_byte_sex) 32 + { 33 + struct swapped_psr { 34 + union { 35 + struct { 36 + unsigned int 37 + cwp:BITS_WIDTH(4,0), 38 + et:BIT_WIDTH(5), 39 + ps:BIT_WIDTH(6), 40 + s:BIT_WIDTH(7), 41 + pil:BITS_WIDTH(11,8), 42 + ef:BIT_WIDTH(12), 43 + ec:BIT_WIDTH(13), 44 + reserved:BITS_WIDTH(19,14), 45 + icc:BITS_WIDTH(23,20), 46 + ver:BITS_WIDTH(27,24), 47 + impl:BITS_WIDTH(31,28); 48 + } fields; 49 + unsigned int word; 50 + } u; 51 + } spsr; 52 + struct p_status *pr_status; 53 + enum NXByteOrder host_byte_sex; 54 + 55 + host_byte_sex = NXHostByteOrder(); 56 + 57 + cpu->regs.r_pc = OSSwapInt32(cpu->regs.r_pc); 58 + cpu->regs.r_npc = OSSwapInt32(cpu->regs.r_npc); 59 + cpu->regs.r_y = OSSwapInt32(cpu->regs.r_y); 60 + cpu->regs.r_g1 = OSSwapInt32(cpu->regs.r_g1); 61 + cpu->regs.r_g2 = OSSwapInt32(cpu->regs.r_g2); 62 + cpu->regs.r_g3 = OSSwapInt32(cpu->regs.r_g3); 63 + cpu->regs.r_g4 = OSSwapInt32(cpu->regs.r_g4); 64 + cpu->regs.r_g5 = OSSwapInt32(cpu->regs.r_g5); 65 + cpu->regs.r_g6 = OSSwapInt32(cpu->regs.r_g6); 66 + cpu->regs.r_g7 = OSSwapInt32(cpu->regs.r_g7); 67 + cpu->regs.r_o0 = OSSwapInt32(cpu->regs.r_o0); 68 + cpu->regs.r_o1 = OSSwapInt32(cpu->regs.r_o1); 69 + cpu->regs.r_o2 = OSSwapInt32(cpu->regs.r_o2); 70 + cpu->regs.r_o3 = OSSwapInt32(cpu->regs.r_o3); 71 + cpu->regs.r_o4 = OSSwapInt32(cpu->regs.r_o4); 72 + cpu->regs.r_o5 = OSSwapInt32(cpu->regs.r_o5); 73 + cpu->regs.r_o6 = OSSwapInt32(cpu->regs.r_o6); 74 + cpu->regs.r_o7 = OSSwapInt32(cpu->regs.r_o7); 75 + 76 + pr_status = (struct p_status *) &(cpu->regs.r_psr); 77 + if(target_byte_sex == host_byte_sex){ 78 + memcpy(&spsr, &(cpu->regs.r_psr), sizeof(struct swapped_psr)); 79 + spsr.u.word = OSSwapInt32(spsr.u.word); 80 + pr_status->PSRREG.psr_bits.cwp = spsr.u.fields.cwp; 81 + pr_status->PSRREG.psr_bits.ps = spsr.u.fields.ps; 82 + pr_status->PSRREG.psr_bits.s = spsr.u.fields.s; 83 + pr_status->PSRREG.psr_bits.pil = spsr.u.fields.pil; 84 + pr_status->PSRREG.psr_bits.ef = spsr.u.fields.ef; 85 + pr_status->PSRREG.psr_bits.ec = spsr.u.fields.ec; 86 + pr_status->PSRREG.psr_bits.reserved = spsr.u.fields.reserved; 87 + pr_status->PSRREG.psr_bits.icc = spsr.u.fields.icc; 88 + pr_status->PSRREG.psr_bits.et = spsr.u.fields.ver; 89 + pr_status->PSRREG.psr_bits.impl = spsr.u.fields.impl; 90 + } 91 + else{ 92 + spsr.u.fields.cwp = pr_status->PSRREG.psr_bits.cwp; 93 + spsr.u.fields.ps = pr_status->PSRREG.psr_bits.ps; 94 + spsr.u.fields.s = pr_status->PSRREG.psr_bits.s; 95 + spsr.u.fields.pil = pr_status->PSRREG.psr_bits.pil; 96 + spsr.u.fields.ef = pr_status->PSRREG.psr_bits.ef; 97 + spsr.u.fields.ec = pr_status->PSRREG.psr_bits.ec; 98 + spsr.u.fields.reserved = pr_status->PSRREG.psr_bits.reserved; 99 + spsr.u.fields.icc = pr_status->PSRREG.psr_bits.icc; 100 + spsr.u.fields.ver = pr_status->PSRREG.psr_bits.et; 101 + spsr.u.fields.impl = pr_status->PSRREG.psr_bits.impl; 102 + spsr.u.word = OSSwapInt32(spsr.u.word); 103 + memcpy(&(cpu->regs.r_psr), &spsr, sizeof(struct swapped_psr)); 104 + } 105 + } 106 + 107 + void 108 + swap_sparc_thread_state_fpu( 109 + struct sparc_thread_state_fpu *fpu, 110 + enum NXByteOrder target_byte_sex) 111 + { 112 + struct swapped_fsr { 113 + union { 114 + struct { 115 + unsigned int 116 + cexc:BITS_WIDTH(4,0), 117 + aexc:BITS_WIDTH(9,5), 118 + fcc:BITS_WIDTH(11,10), 119 + pr:BIT_WIDTH(12), 120 + qne:BIT_WIDTH(13), 121 + ftt:BITS_WIDTH(16,14), 122 + res:BITS_WIDTH(22,17), 123 + tem:BITS_WIDTH(27,23), 124 + rp:BITS_WIDTH(29,28), 125 + rd:BITS_WIDTH(31,30); 126 + } fields; 127 + unsigned int word; 128 + } u; 129 + } sfsr; 130 + uint32_t i; 131 + struct f_status *fpu_status; 132 + enum NXByteOrder host_byte_sex; 133 + 134 + host_byte_sex = NXHostByteOrder(); 135 + 136 + 137 + /* floating point registers */ 138 + for(i = 0; i < 16; i++) /* 16 doubles */ 139 + fpu->fpu.fpu_fr.Fpu_dregs[i] = 140 + OSSwapInt64(fpu->fpu.fpu_fr.Fpu_dregs[i]); 141 + 142 + fpu->fpu.Fpu_q[0].FQu.whole = OSSwapInt64(fpu->fpu.Fpu_q[0].FQu.whole); 143 + fpu->fpu.Fpu_q[1].FQu.whole = OSSwapInt64(fpu->fpu.Fpu_q[1].FQu.whole); 144 + fpu->fpu.Fpu_flags = OSSwapInt32(fpu->fpu.Fpu_flags); 145 + fpu->fpu.Fpu_extra = OSSwapInt32(fpu->fpu.Fpu_extra); 146 + fpu->fpu.Fpu_qcnt = OSSwapInt32(fpu->fpu.Fpu_qcnt); 147 + 148 + fpu_status = (struct f_status *) &(fpu->fpu.Fpu_fsr); 149 + if(target_byte_sex == host_byte_sex){ 150 + memcpy(&sfsr, &(fpu->fpu.Fpu_fsr), sizeof(unsigned int)); 151 + sfsr.u.word = OSSwapInt32(sfsr.u.word); 152 + fpu_status->FPUREG.Fpu_fsr_bits.rd = sfsr.u.fields.rd; 153 + fpu_status->FPUREG.Fpu_fsr_bits.rp = sfsr.u.fields.rp; 154 + fpu_status->FPUREG.Fpu_fsr_bits.tem = sfsr.u.fields.tem; 155 + fpu_status->FPUREG.Fpu_fsr_bits.res = sfsr.u.fields.res; 156 + fpu_status->FPUREG.Fpu_fsr_bits.ftt = sfsr.u.fields.ftt; 157 + fpu_status->FPUREG.Fpu_fsr_bits.qne = sfsr.u.fields.qne; 158 + fpu_status->FPUREG.Fpu_fsr_bits.pr = sfsr.u.fields.pr; 159 + fpu_status->FPUREG.Fpu_fsr_bits.fcc = sfsr.u.fields.fcc; 160 + fpu_status->FPUREG.Fpu_fsr_bits.aexc = sfsr.u.fields.aexc; 161 + fpu_status->FPUREG.Fpu_fsr_bits.cexc = sfsr.u.fields.cexc; 162 + } 163 + else{ 164 + sfsr.u.fields.rd = fpu_status->FPUREG.Fpu_fsr_bits.rd; 165 + sfsr.u.fields.rp = fpu_status->FPUREG.Fpu_fsr_bits.rp; 166 + sfsr.u.fields.tem = fpu_status->FPUREG.Fpu_fsr_bits.tem; 167 + sfsr.u.fields.res = fpu_status->FPUREG.Fpu_fsr_bits.res; 168 + sfsr.u.fields.ftt = fpu_status->FPUREG.Fpu_fsr_bits.ftt; 169 + sfsr.u.fields.qne = fpu_status->FPUREG.Fpu_fsr_bits.qne; 170 + sfsr.u.fields.pr = fpu_status->FPUREG.Fpu_fsr_bits.pr; 171 + sfsr.u.fields.fcc = fpu_status->FPUREG.Fpu_fsr_bits.fcc; 172 + sfsr.u.fields.aexc = fpu_status->FPUREG.Fpu_fsr_bits.aexc; 173 + sfsr.u.fields.cexc = fpu_status->FPUREG.Fpu_fsr_bits.cexc; 174 + sfsr.u.word = OSSwapInt32(sfsr.u.word); 175 + memcpy(&(fpu->fpu.Fpu_fsr), &sfsr, sizeof(struct swapped_fsr)); 176 + } 177 + } 178 + #endif /* !defined(RLD) */
+41
src/libmacho/stuff/openstep_mach.h
··· 1 + /* 2 + * Copyright (c) 2004, Apple Computer, Inc. All rights reserved. 3 + * 4 + * Redistribution and use in source and binary forms, with or without 5 + * modification, are permitted provided that the following conditions 6 + * are met: 7 + * 1. Redistributions of source code must retain the above copyright 8 + * notice, this list of conditions and the following disclaimer. 9 + * 2. Redistributions in binary form must reproduce the above copyright 10 + * notice, this list of conditions and the following disclaimer in the 11 + * documentation and/or other materials provided with the distribution. 12 + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 13 + * its contributors may be used to endorse or promote products derived 14 + * from this software without specific prior written permission. 15 + * 16 + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND 17 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 + * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR 20 + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25 + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 + * POSSIBILITY OF SUCH DAMAGE. 27 + */ 28 + #ifdef __OPENSTEP__ 29 + /* 30 + * This file is used to allow cctools to be compiled for Openstep now that 31 + * the code has been changed to use Mach 3.0 names (which work for MacOS X and 32 + * Rhapsody but not for Openstep). 33 + */ 34 + #define mach_task_self task_self 35 + #define mach_task_self_ task_self_ 36 + #define mach_host_self host_self 37 + #define mach_thread_self thread_self 38 + #define mach_port_allocate port_allocate 39 + #define mach_port_deallocate port_deallocate 40 + #define mach_port_names port_names 41 + #endif /* __OPENSTEP__ */
+781
src/libmacho/swap.c
··· 1 + /* 2 + * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 + * 4 + * @APPLE_LICENSE_HEADER_START@ 5 + * 6 + * This file contains Original Code and/or Modifications of Original Code 7 + * as defined in and that are subject to the Apple Public Source License 8 + * Version 2.0 (the 'License'). You may not use this file except in 9 + * compliance with the License. Please obtain a copy of the License at 10 + * http://www.opensource.apple.com/apsl/ and read it before using this 11 + * file. 12 + * 13 + * The Original Code and all software distributed under the License are 14 + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 + * Please see the License for the specific language governing rights and 19 + * limitations under the License. 20 + * 21 + * @APPLE_LICENSE_HEADER_END@ 22 + */ 23 + #ifndef RLD 24 + #include <stdint.h> 25 + #include <libkern/OSByteOrder.h> 26 + #include <mach-o/swap.h> 27 + #include <string.h> 28 + 29 + void 30 + swap_fat_header( 31 + struct fat_header *fat_header, 32 + enum NXByteOrder target_byte_sex) 33 + { 34 + fat_header->magic = OSSwapInt32(fat_header->magic); 35 + fat_header->nfat_arch = OSSwapInt32(fat_header->nfat_arch); 36 + } 37 + 38 + void 39 + swap_fat_arch( 40 + struct fat_arch *fat_archs, 41 + uint32_t nfat_arch, 42 + enum NXByteOrder target_byte_sex) 43 + { 44 + uint32_t i; 45 + 46 + for(i = 0; i < nfat_arch; i++){ 47 + fat_archs[i].cputype = OSSwapInt32(fat_archs[i].cputype); 48 + fat_archs[i].cpusubtype = OSSwapInt32(fat_archs[i].cpusubtype); 49 + fat_archs[i].offset = OSSwapInt32(fat_archs[i].offset); 50 + fat_archs[i].size = OSSwapInt32(fat_archs[i].size); 51 + fat_archs[i].align = OSSwapInt32(fat_archs[i].align); 52 + } 53 + } 54 + 55 + void 56 + swap_fat_arch_64( 57 + struct fat_arch_64 *fat_archs64, 58 + uint32_t nfat_arch, 59 + enum NXByteOrder target_byte_sex) 60 + { 61 + uint32_t i; 62 + 63 + for(i = 0; i < nfat_arch; i++){ 64 + fat_archs64[i].cputype = OSSwapInt32(fat_archs64[i].cputype); 65 + fat_archs64[i].cpusubtype = OSSwapInt32(fat_archs64[i].cpusubtype); 66 + fat_archs64[i].offset = OSSwapInt64(fat_archs64[i].offset); 67 + fat_archs64[i].size = OSSwapInt64(fat_archs64[i].size); 68 + fat_archs64[i].align = OSSwapInt32(fat_archs64[i].align); 69 + fat_archs64[i].reserved = OSSwapInt32(fat_archs64[i].reserved); 70 + } 71 + } 72 + 73 + void 74 + swap_mach_header( 75 + struct mach_header *mh, 76 + enum NXByteOrder target_byte_sex) 77 + { 78 + mh->magic = OSSwapInt32(mh->magic); 79 + mh->cputype = OSSwapInt32(mh->cputype); 80 + mh->cpusubtype = OSSwapInt32(mh->cpusubtype); 81 + mh->filetype = OSSwapInt32(mh->filetype); 82 + mh->ncmds = OSSwapInt32(mh->ncmds); 83 + mh->sizeofcmds = OSSwapInt32(mh->sizeofcmds); 84 + mh->flags = OSSwapInt32(mh->flags); 85 + } 86 + 87 + void 88 + swap_mach_header_64( 89 + struct mach_header_64 *mh, 90 + enum NXByteOrder target_byte_sex) 91 + { 92 + mh->magic = OSSwapInt32(mh->magic); 93 + mh->cputype = OSSwapInt32(mh->cputype); 94 + mh->cpusubtype = OSSwapInt32(mh->cpusubtype); 95 + mh->filetype = OSSwapInt32(mh->filetype); 96 + mh->ncmds = OSSwapInt32(mh->ncmds); 97 + mh->sizeofcmds = OSSwapInt32(mh->sizeofcmds); 98 + mh->flags = OSSwapInt32(mh->flags); 99 + mh->reserved = OSSwapInt32(mh->reserved); 100 + } 101 + 102 + void 103 + swap_load_command( 104 + struct load_command *lc, 105 + enum NXByteOrder target_byte_sex) 106 + { 107 + lc->cmd = OSSwapInt32(lc->cmd); 108 + lc->cmdsize = OSSwapInt32(lc->cmdsize); 109 + } 110 + 111 + void 112 + swap_segment_command( 113 + struct segment_command *sg, 114 + enum NXByteOrder target_byte_sex) 115 + { 116 + /* segname[16] */ 117 + sg->cmd = OSSwapInt32(sg->cmd); 118 + sg->cmdsize = OSSwapInt32(sg->cmdsize); 119 + sg->vmaddr = OSSwapInt32(sg->vmaddr); 120 + sg->vmsize = OSSwapInt32(sg->vmsize); 121 + sg->fileoff = OSSwapInt32(sg->fileoff); 122 + sg->filesize = OSSwapInt32(sg->filesize); 123 + sg->maxprot = OSSwapInt32(sg->maxprot); 124 + sg->initprot = OSSwapInt32(sg->initprot); 125 + sg->nsects = OSSwapInt32(sg->nsects); 126 + sg->flags = OSSwapInt32(sg->flags); 127 + } 128 + 129 + void 130 + swap_segment_command_64( 131 + struct segment_command_64* sg, 132 + enum NXByteOrder target_byte_sex) 133 + { 134 + /* char segname[16] */ 135 + sg->cmd = OSSwapInt32(sg->cmd); 136 + sg->cmdsize = OSSwapInt32(sg->cmdsize); 137 + sg->vmaddr = OSSwapInt64(sg->vmaddr); 138 + sg->vmsize = OSSwapInt64(sg->vmsize); 139 + sg->fileoff = OSSwapInt64(sg->fileoff); 140 + sg->filesize = OSSwapInt64(sg->filesize); 141 + sg->maxprot = OSSwapInt32(sg->maxprot); 142 + sg->initprot = OSSwapInt32(sg->initprot); 143 + sg->nsects = OSSwapInt32(sg->nsects); 144 + sg->flags = OSSwapInt32(sg->flags); 145 + } 146 + 147 + void 148 + swap_section( 149 + struct section *s, 150 + uint32_t nsects, 151 + enum NXByteOrder target_byte_sex) 152 + { 153 + uint32_t i; 154 + 155 + for(i = 0; i < nsects; i++){ 156 + /* sectname[16] */ 157 + /* segname[16] */ 158 + s[i].addr = OSSwapInt32(s[i].addr); 159 + s[i].size = OSSwapInt32(s[i].size); 160 + s[i].offset = OSSwapInt32(s[i].offset); 161 + s[i].align = OSSwapInt32(s[i].align); 162 + s[i].reloff = OSSwapInt32(s[i].reloff); 163 + s[i].nreloc = OSSwapInt32(s[i].nreloc); 164 + s[i].flags = OSSwapInt32(s[i].flags); 165 + s[i].reserved1 = OSSwapInt32(s[i].reserved1); 166 + s[i].reserved2 = OSSwapInt32(s[i].reserved2); 167 + } 168 + } 169 + 170 + void 171 + swap_section_64( 172 + struct section_64 *s, 173 + uint32_t nsects, 174 + enum NXByteOrder target_byte_sex) 175 + { 176 + uint32_t i; 177 + 178 + for(i = 0; i < nsects; i++){ 179 + /* sectname[16] */ 180 + /* segname[16] */ 181 + s[i].addr = OSSwapInt64(s[i].addr); 182 + s[i].size = OSSwapInt64(s[i].size); 183 + s[i].offset = OSSwapInt32(s[i].offset); 184 + s[i].align = OSSwapInt32(s[i].align); 185 + s[i].reloff = OSSwapInt32(s[i].reloff); 186 + s[i].nreloc = OSSwapInt32(s[i].nreloc); 187 + s[i].flags = OSSwapInt32(s[i].flags); 188 + s[i].reserved1 = OSSwapInt32(s[i].reserved1); 189 + s[i].reserved2 = OSSwapInt32(s[i].reserved2); 190 + s[i].reserved3 = OSSwapInt32(s[i].reserved3); 191 + } 192 + } 193 + 194 + void 195 + swap_symtab_command( 196 + struct symtab_command *st, 197 + enum NXByteOrder target_byte_sex) 198 + { 199 + st->cmd = OSSwapInt32(st->cmd); 200 + st->cmdsize = OSSwapInt32(st->cmdsize); 201 + st->symoff = OSSwapInt32(st->symoff); 202 + st->nsyms = OSSwapInt32(st->nsyms); 203 + st->stroff = OSSwapInt32(st->stroff); 204 + st->strsize = OSSwapInt32(st->strsize); 205 + } 206 + 207 + void 208 + swap_dysymtab_command( 209 + struct dysymtab_command *dyst, 210 + enum NXByteOrder target_byte_sex) 211 + { 212 + dyst->cmd = OSSwapInt32(dyst->cmd); 213 + dyst->cmdsize = OSSwapInt32(dyst->cmdsize); 214 + dyst->ilocalsym = OSSwapInt32(dyst->ilocalsym); 215 + dyst->nlocalsym = OSSwapInt32(dyst->nlocalsym); 216 + dyst->iextdefsym = OSSwapInt32(dyst->iextdefsym); 217 + dyst->nextdefsym = OSSwapInt32(dyst->nextdefsym); 218 + dyst->iundefsym = OSSwapInt32(dyst->iundefsym); 219 + dyst->nundefsym = OSSwapInt32(dyst->nundefsym); 220 + dyst->tocoff = OSSwapInt32(dyst->tocoff); 221 + dyst->ntoc = OSSwapInt32(dyst->ntoc); 222 + dyst->modtaboff = OSSwapInt32(dyst->modtaboff); 223 + dyst->nmodtab = OSSwapInt32(dyst->nmodtab); 224 + dyst->extrefsymoff = OSSwapInt32(dyst->extrefsymoff); 225 + dyst->nextrefsyms = OSSwapInt32(dyst->nextrefsyms); 226 + dyst->indirectsymoff = OSSwapInt32(dyst->indirectsymoff); 227 + dyst->nindirectsyms = OSSwapInt32(dyst->nindirectsyms); 228 + dyst->extreloff = OSSwapInt32(dyst->extreloff); 229 + dyst->nextrel = OSSwapInt32(dyst->nextrel); 230 + dyst->locreloff = OSSwapInt32(dyst->locreloff); 231 + dyst->nlocrel = OSSwapInt32(dyst->nlocrel); 232 + } 233 + 234 + void 235 + swap_symseg_command( 236 + struct symseg_command *ss, 237 + enum NXByteOrder target_byte_sex) 238 + { 239 + ss->cmd = OSSwapInt32(ss->cmd); 240 + ss->cmdsize = OSSwapInt32(ss->cmdsize); 241 + ss->offset = OSSwapInt32(ss->offset); 242 + ss->size = OSSwapInt32(ss->size); 243 + } 244 + 245 + void 246 + swap_fvmlib_command( 247 + struct fvmlib_command *fl, 248 + enum NXByteOrder target_byte_sex) 249 + { 250 + fl->cmd = OSSwapInt32(fl->cmd); 251 + fl->cmdsize = OSSwapInt32(fl->cmdsize); 252 + fl->fvmlib.name.offset = OSSwapInt32(fl->fvmlib.name.offset); 253 + fl->fvmlib.minor_version = OSSwapInt32(fl->fvmlib.minor_version); 254 + fl->fvmlib.header_addr = OSSwapInt32(fl->fvmlib.header_addr); 255 + } 256 + 257 + void 258 + swap_dylib_command( 259 + struct dylib_command *dl, 260 + enum NXByteOrder target_byte_sex) 261 + { 262 + dl->cmd = OSSwapInt32(dl->cmd); 263 + dl->cmdsize = OSSwapInt32(dl->cmdsize); 264 + dl->dylib.name.offset = OSSwapInt32(dl->dylib.name.offset); 265 + dl->dylib.timestamp = OSSwapInt32(dl->dylib.timestamp); 266 + dl->dylib.current_version = OSSwapInt32(dl->dylib.current_version); 267 + dl->dylib.compatibility_version = 268 + OSSwapInt32(dl->dylib.compatibility_version); 269 + } 270 + 271 + void 272 + swap_sub_framework_command( 273 + struct sub_framework_command *sub, 274 + enum NXByteOrder target_byte_sex) 275 + { 276 + sub->cmd = OSSwapInt32(sub->cmd); 277 + sub->cmdsize = OSSwapInt32(sub->cmdsize); 278 + sub->umbrella.offset = OSSwapInt32(sub->umbrella.offset); 279 + } 280 + 281 + void 282 + swap_sub_umbrella_command( 283 + struct sub_umbrella_command *usub, 284 + enum NXByteOrder target_byte_sex) 285 + { 286 + usub->cmd = OSSwapInt32(usub->cmd); 287 + usub->cmdsize = OSSwapInt32(usub->cmdsize); 288 + usub->sub_umbrella.offset = OSSwapInt32(usub->sub_umbrella.offset); 289 + } 290 + 291 + void 292 + swap_sub_library_command( 293 + struct sub_library_command *lsub, 294 + enum NXByteOrder target_byte_sex) 295 + { 296 + lsub->cmd = OSSwapInt32(lsub->cmd); 297 + lsub->cmdsize = OSSwapInt32(lsub->cmdsize); 298 + lsub->sub_library.offset = OSSwapInt32(lsub->sub_library.offset); 299 + } 300 + 301 + void 302 + swap_sub_client_command( 303 + struct sub_client_command *csub, 304 + enum NXByteOrder target_byte_sex) 305 + { 306 + csub->cmd = OSSwapInt32(csub->cmd); 307 + csub->cmdsize = OSSwapInt32(csub->cmdsize); 308 + csub->client.offset = OSSwapInt32(csub->client.offset); 309 + } 310 + 311 + 312 + void 313 + swap_prebound_dylib_command( 314 + struct prebound_dylib_command *pbdylib, 315 + enum NXByteOrder target_byte_sex) 316 + { 317 + pbdylib->cmd = OSSwapInt32(pbdylib->cmd); 318 + pbdylib->cmdsize = OSSwapInt32(pbdylib->cmdsize); 319 + pbdylib->name.offset = OSSwapInt32(pbdylib->name.offset); 320 + pbdylib->nmodules = OSSwapInt32(pbdylib->nmodules); 321 + pbdylib->linked_modules.offset = 322 + OSSwapInt32(pbdylib->linked_modules.offset); 323 + } 324 + 325 + void 326 + swap_dylinker_command( 327 + struct dylinker_command *dyld, 328 + enum NXByteOrder target_byte_sex) 329 + { 330 + dyld->cmd = OSSwapInt32(dyld->cmd); 331 + dyld->cmdsize = OSSwapInt32(dyld->cmdsize); 332 + dyld->name.offset = OSSwapInt32(dyld->name.offset); 333 + } 334 + 335 + void 336 + swap_fvmfile_command( 337 + struct fvmfile_command *ff, 338 + enum NXByteOrder target_byte_sex) 339 + { 340 + ff->cmd = OSSwapInt32(ff->cmd); 341 + ff->cmdsize = OSSwapInt32(ff->cmdsize); 342 + ff->name.offset = OSSwapInt32(ff->name.offset); 343 + ff->header_addr = OSSwapInt32(ff->header_addr); 344 + } 345 + 346 + 347 + void 348 + swap_thread_command( 349 + struct thread_command *ut, 350 + enum NXByteOrder target_byte_sex) 351 + { 352 + ut->cmd = OSSwapInt32(ut->cmd); 353 + ut->cmdsize = OSSwapInt32(ut->cmdsize); 354 + } 355 + 356 + void 357 + swap_ident_command( 358 + struct ident_command *id_cmd, 359 + enum NXByteOrder target_byte_sex) 360 + { 361 + id_cmd->cmd = OSSwapInt32(id_cmd->cmd); 362 + id_cmd->cmdsize = OSSwapInt32(id_cmd->cmdsize); 363 + } 364 + 365 + void 366 + swap_routines_command( 367 + struct routines_command *r_cmd, 368 + enum NXByteOrder target_byte_sex) 369 + { 370 + r_cmd->cmd = OSSwapInt32(r_cmd->cmd); 371 + r_cmd->cmdsize = OSSwapInt32(r_cmd->cmdsize); 372 + r_cmd->init_address = OSSwapInt32(r_cmd->init_address); 373 + r_cmd->init_module = OSSwapInt32(r_cmd->init_module); 374 + r_cmd->reserved1 = OSSwapInt32(r_cmd->reserved1); 375 + r_cmd->reserved2 = OSSwapInt32(r_cmd->reserved2); 376 + r_cmd->reserved3 = OSSwapInt32(r_cmd->reserved3); 377 + r_cmd->reserved4 = OSSwapInt32(r_cmd->reserved4); 378 + r_cmd->reserved5 = OSSwapInt32(r_cmd->reserved5); 379 + r_cmd->reserved6 = OSSwapInt32(r_cmd->reserved6); 380 + } 381 + 382 + void 383 + swap_routines_command_64( 384 + struct routines_command_64 *r_cmd, 385 + enum NXByteOrder target_byte_sex) 386 + { 387 + r_cmd->cmd = OSSwapInt32(r_cmd->cmd); 388 + r_cmd->cmdsize = OSSwapInt32(r_cmd->cmdsize); 389 + r_cmd->init_address = OSSwapInt64(r_cmd->init_address); 390 + r_cmd->init_module = OSSwapInt64(r_cmd->init_module); 391 + r_cmd->reserved1 = OSSwapInt64(r_cmd->reserved1); 392 + r_cmd->reserved2 = OSSwapInt64(r_cmd->reserved2); 393 + r_cmd->reserved3 = OSSwapInt64(r_cmd->reserved3); 394 + r_cmd->reserved4 = OSSwapInt64(r_cmd->reserved4); 395 + r_cmd->reserved5 = OSSwapInt64(r_cmd->reserved5); 396 + r_cmd->reserved6 = OSSwapInt64(r_cmd->reserved6); 397 + } 398 + 399 + void 400 + swap_twolevel_hints_command( 401 + struct twolevel_hints_command *hints_cmd, 402 + enum NXByteOrder target_byte_sex) 403 + { 404 + hints_cmd->cmd = OSSwapInt32(hints_cmd->cmd); 405 + hints_cmd->cmdsize = OSSwapInt32(hints_cmd->cmdsize); 406 + hints_cmd->offset = OSSwapInt32(hints_cmd->offset); 407 + hints_cmd->nhints = OSSwapInt32(hints_cmd->nhints); 408 + } 409 + 410 + void 411 + swap_prebind_cksum_command( 412 + struct prebind_cksum_command *cksum_cmd, 413 + enum NXByteOrder target_byte_sex) 414 + { 415 + cksum_cmd->cmd = OSSwapInt32(cksum_cmd->cmd); 416 + cksum_cmd->cmdsize = OSSwapInt32(cksum_cmd->cmdsize); 417 + cksum_cmd->cksum = OSSwapInt32(cksum_cmd->cksum); 418 + } 419 + 420 + void 421 + swap_uuid_command( 422 + struct uuid_command *uuid_cmd, 423 + enum NXByteOrder target_byte_sex) 424 + { 425 + uuid_cmd->cmd = OSSwapInt32(uuid_cmd->cmd); 426 + uuid_cmd->cmdsize = OSSwapInt32(uuid_cmd->cmdsize); 427 + } 428 + 429 + void 430 + swap_linkedit_data_command( 431 + struct linkedit_data_command *ld, 432 + enum NXByteOrder target_byte_sex) 433 + { 434 + ld->cmd = OSSwapInt32(ld->cmd); 435 + ld->cmdsize = OSSwapInt32(ld->cmdsize); 436 + ld->dataoff = OSSwapInt32(ld->dataoff); 437 + ld->datasize = OSSwapInt32(ld->datasize); 438 + } 439 + 440 + void 441 + swap_version_min_command( 442 + struct version_min_command *ver_cmd, 443 + enum NXByteOrder target_byte_sex) 444 + { 445 + ver_cmd->cmd = OSSwapInt32(ver_cmd->cmd); 446 + ver_cmd->cmdsize = OSSwapInt32(ver_cmd->cmdsize); 447 + ver_cmd->version = OSSwapInt32(ver_cmd->version); 448 + } 449 + 450 + void 451 + swap_rpath_command( 452 + struct rpath_command *rpath_cmd, 453 + enum NXByteOrder target_byte_sex) 454 + { 455 + rpath_cmd->cmd = OSSwapInt32(rpath_cmd->cmd); 456 + rpath_cmd->cmdsize = OSSwapInt32(rpath_cmd->cmdsize); 457 + rpath_cmd->path.offset = OSSwapInt32(rpath_cmd->path.offset); 458 + } 459 + 460 + void 461 + swap_encryption_command( 462 + struct encryption_info_command *ec, 463 + enum NXByteOrder target_byte_sex) 464 + { 465 + ec->cmd = OSSwapInt32(ec->cmd); 466 + ec->cmdsize = OSSwapInt32(ec->cmdsize); 467 + ec->cryptoff = OSSwapInt32(ec->cryptoff); 468 + ec->cryptsize = OSSwapInt32(ec->cryptsize); 469 + ec->cryptid = OSSwapInt32(ec->cryptid); 470 + } 471 + 472 + void 473 + swap_encryption_command_64( 474 + struct encryption_info_command_64 *ec, 475 + enum NXByteOrder target_byte_sex) 476 + { 477 + ec->cmd = OSSwapInt32(ec->cmd); 478 + ec->cmdsize = OSSwapInt32(ec->cmdsize); 479 + ec->cryptoff = OSSwapInt32(ec->cryptoff); 480 + ec->cryptsize = OSSwapInt32(ec->cryptsize); 481 + ec->cryptid = OSSwapInt32(ec->cryptid); 482 + ec->cryptid = OSSwapInt32(ec->pad); 483 + } 484 + 485 + void 486 + swap_linker_option_command( 487 + struct linker_option_command *lo, 488 + enum NXByteOrder target_byte_sex) 489 + { 490 + lo->cmd = OSSwapInt32(lo->cmd); 491 + lo->cmdsize = OSSwapInt32(lo->cmdsize); 492 + lo->count = OSSwapInt32(lo->count); 493 + } 494 + 495 + void 496 + swap_dyld_info_command( 497 + struct dyld_info_command *ed, 498 + enum NXByteOrder target_byte_sex) 499 + { 500 + ed->cmd = OSSwapInt32(ed->cmd); 501 + ed->cmdsize = OSSwapInt32(ed->cmdsize); 502 + ed->rebase_off = OSSwapInt32(ed->rebase_off); 503 + ed->rebase_size = OSSwapInt32(ed->rebase_size); 504 + ed->bind_off = OSSwapInt32(ed->bind_off); 505 + ed->bind_size = OSSwapInt32(ed->bind_size); 506 + ed->weak_bind_off = OSSwapInt32(ed->weak_bind_off); 507 + ed->weak_bind_size = OSSwapInt32(ed->weak_bind_size); 508 + ed->lazy_bind_off = OSSwapInt32(ed->lazy_bind_off); 509 + ed->lazy_bind_size = OSSwapInt32(ed->lazy_bind_size); 510 + ed->export_off = OSSwapInt32(ed->export_off); 511 + ed->export_size = OSSwapInt32(ed->export_size); 512 + } 513 + 514 + void 515 + swap_entry_point_command( 516 + struct entry_point_command *ep, 517 + enum NXByteOrder target_byte_sex) 518 + { 519 + ep->cmd = OSSwapInt32(ep->cmd); 520 + ep->cmdsize = OSSwapInt32(ep->cmdsize); 521 + ep->entryoff = OSSwapInt64(ep->entryoff); 522 + ep->stacksize = OSSwapInt64(ep->stacksize); 523 + } 524 + 525 + void 526 + swap_source_version_command( 527 + struct source_version_command *sv, 528 + enum NXByteOrder target_byte_sex) 529 + { 530 + sv->cmd = OSSwapInt32(sv->cmd); 531 + sv->cmdsize = OSSwapInt32(sv->cmdsize); 532 + sv->version = OSSwapInt64(sv->version); 533 + } 534 + 535 + void 536 + swap_nlist( 537 + struct nlist *symbols, 538 + uint32_t nsymbols, 539 + enum NXByteOrder target_byte_sex) 540 + { 541 + uint32_t i; 542 + 543 + for(i = 0; i < nsymbols; i++){ 544 + symbols[i].n_un.n_strx = OSSwapInt32(symbols[i].n_un.n_strx); 545 + /* n_type */ 546 + /* n_sect */ 547 + symbols[i].n_desc = OSSwapInt16(symbols[i].n_desc); 548 + symbols[i].n_value = OSSwapInt32(symbols[i].n_value); 549 + } 550 + } 551 + 552 + void 553 + swap_nlist_64( 554 + struct nlist_64 *symbols, 555 + uint32_t nsymbols, 556 + enum NXByteOrder target_byte_sex) 557 + { 558 + uint32_t i; 559 + 560 + for(i = 0; i < nsymbols; i++){ 561 + symbols[i].n_un.n_strx = OSSwapInt32(symbols[i].n_un.n_strx); 562 + /* n_type */ 563 + /* n_sect */ 564 + symbols[i].n_desc = OSSwapInt16(symbols[i].n_desc); 565 + symbols[i].n_value = OSSwapInt64(symbols[i].n_value); 566 + } 567 + } 568 + 569 + void 570 + swap_ranlib( 571 + struct ranlib *ranlibs, 572 + uint32_t nranlibs, 573 + enum NXByteOrder target_byte_sex) 574 + { 575 + uint32_t i; 576 + 577 + for(i = 0; i < nranlibs; i++){ 578 + ranlibs[i].ran_un.ran_strx = OSSwapInt32(ranlibs[i].ran_un.ran_strx); 579 + ranlibs[i].ran_off = OSSwapInt32(ranlibs[i].ran_off); 580 + } 581 + } 582 + 583 + void 584 + swap_ranlib_64( 585 + struct ranlib_64 *ranlibs, 586 + uint64_t nranlibs, 587 + enum NXByteOrder target_byte_sex) 588 + { 589 + uint64_t i; 590 + 591 + for(i = 0; i < nranlibs; i++){ 592 + ranlibs[i].ran_un.ran_strx = OSSwapInt64(ranlibs[i].ran_un.ran_strx); 593 + ranlibs[i].ran_off = OSSwapInt64(ranlibs[i].ran_off); 594 + } 595 + } 596 + 597 + void 598 + swap_relocation_info( 599 + struct relocation_info *relocs, 600 + uint32_t nrelocs, 601 + enum NXByteOrder target_byte_sex) 602 + { 603 + uint32_t i; 604 + enum NXByteOrder host_byte_sex; 605 + uint32_t to_host_byte_sex, scattered; 606 + 607 + struct swapped_relocation_info { 608 + uint32_t r_address; 609 + union { 610 + struct { 611 + unsigned int 612 + r_type:4, 613 + r_extern:1, 614 + r_length:2, 615 + r_pcrel:1, 616 + r_symbolnum:24; 617 + } fields; 618 + uint32_t word; 619 + } u; 620 + } sr; 621 + 622 + struct swapped_scattered_relocation_info { 623 + uint32_t word; 624 + uint32_t r_value; 625 + } *ssr; 626 + 627 + host_byte_sex = NXHostByteOrder(); 628 + to_host_byte_sex = target_byte_sex == host_byte_sex; 629 + 630 + for(i = 0; i < nrelocs; i++){ 631 + if(to_host_byte_sex) 632 + scattered = (OSSwapInt32(relocs[i].r_address) & R_SCATTERED) != 0; 633 + else 634 + scattered = ((relocs[i].r_address) & R_SCATTERED) != 0; 635 + if(scattered == FALSE){ 636 + if(to_host_byte_sex){ 637 + memcpy(&sr, relocs + i, sizeof(struct relocation_info)); 638 + sr.r_address = OSSwapInt32(sr.r_address); 639 + sr.u.word = OSSwapInt32(sr.u.word); 640 + relocs[i].r_address = sr.r_address; 641 + relocs[i].r_symbolnum = sr.u.fields.r_symbolnum; 642 + relocs[i].r_pcrel = sr.u.fields.r_pcrel; 643 + relocs[i].r_length = sr.u.fields.r_length; 644 + relocs[i].r_extern = sr.u.fields.r_extern; 645 + relocs[i].r_type = sr.u.fields.r_type; 646 + } 647 + else{ 648 + sr.r_address = relocs[i].r_address; 649 + sr.u.fields.r_symbolnum = relocs[i].r_symbolnum; 650 + sr.u.fields.r_length = relocs[i].r_length; 651 + sr.u.fields.r_pcrel = relocs[i].r_pcrel; 652 + sr.u.fields.r_extern = relocs[i].r_extern; 653 + sr.u.fields.r_type = relocs[i].r_type; 654 + sr.r_address = OSSwapInt32(sr.r_address); 655 + sr.u.word = OSSwapInt32(sr.u.word); 656 + memcpy(relocs + i, &sr, sizeof(struct relocation_info)); 657 + } 658 + } 659 + else{ 660 + ssr = (struct swapped_scattered_relocation_info *)(relocs + i); 661 + ssr->word = OSSwapInt32(ssr->word); 662 + ssr->r_value = OSSwapInt32(ssr->r_value); 663 + } 664 + } 665 + } 666 + 667 + void 668 + swap_indirect_symbols( 669 + uint32_t *indirect_symbols, 670 + uint32_t nindirect_symbols, 671 + enum NXByteOrder target_byte_sex) 672 + { 673 + uint32_t i; 674 + 675 + for(i = 0; i < nindirect_symbols; i++) 676 + indirect_symbols[i] = OSSwapInt32(indirect_symbols[i]); 677 + } 678 + 679 + void 680 + swap_dylib_reference( 681 + struct dylib_reference *refs, 682 + uint32_t nrefs, 683 + enum NXByteOrder target_byte_sex) 684 + { 685 + struct swapped_dylib_reference { 686 + union { 687 + struct { 688 + uint32_t 689 + flags:8, 690 + isym:24; 691 + } fields; 692 + uint32_t word; 693 + } u; 694 + } sref; 695 + 696 + uint32_t i; 697 + enum NXByteOrder host_byte_sex; 698 + 699 + host_byte_sex = NXHostByteOrder(); 700 + 701 + for(i = 0; i < nrefs; i++){ 702 + if(target_byte_sex == host_byte_sex){ 703 + memcpy(&sref, refs + i, sizeof(struct swapped_dylib_reference)); 704 + sref.u.word = OSSwapInt32(sref.u.word); 705 + refs[i].flags = sref.u.fields.flags; 706 + refs[i].isym = sref.u.fields.isym; 707 + } 708 + else{ 709 + sref.u.fields.isym = refs[i].isym; 710 + sref.u.fields.flags = refs[i].flags; 711 + sref.u.word = OSSwapInt32(sref.u.word); 712 + memcpy(refs + i, &sref, sizeof(struct swapped_dylib_reference)); 713 + } 714 + } 715 + 716 + } 717 + 718 + void 719 + swap_dylib_module( 720 + struct dylib_module *mods, 721 + uint32_t nmods, 722 + enum NXByteOrder target_byte_sex) 723 + { 724 + uint32_t i; 725 + 726 + for(i = 0; i < nmods; i++){ 727 + mods[i].module_name = OSSwapInt32(mods[i].module_name); 728 + mods[i].iextdefsym = OSSwapInt32(mods[i].iextdefsym); 729 + mods[i].nextdefsym = OSSwapInt32(mods[i].nextdefsym); 730 + mods[i].irefsym = OSSwapInt32(mods[i].irefsym); 731 + mods[i].nrefsym = OSSwapInt32(mods[i].nrefsym); 732 + mods[i].ilocalsym = OSSwapInt32(mods[i].ilocalsym); 733 + mods[i].nlocalsym = OSSwapInt32(mods[i].nlocalsym); 734 + mods[i].iextrel = OSSwapInt32(mods[i].iextrel); 735 + mods[i].nextrel = OSSwapInt32(mods[i].nextrel); 736 + mods[i].iinit_iterm = OSSwapInt32(mods[i].iinit_iterm); 737 + mods[i].ninit_nterm = OSSwapInt32(mods[i].ninit_nterm); 738 + mods[i].objc_module_info_size = OSSwapInt32(mods[i].objc_module_info_size); 739 + mods[i].objc_module_info_addr = OSSwapInt32(mods[i].objc_module_info_addr); 740 + } 741 + } 742 + 743 + void 744 + swap_dylib_module_64( 745 + struct dylib_module_64 *mods, 746 + uint32_t nmods, 747 + enum NXByteOrder target_byte_sex) 748 + { 749 + uint32_t i; 750 + 751 + for(i = 0; i < nmods; i++){ 752 + mods[i].module_name = OSSwapInt32(mods[i].module_name); 753 + mods[i].iextdefsym = OSSwapInt32(mods[i].iextdefsym); 754 + mods[i].nextdefsym = OSSwapInt32(mods[i].nextdefsym); 755 + mods[i].irefsym = OSSwapInt32(mods[i].irefsym); 756 + mods[i].nrefsym = OSSwapInt32(mods[i].nrefsym); 757 + mods[i].ilocalsym = OSSwapInt32(mods[i].ilocalsym); 758 + mods[i].nlocalsym = OSSwapInt32(mods[i].nlocalsym); 759 + mods[i].iextrel = OSSwapInt32(mods[i].iextrel); 760 + mods[i].nextrel = OSSwapInt32(mods[i].nextrel); 761 + mods[i].iinit_iterm = OSSwapInt32(mods[i].iinit_iterm); 762 + mods[i].ninit_nterm = OSSwapInt32(mods[i].ninit_nterm); 763 + mods[i].objc_module_info_size = OSSwapInt32(mods[i].objc_module_info_size); 764 + mods[i].objc_module_info_addr = OSSwapInt64(mods[i].objc_module_info_addr); 765 + } 766 + } 767 + 768 + void 769 + swap_dylib_table_of_contents( 770 + struct dylib_table_of_contents *tocs, 771 + uint32_t ntocs, 772 + enum NXByteOrder target_byte_sex) 773 + { 774 + uint32_t i; 775 + 776 + for(i = 0; i < ntocs; i++){ 777 + tocs[i].symbol_index = OSSwapInt32(tocs[i].symbol_index); 778 + tocs[i].module_index = OSSwapInt32(tocs[i].module_index); 779 + } 780 + } 781 + #endif /* !defined(RLD) */
+4 -3
src/libsystem/CMakeLists.txt
··· 8 8 9 9 10 10 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostdinc -ggdb") 11 - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib") 11 + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -nostdlib -Wl,-bind_at_load") 12 12 13 13 include_directories(${DARLING_TOP_DIRECTORY}/src/libc/fbsdcompat) 14 14 include_directories(${DARLING_TOP_DIRECTORY}/src/kernel/libsyscall/wrappers) ··· 38 38 add_darling_library(system SHARED ${libsystem_sources}) 39 39 target_link_libraries(system PRIVATE system_malloc system_c system_kernel keymgr 40 40 system_m system_info system_notify system_quarantine libdispatch_shared launch compiler_rt 41 - removefile system_copyfile unwind system_coreservices system_dyld) # objc CommonCrypto 41 + removefile system_copyfile unwind system_coreservices system_dyld macho) # objc CommonCrypto 42 42 set_target_properties(system PROPERTIES OUTPUT_NAME "System.B") 43 43 set_property(TARGET system APPEND_STRING PROPERTY LINK_FLAGS 44 44 " -sub_library libsystem_malloc \ ··· 56 56 -sub_library libunwind \ 57 57 -sub_library libdyld \ 58 58 -sub_library libcompiler_rt \ 59 + -sub_library libmacho \ 59 60 -sub_library libsystem_coreservices \ 60 - -Wl,-dylib_compatibility_version,1.0.0") 61 + -Wl,-dylib_compatibility_version,1238.0.0") 61 62 62 63 install(TARGETS system DESTINATION libexec/darling/usr/lib) 63 64
+6 -3
src/libsystem/init.c
··· 80 80 void cthread_set_errno_self(int); 81 81 void* pthread_self(void); 82 82 83 + extern void _dyld_func_lookup(const char* name, void** p); 84 + 83 85 static void _pthread_exit_if_canceled(int err) { if (pthread_self()) _pthread_testcancel(pthread_self(), 1); } 84 86 85 87 struct ProgramVars ··· 105 107 .free = free, 106 108 .realloc = realloc, 107 109 ._pthread_exit_if_canceled = _pthread_exit_if_canceled, 110 + .dyld_func_lookup = _dyld_func_lookup, 108 111 }; 109 - 112 + 110 113 _darling_initialize_commpage(); 111 114 __libkernel_init(&libkernel_funcs, *envp, apple, &vars); 112 115 113 - bootstrap_init(); 116 + // bootstrap_init(); // currently aborts 114 117 mach_init(); 115 118 pthread_init(); 116 - __libc_init(&vars, libSystem_atfork_prepare, libSystem_atfork_parent, libSystem_atfork_child, apple); 119 + __libc_init(vars, libSystem_atfork_prepare, libSystem_atfork_parent, libSystem_atfork_child, apple); 117 120 __malloc_init(apple); 118 121 __keymgr_initializer(); 119 122 _dyld_initializer();