1 /* 2 * Copyright 2005-2012 Gentoo Foundation 3 * Distributed under the terms of the GNU General Public License v2 4 * 5 * Copyright 2005-2012 Ned Ludd - <solar@gentoo.org> 6 * Copyright 2005-2012 Mike Frysinger - <vapier@gentoo.org> 7 * 8 * Make sure all of the common elf stuff is setup as we expect 9 */ 10 11 #ifndef _PORTING_H 12 #define _PORTING_H 13 14 #ifdef HAVE_CONFIG_H 15 # include "config.h" 16 #endif 17 18 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*arr)) 19 20 #include <assert.h> 21 #include <ctype.h> 22 #include <dirent.h> 23 #include <errno.h> 24 #include <fcntl.h> 25 #include <fnmatch.h> 26 #include <getopt.h> 27 #include <inttypes.h> 28 #include <libgen.h> 29 #include <limits.h> 30 #include <pwd.h> 31 #include <regex.h> 32 #include <sched.h> 33 #include <signal.h> 34 #include <stdbool.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <time.h> 39 #include <unistd.h> 40 #include <sys/mman.h> 41 #include <sys/stat.h> 42 #include <sys/types.h> 43 #include "elf.h" 44 #if !defined(__FreeBSD__) && !defined(__DragonFly__) && !defined(__OpenBSD__) 45 # include <alloca.h> 46 #endif 47 #if defined(__linux__) 48 # include <sys/prctl.h> 49 # if !defined(HAVE_CONFIG_H) || defined(HAVE_LINUX_SECCOMP_H) 50 # include <linux/seccomp.h> 51 # endif 52 # if !defined(HAVE_CONFIG_H) || defined(HAVE_LINUX_SECUREBITS_H) 53 # include <linux/securebits.h> 54 # endif 55 #endif 56 #if defined(__GLIBC__) || defined(__UCLIBC__) || defined(__ANDROID__) 57 # include <byteswap.h> 58 # include <endian.h> 59 #elif defined(__FreeBSD__) || defined(__DragonFly__) 60 # include <sys/endian.h> 61 #elif defined(__sun__) 62 # include <sys/isa_defs.h> 63 #elif defined(__MACH__) 64 # include <machine/endian.h> 65 #endif 66 67 #if defined(__GLIBC__) || defined(__UCLIBC__) || defined(__NetBSD__) 68 # include <glob.h> 69 #endif 70 71 #if defined(__GLIBC__) || defined(__UCLIBC__) || defined(__NetBSD__) 72 # define __PAX_UTILS_DEFAULT_LD_CACHE_CONFIG "/etc/ld.so.conf" 73 #elif defined(__FreeBSD__) || defined(__DragonFly__) 74 # include <elf-hints.h> 75 # define __PAX_UTILS_DEFAULT_LD_CACHE_CONFIG _PATH_ELF_HINTS 76 #else 77 # define __PAX_UTILS_DEFAULT_LD_CACHE_CONFIG "" 78 #endif 79 80 #undef PAX_UTILS_CLEANUP 81 /* bounds checking code will fart on free(NULL) even though that 82 * is valid usage. So let's wrap it if need be. 83 */ 84 #ifdef __BOUNDS_CHECKING_ON 85 # define free(ptr) do { if (ptr) free(ptr); } while (0) 86 # define PAX_UTILS_CLEANUP 1 87 #endif 88 /* LSAN (Leak Sanitizer) will complain about things we leak. */ 89 #ifdef __SANITIZE_ADDRESS__ 90 # define PAX_UTILS_CLEANUP 1 91 #endif 92 /* Coverity catches some things we leak on purpose. */ 93 #ifdef __COVERITY__ 94 # define PAX_UTILS_CLEANUP 1 95 #endif 96 #ifndef PAX_UTILS_CLEANUP 97 # define PAX_UTILS_CLEANUP 0 98 #endif 99 100 /* Support for libFuzzer: https://llvm.org/docs/LibFuzzer.html */ 101 #ifndef PAX_UTILS_LIBFUZZ 102 # define PAX_UTILS_LIBFUZZ 0 103 #endif 104 105 /* Few arches can safely do unaligned accesses */ 106 #if defined(__cris__) || \ 107 defined(__i386__) || \ 108 defined(__powerpc__) || \ 109 defined(__s390__) || \ 110 defined(__x86_64__) 111 # define __PAX_UNALIGNED_OK 1 112 #else 113 # define __PAX_UNALIGNED_OK 0 114 #endif 115 116 #if !defined(bswap_16) 117 # if defined(bswap16) 118 # define bswap_16 bswap16 119 # define bswap_32 bswap32 120 # define bswap_64 bswap64 121 # else 122 # define bswap_16(x) \ 123 ((((x) & 0xff00) >> 8) | \ 124 (((x) & 0x00ff) << 8)) 125 # define bswap_32(x) \ 126 ((((x) & 0xff000000) >> 24) | \ 127 (((x) & 0x00ff0000) >> 8) | \ 128 (((x) & 0x0000ff00) << 8) | \ 129 (((x) & 0x000000ff) << 24)) 130 # if defined(__GNUC__) 131 # define bswap_64(x) \ 132 ((((x) & 0xff00000000000000ull) >> 56) | \ 133 (((x) & 0x00ff000000000000ull) >> 40) | \ 134 (((x) & 0x0000ff0000000000ull) >> 24) | \ 135 (((x) & 0x000000ff00000000ull) >> 8) | \ 136 (((x) & 0x00000000ff000000ull) << 8) | \ 137 (((x) & 0x0000000000ff0000ull) << 24) | \ 138 (((x) & 0x000000000000ff00ull) << 40) | \ 139 (((x) & 0x00000000000000ffull) << 56)) 140 # else 141 # define bswap_64(x) \ 142 ((((x) & 0xff00000000000000) >> 56) | \ 143 (((x) & 0x00ff000000000000) >> 40) | \ 144 (((x) & 0x0000ff0000000000) >> 24) | \ 145 (((x) & 0x000000ff00000000) >> 8) | \ 146 (((x) & 0x00000000ff000000) << 8) | \ 147 (((x) & 0x0000000000ff0000) << 24) | \ 148 (((x) & 0x000000000000ff00) << 40) | \ 149 (((x) & 0x00000000000000ff) << 56)) 150 # endif 151 # endif 152 #endif 153 154 #define _minmax(x, y, op) \ 155 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); (__x op __y ? __x : __y); }) 156 #if !defined(min) 157 # define min(x, y) _minmax(x, y, <) 158 #endif 159 #if !defined(max) 160 # define max(x, y) _minmax(x, y, >) 161 #endif 162 163 #if !defined(_POSIX_PATH_MAX) && !defined(PATH_MAX) /* __PAX_UTILS_PATH_MAX */ 164 # define __PAX_UTILS_PATH_MAX 8192 165 #elif _POSIX_PATH_MAX > PATH_MAX /* __PAX_UTILS_PATH_MAX */ 166 # define __PAX_UTILS_PATH_MAX _POSIX_PATH_MAX 167 #else 168 # define __PAX_UTILS_PATH_MAX PATH_MAX 169 #endif 170 171 /* fall back case for non-Linux hosts ... so lame */ 172 #if !defined(ELF_DATA) 173 # if defined(BYTE_ORDER) 174 # if BYTE_ORDER == LITTLE_ENDIAN 175 # define ELF_DATA ELFDATA2LSB 176 # elif BYTE_ORDER == BIG_ENDIAN 177 # define ELF_DATA ELFDATA2MSB 178 # else 179 # error "BYTE_ORDER: you fail" 180 # endif 181 # elif defined(__BYTE_ORDER) 182 # if __BYTE_ORDER == __LITTLE_ENDIAN 183 # define ELF_DATA ELFDATA2LSB 184 # elif __BYTE_ORDER == __BIG_ENDIAN 185 # define ELF_DATA ELFDATA2BSB 186 # else 187 # error "__BYTE_ORDER: you fail" 188 # endif 189 # elif defined(WORDS_LITTLENDIAN) 190 # define ELF_DATA ELFDATA2LSB 191 # elif defined(WORDS_BIGENDIAN) 192 # define ELF_DATA ELFDATA2MSB 193 # elif defined(_LITTLE_ENDIAN) 194 # define ELF_DATA ELFDATA2LSB 195 # elif defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN__) 196 # define ELF_DATA ELFDATA2MSB 197 # else 198 # error "no idea what the native byte order is" 199 # endif 200 #endif 201 202 /* 203 * propably will never be official added to the toolchain. 204 * But none the less we should try to get 0x65041580 reserved 205 */ 206 #ifndef PT_PAX_FLAGS 207 # define PT_PAX_FLAGS 0x65041580 208 209 # define PF_PAGEEXEC (1 << 4) /* Enable PAGEEXEC */ 210 # define PF_NOPAGEEXEC (1 << 5) /* Disable PAGEEXEC */ 211 # define PF_SEGMEXEC (1 << 6) /* Enable SEGMEXEC */ 212 # define PF_NOSEGMEXEC (1 << 7) /* Disable SEGMEXEC */ 213 # define PF_MPROTECT (1 << 8) /* Enable MPROTECT */ 214 # define PF_NOMPROTECT (1 << 9) /* Disable MPROTECT */ 215 # define PF_RANDEXEC (1 << 10) /* Enable RANDEXEC */ 216 # define PF_NORANDEXEC (1 << 11) /* Disable RANDEXEC */ 217 # define PF_EMUTRAMP (1 << 12) /* Enable EMUTRAMP */ 218 # define PF_NOEMUTRAMP (1 << 13) /* Disable EMUTRAMP */ 219 # define PF_RANDMMAP (1 << 14) /* Enable RANDMMAP */ 220 # define PF_NORANDMMAP (1 << 15) /* Disable RANDMMAP */ 221 #endif /* PT_PAX_ */ 222 223 /* older glibc/uclibc will need this since they typo-ed the define */ 224 #ifndef EM_ST19 225 # ifdef EM_AT19 226 # define EM_ST19 EM_AT19 227 # else 228 # define EM_ST19 74 229 # endif 230 #endif 231 232 #ifndef O_CLOEXEC 233 # define O_CLOEXEC 0 234 #endif 235 #ifndef O_PATH 236 # define O_PATH 0 237 #endif 238 239 #define __unused__ __attribute__((__unused__)) 240 241 #endif /* _PORTING_H */ 242