1 |
207 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
204 |
dgisselq |
//
|
3 |
|
|
// Filename: zipelf.cpp
|
4 |
|
|
//
|
5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
6 |
|
|
//
|
7 |
|
|
// Purpose:
|
8 |
|
|
//
|
9 |
|
|
//
|
10 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
11 |
|
|
// Gisselquist Technology, LLC
|
12 |
|
|
//
|
13 |
207 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
14 |
204 |
dgisselq |
//
|
15 |
207 |
dgisselq |
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
|
16 |
204 |
dgisselq |
//
|
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 |
207 |
dgisselq |
// 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 |
204 |
dgisselq |
// License: GPL, v3, as defined and found on www.gnu.org,
|
33 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
34 |
|
|
//
|
35 |
|
|
//
|
36 |
207 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
37 |
204 |
dgisselq |
//
|
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 |
207 |
dgisselq |
fprintf(stderr," %-20s 0x%jx\n","e_type", (uintmax_t)ehdr.e_type);
|
123 |
|
|
fprintf(stderr," %-20s 0x%jx\n","e_machine", (uintmax_t)ehdr.e_machine);
|
124 |
|
|
fprintf(stderr," %-20s 0x%jx\n","e_version", (uintmax_t)ehdr.e_version);
|
125 |
|
|
fprintf(stderr," %-20s 0x%jx\n","e_entry",(uintmax_t)ehdr.e_entry);
|
126 |
|
|
fprintf(stderr," %-20s 0x%jx\n","e_phoff",(uintmax_t)ehdr.e_phoff);
|
127 |
|
|
fprintf(stderr," %-20s 0x%jx\n","e_shoff",(uintmax_t)ehdr.e_shoff);
|
128 |
|
|
fprintf(stderr," %-20s 0x%jx\n","e_flags",(uintmax_t)ehdr.e_flags);
|
129 |
|
|
fprintf(stderr," %-20s 0x%jx\n","e_ehsize",(uintmax_t)ehdr.e_ehsize);
|
130 |
|
|
fprintf(stderr," %-20s 0x%jx\n","e_phentsize", (uintmax_t)ehdr.e_phentsize);
|
131 |
|
|
fprintf(stderr," %-20s 0x%jx\n","e_shentsize", (uintmax_t)ehdr.e_shentsize);
|
132 |
|
|
fprintf(stderr,"\n");
|
133 |
204 |
dgisselq |
}
|
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 |
207 |
dgisselq |
fprintf(stderr, " %-20s 0x%x\n", "p_type", phdr.p_type);
|
165 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_offset", phdr.p_offset);
|
166 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_vaddr", phdr.p_vaddr);
|
167 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_paddr", phdr.p_paddr);
|
168 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
|
169 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_memsz", phdr.p_memsz);
|
170 |
|
|
fprintf(stderr, " %-20s 0x%x [", "p_flags", phdr.p_flags);
|
171 |
204 |
dgisselq |
|
172 |
207 |
dgisselq |
if (phdr.p_flags & PF_X) fprintf(stderr, " Execute");
|
173 |
|
|
if (phdr.p_flags & PF_R) fprintf(stderr, " Read");
|
174 |
|
|
if (phdr.p_flags & PF_W) fprintf(stderr, " Write");
|
175 |
|
|
fprintf(stderr, "]\n");
|
176 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_align", phdr.p_align);
|
177 |
204 |
dgisselq |
}
|
178 |
|
|
|
179 |
207 |
dgisselq |
total_octets += phdr.p_filesz;
|
180 |
204 |
dgisselq |
}
|
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 |
207 |
dgisselq |
if (dbg) fprintf(stderr, "Working with &d[%d]\n", current_offset);
|
191 |
204 |
dgisselq |
r[i] = (ELFSECTION *)(&d[current_offset]);
|
192 |
|
|
|
193 |
|
|
if (gelf_getphdr(e, i, &phdr) != &phdr) {
|
194 |
|
|
fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
|
195 |
|
|
exit(EXIT_FAILURE);
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
if (dbg) {
|
199 |
207 |
dgisselq |
fprintf(stderr, " %-20s 0x%jx\n", "p_offset", phdr.p_offset);
|
200 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_vaddr", phdr.p_vaddr);
|
201 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_paddr", phdr.p_paddr);
|
202 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
|
203 |
|
|
fprintf(stderr, " %-20s 0x%jx\n", "p_memsz", phdr.p_memsz);
|
204 |
|
|
fprintf(stderr, " %-20s 0x%x [", "p_flags", phdr.p_flags);
|
205 |
204 |
dgisselq |
|
206 |
207 |
dgisselq |
if (phdr.p_flags & PF_X) fprintf(stderr, " Execute");
|
207 |
|
|
if (phdr.p_flags & PF_R) fprintf(stderr, " Read");
|
208 |
|
|
if (phdr.p_flags & PF_W) fprintf(stderr, " Write");
|
209 |
|
|
fprintf(stderr, "]\n");
|
210 |
204 |
dgisselq |
|
211 |
207 |
dgisselq |
fprintf(stderr, " %-20s 0x%jx\n", "p_align", phdr.p_align);
|
212 |
204 |
dgisselq |
}
|
213 |
|
|
|
214 |
|
|
current_section++;
|
215 |
|
|
|
216 |
|
|
r[i]->m_start = phdr.p_paddr;
|
217 |
|
|
r[i]->m_len = phdr.p_filesz;
|
218 |
209 |
dgisselq |
r[i]->m_vaddr = phdr.p_vaddr;
|
219 |
204 |
dgisselq |
|
220 |
207 |
dgisselq |
current_offset += phdr.p_filesz + sizeof(ELFSECTION);
|
221 |
204 |
dgisselq |
|
222 |
|
|
// Now, let's read in our section ...
|
223 |
|
|
if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
|
224 |
209 |
dgisselq |
fprintf(stderr, "Could not seek to file position %08lx\n", (unsigned long)phdr.p_offset);
|
225 |
204 |
dgisselq |
perror("O/S Err:");
|
226 |
|
|
exit(EXIT_FAILURE);
|
227 |
|
|
} if (phdr.p_filesz > phdr.p_memsz)
|
228 |
|
|
phdr.p_filesz = 0;
|
229 |
|
|
if (read(fd, r[i]->m_data, phdr.p_filesz) != (int)phdr.p_filesz) {
|
230 |
|
|
fprintf(stderr, "Didnt read entire section\n");
|
231 |
|
|
perror("O/S Err:");
|
232 |
|
|
exit(EXIT_FAILURE);
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
/*
|
236 |
|
|
// Next, we need to byte swap it from big to little endian
|
237 |
|
|
for(unsigned j=0; j<r[i]->m_len; j++)
|
238 |
|
|
r[i]->m_data[j] = byteswap(r[i]->m_data[j]);
|
239 |
|
|
*/
|
240 |
|
|
|
241 |
|
|
if (dbg) for(unsigned j=0; j<r[i]->m_len; j++)
|
242 |
|
|
fprintf(stderr, "ADR[%04x] = %02x\n", r[i]->m_start+j,
|
243 |
|
|
r[i]->m_data[j] & 0x0ff);
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
r[i] = (ELFSECTION *)(&d[current_offset]);
|
247 |
|
|
r[current_section]->m_start = 0;
|
248 |
|
|
r[current_section]->m_len = 0;
|
249 |
|
|
|
250 |
|
|
elf_end(e);
|
251 |
|
|
close(fd);
|
252 |
|
|
}
|
253 |
|
|
|