]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/elf2aout/elf2aout.c
This commit was generated by cvs2svn to compensate for changes in r93507,
[FreeBSD/FreeBSD.git] / usr.sbin / 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/mman.h>
33 #include <sys/stat.h>
34
35 #include <err.h>
36 #include <fcntl.h>
37
38 #define be16toh(x)      (x)
39 #define be32toh(x)      (x)
40 #define be64toh(x)      (x)
41 #define htobe32(x)      (x)
42
43 struct exec {
44         u_int   a_magic;
45         u_int   a_text;
46         u_int   a_data;
47         u_int   a_bss;
48         u_int   a_syms;
49         u_int   a_entry;
50         u_int   a_trsize;
51         u_int   a_drsize;
52 };
53 #define A_MAGIC 0x01030107
54
55 extern char *optarg;
56 extern int optind;
57
58 static void usage(void);
59
60 /*
61  * elf to a.out converter for freebsd/sparc64 bootblocks.
62  */
63 int
64 main(int ac, char **av)
65 {
66         Elf64_Quarter phentsize;
67         Elf64_Quarter machine;
68         Elf64_Quarter phnum;
69         Elf64_Size filesz;
70         Elf64_Size memsz;
71         Elf64_Addr entry;
72         Elf64_Off offset;
73         Elf64_Off phoff;
74         Elf64_Half type;
75         struct stat sb;
76         struct exec a;
77         Elf64_Phdr *p;
78         Elf64_Ehdr *e;
79         void *v;
80         int efd;
81         int fd;
82         int c;
83         int i;
84
85         while ((c = getopt(ac, av, "o:")) != -1)
86                 switch (c) {
87                 case 'o':
88                         if ((fd = open(optarg, O_CREAT|O_RDWR, 0644)) < 0)
89                                 err(1, "%s", optarg);
90                         break;
91                 case '?':
92                 default:
93                         usage();
94                 }
95         ac -= optind;
96         av += optind;
97         if (ac == 0)
98                 usage();
99
100         if ((efd = open(*av, O_RDONLY)) < 0 || fstat(efd, &sb) < 0)
101                 err(1, NULL);
102         v = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, efd, 0);
103         if ((e = v) == MAP_FAILED)
104                 err(1, NULL);
105
106         if (!IS_ELF(*e))
107                 errx(1, "not an elf file");
108         if (e->e_ident[EI_CLASS] != ELFCLASS64)
109                 errx(1, "wrong class");
110         if (e->e_ident[EI_DATA] != ELFDATA2MSB)
111                 errx(1, "wrong data format");
112         if (e->e_ident[EI_VERSION] != EV_CURRENT)
113                 errx(1, "wrong elf version");
114         machine = be16toh(e->e_machine);
115         if (machine != EM_SPARCV9)
116                 errx(1, "wrong machine type");
117         phentsize = be16toh(e->e_phentsize);
118         if (phentsize != sizeof(*p))
119                 errx(1, "phdr size mismatch");
120
121         entry = be64toh(e->e_entry);
122         phoff = be64toh(e->e_phoff);
123         phnum = be16toh(e->e_phnum);
124         p = (Elf64_Phdr *)((char *)e + phoff);
125         bzero(&a, sizeof(a));
126         for (i = 0; i < phnum; i++) {
127                 type = be32toh(p[i].p_type);
128                 switch (type) {
129                 case PT_LOAD:
130                         if (a.a_magic != 0)
131                                 errx(1, "too many loadable segments");
132                         filesz = be64toh(p[i].p_filesz);
133                         memsz = be64toh(p[i].p_memsz);
134                         offset = be64toh(p[i].p_offset);
135                         a.a_magic = htobe32(A_MAGIC);
136                         a.a_text = htobe32(filesz);
137                         a.a_bss = htobe32(memsz - filesz);
138                         a.a_entry = htobe32(entry);
139                         if (write(fd, &a, sizeof(a)) != sizeof(a) ||
140                             write(fd, (char *)e + offset, filesz) != filesz)
141                                 err(1, NULL);
142                         break;
143                 default:
144                         break;
145                 }
146         }
147
148         return (0);
149 }
150
151 static void
152 usage(void)
153 {
154
155         errx(1, "usage: elftoaout -o outfile infile");
156 }