Reading the ELF file
Posted: Tue Apr 12, 2011 3:07 am
Hello , everyone .
I'm have developed a loader (a full proof code , no errors) .But now I have to develop a stub function which fills in the segment tables etc information for the loader to load the image . The test code is simple
And this is what I do in the stub function
That is , I just copy up the segments from the elf file into these structures . And that's all to do , linking (dynamic linking etc ) will all be handled by the linker
The code prints the global string fine . But it does not print up the local string . Is there something I'm missing ?? Or is it the loader ??
debugging gave me that one of the segment has type as "segment type 1685382481 offset 0" . This is not a valid segment either .
I'm have developed a loader (a full proof code , no errors) .But now I have to develop a stub function which fills in the segment tables etc information for the loader to load the image . The test code is simple
Code: Select all
void ELF_Print(char* msg);
char s1[40] = "Hi ! This is the first string\n";
int main(int argc, char** argv)
{
char s2[40] = "Hi ! This is the second string\n";
ELF_Print(s1);
ELF_Print(s2);
return 0;
}
Code: Select all
int Parse_ELF_Executable(
char *exeFileData,
ulong_t exeFileLength,
struct Exe_Format *exeFormat)
{
//var declaration
int i;
//map an elfHeader to the exeFileData
elfHeader* eh = (elfHeader*) exeFileData;
//check the elf-identification
if(eh->ident[0] != 0x7F &&
eh->ident[1] != 'E' &&
eh->ident[2] != 'L' &&
eh->ident[3] != 'F')
{
//TODO is it necessary, that the other bytes are checked too?
Print("\nnot an executable file\n");
return ENOEXEC;
}
if(eh->ident[4] == 0 ||
eh->ident[4] == 2)
{
Print("\ninvalid class or 64bit object\n");
return ENOEXEC;
}
//check header for correct data
/*if(eh->type != 2)
{ //executable type - first we only implement exec files
Print("\nunimplemented executable type: %d\n", eh->type);
return ENOEXEC;
}*/
/*if(eh->machine != 2)
{ //machine type - we implement this on x86 machines
Print("\nunsupported machine type: %d\n", eh->machine);
return ENOEXEC;
}*/
//fill in data to exeFormat
//- set nr. of exeSegments
exeFormat->numSegments = eh->phnum;
//- set the entry address
exeFormat->entryAddr = eh->entry;
//- get exeSegments
if(eh->phoff == 0)
{
Print("\nno program header available\n");
return ENOEXEC;
}
int nextProgramHeaderOffset = (eh->phoff);
for(i=0; i<eh->phnum; i++)
{
if(i >= EXE_MAX_SEGMENTS)
{ //the number of segments must be less or equals the maximum numbers set for the exeFormat
Print("\nmaximum number of segments exceeded\n");
return ENOEXEC;
}
//map the programHeader to the exeFileData
programHeader* ph = (programHeader*) (exeFileData+nextProgramHeaderOffset);
//fill in the sections in the exeFormat
(exeFormat->segmentList[i]).offsetInFile = ph->offset;
(exeFormat->segmentList[i]).lengthInFile = ph->fileSize;
(exeFormat->segmentList[i]).startAddress = ph->vaddr;
(exeFormat->segmentList[i]).sizeInMemory = ph->memSize;
(exeFormat->segmentList[i]).protFlags = ph->flags;
//get the address of the next programHeader
nextProgramHeaderOffset += eh->phentsize;
}
return 0;
}
The code prints the global string fine . But it does not print up the local string . Is there something I'm missing ?? Or is it the loader ??
debugging gave me that one of the segment has type as "segment type 1685382481 offset 0" . This is not a valid segment either .