ArchInfo.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/ArchInfo.h"
  16. #include "util/Endian.h"
  17. /*
  18. Autogenerated list of arch-info, using the following:
  19. #include "linux/elf-em.h"
  20. #include "linux/audit.h"
  21. #include "stdio.h"
  22. #define EM_OPENRISC 92
  23. static const struct { char* name; unsigned int id; } archs[] = {
  24. #include "arch.x"
  25. {0}
  26. };
  27. int main()
  28. {
  29. int i;
  30. for (i=0; archs[i].name; i++) {
  31. printf("#define ArchInfo_%s 0x%08x\n", archs[i].name, archs[i].id);
  32. }
  33. return 0;
  34. }
  35. echo '#include "linux/audit.h"' | gcc -E -dM - | grep '#define AUDIT_ARCH' | \
  36. awk '{print "{ .name=\""$2"\", .id="$2" },"}' > arch.x
  37. gcc arch.c
  38. ./a.out
  39. */
  40. #define ArchInfo_AUDIT_ARCH_SPARC64 0x8000002b
  41. #define ArchInfo_AUDIT_ARCH_MIPS 0x00000008
  42. #define ArchInfo_AUDIT_ARCH_S390 0x00000016
  43. #define ArchInfo_AUDIT_ARCH_PPC64 0x80000015
  44. #define ArchInfo_AUDIT_ARCH_SHEL64 0xc000002a
  45. #define ArchInfo_AUDIT_ARCH_SH 0x0000002a
  46. #define ArchInfo_AUDIT_ARCH_PARISC64 0x8000000f
  47. #define ArchInfo_AUDIT_ARCH_MIPSEL64 0xc0000008
  48. #define ArchInfo_AUDIT_ARCH_MIPS64N32 0xa0000008
  49. #define ArchInfo_AUDIT_ARCH_IA64 0xc0000032
  50. #define ArchInfo_AUDIT_ARCH_PARISC 0x0000000f
  51. #define ArchInfo_AUDIT_ARCH_MIPS64 0x80000008
  52. #define ArchInfo_AUDIT_ARCH_RISCV64 0xc00000f3
  53. #define ArchInfo_AUDIT_ARCH_PPC64LE 0xc0000015
  54. #define ArchInfo_AUDIT_ARCH_ALPHA 0xc0009026
  55. #define ArchInfo_AUDIT_ARCH_MIPSEL 0x40000008
  56. #define ArchInfo_AUDIT_ARCH_SPARC 0x00000002
  57. #define ArchInfo_AUDIT_ARCH_I386 0x40000003
  58. #define ArchInfo_AUDIT_ARCH_OPENRISC 0x0000005c
  59. #define ArchInfo_AUDIT_ARCH_AARCH64 0xc00000b7
  60. #define ArchInfo_AUDIT_ARCH_FRV 0x00005441
  61. #define ArchInfo_AUDIT_ARCH_ARM 0x40000028
  62. #define ArchInfo_AUDIT_ARCH_X86_64 0xc000003e
  63. #define ArchInfo_AUDIT_ARCH_ARMEB 0x00000028
  64. #define ArchInfo_AUDIT_ARCH_M68K 0x00000004
  65. #define ArchInfo_AUDIT_ARCH_S390X 0x80000016
  66. #define ArchInfo_AUDIT_ARCH_PPC 0x00000014
  67. #define ArchInfo_AUDIT_ARCH_SH64 0x8000002a
  68. #define ArchInfo_AUDIT_ARCH_MIPSEL64N32 0xe0000008
  69. #define ArchInfo_AUDIT_ARCH_SHEL 0x4000002a
  70. #define ArchInfo_AUDIT_ARCH_M32R 0x00000058
  71. #define ArchInfo_AUDIT_ARCH_CRIS 0x4000004c
  72. /**
  73. * Manually generated detection routine...
  74. */
  75. #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
  76. defined(_M_X64) || defined(__amd64)
  77. #define ARCH ArchInfo_AUDIT_ARCH_X86_64
  78. #define ARCHSTR "amd64"
  79. #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
  80. defined(_M_IX86) || defined(__i386)
  81. #define ARCH ArchInfo_AUDIT_ARCH_I386
  82. #define ARCHSTR "i386"
  83. #elif defined(__aarch64__)
  84. #define ARCH ArchInfo_AUDIT_ARCH_AARCH64
  85. #define ARCHSTR "arm64"
  86. #elif defined(__ARM_EABI__) || defined(__arm__)
  87. #if defined(Endian_BIG)
  88. #define ARCH ArchInfo_AUDIT_ARCH_ARMEB
  89. #define ARCHSTR "armeb"
  90. #elif defined(Endian_LITTLE)
  91. #define ARCH ArchInfo_AUDIT_ARCH_ARM
  92. #define ARCHSTR "armel"
  93. #else
  94. #error unknown endian
  95. #endif
  96. #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || defined(_ARCH_PPC64)
  97. #if defined(Endian_BIG)
  98. #define ARCH ArchInfo_AUDIT_ARCH_PPC64
  99. #define ARCHSTR "ppc64"
  100. #elif defined(Endian_LITTLE)
  101. #define ARCH ArchInfo_AUDIT_ARCH_PPC64LE
  102. #define ARCHSTR "ppc64le"
  103. #else
  104. #error unknown endian
  105. #endif
  106. #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(_ARCH_PPC)
  107. #define ARCH ArchInfo_AUDIT_ARCH_PPC
  108. #define ARCHSTR "ppc"
  109. #elif defined(__sparcv9__) || defined(__sparcv9)
  110. #define ARCH ArchInfo_AUDIT_ARCH_SPARC64
  111. #define ARCHSTR "sparc64"
  112. Assert_compileTime(sizeof(long) == 8);
  113. #elif defined(__sparc_v8__)
  114. #define ARCH ArchInfo_AUDIT_ARCH_SPARC
  115. #define ARCHSTR "sparc"
  116. Assert_compileTime(sizeof(long) == 4);
  117. #elif defined(__sparc__) || defined(__sparc)
  118. #if defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8
  119. #define ARCH ArchInfo_AUDIT_ARCH_SPARC64
  120. #define ARCHSTR "sparc64"
  121. #elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 4
  122. #define ARCH ArchInfo_AUDIT_ARCH_SPARC
  123. #define ARCHSTR "sparc"
  124. #else
  125. #error unknown bit width
  126. #endif
  127. #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
  128. #if defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8
  129. #if defined(Endian_BIG)
  130. #define ARCH ArchInfo_AUDIT_ARCH_MIPS64
  131. #define ARCHSTR "mips64"
  132. #elif defined(Endian_LITTLE)
  133. #define ARCH ArchInfo_AUDIT_ARCH_MIPSEL64
  134. #define ARCHSTR "mipsel64"
  135. #else
  136. #error unknown endian
  137. #endif
  138. #elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 4
  139. #if defined(Endian_BIG)
  140. #define ARCH ArchInfo_AUDIT_ARCH_MIPS
  141. #define ARCHSTR "mips"
  142. #elif defined(Endian_LITTLE)
  143. #define ARCH ArchInfo_AUDIT_ARCH_MIPSEL
  144. #define ARCHSTR "mipsel"
  145. #else
  146. #error unknown endian
  147. #endif
  148. #else
  149. #error unknown bit width
  150. #endif
  151. #elif defined(__s390x__)
  152. #define ARCH ArchInfo_AUDIT_ARCH_S390X
  153. #define ARCHSTR "s390x"
  154. #elif defined(__riscv) && __riscv_xlen == 64
  155. #define ARCH ArchInfo_AUDIT_ARCH_RISCV64
  156. #define ARCHSTR "riscv64"
  157. #else
  158. #error architecture unknown
  159. #endif
  160. uint32_t ArchInfo_getAuditArch(void)
  161. {
  162. return ARCH;
  163. }
  164. char* ArchInfo_getArchStr(void)
  165. {
  166. return ARCHSTR;
  167. }