-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf.cpp
201 lines (159 loc) · 6.17 KB
/
elf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "cpu.h"
#include "mem.h"
#include "elf.h"
extern Cpu cpu;
extern Mem mem;
#define ELF_DEBUG 0
// TODO: stop re-inventing the wheel and use libELF
int loadElf(char *filename) {
// try to open the file in read-only/binary mode
FILE* ElfFile = fopen(filename, "rb");
if (ElfFile == NULL) {
std::cerr << "Can't open file " << filename << std::endl;
return 3001;
}
// read the ELF header
Elf32Header header;
if (fread(&header, 1, sizeof(header), ElfFile) != sizeof(header)) {
std::cerr << "Could not load the ELF header (file too small ?)\n";
return 3002;
}
// check the magic #
if (header.e_ident[0] != 0x7F || header.e_ident[1] != 'E'
|| header.e_ident[2] != 'L' || header.e_ident[3] != 'F') {
std::cerr << "Not an ELF file\n";
return 3003;
}
// check class
if (header.e_ident[4] != 1) {
std::cerr << "Not a 32 bits ELF file\n";
return 3004;
}
// check our ELF header struct size matches the one in the ELF file
if (header.e_ehsize != sizeof(header)) {
std::cerr << "ELF header sizes inconsistency\n";
return 3005;
}
// check enfianness
if (header.e_ident[5] != 1) {
std::cerr << "Not a Little Endian encoded binary\n";
return 3006;
}
// check type of binary
if ((header.e_type == 0) || (header.e_type > 3)) {
std::cerr << "Not an executable ELF file\n";
return 3007;
}
// check architecture
if (header.e_machine != 0xF3) {
std::cerr << "Not a RISC-V ELF file\n";
return 3008;
}
// header looks fine so far... let's read the program table entries
// read the program headers : we expect only one - but might be more
Elf32ProgramHeader pHeader;
// check we have at least one .text segment (program)
if (header.e_phoff == 0) {
std::cerr << "No executable found in ELF file " << filename << std::endl;
return 3009;
}
// check program header size against the ELF file header
if (header.e_phentsize != sizeof(pHeader)) {
std::cerr << "Segment header sizes inconsistency\n";
return 3010;
}
// now, we know there are "header.e_phnum" segments of "header.e_phentsize" bytes each
// move the file cursor into the first segment header
if (header.e_phoff != header.e_ehsize) {
if (ELF_DEBUG) std::cout << "Moving to the first entry in the segment table\n";
if (fseek(ElfFile, (long)(header.e_phoff - header.e_ehsize), SEEK_CUR) != 0) {
std::cerr << "Can't seek into segment table position\n";
return 3011;
}
}
long nextPHeaderPosition = {0};
if (ELF_DEBUG) std::cout << "Number of segments : " << header.e_phnum << std::endl;
// lets read all of them into memory (we expect only one but might be more ...)
for (int segmentNum = 0; segmentNum < header.e_phnum; segmentNum++) {
nextPHeaderPosition = 0;
if (fread(&pHeader, 1, sizeof(pHeader), ElfFile) != sizeof(pHeader)) {
std::cerr << "Error while reading Program Header # " << segmentNum << std::endl;
return 3012;
}
// check segment type
if (pHeader.type != 1){
if (ELF_DEBUG) std::cout << "This segment is not a valid load\n";
// return 3013;
continue; // move to next segment
}
// if the segment is not right after this program header
if (pHeader.offset != 0) {
// save the file position, for the next segment header
nextPHeaderPosition = ftell(ElfFile);
if (nextPHeaderPosition < 0) {
std::cerr << "Can't save cursor position in file " << filename << std::endl;
return 3014;
}
// and move up to the segment itsELF
if (fseek(ElfFile, pHeader.offset, SEEK_SET) != 0) {
std::cerr << "Can't seek into segment #" << segmentNum << std::endl;
return 3015;
}
}
// display some information about the segment we are about to load
if (ELF_DEBUG) {
std::cout << std::hex;
std::cout << "\nLoading segment : 0x" << segmentNum << std::endl;
std::cout << "Flags : 0x" << pHeader.flags << std::endl;
std::cout << "Size in file : 0x" << pHeader.filesz << std::endl;
std::cout << "Size in memory : 0x" << pHeader.memsz << std::endl;
std::cout << std::setfill('0') << std::setw(8);
std::cout << "Physical address : 0x" << pHeader.paddr << std::endl;
std::cout << "Virtual address : 0x" << pHeader.vaddr << std::endl;
}
// check we have enough memory
if (pHeader.memsz > RAMSIZE) {
std::cerr << "Can't fit segment #" << segmentNum << " into memory\n";
return 3016;
}
if (segmentNum == 0) {
// initialize virtual address
mem.setRamStartAddress(pHeader.paddr);
if (ELF_DEBUG) std::cout << "start of memory : 0x" << mem.getRamStartAddress() << std::endl;
// initialise program counter
cpu.setPC(header.e_entry);
// cpu.setPC(pHeader.paddr);
if (ELF_DEBUG) std::cout << "Entry point : 0x" << cpu.getPC() << std::endl;
// initialise stack pointer
cpu.setReg(2, mem.getRamStartAddress() + RAMSIZE - 1); // set SP to last address in RAM
if (ELF_DEBUG) std::cout << "Stack Pointer : 0x" << cpu.getReg(2) << std::endl;
}
// read the segment into memory
if (fread(mem.ram8 + (int)(pHeader.paddr - mem.getRamStartAddress()), 1, pHeader.filesz, ElfFile) != pHeader.filesz) {
std::cerr << "Can't load segment #" << segmentNum << "into RAM\n";
return 3017;
}
// reset the un-initialized data into memory (arrays, etc...)
for (uint32_t i = 0; i < (pHeader.memsz - pHeader.filesz); i++) {
mem.set8(pHeader.filesz + i , 0);
}
// restore the file cursor for the next segment header
if (nextPHeaderPosition != 0) {
if (fseek(ElfFile, nextPHeaderPosition, SEEK_SET) != 0) {
std::cerr << "can't restore cursor position in file " << filename << std::endl;
return 3018;
}
}
// dump the memory
if (ELF_DEBUG) {
for (uint32_t address = pHeader.paddr; address < pHeader.paddr+pHeader.memsz; address += 4) {
std::cout << std::setfill('0') << std::setw(8) << address << " ";
std::cout << std::setfill('0') << std::setw(8) << mem.get32(address) << std::endl;
}
}
}
return 0;
}