vdsomunge.c 5.0 KB

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