d2elf.cxx
9.5 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <string.h>
#include <fstream.h>
#include <elf.h>
#include <stdlib.h>
#include "rsp2elf.h"
/*
Writes the following ELF file:
ELFheader
Sect 0 null
Sect 1 data
Sect 2 stringtab
Sect 3 symtab
Sect 4 secthdr stringtab
Section Headers
*/
char * tag;
ofstream ELFfile;
char * outFileName;
int outFileOpen;
int dataSectionSize;
int stringTableSize;
long stringTableOffset, symbolTableOffset, shStringTableOffset,
sectionTableOffset, dataSectionOffset;
int dataName, stringName, symtName, shstName, sectStrings, symStrings,
symbolTableSize;
void usage( void )
{
cerr << "usage: d2elf -t tag [-o outfilename] filename..." << endl;
exit( 1 );
}
void nospace( void )
{
cerr << "out of memory" << endl;
exit( 1 );
}
/************************************************************************
Function: WriteELFHeader
Args: none
Type: void
Purpose: This function writes the ELF header at the very beginning of
the ELF file. Some of the information is not yet available
since this gets called very early in the program. Those variables
are fixed up later when their values become known.
************************************************************************/
void WriteELFHeader( void )
{
register int i;
Elf32_Ehdr header;
header.e_ident[0] = 0x7f;
strcpy( (char *) &header.e_ident[1], "ELF" );
header.e_ident[4] = 1; /* 32 bit */
header.e_ident[5] = 2; /* MS bit */
header.e_ident[6] = 1; /* version */
for ( i = 7; i < 16; ++i )
header.e_ident[i] = 0;
header.e_phoff = 0;
header.e_type = ET_REL;
header.e_phnum = 0;
header.e_phentsize = 0;
header.e_shnum = 5;
header.e_machine = EM_MIPS;
header.e_version = EV_CURRENT;
header.e_entry = 0;
header.e_shoff = 0; /* fix up later */
header.e_flags = 0x10000000; /* MIPS-2 */
header.e_ehsize = sizeof( Elf32_Ehdr );
header.e_shentsize = sizeof( Elf32_Shdr );
header.e_shstrndx = header.e_shnum - 1;
ELFfile.write( (char *) &header, sizeof( Elf32_Ehdr ) );
if ( ELFfile.fail() )
cerr << "Write failed in WriteElfHeader" << endl;
dataSectionOffset = ELFfile.tellp();
}
void ConvertFile( char * s )
{
ifstream in;
int c;
in.open( s );
if ( in.fail() )
{
cerr << "Can't open " << s << endl;
return;
}
while ( (c = in.get()) != EOF )
{
ELFfile.put( c );
++dataSectionSize;
}
in.close();
}
/************************************************************************
Function: WriteString
Args: char * s - The string to write (what else?)
Type: void
Purpose: This function writes a string to the ELF file and increments
the global stringTableSize variable. This global variable is
cleared at the start of every table. Thus, checking it before
calling WriteString always returns the index of the string in
the table. Note that this function writes a zero byte in the
file after every string. Thus calling it with the null string
("", not 0) results in writing one zero byte into the file.
************************************************************************/
void WriteString( char * s )
{
char c;
stringTableSize += (strlen( s ) + 1);
while ( c = *s++ )
ELFfile.put( c );
ELFfile.put( c ); /* a null, too */
}
/************************************************************************
Function: WriteSymbolTable
Args: none
Type: void
Purpose: This function writes the symbol table to the ELF file. The
debugger does not use this information (or so it seems), but
someone might. Also, it lets elfdump work on these files.
************************************************************************/
void WriteSymbolTable( void )
{
Elf32_Sym header;
int secondSym;
char * symName;
stringTableSize = 0;
stringTableOffset = ELFfile.tellp();
WriteString( "" );
symName = (char *) malloc( strlen( tag ) + 6 );
if ( !symName )
nospace();
strcat( strcpy( symName, tag ), "Start" );
WriteString( symName );
secondSym = stringTableSize;
strcat( strcpy( symName, tag ), "End" );
WriteString( symName );
symStrings = stringTableSize;
while ( stringTableSize & 3 )
WriteString( "" ); /* should be even */
symbolTableOffset = ELFfile.tellp();
cout << "Writing symbol table at " << hex << symbolTableOffset
<< endl;
/* Write out 0 symbol */
header.st_name = 0;
header.st_value = 0;
header.st_size = 0;
header.st_info = 0;
header.st_other = 0;
header.st_shndx = SHN_UNDEF;
ELFfile.write( (char *) &header, sizeof( Elf32_Sym ) );
if ( ELFfile.fail() )
cerr << "Write failed in WriteSymTab" << endl;
header.st_name = 1;
header.st_value = 0;
header.st_size = dataSectionSize;
header.st_info = (STB_GLOBAL << 4) + ELF32_ST_TYPE(STT_OBJECT);
header.st_shndx = 1;
ELFfile.write( (char *) &header, sizeof( Elf32_Sym ) );
if ( ELFfile.fail() )
cerr << "Write failed in WriteSymTab" << endl;
header.st_name = secondSym;
header.st_value = dataSectionSize;
header.st_size = 0;
header.st_info = (STB_GLOBAL << 4) + ELF32_ST_TYPE(STT_OBJECT);
header.st_shndx = 1;
ELFfile.write( (char *) &header, sizeof( Elf32_Sym ) );
if ( ELFfile.fail() )
cerr << "Write failed in WriteSymTab" << endl;
symbolTableSize = 3 * sizeof( Elf32_Sym );
}
/************************************************************************
Function: WriteShStringTable
Args: none
Type: void
Purpose: This function writes the string table for the section headers
to the ELF file.
************************************************************************/
void WriteShStringTab( void )
{
stringTableSize = 0;
shStringTableOffset = ELFfile.tellp();
WriteString( "" );
dataName = stringTableSize;
WriteString( ".data" );
stringName = stringTableSize;
WriteString( ".strtab" );
symtName = stringTableSize;
WriteString( ".symtab" );
shstName = stringTableSize;
WriteString( ".shstrtab" );
while ( stringTableSize & 3 )
WriteString( "" ); /* should be even */
sectStrings = stringTableSize;
}
/************************************************************************
Function: WriteSectionHeader
Args: none
Type: void
Purpose: This function writes the section header table to the ELF file.
************************************************************************/
void WriteSectionHeader( void )
{
Elf32_Shdr header;
sectionTableOffset = ELFfile.tellp();
cout << "Writing section headers at " << hex << sectionTableOffset
<< endl;
/* section 0 */
header.sh_name = 0;
header.sh_type = SHT_NULL;
header.sh_flags = 0;
header.sh_addr = 0;
header.sh_offset = 0;
header.sh_size = 0;
header.sh_link = SHN_UNDEF;
header.sh_info = 0;
header.sh_addralign = 0;
header.sh_entsize = 0;
ELFfile.write( (char *) &header, sizeof( Elf32_Shdr ) );
if ( ELFfile.fail() )
cerr << "Write failed in WriteSectHdr" << endl;
/* data section */
header.sh_name = dataName;
header.sh_type = SHT_PROGBITS;
header.sh_flags = SHF_ALLOC + SHF_WRITE;
header.sh_addr = 0;
header.sh_offset = dataSectionOffset;
header.sh_size = dataSectionSize;
header.sh_link = SHN_UNDEF;
header.sh_info = 0;
header.sh_addralign = 16;
header.sh_entsize = 0;
ELFfile.write( (char *) &header, sizeof( Elf32_Shdr ) );
if ( ELFfile.fail() )
cerr << "Write failed in WriteSectHdr" << endl;
/* string table section */
header.sh_name = stringName;
header.sh_type = SHT_STRTAB;
header.sh_flags = 0;
header.sh_addr = 0;
header.sh_offset = stringTableOffset;
header.sh_size = symStrings;
header.sh_link = SHN_UNDEF;
header.sh_info = 0;
header.sh_addralign = 0;
header.sh_entsize = 0;
ELFfile.write( (char *) &header, sizeof( Elf32_Shdr ) );
if ( ELFfile.fail() )
cerr << "Write failed in WriteSectHdr" << endl;
/* symbol table section */
header.sh_name = symtName;
header.sh_type = SHT_SYMTAB;
header.sh_flags = 0;
header.sh_addr = 0;
header.sh_offset = symbolTableOffset;
header.sh_size = symbolTableSize;
header.sh_link = 2;
header.sh_info = 1;
header.sh_addralign = 0;
header.sh_entsize = sizeof( Elf32_Sym );
ELFfile.write( (char *) &header, sizeof( Elf32_Shdr ) );
if ( ELFfile.fail() )
cerr << "Write failed in WriteSectHdr" << endl;
/* section header string table section */
header.sh_name = shstName;
header.sh_type = SHT_STRTAB;
header.sh_flags = 0;
header.sh_addr = 0;
header.sh_offset = shStringTableOffset;
header.sh_size = sectStrings;
header.sh_link = SHN_UNDEF;
header.sh_info = 0;
header.sh_addralign = 0;
header.sh_entsize = 0;
ELFfile.write( (char *) &header, sizeof( Elf32_Shdr ) );
if ( ELFfile.fail() )
cerr << "Write failed in WriteSectHdr" << endl;
}
int main( int argc, char ** argv )
{
int pad;
cout << endl << "data to ELF Converter V0.1" << endl;
if ( argc < 2 )
usage();
--argc;
++argv;
while ( argc-- )
{
if ( argv[0][0] == '-' )
if ( argv[0][1] == 't' )
{
tag = argv[1];
++argv;
--argc;
}
else if ( argv[0][1] == 'o' )
{
outFileName = argv[1];
++argv;
--argc;
}
else
usage();
else
{
if ( !tag )
usage();
if ( !outFileName )
{
outFileName = (char *) malloc( strlen( argv[0] )
+ 3 );
if ( !outFileName )
nospace();
strcat( strcpy( outFileName, argv[0] ), ".o" );
}
if ( !outFileOpen )
{
ELFfile.open( outFileName );
if ( ELFfile.fail() )
{
cerr << "Can't create " << outFileName
<< endl;
exit( 1 );
}
++outFileOpen;
WriteELFHeader();
}
ConvertFile( argv[0] );
}
++argv; // point to next file
}
pad = dataSectionSize;
while ( pad & 3 )
{
++pad;
ELFfile.put( 0 ); /* should be even */
}
WriteSymbolTable();
WriteShStringTab();
WriteSectionHeader();
/* fix up ptr to section table */
ELFfile.seekp( 32L );
ELFfile.write( (char *) §ionTableOffset, 4 );
if ( ELFfile.fail() )
cerr << "Write failed during fixup" << endl;
ELFfile.close();
return 0;
}