vdsomunge.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #define _GNU_SOURCE
  48. #include <byteswap.h>
  49. #include <elf.h>
  50. #include <errno.h>
  51. #include <error.h>
  52. #include <fcntl.h>
  53. #include <stdbool.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <sys/mman.h>
  58. #include <sys/stat.h>
  59. #include <sys/types.h>
  60. #include <unistd.h>
  61. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  62. #define HOST_ORDER ELFDATA2LSB
  63. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  64. #define HOST_ORDER ELFDATA2MSB
  65. #endif
  66. /* Some of the ELF constants we'd like to use were added to <elf.h>
  67. * relatively recently.
  68. */
  69. #ifndef EF_ARM_EABI_VER5
  70. #define EF_ARM_EABI_VER5 0x05000000
  71. #endif
  72. #ifndef EF_ARM_ABI_FLOAT_SOFT
  73. #define EF_ARM_ABI_FLOAT_SOFT 0x200
  74. #endif
  75. #ifndef EF_ARM_ABI_FLOAT_HARD
  76. #define EF_ARM_ABI_FLOAT_HARD 0x400
  77. #endif
  78. static const char *outfile;
  79. static void cleanup(void)
  80. {
  81. if (error_message_count > 0 && outfile != NULL)
  82. unlink(outfile);
  83. }
  84. static Elf32_Word read_elf_word(Elf32_Word word, bool swap)
  85. {
  86. return swap ? bswap_32(word) : word;
  87. }
  88. static Elf32_Half read_elf_half(Elf32_Half half, bool swap)
  89. {
  90. return swap ? bswap_16(half) : half;
  91. }
  92. static void write_elf_word(Elf32_Word val, Elf32_Word *dst, bool swap)
  93. {
  94. *dst = swap ? bswap_32(val) : val;
  95. }
  96. int main(int argc, char **argv)
  97. {
  98. const Elf32_Ehdr *inhdr;
  99. bool clear_soft_float;
  100. const char *infile;
  101. Elf32_Word e_flags;
  102. const void *inbuf;
  103. struct stat stat;
  104. void *outbuf;
  105. bool swap;
  106. int outfd;
  107. int infd;
  108. atexit(cleanup);
  109. if (argc != 3)
  110. error(EXIT_FAILURE, 0, "Usage: %s [infile] [outfile]", argv[0]);
  111. infile = argv[1];
  112. outfile = argv[2];
  113. infd = open(infile, O_RDONLY);
  114. if (infd < 0)
  115. error(EXIT_FAILURE, errno, "Cannot open %s", infile);
  116. if (fstat(infd, &stat) != 0)
  117. error(EXIT_FAILURE, errno, "Failed stat for %s", infile);
  118. inbuf = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, infd, 0);
  119. if (inbuf == MAP_FAILED)
  120. error(EXIT_FAILURE, errno, "Failed to map %s", infile);
  121. close(infd);
  122. inhdr = inbuf;
  123. if (memcmp(&inhdr->e_ident, ELFMAG, SELFMAG) != 0)
  124. error(EXIT_FAILURE, 0, "Not an ELF file");
  125. if (inhdr->e_ident[EI_CLASS] != ELFCLASS32)
  126. error(EXIT_FAILURE, 0, "Unsupported ELF class");
  127. swap = inhdr->e_ident[EI_DATA] != HOST_ORDER;
  128. if (read_elf_half(inhdr->e_type, swap) != ET_DYN)
  129. error(EXIT_FAILURE, 0, "Not a shared object");
  130. if (read_elf_half(inhdr->e_machine, swap) != EM_ARM) {
  131. error(EXIT_FAILURE, 0, "Unsupported architecture %#x",
  132. inhdr->e_machine);
  133. }
  134. e_flags = read_elf_word(inhdr->e_flags, swap);
  135. if (EF_ARM_EABI_VERSION(e_flags) != EF_ARM_EABI_VER5) {
  136. error(EXIT_FAILURE, 0, "Unsupported EABI version %#x",
  137. EF_ARM_EABI_VERSION(e_flags));
  138. }
  139. if (e_flags & EF_ARM_ABI_FLOAT_HARD)
  140. error(EXIT_FAILURE, 0,
  141. "Unexpected hard-float flag set in e_flags");
  142. clear_soft_float = !!(e_flags & EF_ARM_ABI_FLOAT_SOFT);
  143. outfd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  144. if (outfd < 0)
  145. error(EXIT_FAILURE, errno, "Cannot open %s", outfile);
  146. if (ftruncate(outfd, stat.st_size) != 0)
  147. error(EXIT_FAILURE, errno, "Cannot truncate %s", outfile);
  148. outbuf = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  149. outfd, 0);
  150. if (outbuf == MAP_FAILED)
  151. error(EXIT_FAILURE, errno, "Failed to map %s", outfile);
  152. close(outfd);
  153. memcpy(outbuf, inbuf, stat.st_size);
  154. if (clear_soft_float) {
  155. Elf32_Ehdr *outhdr;
  156. outhdr = outbuf;
  157. e_flags &= ~EF_ARM_ABI_FLOAT_SOFT;
  158. write_elf_word(e_flags, &outhdr->e_flags, swap);
  159. }
  160. if (msync(outbuf, stat.st_size, MS_SYNC) != 0)
  161. error(EXIT_FAILURE, errno, "Failed to sync %s", outfile);
  162. return EXIT_SUCCESS;
  163. }