OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sim/] [cpp/] [zipelf.cpp] - Blame information for rev 204

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 204 dgisselq
///////////////////////////////////////////////////////////////////////////////
2
//
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
///////////////////////////////////////////////////////////////////////////////
14
//
15
// Copyright (C) 2015-2016, 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
// License:     GPL, v3, as defined and found on www.gnu.org,
28
//              http://www.gnu.org/licenses/gpl.html
29
//
30
//
31
///////////////////////////////////////////////////////////////////////////////
32
//
33
//
34
 
35
#include <stdlib.h>
36
#include <stdio.h>
37
#include <stdint.h>
38
#include <unistd.h>
39
#include <sys/types.h>
40
#include <sys/stat.h>
41
#include <fcntl.h>
42
#include <libelf.h>
43
#include <assert.h>
44
#include <gelf.h>
45
#include <string.h>
46
 
47
#include "zipelf.h"
48
 
49
bool
50
iself(const char *fname)
51
{
52
        FILE    *fp;
53
        bool    ret = true;
54
        fp = fopen(fname, "rb");
55
 
56
        if (!fp)        return false;
57
        if (0x7f != fgetc(fp))  ret = false;
58
        if ('E'  != fgetc(fp))  ret = false;
59
        if ('L'  != fgetc(fp))  ret = false;
60
        if ('F'  != fgetc(fp))  ret = false;
61
        fclose(fp);
62
        return  ret;
63
}
64
 
65
void    elfread(const char *fname, unsigned &entry, ELFSECTION **&sections)
66
{
67
        Elf     *e;
68
        int     fd, i;
69
        size_t  n;
70
        char    *id;
71
        Elf_Kind        ek;
72
        GElf_Ehdr       ehdr;
73
        GElf_Phdr       phdr;
74
        const   bool    dbg = false;
75
 
76
        if (elf_version(EV_CURRENT) == EV_NONE) {
77
                fprintf(stderr, "ELF library initialization err, %s\n", elf_errmsg(-1));
78
                perror("O/S Err:");
79
                exit(EXIT_FAILURE);
80
        } if ((fd = open(fname, O_RDONLY, 0)) < 0) {
81
                fprintf(stderr, "Could not open %s\n", fname);
82
                perror("O/S Err:");
83
                exit(EXIT_FAILURE);
84
        } if ((e = elf_begin(fd, ELF_C_READ, NULL))==NULL) {
85
                fprintf(stderr, "Could not run elf_begin, %s\n", elf_errmsg(-1));
86
                exit(EXIT_FAILURE);
87
        }
88
 
89
        ek = elf_kind(e);
90
        if (ek == ELF_K_ELF) {
91
                ; // This is the kind of file we should expect
92
        } else if (ek == ELF_K_AR) {
93
                fprintf(stderr, "Cannot run an archive!\n");
94
                exit(EXIT_FAILURE);
95
        } else if (ek == ELF_K_NONE) {
96
                ;
97
        } else {
98
                fprintf(stderr, "Unexpected ELF file kind!\n");
99
                exit(EXIT_FAILURE);
100
        }
101
 
102
        if (gelf_getehdr(e, &ehdr) == NULL) {
103
                fprintf(stderr, "getehdr() failed: %s\n", elf_errmsg(-1));
104
                exit(EXIT_FAILURE);
105
        } if ((i=gelf_getclass(e)) == ELFCLASSNONE) {
106
                fprintf(stderr, "getclass() failed: %s\n", elf_errmsg(-1));
107
                exit(EXIT_FAILURE);
108
        } if ((id = elf_getident(e, NULL)) == NULL) {
109
                fprintf(stderr, "getident() failed: %s\n", elf_errmsg(-1));
110
                exit(EXIT_FAILURE);
111
        } if (i != ELFCLASS32) {
112
                fprintf(stderr, "This is a 64-bit ELF file, ZipCPU ELF files are all 32-bit\n");
113
                exit(EXIT_FAILURE);
114
        }
115
 
116
        if (dbg) {
117
        printf("    %-20s 0x%jx\n", "e_type", (uintmax_t)ehdr.e_type);
118
        printf("    %-20s 0x%jx\n", "e_machine", (uintmax_t)ehdr.e_machine);
119
        printf("    %-20s 0x%jx\n", "e_version", (uintmax_t)ehdr.e_version);
120
        printf("    %-20s 0x%jx\n", "e_entry", (uintmax_t)ehdr.e_entry);
121
        printf("    %-20s 0x%jx\n", "e_phoff", (uintmax_t)ehdr.e_phoff);
122
        printf("    %-20s 0x%jx\n", "e_shoff", (uintmax_t)ehdr.e_shoff);
123
        printf("    %-20s 0x%jx\n", "e_flags", (uintmax_t)ehdr.e_flags);
124
        printf("    %-20s 0x%jx\n", "e_ehsize", (uintmax_t)ehdr.e_ehsize);
125
        printf("    %-20s 0x%jx\n", "e_phentsize", (uintmax_t)ehdr.e_phentsize);
126
        printf("    %-20s 0x%jx\n", "e_shentsize", (uintmax_t)ehdr.e_shentsize);
127
        printf("\n");
128
        }
129
 
130
 
131
        // Check whether or not this is an ELF file for the ZipCPU ...
132
        if (ehdr.e_machine != 0x0dad1) {
133
                fprintf(stderr, "This is not a ZipCPU/8 ELF file\n");
134
                exit(EXIT_FAILURE);
135
        }
136
 
137
        // Get our entry address
138
        entry = ehdr.e_entry;
139
 
140
 
141
        // Now, let's go look at the program header
142
        if (elf_getphdrnum(e, &n) != 0) {
143
                fprintf(stderr, "elf_getphdrnum() failed: %s\n", elf_errmsg(-1));
144
                exit(EXIT_FAILURE);
145
        }
146
 
147
assert(n != 0);
148
 
149
        unsigned total_octets = 0, current_offset=0, current_section=0;
150
        for(i=0; i<(int)n; i++) {
151
                total_octets += sizeof(ELFSECTION *)+sizeof(ELFSECTION);
152
 
153
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
154
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
155
                        exit(EXIT_FAILURE);
156
                }
157
 
158
                if (dbg) {
159
                printf("    %-20s 0x%x\n", "p_type",   phdr.p_type);
160
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
161
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
162
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
163
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
164
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
165
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
166
 
167
                if (phdr.p_flags & PF_X)        printf(" Execute");
168
                if (phdr.p_flags & PF_R)        printf(" Read");
169
                if (phdr.p_flags & PF_W)        printf(" Write");
170
                printf("]\n");
171
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
172
                }
173
 
174
                total_octets += phdr.p_memsz;
175
        }
176
 
177
        char    *d = (char *)malloc(total_octets + sizeof(ELFSECTION)+sizeof(ELFSECTION *));
178
        memset(d, 0, total_octets);
179
 
180
        ELFSECTION **r = sections = (ELFSECTION **)d;
181
        current_offset = (n+1)*sizeof(ELFSECTION *);
182
        current_section = 0;
183
 
184
        for(i=0; i<(int)n; i++) {
185
                r[i] = (ELFSECTION *)(&d[current_offset]);
186
 
187
                if (gelf_getphdr(e, i, &phdr) != &phdr) {
188
                        fprintf(stderr, "getphdr() failed: %s\n", elf_errmsg(-1));
189
                        exit(EXIT_FAILURE);
190
                }
191
 
192
                if (dbg) {
193
                printf("    %-20s 0x%jx\n", "p_offset", phdr.p_offset);
194
                printf("    %-20s 0x%jx\n", "p_vaddr",  phdr.p_vaddr);
195
                printf("    %-20s 0x%jx\n", "p_paddr",  phdr.p_paddr);
196
                printf("    %-20s 0x%jx\n", "p_filesz", phdr.p_filesz);
197
                printf("    %-20s 0x%jx\n", "p_memsz",  phdr.p_memsz);
198
                printf("    %-20s 0x%x [", "p_flags",  phdr.p_flags);
199
 
200
                if (phdr.p_flags & PF_X)        printf(" Execute");
201
                if (phdr.p_flags & PF_R)        printf(" Read");
202
                if (phdr.p_flags & PF_W)        printf(" Write");
203
                printf("]\n");
204
 
205
                printf("    %-20s 0x%jx\n", "p_align", phdr.p_align);
206
                }
207
 
208
                current_section++;
209
 
210
                r[i]->m_start = phdr.p_paddr;
211
                r[i]->m_len   = phdr.p_filesz;
212
 
213
                current_offset += phdr.p_memsz + sizeof(ELFSECTION);
214
 
215
                // Now, let's read in our section ...
216
                if (lseek(fd, phdr.p_offset, SEEK_SET) < 0) {
217
                        fprintf(stderr, "Could not seek to file position %08lx\n", phdr.p_offset);
218
                        perror("O/S Err:");
219
                        exit(EXIT_FAILURE);
220
                } if (phdr.p_filesz > phdr.p_memsz)
221
                        phdr.p_filesz = 0;
222
                if (read(fd, r[i]->m_data, phdr.p_filesz) != (int)phdr.p_filesz) {
223
                        fprintf(stderr, "Didnt read entire section\n");
224
                        perror("O/S Err:");
225
                        exit(EXIT_FAILURE);
226
                }
227
 
228
                /*
229
                // Next, we need to byte swap it from big to little endian
230
                for(unsigned j=0; j<r[i]->m_len; j++)
231
                        r[i]->m_data[j] = byteswap(r[i]->m_data[j]);
232
                */
233
 
234
                if (dbg) for(unsigned j=0; j<r[i]->m_len; j++)
235
                        fprintf(stderr, "ADR[%04x] = %02x\n", r[i]->m_start+j,
236
                                r[i]->m_data[j] & 0x0ff);
237
        }
238
 
239
        r[i] = (ELFSECTION *)(&d[current_offset]);
240
        r[current_section]->m_start = 0;
241
        r[current_section]->m_len   = 0;
242
 
243
        elf_end(e);
244
        close(fd);
245
}
246
 

powered by: WebSVN 2.1.0

© copyright 1999-2022 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.