ArchInfo.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_PPC64LE 0xc0000015
  53. #define ArchInfo_AUDIT_ARCH_ALPHA 0xc0009026
  54. #define ArchInfo_AUDIT_ARCH_MIPSEL 0x40000008
  55. #define ArchInfo_AUDIT_ARCH_SPARC 0x00000002
  56. #define ArchInfo_AUDIT_ARCH_I386 0x40000003
  57. #define ArchInfo_AUDIT_ARCH_OPENRISC 0x0000005c
  58. #define ArchInfo_AUDIT_ARCH_AARCH64 0xc00000b7
  59. #define ArchInfo_AUDIT_ARCH_FRV 0x00005441
  60. #define ArchInfo_AUDIT_ARCH_ARM 0x40000028
  61. #define ArchInfo_AUDIT_ARCH_X86_64 0xc000003e
  62. #define ArchInfo_AUDIT_ARCH_ARMEB 0x00000028
  63. #define ArchInfo_AUDIT_ARCH_M68K 0x00000004
  64. #define ArchInfo_AUDIT_ARCH_S390X 0x80000016
  65. #define ArchInfo_AUDIT_ARCH_PPC 0x00000014
  66. #define ArchInfo_AUDIT_ARCH_SH64 0x8000002a
  67. #define ArchInfo_AUDIT_ARCH_MIPSEL64N32 0xe0000008
  68. #define ArchInfo_AUDIT_ARCH_SHEL 0x4000002a
  69. #define ArchInfo_AUDIT_ARCH_M32R 0x00000058
  70. #define ArchInfo_AUDIT_ARCH_CRIS 0x4000004c
  71. /**
  72. * Manually generated detection routine...
  73. */
  74. #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
  75. defined(_M_X64) || defined(__amd64)
  76. #define ARCH ArchInfo_AUDIT_ARCH_X86_64
  77. #define ARCHSTR "amd64"
  78. #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
  79. defined(_M_IX86) || defined(__i386)
  80. #define ARCH ArchInfo_AUDIT_ARCH_I386
  81. #define ARCHSTR "i386"
  82. #elif defined(__aarch64__)
  83. #define ARCH ArchInfo_AUDIT_ARCH_AARCH64
  84. #define ARCHSTR "arm64"
  85. #elif defined(__ARM_EABI__) || defined(__arm__)
  86. #if defined(Endian_BIG)
  87. #define ARCH ArchInfo_AUDIT_ARCH_ARMEB
  88. #define ARCHSTR "armeb"
  89. #elif defined(Endian_LITTLE)
  90. #define ARCH ArchInfo_AUDIT_ARCH_ARM
  91. #define ARCHSTR "armel"
  92. #else
  93. #error unknown endian
  94. #endif
  95. #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || defined(_ARCH_PPC64)
  96. #if defined(Endian_BIG)
  97. #define ARCH ArchInfo_AUDIT_ARCH_PPC64
  98. #define ARCHSTR "ppc64"
  99. #elif defined(Endian_LITTLE)
  100. #define ARCH ArchInfo_AUDIT_ARCH_PPC64LE
  101. #define ARCHSTR "ppc64le"
  102. #else
  103. #error unknown endian
  104. #endif
  105. #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || defined(_ARCH_PPC)
  106. #define ARCH ArchInfo_AUDIT_ARCH_PPC
  107. #define ARCHSTR "ppc"
  108. #elif defined(__sparcv9__) || defined(__sparcv9)
  109. #define ARCH ArchInfo_AUDIT_ARCH_SPARC64
  110. #define ARCHSTR "sparc64"
  111. Assert_compileTime(sizeof(long) == 8);
  112. #elif defined(__sparc_v8__)
  113. #define ARCH ArchInfo_AUDIT_ARCH_SPARC
  114. #define ARCHSTR "sparc"
  115. Assert_compileTime(sizeof(long) == 4);
  116. #elif defined(__sparc__) || defined(__sparc)
  117. #if defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8
  118. #define ARCH ArchInfo_AUDIT_ARCH_SPARC64
  119. #define ARCHSTR "sparc64"
  120. #elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 4
  121. #define ARCH ArchInfo_AUDIT_ARCH_SPARC
  122. #define ARCHSTR "sparc"
  123. #else
  124. #error unknown bit width
  125. #endif
  126. #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
  127. #if defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8
  128. #if defined(Endian_BIG)
  129. #define ARCH ArchInfo_AUDIT_ARCH_MIPS64
  130. #define ARCHSTR "mips64"
  131. #elif defined(Endian_LITTLE)
  132. #define ARCH ArchInfo_AUDIT_ARCH_MIPSEL64
  133. #define ARCHSTR "mipsel64"
  134. #else
  135. #error unknown endian
  136. #endif
  137. #elif defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 4
  138. #if defined(Endian_BIG)
  139. #define ARCH ArchInfo_AUDIT_ARCH_MIPS
  140. #define ARCHSTR "mips"
  141. #elif defined(Endian_LITTLE)
  142. #define ARCH ArchInfo_AUDIT_ARCH_MIPSEL
  143. #define ARCHSTR "mipsel"
  144. #else
  145. #error unknown endian
  146. #endif
  147. #else
  148. #error unknown bit width
  149. #endif
  150. #elif defined(__s390x__)
  151. #define ARCH ArchInfo_AUDIT_ARCH_S390X
  152. #define ARCHSTR "s390x"
  153. #else
  154. #error architecture unknown
  155. #endif
  156. uint32_t ArchInfo_getAuditArch(void)
  157. {
  158. return ARCH;
  159. }
  160. char* ArchInfo_getArchStr(void)
  161. {
  162. return ARCHSTR;
  163. }