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