1 /* 2 * Copyright (c) 2018 Google, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <string.h> 18 #include <stdlib.h> 19 20 typedef enum { 21 BCC_ARCH_PPC, 22 BCC_ARCH_PPC_LE, 23 BCC_ARCH_S390X, 24 BCC_ARCH_ARM64, 25 BCC_ARCH_MIPS, 26 BCC_ARCH_X86 27 } bcc_arch_t; 28 29 typedef void *(*arch_callback_t)(bcc_arch_t arch, bool for_syscall); 30 31 static void *run_arch_callback(arch_callback_t fn, bool for_syscall = false) 32 { 33 const char *archenv = getenv("ARCH"); 34 35 /* If ARCH is not set, detect from local arch clang is running on */ 36 if (!archenv) { 37 #if defined(__powerpc64__) 38 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 39 return fn(BCC_ARCH_PPC_LE, for_syscall); 40 #else 41 return fn(BCC_ARCH_PPC, for_syscall); 42 #endif 43 #elif defined(__s390x__) 44 return fn(BCC_ARCH_S390X, for_syscall); 45 #elif defined(__aarch64__) 46 return fn(BCC_ARCH_ARM64, for_syscall); 47 #elif defined(__mips__) 48 return fn(BCC_ARCH_MIPS, for_syscall); 49 #else 50 return fn(BCC_ARCH_X86, for_syscall); 51 #endif 52 } 53 54 /* Otherwise read it from ARCH */ 55 if (!strcmp(archenv, "powerpc")) { 56 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 57 return fn(BCC_ARCH_PPC_LE, for_syscall); 58 #else 59 return fn(BCC_ARCH_PPC, for_syscall); 60 #endif 61 } else if (!strcmp(archenv, "s390x")) { 62 return fn(BCC_ARCH_S390X, for_syscall); 63 } else if (!strcmp(archenv, "arm64")) { 64 return fn(BCC_ARCH_ARM64, for_syscall); 65 } else if (!strcmp(archenv, "mips")) { 66 return fn(BCC_ARCH_MIPS, for_syscall); 67 } else { 68 return fn(BCC_ARCH_X86, for_syscall); 69 } 70 } 71