20 |
|
|
21 |
#include "loader.h" |
#include "loader.h" |
22 |
#include "exception.h" |
#include "exception.h" |
23 |
|
#include <fstream> |
24 |
|
|
25 |
/* |
/* |
26 |
* binary format |
* executable format |
27 |
* |
* |
28 |
SMARC<ver[1bajt]> |
SMARC<ver[1bajt]> |
29 |
DATA<dlen[4bajty]><data section [dlen bajtow]> |
DATA<dlen[4bajty]><data section [dlen bajtow]> |
30 |
INSTR<ilen[4bajty]><instrsection[ilen bajtow]> |
INSTR<ilen[4bajty]><instrsection[ilen bajtow]> |
31 |
|
|
32 |
|
* dlen % 4 == 0 && ilen % 4 == 0 |
33 |
|
* |
34 |
|
* ver - current version of format [ascii digit] |
35 |
|
* |
36 |
|
* numbers in big-endian |
37 |
|
* |
38 |
|
* current [and only] version of executable is 0 |
39 |
*/ |
*/ |
40 |
|
|
41 |
|
unsigned long loader::to_little_endian( unsigned char *a ) |
42 |
|
{ |
43 |
|
unsigned long val; |
44 |
|
|
45 |
|
val = a[0]; |
46 |
|
val <<= 8; |
47 |
|
|
48 |
|
val |= a[1]; |
49 |
|
val <<= 8; |
50 |
|
|
51 |
|
val |= a[2]; |
52 |
|
val <<= 8; |
53 |
|
|
54 |
|
val |= a[3]; |
55 |
|
|
56 |
|
return val; |
57 |
|
} |
58 |
void loader::load_file( string fname ) |
void loader::load_file( string fname ) |
59 |
{ |
{ |
60 |
ifstream file; |
ifstream file; |
64 |
throw ex<loader>("loader::load_file: Can't open file. " |
throw ex<loader>("loader::load_file: Can't open file. " |
65 |
"Doesn't exists or permission denied."); |
"Doesn't exists or permission denied."); |
66 |
|
|
67 |
|
char buf[20]; |
68 |
|
unsigned char word[4]; |
69 |
|
|
70 |
|
file.read(buf, 5); |
71 |
|
if ( file.bad() || strncmp( buf, "SMARC", 5 ) != 0 ) |
72 |
|
throw ex<loader>("loader::load_file: " |
73 |
|
"Executable format is invalid. " |
74 |
|
"Couldn't find SMARC tag."); |
75 |
|
|
76 |
|
file.read(buf, 1); |
77 |
|
if ( file.bad() || buf[0] != '0' ) |
78 |
|
throw ex<loader>("loader::load_file: " |
79 |
|
"Unknown SmArc executable format version."); |
80 |
|
|
81 |
|
file.read(buf, 4); |
82 |
|
if ( file.bad() || strncmp( buf, "DATA", 4 ) != 0 ) |
83 |
|
throw ex<loader>("loader::load_file: " |
84 |
|
"Executable format is invalid. " |
85 |
|
"Couldn't find DATA section tag."); |
86 |
|
|
87 |
|
file.read(word, 4); |
88 |
|
unsigned long dlen = to_little_endian( word ); |
89 |
|
cout<<"DATALEN = "<<dlen<<endl; |
90 |
|
cout.form("%08X\n",dlen); |
91 |
|
|
92 |
|
for( unsigned int i = 0 ; i < dlen ; i+= 4 ){ |
93 |
|
unsigned long val; |
94 |
|
|
95 |
|
file.read(word, 4); |
96 |
|
|
97 |
|
if ( file.eof() ) |
98 |
|
throw ex<loader>("loader::load_file: " |
99 |
|
"Executable format is invalid. " |
100 |
|
"Unexpected end of file in DATA section."); |
101 |
|
|
102 |
|
val = to_little_endian( word ); |
103 |
|
data.push_back( val ); |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
file.read(buf, 5); |
108 |
|
if ( file.bad() || strncmp( buf, "INSTR", 5 ) != 0 ) |
109 |
|
throw ex<loader>("loader::load_file: " |
110 |
|
"Executable format is invalid. " |
111 |
|
"Couldn't find INSTR section tag."); |
112 |
|
|
113 |
|
file.read(word, 4); |
114 |
|
unsigned long ilen = to_little_endian( word ); |
115 |
|
cout<<"ILEN = "<<ilen<<endl; |
116 |
|
|
117 |
|
for( unsigned int i = 0 ; i < ilen ; i+= 4 ){ |
118 |
|
unsigned long val; |
119 |
|
|
120 |
|
file.read(word, 4); |
121 |
|
|
122 |
|
if ( file.eof() ) |
123 |
|
throw ex<loader>("loader::load_file: " |
124 |
|
"Executable format is invalid. " |
125 |
|
"Unexpected end of file in INSTR section."); |
126 |
|
|
127 |
|
val = to_little_endian( word ); |
128 |
|
instr.push_back( val ); |
129 |
|
} |
130 |
|
|
131 |
loaded = true; |
loaded = true; |
132 |
} |
} |