]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.bin/elf2aout/elf2aout.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.bin / elf2aout / elf2aout.c
1 /*-
2  * Copyright (c) 2002 Jake Burkholder
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <sys/elf64.h>
32 #include <sys/endian.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35
36 #include <err.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #define xe16toh(x)      ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
44 #define xe32toh(x)      ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
45 #define xe64toh(x)      ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
46 #define htoxe32(x)      ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
47
48 struct exec {
49         u_int32_t       a_magic;
50         u_int32_t       a_text;
51         u_int32_t       a_data;
52         u_int32_t       a_bss;
53         u_int32_t       a_syms;
54         u_int32_t       a_entry;
55         u_int32_t       a_trsize;
56         u_int32_t       a_drsize;
57 };
58 #define A_MAGIC 0x01030107
59
60 static void usage(void);
61
62 /*
63  * elf to a.out converter for freebsd/sparc64 bootblocks.
64  */
65 int
66 main(int ac, char **av)
67 {
68         Elf64_Half phentsize;
69         Elf64_Half machine;
70         Elf64_Half phnum;
71         Elf64_Xword filesz;
72         Elf64_Xword memsz;
73         Elf64_Addr entry;
74         Elf64_Off offset;
75         Elf64_Off phoff;
76         Elf64_Word type;
77         unsigned char data;
78         struct stat sb;
79         struct exec a;
80         Elf64_Phdr *p;
81         Elf64_Ehdr *e;
82         void *v;
83         int efd;
84         int fd;
85         int c;
86         int i;
87
88         fd = STDIN_FILENO;
89         while ((c = getopt(ac, av, "o:")) != -1)
90                 switch (c) {
91                 case 'o':
92                         if ((fd = open(optarg, O_CREAT|O_RDWR, 0644)) < 0)
93                                 err(1, "%s", optarg);
94                         break;
95                 case '?':
96                 default:
97                         usage();
98                 }
99         ac -= optind;
100         av += optind;
101         if (ac == 0)
102                 usage();
103
104         if ((efd = open(*av, O_RDONLY)) < 0 || fstat(efd, &sb) < 0)
105                 err(1, NULL);
106         v = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, efd, 0);
107         if ((e = v) == MAP_FAILED)
108                 err(1, NULL);
109
110         if (!IS_ELF(*e))
111                 errx(1, "not an elf file");
112         if (e->e_ident[EI_CLASS] != ELFCLASS64)
113                 errx(1, "wrong class");
114         data = e->e_ident[EI_DATA];
115         if (data != ELFDATA2MSB && data != ELFDATA2LSB)
116                 errx(1, "wrong data format");
117         if (e->e_ident[EI_VERSION] != EV_CURRENT)
118                 errx(1, "wrong elf version");
119         machine = xe16toh(e->e_machine);
120         if (machine != EM_SPARCV9 && machine != EM_ALPHA)
121                 errx(1, "wrong machine type");
122         phentsize = xe16toh(e->e_phentsize);
123         if (phentsize != sizeof(*p))
124                 errx(1, "phdr size mismatch");
125
126         entry = xe64toh(e->e_entry);
127         phoff = xe64toh(e->e_phoff);
128         phnum = xe16toh(e->e_phnum);
129         p = (Elf64_Phdr *)((char *)e + phoff);
130         bzero(&a, sizeof(a));
131         for (i = 0; i < phnum; i++) {
132                 type = xe32toh(p[i].p_type);
133                 switch (type) {
134                 case PT_LOAD:
135                         if (a.a_magic != 0)
136                                 errx(1, "too many loadable segments");
137                         filesz = xe64toh(p[i].p_filesz);
138                         memsz = xe64toh(p[i].p_memsz);
139                         offset = xe64toh(p[i].p_offset);
140                         a.a_magic = htoxe32(A_MAGIC);
141                         a.a_text = htoxe32(filesz);
142                         a.a_bss = htoxe32(memsz - filesz);
143                         a.a_entry = htoxe32(entry);
144                         if (write(fd, &a, sizeof(a)) != sizeof(a) ||
145                             write(fd, (char *)e + offset, filesz) != (ssize_t)filesz)
146                                 err(1, NULL);
147                         break;
148                 default:
149                         break;
150                 }
151         }
152
153         return (0);
154 }
155
156 static void
157 usage(void)
158 {
159
160         fprintf(stderr, "usage: elf2aout [-o outfile] infile\n");
161         exit(1);
162 }