^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2015 Mentor Graphics Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * vdsomunge - Host program which produces a shared object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * architecturally specified to be usable by both soft- and hard-float
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * programs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * The Procedure Call Standard for the ARM Architecture (ARM IHI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * 0042E) says:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * 6.4.1 VFP and Base Standard Compatibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Code compiled for the VFP calling standard is compatible with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * the base standard (and vice-versa) if no floating-point or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * containerized vector arguments or results are used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * And ELF for the ARM Architecture (ARM IHI 0044E) (Table 4-2) says:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * If both EF_ARM_ABI_FLOAT_XXXX bits are clear, conformance to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * base procedure-call standard is implied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * The VDSO is built with -msoft-float, as with the rest of the ARM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * kernel, and uses no floating point arguments or results. The build
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * process will produce a shared object that may or may not have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * EF_ARM_ABI_FLOAT_SOFT flag set (it seems to depend on the binutils
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * version; binutils starting with 2.24 appears to set it). The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * EF_ARM_ABI_FLOAT_HARD flag should definitely not be set, and this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * program will error out if it is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * If the soft-float flag is set, this program clears it. That's all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * it does.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define swab16(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ((((x) & 0x00ff) << 8) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) (((x) & 0xff00) >> 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define swab32(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ((((x) & 0x000000ff) << 24) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) (((x) & 0x0000ff00) << 8) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) (((x) & 0x00ff0000) >> 8) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) (((x) & 0xff000000) >> 24))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define HOST_ORDER ELFDATA2LSB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define HOST_ORDER ELFDATA2MSB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* Some of the ELF constants we'd like to use were added to <elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * relatively recently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #ifndef EF_ARM_EABI_VER5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #define EF_ARM_EABI_VER5 0x05000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #ifndef EF_ARM_ABI_FLOAT_SOFT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define EF_ARM_ABI_FLOAT_SOFT 0x200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #ifndef EF_ARM_ABI_FLOAT_HARD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define EF_ARM_ABI_FLOAT_HARD 0x400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static const char *argv0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static const char *outfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static void fail(const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) failed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) fprintf(stderr, "%s: ", argv0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) va_start(ap, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) vfprintf(stderr, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static void cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (failed && outfile != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) unlink(outfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static Elf32_Word read_elf_word(Elf32_Word word, bool swap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return swap ? swab32(word) : word;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static Elf32_Half read_elf_half(Elf32_Half half, bool swap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return swap ? swab16(half) : half;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static void write_elf_word(Elf32_Word val, Elf32_Word *dst, bool swap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *dst = swap ? swab32(val) : val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) const Elf32_Ehdr *inhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) bool clear_soft_float;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) const char *infile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) Elf32_Word e_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) const void *inbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct stat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) void *outbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) bool swap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int outfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int infd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) atexit(cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) argv0 = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (argc != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) fail("Usage: %s [infile] [outfile]\n", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) infile = argv[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) outfile = argv[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) infd = open(infile, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (infd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) fail("Cannot open %s: %s\n", infile, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (fstat(infd, &stat) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) fail("Failed stat for %s: %s\n", infile, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) inbuf = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, infd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (inbuf == MAP_FAILED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) fail("Failed to map %s: %s\n", infile, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) close(infd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) inhdr = inbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (memcmp(&inhdr->e_ident, ELFMAG, SELFMAG) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) fail("Not an ELF file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (inhdr->e_ident[EI_CLASS] != ELFCLASS32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) fail("Unsupported ELF class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) swap = inhdr->e_ident[EI_DATA] != HOST_ORDER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (read_elf_half(inhdr->e_type, swap) != ET_DYN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) fail("Not a shared object\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (read_elf_half(inhdr->e_machine, swap) != EM_ARM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) fail("Unsupported architecture %#x\n", inhdr->e_machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) e_flags = read_elf_word(inhdr->e_flags, swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (EF_ARM_EABI_VERSION(e_flags) != EF_ARM_EABI_VER5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) fail("Unsupported EABI version %#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) EF_ARM_EABI_VERSION(e_flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (e_flags & EF_ARM_ABI_FLOAT_HARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) fail("Unexpected hard-float flag set in e_flags\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) clear_soft_float = !!(e_flags & EF_ARM_ABI_FLOAT_SOFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) outfd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (outfd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) fail("Cannot open %s: %s\n", outfile, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (ftruncate(outfd, stat.st_size) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) fail("Cannot truncate %s: %s\n", outfile, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) outbuf = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) outfd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (outbuf == MAP_FAILED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) fail("Failed to map %s: %s\n", outfile, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) close(outfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) memcpy(outbuf, inbuf, stat.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (clear_soft_float) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) Elf32_Ehdr *outhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) outhdr = outbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) e_flags &= ~EF_ARM_ABI_FLOAT_SOFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) write_elf_word(e_flags, &outhdr->e_flags, swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (msync(outbuf, stat.st_size, MS_SYNC) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) fail("Failed to sync %s: %s\n", outfile, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return EXIT_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }