library/src/ELF/ELF32/BP-ELFExtraction_mem.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_Mem(P_ELF_32BIT_LOOKUP_TABLE fullHeader, P_DEBUG_HANDLE dbgHandle, P_MEMORY_MAP memMap){
	P_ELF_HDR_32BIT elfHdr = fullHeader->header;
	P_ELF_SECTION_HDR_32BIT testVal = NULL;
	if(elfHdr == NULL)
		return NULL;
	size_t sOffset = (size_t) elfHdr->elfSectionHeaderOffset + (unsigned int) memMap->lowAddr;
	unsigned short sectionCount = elfHdr->elfNumberSHeaderEntries;
	if(sectionCount < 1)
		return NULL;
	P_ELF_SECTION_LIST_32BIT sectionList = NULL;
	if(sectionCount > 0){
		sectionList = ELF32_NewSectionList();
		if(sectionList != NULL){
			memset(sectionList, 0x00, sizeof(ELF_SECTION_LIST_32BIT));
		} else {
			return NULL;
		}
		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--){
		testVal = ELF32_ExtractSectionHeader_Mem(fullHeader, sOffset, dbgHandle, memMap);
		if(testVal == NULL){
			sectionList = NULL;
			break;
		} else {
			sectionList->section = testVal;
		}
		if(sectionList->section != NULL){
			sOffset += sizeof(ELF_SECTION_HDR_32BIT);
		}
		if( (sectionCount - 1) == 0)
			break;
		sectionList->next = ELF32_NewSectionList();
			sectionList->next->prev  = sectionList;
			sectionList->next->first = sectionList->first;
			sectionList = sectionList->next;
			sectionList->next = NULL;
	}
	if(sectionList != NULL){
		sectionList = sectionList->first;
	}
	i = 0;
	size_t sTableOffset = 0;
	P_ELF_SECTION_LIST_32BIT firstPtr = NULL;
	if(sectionList != NULL){
		firstPtr = sectionList->first;
		for(sectionList = sectionList->first; sectionList != NULL; sectionList = sectionList->next){
			if( i == stringTableIndex && sectionList != NULL){
				fullHeader->stringTableSize = sectionList->section->size;
				sTableOffset = sectionList->section->offset + (size_t) memMap->lowAddr;
				fullHeader->stringTable = (char *) DebugReadMemory(dbgHandle, (void *)  sTableOffset, fullHeader->stringTableSize);
				break;
			}
			i++;
		}
	}
	if(sectionList != NULL && firstPtr != NULL){
		sectionList = firstPtr;
		for(sectionList = sectionList->first; sectionList != NULL; sectionList = sectionList->next){
			if(sectionList->section->name > 0){
				sectionList->name = bpstrndup(&fullHeader->stringTable[sectionList->section->name], ELF32_MAX_NAME_LENGTH);
			} else
				sectionList->name = "NULL";
		}
	}
	if(firstPtr != NULL){
		sectionList = firstPtr;
	}
	return sectionList;
}
P_ELF_SECTION_LIST_32BIT ELF32_ExtractSectionHeaders_Mem ( P_ELF_32BIT_LOOKUP_TABLE fullHeader , P_DEBUG_HANDLE dbgHandle , P_MEMORY_MAP memMap ) { P_ELF_HDR_32BIT elfHdr = fullHeader -> header ; P_ELF_SECTION_HDR_32BIT testVal = NULL ; if ( elfHdr == NULL ) return NULL ; size_t sOffset = ( size_t ) elfHdr -> elfSectionHeaderOffset + ( unsigned int ) memMap -> lowAddr ; unsigned short sectionCount = elfHdr -> elfNumberSHeaderEntries ; if ( sectionCount < 1 ) return NULL ; P_ELF_SECTION_LIST_32BIT sectionList = NULL ; if ( sectionCount > 0 ) { sectionList = ELF32_NewSectionList ( ) ; if ( sectionList != NULL ) { memset ( sectionList , 0x00 , sizeof ( ELF_SECTION_LIST_32BIT ) ) ; } else { return NULL ; } 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 -- ) { testVal = ELF32_ExtractSectionHeader_Mem ( fullHeader , sOffset , dbgHandle , memMap ) ; if ( testVal == NULL ) { sectionList = NULL ; break ; } else { sectionList -> section = testVal ; } if ( sectionList -> section != NULL ) { sOffset += sizeof ( ELF_SECTION_HDR_32BIT ) ; } if ( ( sectionCount - 1 ) == 0 ) break ; sectionList -> next = ELF32_NewSectionList ( ) ; sectionList -> next -> prev = sectionList ; sectionList -> next -> first = sectionList -> first ; sectionList = sectionList -> next ; sectionList -> next = NULL ; } if ( sectionList != NULL ) { sectionList = sectionList -> first ; } i = 0 ; size_t sTableOffset = 0 ; P_ELF_SECTION_LIST_32BIT firstPtr = NULL ; if ( sectionList != NULL ) { firstPtr = sectionList -> first ; for ( sectionList = sectionList -> first ; sectionList != NULL ; sectionList = sectionList -> next ) { if ( i == stringTableIndex && sectionList != NULL ) { fullHeader -> stringTableSize = sectionList -> section -> size ; sTableOffset = sectionList -> section -> offset + ( size_t ) memMap -> lowAddr ; fullHeader -> stringTable = ( char * ) DebugReadMemory ( dbgHandle , ( void * ) sTableOffset , fullHeader -> stringTableSize ) ; break ; } i ++ ; } } if ( sectionList != NULL && firstPtr != NULL ) { sectionList = firstPtr ; for ( sectionList = sectionList -> first ; sectionList != NULL ; sectionList = sectionList -> next ) { if ( sectionList -> section -> name > 0 ) { sectionList -> name = bpstrndup ( & fullHeader -> stringTable [ sectionList -> section -> name ] , ELF32_MAX_NAME_LENGTH ) ; } else sectionList -> name = " " ; } } if ( firstPtr != NULL ) { 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 .
// Section Header Offset, passed to sub function to extract headers // 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 // printf("\n %x %i WTFn", sectionList->section->offset, sectionCount); // perform next assignments // Reset position //------------------------------------------------------------- // Find and store the string table // Save first pointer before loop // Condition Reached: String Table Index Found // Extract table from file // rewind(elfFile); //fseek(elfFile, sTableOffset, SEEK_SET); // fread(fullHeader->stringTable, fullHeader->stringTableSize, 1, elfFile); // printf("\n WTF: %p, %p, %p", dbgHandle, (void *)  sTableOffset, fullHeader->stringTableSize); // Now that the string table has been retrieved, assign each section its appropriate // name.