blob: aedec81d11988181e3e6f0631113fd01e4737ac8 [file] [log] [blame]
Nathan Lynch85122872015-03-25 19:14:22 +01001/*
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
Nathan Lynch85122872015-03-25 19:14:22 +010048#include <byteswap.h>
49#include <elf.h>
50#include <errno.h>
Nathan Lynch85122872015-03-25 19:14:22 +010051#include <fcntl.h>
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +010052#include <stdarg.h>
Nathan Lynch85122872015-03-25 19:14:22 +010053#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
62#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
63#define HOST_ORDER ELFDATA2LSB
64#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
65#define HOST_ORDER ELFDATA2MSB
66#endif
67
68/* Some of the ELF constants we'd like to use were added to <elf.h>
69 * relatively recently.
70 */
71#ifndef EF_ARM_EABI_VER5
72#define EF_ARM_EABI_VER5 0x05000000
73#endif
74
75#ifndef EF_ARM_ABI_FLOAT_SOFT
76#define EF_ARM_ABI_FLOAT_SOFT 0x200
77#endif
78
79#ifndef EF_ARM_ABI_FLOAT_HARD
80#define EF_ARM_ABI_FLOAT_HARD 0x400
81#endif
82
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +010083static int failed;
84static const char *argv0;
Nathan Lynch85122872015-03-25 19:14:22 +010085static const char *outfile;
86
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +010087static void fail(const char *fmt, ...)
88{
89 va_list ap;
90
91 failed = 1;
92 fprintf(stderr, "%s: ", argv0);
93 va_start(ap, fmt);
94 vfprintf(stderr, fmt, ap);
95 va_end(ap);
96 exit(EXIT_FAILURE);
97}
98
Nathan Lynch85122872015-03-25 19:14:22 +010099static void cleanup(void)
100{
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100101 if (failed && outfile != NULL)
Nathan Lynch85122872015-03-25 19:14:22 +0100102 unlink(outfile);
103}
104
105static Elf32_Word read_elf_word(Elf32_Word word, bool swap)
106{
107 return swap ? bswap_32(word) : word;
108}
109
110static Elf32_Half read_elf_half(Elf32_Half half, bool swap)
111{
112 return swap ? bswap_16(half) : half;
113}
114
115static void write_elf_word(Elf32_Word val, Elf32_Word *dst, bool swap)
116{
117 *dst = swap ? bswap_32(val) : val;
118}
119
120int main(int argc, char **argv)
121{
122 const Elf32_Ehdr *inhdr;
123 bool clear_soft_float;
124 const char *infile;
125 Elf32_Word e_flags;
126 const void *inbuf;
127 struct stat stat;
128 void *outbuf;
129 bool swap;
130 int outfd;
131 int infd;
132
133 atexit(cleanup);
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100134 argv0 = argv[0];
Nathan Lynch85122872015-03-25 19:14:22 +0100135
136 if (argc != 3)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100137 fail("Usage: %s [infile] [outfile]\n", argv[0]);
Nathan Lynch85122872015-03-25 19:14:22 +0100138
139 infile = argv[1];
140 outfile = argv[2];
141
142 infd = open(infile, O_RDONLY);
143 if (infd < 0)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100144 fail("Cannot open %s: %s\n", infile, strerror(errno));
Nathan Lynch85122872015-03-25 19:14:22 +0100145
146 if (fstat(infd, &stat) != 0)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100147 fail("Failed stat for %s: %s\n", infile, strerror(errno));
Nathan Lynch85122872015-03-25 19:14:22 +0100148
149 inbuf = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, infd, 0);
150 if (inbuf == MAP_FAILED)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100151 fail("Failed to map %s: %s\n", infile, strerror(errno));
Nathan Lynch85122872015-03-25 19:14:22 +0100152
153 close(infd);
154
155 inhdr = inbuf;
156
157 if (memcmp(&inhdr->e_ident, ELFMAG, SELFMAG) != 0)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100158 fail("Not an ELF file\n");
Nathan Lynch85122872015-03-25 19:14:22 +0100159
160 if (inhdr->e_ident[EI_CLASS] != ELFCLASS32)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100161 fail("Unsupported ELF class\n");
Nathan Lynch85122872015-03-25 19:14:22 +0100162
163 swap = inhdr->e_ident[EI_DATA] != HOST_ORDER;
164
165 if (read_elf_half(inhdr->e_type, swap) != ET_DYN)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100166 fail("Not a shared object\n");
Nathan Lynch85122872015-03-25 19:14:22 +0100167
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100168 if (read_elf_half(inhdr->e_machine, swap) != EM_ARM)
169 fail("Unsupported architecture %#x\n", inhdr->e_machine);
Nathan Lynch85122872015-03-25 19:14:22 +0100170
171 e_flags = read_elf_word(inhdr->e_flags, swap);
172
173 if (EF_ARM_EABI_VERSION(e_flags) != EF_ARM_EABI_VER5) {
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100174 fail("Unsupported EABI version %#x\n",
175 EF_ARM_EABI_VERSION(e_flags));
Nathan Lynch85122872015-03-25 19:14:22 +0100176 }
177
178 if (e_flags & EF_ARM_ABI_FLOAT_HARD)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100179 fail("Unexpected hard-float flag set in e_flags\n");
Nathan Lynch85122872015-03-25 19:14:22 +0100180
181 clear_soft_float = !!(e_flags & EF_ARM_ABI_FLOAT_SOFT);
182
183 outfd = open(outfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
184 if (outfd < 0)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100185 fail("Cannot open %s: %s\n", outfile, strerror(errno));
Nathan Lynch85122872015-03-25 19:14:22 +0100186
187 if (ftruncate(outfd, stat.st_size) != 0)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100188 fail("Cannot truncate %s: %s\n", outfile, strerror(errno));
Nathan Lynch85122872015-03-25 19:14:22 +0100189
190 outbuf = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED,
191 outfd, 0);
192 if (outbuf == MAP_FAILED)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100193 fail("Failed to map %s: %s\n", outfile, strerror(errno));
Nathan Lynch85122872015-03-25 19:14:22 +0100194
195 close(outfd);
196
197 memcpy(outbuf, inbuf, stat.st_size);
198
199 if (clear_soft_float) {
200 Elf32_Ehdr *outhdr;
201
202 outhdr = outbuf;
203 e_flags &= ~EF_ARM_ABI_FLOAT_SOFT;
204 write_elf_word(e_flags, &outhdr->e_flags, swap);
205 }
206
207 if (msync(outbuf, stat.st_size, MS_SYNC) != 0)
Szabolcs Nagy13ee9fd2015-07-01 23:08:10 +0100208 fail("Failed to sync %s: %s\n", outfile, strerror(errno));
Nathan Lynch85122872015-03-25 19:14:22 +0100209
210 return EXIT_SUCCESS;
211}