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_X86
26 } bcc_arch_t;
27 
28 typedef void *(*arch_callback_t)(bcc_arch_t arch);
29 
run_arch_callback(arch_callback_t fn)30 static void *run_arch_callback(arch_callback_t fn)
31 {
32   const char *archenv = getenv("ARCH");
33 
34   /* If ARCH is not set, detect from local arch clang is running on */
35   if (!archenv) {
36 #if defined(__powerpc64__)
37 #if defined(_CALL_ELF) && _CALL_ELF == 2
38     return fn(BCC_ARCH_PPC_LE);
39 #else
40     return fn(BCC_ARCH_PPC);
41 #endif
42 #elif defined(__s390x__)
43     return fn(BCC_ARCH_S390X);
44 #elif defined(__aarch64__)
45     return fn(BCC_ARCH_ARM64);
46 #else
47     return fn(BCC_ARCH_X86);
48 #endif
49   }
50 
51   /* Otherwise read it from ARCH */
52   if (!strcmp(archenv, "powerpc")) {
53 #if defined(_CALL_ELF) && _CALL_ELF == 2
54     return fn(BCC_ARCH_PPC_LE);
55 #else
56     return fn(BCC_ARCH_PPC);
57 #endif
58   } else if (!strcmp(archenv, "s390x")) {
59     return fn(BCC_ARCH_S390X);
60   } else if (!strcmp(archenv, "arm64")) {
61     return fn(BCC_ARCH_ARM64);
62   } else {
63     return fn(BCC_ARCH_X86);
64   }
65 }
66