Example #1
0
void getElfInfoBySectionView(ElfInfo &info, const ElfHandle *handle){

	info.handle = handle;
	info.elf_base = (uint8_t *) handle->base;
	info.ehdr = reinterpret_cast<Elf32_Ehdr *>(info.elf_base);
	info.shdr = reinterpret_cast<Elf32_Shdr *>(info.elf_base + info.ehdr->e_shoff);
	info.phdr = reinterpret_cast<Elf32_Phdr *>(info.elf_base + info.ehdr->e_phoff);

	Elf32_Shdr *shstr = (Elf32_Shdr *)(info.shdr + info.ehdr->e_shstrndx);
	info.shstr = reinterpret_cast<char *>(info.elf_base + shstr->sh_offset);

	getSectionInfo(info, ".dynstr", NULL, NULL, &info.symstr);
	getSectionInfo(info, ".dynamic", &info.dynsz, NULL, &info.dyn);
	getSectionInfo(info, ".dynsym", &info.symsz, NULL, &info.sym);
	getSectionInfo(info, ".rel.dyn", &info.reldynsz, NULL, &info.reldyn);
	getSectionInfo(info, ".rel.plt", &info.relpltsz, NULL, &info.relplt);

	Elf32_Shdr *hash = findSectionByName(info, ".hash");
	if(hash){
		uint32_t *rawdata = reinterpret_cast<uint32_t *>(info.elf_base + hash->sh_offset);
		info.nbucket = rawdata[0];
		info.nchain = rawdata[1];
		info.bucket = rawdata + 2;
		info.chain = info.bucket + info.nbucket;
	}
}
Example #2
0
bool OleMainStream::readSectionsInfoTable(const char *headerBuffer, const OleEntry &tableEntry) {
	//PlcfSed structure is a section table
	unsigned int beginOfText = OleUtil::getU4Bytes(headerBuffer, 0x18); //address of text's begin in main stream
	unsigned int beginSectInfo = OleUtil::getU4Bytes(headerBuffer, 0xca); //address if PlcfSed structure

	std::size_t sectInfoLen = (std::size_t)OleUtil::getU4Bytes(headerBuffer, 0xce); //length of PlcfSed structure
	if (sectInfoLen < 4) {
		return false;
	}

	OleStream tableStream(myStorage, tableEntry, myBaseStream);
	std::string buffer;
	if (!readToBuffer(buffer, beginSectInfo, sectInfoLen, tableStream)) {
		return false;
	}

	static const unsigned int SED_SIZE = 12;
	std::size_t decriptorsCount = calcCountOfPLC(sectInfoLen, SED_SIZE);

	//saving the section offsets (in character positions)
	std::vector<unsigned int> charPos;
	for (std::size_t index = 0, tOffset = 0; index < decriptorsCount; ++index, tOffset += 4) {
		unsigned int ulTextOffset = OleUtil::getU4Bytes(buffer.c_str(), tOffset);
		charPos.push_back(beginOfText + ulTextOffset);
	}

	//saving sepx offsets
	std::vector<unsigned int> sectPage;
	for (std::size_t index = 0, tOffset = (decriptorsCount + 1) * 4; index < decriptorsCount; ++index, tOffset += SED_SIZE) {
		sectPage.push_back(OleUtil::getU4Bytes(buffer.c_str(), tOffset + 2));
	}

	//reading the section properties
	char tmpBuffer[2];
	for (std::size_t index = 0; index < sectPage.size(); ++index) {
		if (sectPage.at(index) == 0xffffffffUL) { //check for invalid record, to make default section info
			SectionInfo sectionInfo;
			sectionInfo.CharPosition = charPos.at(index);
			mySectionInfoList.push_back(sectionInfo);
			continue;
		}
		//getting number of bytes to read
		if (!seek(sectPage.at(index), true)) {
			continue;
		}
		if (read(tmpBuffer, 2) != 2) {
			continue;
		}
		std::size_t bytes = 2 + (std::size_t)OleUtil::getU2Bytes(tmpBuffer, 0);

		if (!seek(sectPage.at(index), true)) {
			continue;
		}
		char *formatPageBuffer = new char[bytes];
		if (read(formatPageBuffer, bytes) != bytes) {
			delete[] formatPageBuffer;
			continue;
		}
		SectionInfo sectionInfo;
		sectionInfo.CharPosition = charPos.at(index);
		getSectionInfo(formatPageBuffer + 2, bytes - 2, sectionInfo);
		mySectionInfoList.push_back(sectionInfo);
		delete[] formatPageBuffer;
	}
	return true;
}