library/src/ELF/ELF32/BP-ELFExtraction.cc
// returns a section list of all sections in a file given
// a valid elf header.
returns a section list of all sections in a file given
a valid elf header.
returns a section list of all sections in a file given a valid elf header .
P_ELF_SECTION_LIST_32BIT ELF32_ExtractSectionHeaders_File(P_ELF_32BIT_LOOKUP_TABLE fullHeader, FILE *elfFile){
	P_ELF_HDR_32BIT elfHdr = fullHeader->header;
	if(elfHdr == NULL)
		return NULL;
	if(elfFile == NULL)
		return NULL;
	rewind(elfFile);
	if(fseek(elfFile, elfHdr->elfSectionHeaderOffset, SEEK_SET) == -1)
		return NULL;
	unsigned short sectionCount = elfHdr->elfNumberSHeaderEntries;
	if(sectionCount < 1)
		return NULL;
	P_ELF_SECTION_LIST_32BIT sectionList;
	if(sectionCount > 0){
		sectionList = ELF32_NewSectionList();
		if(sectionList != NULL)
			memset(sectionList, 0x00, sizeof(ELF_SECTION_LIST_32BIT));
		sectionList->first = sectionList;
		sectionList->next = NULL;
		sectionList->prev = NULL;
	} else {
		return NULL;
	}
	size_t stringTableIndex = elfHdr->elfSHeaderStringTableIndex;
	size_t i = 0;
	for(;sectionCount != 0; sectionCount--){
		sectionList->section = ELF32_ExtractSectionHeader_File(fullHeader, elfFile);
		if( (sectionCount - 1) == 0)
			break;
		sectionList->next = ELF32_NewSectionList();
			sectionList->next->prev  = sectionList;
			sectionList->next->first = sectionList->first;
			sectionList = sectionList->next;
			sectionList->next = NULL;
	}
	i = 0;
	size_t sTableOffset = 0;
	P_ELF_SECTION_LIST_32BIT firstPtr = sectionList->first;
	for(sectionList = sectionList->first; sectionList != NULL; sectionList = sectionList->next){
		if(sectionList->section == NULL)
			continue;
		if( i == stringTableIndex){
			fullHeader->stringTable = (char *) bpmalloc(sectionList->section->size+10);
				memset(fullHeader->stringTable, 0x00, sectionList->section->size+10);
			fullHeader->stringTableSize = sectionList->section->size;
			sTableOffset = sectionList->section->offset;
			rewind(elfFile);
			fseek(elfFile, sTableOffset, SEEK_SET);
			fread(fullHeader->stringTable, fullHeader->stringTableSize, 1, elfFile);
			break;
		}
		i++;
	}
	sectionList = firstPtr;
	for(sectionList = sectionList->first; sectionList != NULL; sectionList = sectionList->next){
		if(sectionList->section == NULL)
			continue;
		if(sectionList->section->name > 0){
			sectionList->name = bpstrndup(&fullHeader->stringTable[sectionList->section->name], ELF32_MAX_NAME_LENGTH);
		} else
			sectionList->name = "NULL";
	}
	sectionList = firstPtr;
	return sectionList;
}
P_ELF_SECTION_LIST_32BIT ELF32_ExtractSectionHeaders_File ( P_ELF_32BIT_LOOKUP_TABLE fullHeader , FILE * elfFile ) { P_ELF_HDR_32BIT elfHdr = fullHeader -> header ; if ( elfHdr == NULL ) return NULL ; if ( elfFile == NULL ) return NULL ; rewind ( elfFile ) ; if ( fseek ( elfFile , elfHdr -> elfSectionHeaderOffset , SEEK_SET ) == -1 ) return NULL ; unsigned short sectionCount = elfHdr -> elfNumberSHeaderEntries ; if ( sectionCount < 1 ) return NULL ; P_ELF_SECTION_LIST_32BIT sectionList ; if ( sectionCount > 0 ) { sectionList = ELF32_NewSectionList ( ) ; if ( sectionList != NULL ) memset ( sectionList , 0x00 , sizeof ( ELF_SECTION_LIST_32BIT ) ) ; sectionList -> first = sectionList ; sectionList -> next = NULL ; sectionList -> prev = NULL ; } else { return NULL ; } size_t stringTableIndex = elfHdr -> elfSHeaderStringTableIndex ; size_t i = 0 ; for ( ; sectionCount != 0 ; sectionCount -- ) { sectionList -> section = ELF32_ExtractSectionHeader_File ( fullHeader , elfFile ) ; if ( ( sectionCount - 1 ) == 0 ) break ; sectionList -> next = ELF32_NewSectionList ( ) ; sectionList -> next -> prev = sectionList ; sectionList -> next -> first = sectionList -> first ; sectionList = sectionList -> next ; sectionList -> next = NULL ; } i = 0 ; size_t sTableOffset = 0 ; P_ELF_SECTION_LIST_32BIT firstPtr = sectionList -> first ; for ( sectionList = sectionList -> first ; sectionList != NULL ; sectionList = sectionList -> next ) { if ( sectionList -> section == NULL ) continue ; if ( i == stringTableIndex ) { fullHeader -> stringTable = ( char * ) bpmalloc ( sectionList -> section -> size + 10 ) ; memset ( fullHeader -> stringTable , 0x00 , sectionList -> section -> size + 10 ) ; fullHeader -> stringTableSize = sectionList -> section -> size ; sTableOffset = sectionList -> section -> offset ; rewind ( elfFile ) ; fseek ( elfFile , sTableOffset , SEEK_SET ) ; fread ( fullHeader -> stringTable , fullHeader -> stringTableSize , 1 , elfFile ) ; break ; } i ++ ; } sectionList = firstPtr ; for ( sectionList = sectionList -> first ; sectionList != NULL ; sectionList = sectionList -> next ) { if ( sectionList -> section == NULL ) continue ; if ( sectionList -> section -> name > 0 ) { sectionList -> name = bpstrndup ( & fullHeader -> stringTable [ sectionList -> section -> name ] , ELF32_MAX_NAME_LENGTH ) ; } else sectionList -> name = " " ; } sectionList = firstPtr ; return sectionList ; }
returns a section list of all sections in a file given
a valid elf header.
returns a section list of all sections in a file given a valid elf header .
// rewind elf file // Retrieve Section Count // create the first section list // Create offset to string table // Section Header offset + Section String Table Offset // attempt to extract sectionCount number of section headers from // the binary // Extract Actual Section Info // perform next assignments //------------------------------------------------------------- // Find and store the string table // Save first pointer before loop // arbitrary continue on a null section // Condition Reached: String Table Index Found // Full Header String Table // Extract table from file // Now that the string table has been retrieved, assign each section its appropriate // name. // arbitrary continue on null section // reset pointer and return