vdsomunge.c 5.6 KB

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