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