| 1 |
/** |
| 2 |
* Tool utility functions |
| 3 |
* |
| 4 |
* Copyright (c) 2014 Red Hat <[email protected]> |
| 5 |
* Author: Paul Moore <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* This library is free software; you can redistribute it and/or modify it |
| 10 |
* under the terms of version 2.1 of the GNU Lesser General Public License as |
| 11 |
* published by the Free Software Foundation. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, but WITHOUT |
| 14 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 16 |
* for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with this library; if not, see <http://www.gnu.org/licenses>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <errno.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <linux/audit.h> |
| 26 |
|
| 27 |
#ifndef _BSD_SOURCE |
| 28 |
#define _BSD_SOURCE |
| 29 |
#endif |
| 30 |
#include <endian.h> |
| 31 |
|
| 32 |
#include "util.h" |
| 33 |
|
| 34 |
/* determine the native architecture */ |
| 35 |
#if __i386__ |
| 36 |
#define ARCH_NATIVE AUDIT_ARCH_I386 |
| 37 |
#elif __x86_64__ |
| 38 |
#ifdef __ILP32__ |
| 39 |
#define ARCH_NATIVE AUDIT_ARCH_X86_64 |
| 40 |
#else |
| 41 |
#define ARCH_NATIVE AUDIT_ARCH_X86_64 |
| 42 |
#endif /* __ILP32__ */ |
| 43 |
#elif __arm__ |
| 44 |
#define ARCH_NATIVE AUDIT_ARCH_ARM |
| 45 |
#elif __aarch64__ |
| 46 |
#define ARCH_NATIVE AUDIT_ARCH_AARCH64 |
| 47 |
#elif __loongarch_lp64 |
| 48 |
#define ARCH_NATIVE AUDIT_ARCH_LOONGARCH64 |
| 49 |
#elif __m68k__ |
| 50 |
#define ARCH_NATIVE AUDIT_ARCH_M68K |
| 51 |
#elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI32 |
| 52 |
#if __MIPSEB__ |
| 53 |
#define ARCH_NATIVE AUDIT_ARCH_MIPS |
| 54 |
#elif __MIPSEL__ |
| 55 |
#define ARCH_NATIVE AUDIT_ARCH_MIPSEL |
| 56 |
#endif /* _MIPS_SIM_ABI32 */ |
| 57 |
#elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI64 |
| 58 |
#if __MIPSEB__ |
| 59 |
#define ARCH_NATIVE AUDIT_ARCH_MIPS64 |
| 60 |
#elif __MIPSEL__ |
| 61 |
#define ARCH_NATIVE AUDIT_ARCH_MIPSEL64 |
| 62 |
#endif /* _MIPS_SIM_ABI64 */ |
| 63 |
#elif __mips__ && _MIPS_SIM == _MIPS_SIM_NABI32 |
| 64 |
#if __MIPSEB__ |
| 65 |
#define ARCH_NATIVE AUDIT_ARCH_MIPS64N32 |
| 66 |
#elif __MIPSEL__ |
| 67 |
#define ARCH_NATIVE AUDIT_ARCH_MIPSEL64N32 |
| 68 |
#endif /* _MIPS_SIM_NABI32 */ |
| 69 |
#elif __hppa64__ |
| 70 |
#define ARCH_NATIVE AUDIT_ARCH_PARISC64 |
| 71 |
#elif __hppa__ |
| 72 |
#define ARCH_NATIVE AUDIT_ARCH_PARISC |
| 73 |
#elif __PPC64__ |
| 74 |
#ifdef __BIG_ENDIAN__ |
| 75 |
#define ARCH_NATIVE AUDIT_ARCH_PPC64 |
| 76 |
#else |
| 77 |
#define ARCH_NATIVE AUDIT_ARCH_PPC64LE |
| 78 |
#endif |
| 79 |
#elif __PPC__ |
| 80 |
#define ARCH_NATIVE AUDIT_ARCH_PPC |
| 81 |
#elif __s390x__ /* s390x must be checked before s390 */ |
| 82 |
#define ARCH_NATIVE AUDIT_ARCH_S390X |
| 83 |
#elif __s390__ |
| 84 |
#define ARCH_NATIVE AUDIT_ARCH_S390 |
| 85 |
#elif __riscv && __riscv_xlen == 64 |
| 86 |
#define ARCH_NATIVE AUDIT_ARCH_RISCV64 |
| 87 |
#elif __sh__ |
| 88 |
#ifdef __BIG_ENDIAN__ |
| 89 |
#define ARCH_NATIVE AUDIT_ARCH_SH |
| 90 |
#else |
| 91 |
#define ARCH_NATIVE AUDIT_ARCH_SHEL |
| 92 |
#endif |
| 93 |
#else |
| 94 |
#error the simulator code needs to know about your machine type |
| 95 |
#endif |
| 96 |
|
| 97 |
/* default to the native arch */ |
| 98 |
uint32_t arch = ARCH_NATIVE; |
| 99 |
|
| 100 |
/** |
| 101 |
* Convert a 16-bit target integer into the host's endianness |
| 102 |
* @param arch_token the architecture token |
| 103 |
* @param val the 16-bit integer |
| 104 |
* |
| 105 |
* Convert the endianness of the supplied value and return it to the caller. |
| 106 |
* |
| 107 |
*/ |
| 108 |
uint16_t ttoh16(uint32_t arch_token, uint16_t val) |
| 109 |
{ |
| 110 |
if (arch_token & __AUDIT_ARCH_LE) |
| 111 |
return le16toh(val); |
| 112 |
else |
| 113 |
return be16toh(val); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Convert a 32-bit target integer into the host's endianness |
| 118 |
* @param arch_token the architecture token |
| 119 |
* @param val the 32-bit integer |
| 120 |
* |
| 121 |
* Convert the endianness of the supplied value and return it to the caller. |
| 122 |
* |
| 123 |
*/ |
| 124 |
uint32_t ttoh32(uint32_t arch_token, uint32_t val) |
| 125 |
{ |
| 126 |
if (arch_token & __AUDIT_ARCH_LE) |
| 127 |
return le32toh(val); |
| 128 |
else |
| 129 |
return be32toh(val); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Convert a 32-bit host integer into the target's endianness |
| 134 |
* @param arch_token the architecture token |
| 135 |
* @param val the 32-bit integer |
| 136 |
* |
| 137 |
* Convert the endianness of the supplied value and return it to the caller. |
| 138 |
* |
| 139 |
*/ |
| 140 |
uint32_t htot32(uint32_t arch_token, uint32_t val) |
| 141 |
{ |
| 142 |
if (arch_token & __AUDIT_ARCH_LE) |
| 143 |
return htole32(val); |
| 144 |
else |
| 145 |
return htobe32(val); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Convert a 64-bit host integer into the target's endianness |
| 150 |
* @param arch_token the architecture token |
| 151 |
* @param val the 64-bit integer |
| 152 |
* |
| 153 |
* Convert the endianness of the supplied value and return it to the caller. |
| 154 |
* |
| 155 |
*/ |
| 156 |
uint64_t htot64(uint32_t arch_token, uint64_t val) |
| 157 |
{ |
| 158 |
if (arch_token & __AUDIT_ARCH_LE) |
| 159 |
return htole64(val); |
| 160 |
else |
| 161 |
return htobe64(val); |
| 162 |
} |