d2elf.cxx 9.5 KB
#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 *) &sectionTableOffset, 4 );
	if ( ELFfile.fail() )
        	cerr << "Write failed during fixup" << endl;
	ELFfile.close();
	return 0;
}