1 |
49 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: zipelf.cpp
|
4 |
|
|
//
|
5 |
|
|
// Project: OpenArty, an entirely open SoC based upon the Arty platform
|
6 |
|
|
//
|
7 |
|
|
// Purpose:
|
8 |
|
|
//
|
9 |
|
|
//
|
10 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
11 |
|
|
// Gisselquist Technology, LLC
|
12 |
|
|
//
|
13 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
14 |
|
|
//
|
15 |
|
|
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
|
16 |
|
|
//
|
17 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
18 |
|
|
// modify it under the terms of the GNU General Public License as published
|
19 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
20 |
|
|
// your option) any later version.
|
21 |
|
|
//
|
22 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
23 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
24 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
25 |
|
|
// for more details.
|
26 |
|
|
//
|
27 |
|
|
// You should have received a copy of the GNU General Public License along
|
28 |
|
|
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
29 |
|
|
// target there if the PDF file isn't present.) If not, see
|
30 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
31 |
|
|
//
|
32 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
33 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
34 |
|
|
//
|
35 |
|
|
//
|
36 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
37 |
|
|
//
|
38 |
|
|
//
|
39 |
|
|
|
40 |
|
|
#include <stdlib.h>
|
41 |
|
|
#include <stdio.h>
|
42 |
|
|
#include <stdint.h>
|
43 |
|
|
#include <unistd.h>
|
44 |
|
|
#include <sys/types.h>
|
45 |
|
|
#include <sys/stat.h>
|
46 |
|
|
#include <fcntl.h>
|
47 |
|
|
#include <libelf.h>
|
48 |
|
|
#include <assert.h>
|
49 |
|
|
#include <gelf.h>
|
50 |
|
|
#include <string.h>
|
51 |
|
|
|
52 |
|
|
#include "zipelf.h"
|
53 |
|
|
|
54 |
|
|
bool
|
55 |
|
|
iself(const char *fname)
|
56 |
|
|
{
|
57 |
|
|
FILE *fp;
|
58 |
|
|
bool ret = true;
|
59 |
|
|
fp = fopen(fname, "rb");
|
60 |
|
|
|
61 |
|
|
if (!fp) return false;
|
62 |
|
|
if (0x7f != fgetc(fp)) ret = false;
|
63 |
|
|
if ('E' != fgetc(fp)) ret = false;
|
64 |
|
|
if ('L' != fgetc(fp)) ret = false;
|
65 |
|
|
if ('F' != fgetc(fp)) ret = false;
|
66 |
|
|
fclose(fp);
|
67 |
|
|
return ret;
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
void elfread(const char *fname, unsigned &entry, ELFSECTION **§ions)
|
71 |
|
|
{
|
72 |
|
|
Elf *e;
|
73 |
|
|
int fd, i;
|
74 |
|
|
size_t n;
|
75 |
|
|
char *id;
|
76 |
|
|
Elf_Kind ek;
|
77 |
|
|
GElf_Ehdr ehdr;
|
78 |
|
|
GElf_Phdr phdr;
|
79 |
|
|
const bool dbg = false;
|
80 |
|
|
|
81 |
|
|
if (elf_version(EV_CURRENT) == EV_NONE) {
|
82 |
|
|
fprintf(stderr, "ELF library initialization err, %s\n", elf_errmsg(-1));
|
83 |
|
|
perror("O/S Err:");
|
84 |
|
|
exit(EXIT_FAILURE);
|
85 |
|
|
} if ((fd = open(fname, O_RDONLY, 0)) < 0) {
|
86 |
|
|
fprintf(stderr, "Could not open %s\n", fname);
|
87 |
|
|
perror("O/S Err:");
|
88 |
|
|
exit(EXIT_FAILURE);
|
89 |
|
|
} if ((e = elf_begin(fd, ELF_C_READ, NULL))==NULL) {
|
90 |
|
|
fprintf(stderr, "Could not run elf_begin, %s\n", elf_errmsg(-1));
|
91 |
|
|
exit(EXIT_FAILURE);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
ek = elf_kind(e);
|
95 |
|
|
if (ek == ELF_K_ELF) {
|
96 |
|
|
; // This is the kind of file we should expect
|
97 |
|
|
} else if (ek == ELF_K_AR) {
|
98 |
|
|
fprintf(stderr, "Cannot run an archive!\n");
|
99 |
|
|
exit(EXIT_FAILURE);
|
100 |
|
|
} else if (ek == ELF_K_NONE) {
|
101 |
|
|
;
|
102 |
|
|
} else {
|
103 |
|
|
fprintf(stderr, "Unexpected ELF file kind!\n");
|
104 |
|
|
exit(EXIT_FAILURE);
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
if (gelf_getehdr(e, &ehdr) == NULL) {
|
108 |
|
|
fprintf(stderr, "getehdr() failed: %s\n", elf_errmsg(-1));
|
109 |
|
|
exit(EXIT_FAILURE);
|
110 |
|
|
} if ((i=gelf_getclass(e)) == ELFCLASSNONE) {
|
111 |
|
|
fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1));
|
112 |
|
|
exit(EXIT_FAILURE);
|
113 |
|
|
} if ((id = elf_getident(e, NULL)) == NULL) {
|
114 |
|
|
fprintf(stderr, "getident() failed: %s\n", elf_errmsg(-1));
|
115 |
|
|
exit(EXIT_FAILURE);
|
116 |
|
|
} if (i != ELFCLASS32) {
|
117 |
|
|
fprintf(stderr, "This is a 64-bit ELF file, ZipCPU ELF files are all 32-bit\n");
|
118 |
|
|
exit(EXIT_FAILURE);
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
if (dbg) {
|
122 |
|
|
printf(" %-20s 0x%jx\n", "e_type", (uintmax_t)ehdr.e_type);
|
123 |
|
|
printf(" %-20s 0x%jx\n", "e_machine", (uintmax_t)ehdr.e_machine);
|
124 |
|
|
printf(" %-20s 0x%jx\n", "e_version", (uintmax_t)ehdr.e_version);
|
125 |
|
|
printf(" %-20s 0x%jx\n", "e_entry", (uintmax_t)ehdr.e_entry);
|
126 |
|
|
printf(" %-20s 0x%jx\n", "e_phoff", (uintmax_t)ehdr.e_phoff);
|
127 |
|
|
printf(" %-20s 0x%jx\n", "e_shoff", (uintmax_t)ehdr.e_shoff);
|
128 |
|
|
printf(" %-20s 0x%jx\n", "e_flags", (uintmax_t)ehdr.e_flags);
|
129 |
|
|
printf(" %-20s 0x%jx\n", "e_ehsize", (uintmax_t)ehdr.e_ehsize);
|
130 |
|
|
printf(" %-20s 0x%jx\n", "e_phentsize", (uintmax_t)ehdr.e_phentsize);
|
131 |
|
|
printf(" %-20s 0x%jx\n", "e_shentsize", (uintmax_t)ehdr.e_shentsize);
|
132 |
|
|
printf("\n");
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
// Check whether or not this is an ELF file for the ZipCPU ...
|
137 |
|
|
if (ehdr.e_machine != 0x0dad1) {
|
138 |
|
|
fprintf(stderr, "This is not a ZipCPU/8 ELF file\n");
|
139 |
|
|
exit(EXIT_FAILURE);
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
// Get our entry address
|
143 |
|
|
entry = ehdr.e_entry;
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
// Now, let's go look at the program header
|
147 |
|
|
if (elf_getphdrnum(e, &n) != 0) {
|
148 |
|
|
fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
|
149 |
|
|
exit(EXIT_FAILURE);
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
assert(n != 0);
|
153 |
|
|
|
154 |
|
|
unsigned total_octets = 0, current_offset=0, current_section=0;
|
155 |
|
|
for(i=0; i<(int)n; i++) {
|
156 |
|
|
total_octets += sizeof(ELFSECTION *)+sizeof(ELFSECTION);
|
157 |
|
|
|
158 |
|
|
if (gelf_getphdr(e, i, &phdr) != &phdr) {
|
159 |
|
|
fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
|
160 |
|
|
exit(EXIT_FAILURE);
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
if (dbg) {
|
164 |
|
|
printf(" %-20s 0x%x\n", "p_type", phdr.p_type);
|
165 |
|
|
printf(" %-20s 0x%jx\n", "p_offset", phdr.p_offset);
|
166 |
|
|
printf(" %-20s 0x%jx\n", "p_vaddr", phdr.p_vaddr);
|
167 |
|
|
printf(" %-20s 0x%jx\n", "p_paddr", phdr.p_paddr);
|
168 |
|
|
printf(" %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
|
169 |
|
|
printf(" %-20s 0x%jx\n", "p_memsz", phdr.p_memsz);
|
170 |
|
|
printf(" %-20s 0x%x [", "p_flags", phdr.p_flags);
|
171 |
|
|
|
172 |
|
|
if (phdr.p_flags & PF_X) printf(" Execute");
|
173 |
|
|
if (phdr.p_flags & PF_R) printf(" Read");
|
174 |
|
|
if (phdr.p_flags & PF_W) printf(" Write");
|
175 |
|
|
printf("]\n");
|
176 |
|
|
printf(" %-20s 0x%jx\n", "p_align", phdr.p_align);
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
total_octets += phdr.p_memsz;
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
char *d = (char *)malloc(total_octets + sizeof(ELFSECTION)+sizeof(ELFSECTION *));
|
183 |
|
|
memset(d, 0, total_octets);
|
184 |
|
|
|
185 |
|
|
ELFSECTION **r = sections = (ELFSECTION **)d;
|
186 |
|
|
current_offset = (n+1)*sizeof(ELFSECTION *);
|
187 |
|
|
current_section = 0;
|
188 |
|
|
|
189 |
|
|
for(i=0; i<(int)n; i++) {
|
190 |
|
|
r[i] = (ELFSECTION *)(&d[current_offset]);
|
191 |
|
|
|
192 |
|
|
if (gelf_getphdr(e, i, &phdr) != &phdr) {
|
193 |
|
|
fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
|
194 |
|
|
exit(EXIT_FAILURE);
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
if (dbg) {
|
198 |
|
|
printf(" %-20s 0x%jx\n", "p_offset", phdr.p_offset);
|
199 |
|
|
printf(" %-20s 0x%jx\n", "p_vaddr", phdr.p_vaddr);
|
200 |
|
|
printf(" %-20s 0x%jx\n", "p_paddr", phdr.p_paddr);
|
201 |
|
|
printf(" %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
|
202 |
|
|
printf(" %-20s 0x%jx\n", "p_memsz", phdr.p_memsz);
|
203 |
|
|
printf(" %-20s 0x%x [", "p_flags", phdr.p_flags);
|
204 |
|
|
|
205 |
|
|
if (phdr.p_flags & PF_X) printf(" Execute");
|
206 |
|
|
if (phdr.p_flags & PF_R) printf(" Read");
|
207 |
|
|
if (phdr.p_flags & PF_W) printf(" Write");
|
208 |
|
|
printf("]\n");
|
209 |
|
|
|
210 |
|
|
printf(" %-20s 0x%jx\n", "p_align", phdr.p_align);
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
current_section++;
|
214 |
|
|
|
215 |
|
|
r[i]->m_start = phdr.p_paddr;
|
216 |
|
|
r[i]->m_len = phdr.p_filesz;
|
217 |
|
|
|
218 |
|
|
current_offset += phdr.p_memsz + sizeof(ELFSECTION);
|
219 |
|
|
|
220 |
|
|
// Now, let's read in our section ...
|
221 |
|
|
if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
|
222 |
|
|
fprintf(stderr, "Could not seek to file position %08lx\n", phdr.p_offset);
|
223 |
|
|
perror("O/S Err:");
|
224 |
|
|
exit(EXIT_FAILURE);
|
225 |
|
|
} if (phdr.p_filesz > phdr.p_memsz)
|
226 |
|
|
phdr.p_filesz = 0;
|
227 |
|
|
if (read(fd, r[i]->m_data, phdr.p_filesz) != (int)phdr.p_filesz) {
|
228 |
|
|
fprintf(stderr, "Didnt read entire section\n");
|
229 |
|
|
perror("O/S Err:");
|
230 |
|
|
exit(EXIT_FAILURE);
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
/*
|
234 |
|
|
// Next, we need to byte swap it from big to little endian
|
235 |
|
|
for(unsigned j=0; j<r[i]->m_len; j++)
|
236 |
|
|
r[i]->m_data[j] = byteswap(r[i]->m_data[j]);
|
237 |
|
|
*/
|
238 |
|
|
|
239 |
|
|
if (dbg) for(unsigned j=0; j<r[i]->m_len; j++)
|
240 |
|
|
fprintf(stderr, "ADR[%04x] = %02x\n", r[i]->m_start+j,
|
241 |
|
|
r[i]->m_data[j] & 0x0ff);
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
r[i] = (ELFSECTION *)(&d[current_offset]);
|
245 |
|
|
r[current_section]->m_start = 0;
|
246 |
|
|
r[current_section]->m_len = 0;
|
247 |
|
|
|
248 |
|
|
elf_end(e);
|
249 |
|
|
close(fd);
|
250 |
|
|
}
|
251 |
|
|
|