Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * Hypervisor Software File: elf2bin.c | |
5 | * | |
6 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
7 | * | |
8 | * - Do no alter or remove copyright notices | |
9 | * | |
10 | * - Redistribution and use of this software in source and binary forms, with | |
11 | * or without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistribution of source code must retain the above copyright notice, | |
15 | * this list of conditions and the following disclaimer. | |
16 | * | |
17 | * - Redistribution in binary form must reproduce the above copyright notice, | |
18 | * this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * | |
21 | * Neither the name of Sun Microsystems, Inc. or the names of contributors | |
22 | * may be used to endorse or promote products derived from this software | |
23 | * without specific prior written permission. | |
24 | * | |
25 | * This software is provided "AS IS," without a warranty of any kind. | |
26 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
27 | * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
28 | * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
29 | * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
30 | * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
31 | * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
32 | * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
33 | * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
34 | * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
35 | * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
36 | * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
37 | * | |
38 | * You acknowledge that this software is not designed, licensed or | |
39 | * intended for use in the design, construction, operation or maintenance of | |
40 | * any nuclear facility. | |
41 | * | |
42 | * ========== Copyright Header End ============================================ | |
43 | */ | |
44 | /* | |
45 | * id: @(#)elf2bin.c 1.1 96/06/18 | |
46 | * purpose: | |
47 | * copyright: Copyright 1996 Sun Microsystems, Inc. All Rights Reserved | |
48 | */ | |
49 | ||
50 | #include <sys/types.h> | |
51 | #include <sys/stat.h> | |
52 | #include <fcntl.h> | |
53 | #include <string.h> | |
54 | #include <stdio.h> | |
55 | #include <libelf.h> | |
56 | ||
57 | int debug = 0; | |
58 | ||
59 | char *sh_types[SHT_NUM] = { | |
60 | "SHT_NULL", | |
61 | "SHT_PROGBITS", | |
62 | "SHT_SYMTAB", | |
63 | "SHT_STRTAB", | |
64 | "SHT_RELA", | |
65 | "SHT_HASH", | |
66 | "SHT_DYNAMIC", | |
67 | "SHT_NOTE", | |
68 | "SHT_NOBITS", | |
69 | "SHT_REL", | |
70 | "SHT_SHLIB", | |
71 | "SHT_DYNSYM" | |
72 | }; | |
73 | ||
74 | ||
75 | char *sh_flags[] = { | |
76 | "", /* 0 */ | |
77 | "SHF_WRITE", /* 1 */ | |
78 | "SHF_ALLOC", /* 2 */ | |
79 | 0, /* 3 */ | |
80 | "SHF_EXECINSTR" /* 4 */ | |
81 | }; | |
82 | ||
83 | ||
84 | int | |
85 | main(int argc, char **argv) | |
86 | { | |
87 | int ifd, ofd; | |
88 | ||
89 | if (argc != 3 ) usage(); | |
90 | ||
91 | if ((ifd = open(argv[1], O_RDWR)) == -1) { | |
92 | perror("elf2bin: Can't open input file "); | |
93 | fprintf(stderr, "%s\n", argv[1]); | |
94 | exit(-1); | |
95 | } | |
96 | ||
97 | if ((ofd = open(argv[2], O_CREAT|O_TRUNC|O_WRONLY, 0666)) == -1) { | |
98 | perror("elf2bin: Can't open output file "); | |
99 | fprintf(stderr, "%s\n", argv[2]); | |
100 | close(ifd); | |
101 | exit(-2); | |
102 | ||
103 | } | |
104 | ||
105 | return elf2bin(ifd, ofd); | |
106 | ||
107 | } | |
108 | ||
109 | int | |
110 | elf2bin(int ifd, int ofd) | |
111 | { | |
112 | Elf *elf, *arf; | |
113 | Elf_Cmd cmd; | |
114 | ||
115 | if (elf_version(EV_CURRENT) == EV_NONE) { | |
116 | /* library out of date */ | |
117 | /* recover from error */ | |
118 | } | |
119 | cmd = ELF_C_READ; | |
120 | arf = elf_begin(ifd, cmd, (Elf *)0); | |
121 | elf_fill(0); | |
122 | while ((elf = elf_begin(ifd, cmd, arf)) != 0) { | |
123 | Elf32_Ehdr *ehdr; | |
124 | if ((ehdr = elf32_getehdr(elf)) != 0) { | |
125 | /* process the file ... */ | |
126 | unsigned int size = 0; | |
127 | unsigned int base = 0; | |
128 | Elf_Scn *scn = 0; | |
129 | ||
130 | while ((scn = elf_nextscn(elf, scn)) != 0) { | |
131 | /* process section */ | |
132 | Elf32_Shdr *shdr; | |
133 | Elf_Data *data = 0; | |
134 | ||
135 | shdr = elf32_getshdr(scn); | |
136 | ||
137 | if (debug) dump_shdr(shdr); | |
138 | ||
139 | if ((data = elf_getdata(scn, data)) == 0 || | |
140 | data->d_size == 0) { | |
141 | /* error or no data */ | |
142 | } else { | |
143 | if (debug) dump_data(data); | |
144 | ||
145 | if (base == 0) base = shdr->sh_addr; | |
146 | ||
147 | if ((shdr->sh_type == SHT_PROGBITS) && | |
148 | ((shdr->sh_flags & SHF_ALLOC) == SHF_ALLOC)) { | |
149 | /* interesting section ... */ | |
150 | if (base + size != shdr->sh_addr) { | |
151 | unsigned int pad = shdr->sh_addr - base - size; | |
152 | if (pad > 0x1000) { | |
153 | base = shdr->sh_addr; | |
154 | size = 0; | |
155 | } else { | |
156 | while (pad) { | |
157 | long b = 0; | |
158 | write(ofd, &b, sizeof(long)); | |
159 | pad -= sizeof(long); | |
160 | } | |
161 | } | |
162 | } | |
163 | if (write(ofd, data->d_buf, data->d_size) != | |
164 | data->d_size) { | |
165 | close(ifd); | |
166 | close(ofd); | |
167 | return(1); | |
168 | } else { | |
169 | size += data->d_size; | |
170 | } | |
171 | } | |
172 | } | |
173 | } | |
174 | ||
175 | } | |
176 | cmd = elf_next(elf); | |
177 | elf_end(elf); | |
178 | } | |
179 | elf_end(arf); | |
180 | close(ofd); | |
181 | close(ifd); | |
182 | return(0); | |
183 | } | |
184 | ||
185 | dump_shdr(Elf32_Shdr *shdr) | |
186 | { | |
187 | printf("sh_name = 0x%x\n", shdr->sh_name); | |
188 | printf("sh_type = 0x%x %s\n", | |
189 | shdr->sh_type, | |
190 | sh_types[shdr->sh_type] | |
191 | ); | |
192 | printf("sh_flags = 0x%x %s %s %s\n", | |
193 | shdr->sh_flags, | |
194 | sh_flags[shdr->sh_flags & SHF_WRITE], | |
195 | sh_flags[shdr->sh_flags & SHF_ALLOC], | |
196 | sh_flags[shdr->sh_flags & SHF_EXECINSTR] | |
197 | ); | |
198 | printf("sh_addr 0x%x\n", shdr->sh_addr); | |
199 | printf("sh_offset 0x%x\n", shdr->sh_offset); | |
200 | printf("sh_size 0x%x\n", shdr->sh_size); | |
201 | printf("sh_link 0x%x\n", shdr->sh_link); | |
202 | printf("sh_info 0x%x\n", shdr->sh_info); | |
203 | printf("sh_addralign 0x%x\n", shdr->sh_addralign); | |
204 | printf("sh_entsize 0x%x\n", shdr->sh_entsize); | |
205 | ||
206 | } | |
207 | ||
208 | dump_data(Elf_Data *data) | |
209 | { | |
210 | printf("\td_buf 0x%x\n", data->d_buf); | |
211 | printf("\td_type 0x%x\n", data->d_type); | |
212 | printf("\td_size 0x%x\n", data->d_size); | |
213 | printf("\td_off 0x%x\n", data->d_off); | |
214 | printf("\td_align 0x%x\n", data->d_align); | |
215 | printf("\td_version 0x%x\n", data->d_version); | |
216 | printf("\n"); | |
217 | } | |
218 | ||
219 | int | |
220 | usage() | |
221 | { | |
222 | fprintf(stderr, "Usage: elf2bin forth.prom forth.bin\n"); | |
223 | exit(-3); | |
224 | } |