library/src/ELF/ELF32/BP-ELFExtraction.cc
// returns a linked list representing all of the program headers found in the
// file.
returns a linked list representing all of the program headers found in the
file.
returns a linked list representing all of the program headers found in the file .
P_ELF_PROGRAM_HEADER_LIST_32BIT ELF32_ExtractProgramHeaders_File(P_ELF_32BIT_LOOKUP_TABLE fullHeader, FILE *elfFile){
	if(fullHeader == NULL)
		return NULL;
	if(elfFile == NULL)
		return NULL;
	size_t phOffset  = fullHeader->header->elfProgramHeaderOffset;
	size_t phSize    = fullHeader->header->elfProgramHeaderSize;
	size_t phEntries = fullHeader->header->elfNumberPHeaderEntries;
	size_t phCurrentOffset = phOffset;
	rewind(elfFile);
	P_ELF_PROGRAM_HEADER_LIST_32BIT pHeaderList;
	if(phEntries > 0){
		pHeaderList = (P_ELF_PROGRAM_HEADER_LIST_32BIT) bpmalloc(sizeof(ELF_PROGRAM_HEADER_LIST_32BIT));
			memset(pHeaderList, 0x00, sizeof(ELF_PROGRAM_HEADER_LIST_32BIT));
		pHeaderList->first = pHeaderList;
		pHeaderList->header = (P_ELF_PROGRAM_HEADER_32BIT) bpmalloc(sizeof(ELF_PROGRAM_HEADER_32BIT));
			memset(pHeaderList->header, 0x00, sizeof(ELF_PROGRAM_HEADER_32BIT));
	} else {
		return NULL;
	}
	for(;phEntries > 0; phEntries--){
		fseek(elfFile, phCurrentOffset, SEEK_SET);
		fread(pHeaderList->header, 1, phSize, elfFile);
		if(phEntries - 1 == 0){
			pHeaderList->next = NULL;
			break;
		} else {
			pHeaderList->next = (P_ELF_PROGRAM_HEADER_LIST_32BIT) bpmalloc(sizeof(ELF_PROGRAM_HEADER_LIST_32BIT));
				memset(pHeaderList->next, 0x00, sizeof(ELF_PROGRAM_HEADER_LIST_32BIT));
			pHeaderList->next->header = (P_ELF_PROGRAM_HEADER_32BIT) bpmalloc(sizeof(ELF_PROGRAM_HEADER_32BIT));
				memset(pHeaderList->next->header, 0x00, sizeof(ELF_PROGRAM_HEADER_32BIT));
			pHeaderList->next->prev = pHeaderList;
			pHeaderList->next->first = pHeaderList->first;
			pHeaderList->next->next = NULL;
			pHeaderList = pHeaderList->next;
			rewind(elfFile);
		}
		phCurrentOffset += phSize;
	}
	rewind(elfFile);
	return pHeaderList->first;
}
P_ELF_PROGRAM_HEADER_LIST_32BIT ELF32_ExtractProgramHeaders_File ( P_ELF_32BIT_LOOKUP_TABLE fullHeader , FILE * elfFile ) { if ( fullHeader == NULL ) return NULL ; if ( elfFile == NULL ) return NULL ; size_t phOffset = fullHeader -> header -> elfProgramHeaderOffset ; size_t phSize = fullHeader -> header -> elfProgramHeaderSize ; size_t phEntries = fullHeader -> header -> elfNumberPHeaderEntries ; size_t phCurrentOffset = phOffset ; rewind ( elfFile ) ; P_ELF_PROGRAM_HEADER_LIST_32BIT pHeaderList ; if ( phEntries > 0 ) { pHeaderList = ( P_ELF_PROGRAM_HEADER_LIST_32BIT ) bpmalloc ( sizeof ( ELF_PROGRAM_HEADER_LIST_32BIT ) ) ; memset ( pHeaderList , 0x00 , sizeof ( ELF_PROGRAM_HEADER_LIST_32BIT ) ) ; pHeaderList -> first = pHeaderList ; pHeaderList -> header = ( P_ELF_PROGRAM_HEADER_32BIT ) bpmalloc ( sizeof ( ELF_PROGRAM_HEADER_32BIT ) ) ; memset ( pHeaderList -> header , 0x00 , sizeof ( ELF_PROGRAM_HEADER_32BIT ) ) ; } else { return NULL ; } for ( ; phEntries > 0 ; phEntries -- ) { fseek ( elfFile , phCurrentOffset , SEEK_SET ) ; fread ( pHeaderList -> header , 1 , phSize , elfFile ) ; if ( phEntries - 1 == 0 ) { pHeaderList -> next = NULL ; break ; } else { pHeaderList -> next = ( P_ELF_PROGRAM_HEADER_LIST_32BIT ) bpmalloc ( sizeof ( ELF_PROGRAM_HEADER_LIST_32BIT ) ) ; memset ( pHeaderList -> next , 0x00 , sizeof ( ELF_PROGRAM_HEADER_LIST_32BIT ) ) ; pHeaderList -> next -> header = ( P_ELF_PROGRAM_HEADER_32BIT ) bpmalloc ( sizeof ( ELF_PROGRAM_HEADER_32BIT ) ) ; memset ( pHeaderList -> next -> header , 0x00 , sizeof ( ELF_PROGRAM_HEADER_32BIT ) ) ; pHeaderList -> next -> prev = pHeaderList ; pHeaderList -> next -> first = pHeaderList -> first ; pHeaderList -> next -> next = NULL ; pHeaderList = pHeaderList -> next ; rewind ( elfFile ) ; } phCurrentOffset += phSize ; } rewind ( elfFile ) ; return pHeaderList -> first ; }
returns a linked list representing all of the program headers found in the
file.
returns a linked list representing all of the program headers found in the file .
// Rewind to assure position // create first structure // Create first element // Set first pointer, other pointers are null by memset // Create space for actual header // return Null if there are no program header entries // Loop through every entry, collecting and saving the structures // Seek to the current offset // Copy in header // Move list position // Create Next List // Create Next Header Structure // Reset Pointers // Move List Forward // Rewind file // Increment offset // Reset file position // Return first element