コード例 #1
0
Builder::Builder( vector<string> &objFilePath,string lib,string output )
{
	libDirPath_ = lib;
	outputPath_ = output;
	entryFunc_ = "mini_crt_entry";
	myEntryFuncObj_ =  lib + "/entry.o";
	objFilePath.push_back(myEntryFuncObj_);
	for( unsigned int i = 0;i < objFilePath.size();++i )
	{
		//这些变量在后面都会以指针形式被保存下来,所以不能用局部变亮
		ObjectFile *pObj = new ObjectFile( objFilePath[i] );
		if( !pObj->IsValid() )
		{
			cout << objFilePath[i] << " is invalid. * Ignore *" << endl;
			continue;
		}
		ElfHeader *pEh = new ElfHeader;
		pEh->GetHeader( *pObj );
		Section *pSec = new Section();
		if( !pSec->GetSection(*pEh) )
		{
			cout << "error while getting section:" << objFilePath[i] << endl;
			exit(0);
		}
		if( !pSec->GetSymbol() )
		{
			cout << "error while getting symbols:" << objFilePath[i] << endl;
			exit(0);
		}

		rawSection_.push_back(pSec);
	}
}
コード例 #2
0
bool Builder::CollectSymbol()
{
	//如果能够集齐返回真

	//先处理rawSection,不够的再到标准库中找
	for( unsigned int i = 0;i < rawSection_.size();++i )
	{
		for( unsigned int j = 0;j < rawSection_[i]->symArr_.size();++j )
		{
			if( !NewSymbol( rawSection_[i]->symArr_[j] ) )
				return false;
		}
	}

	map<string,Symbol>::iterator it = finded_.begin();
	while( it != finded_.end() )
	{
		if( it->second.symStr_->st_shndx == SHN_COMMON )
		{
			cout << "error:" << it->first << "没有初始化" << endl;
			return false;
		}
		++it;
	}

	if( needed_.empty() )
		return true;

	cout << "正在链接标准库" <<endl;

	/*
	 * 库中所有的o文件全部载入
	 */

	vector<string> libObjPath;
	GetLibObjPath( libDirPath_,libObjPath );

	for( unsigned int i = 0;i < libObjPath.size();++i )
	{
//		cout << "正在添加库:" << libObjPath[i] << endl;
		//方法同Builder构造,只是同时把inLib构建好了
		ObjectFile *pObj = new ObjectFile( libObjPath[i] );
		if( !pObj->IsValid() )
		{
			cout << libObjPath[i] << " is invalid. * Ignore *" << endl;
			continue;
		}
		ElfHeader *pEh = new ElfHeader;
		pEh->GetHeader( *pObj );
		Section *pSec = new Section();
		if( !pSec->GetSection(*pEh) )
		{
			cout << "error while getting section:" << libObjPath[i] << endl;
			exit(0);
		}
		if( !pSec->GetSymbol() )
		{
			cout << "error while getting symbols:" << libObjPath[i] << endl;
			exit(0);
		}

		for( unsigned int j = 0;j < pSec->symArr_.size();++j )
		{
			if( pSec->symArr_[j].name_ == "" )
				continue;

			if( pSec->symArr_[j].symStr_->st_shndx == SHN_UNDEF ||
				pSec->symArr_[j].symStr_->st_shndx == SHN_COMMON )
			{
				continue;
			}
			if( inLib_.find( pSec->symArr_[j].name_ ) != inLib_.end() )
			{
				cout << "多次发现:" << pSec->symArr_[j].name_ << " Ignore" << endl;
				continue;
			}
			inLib_.insert( pair<string,Symbol>(pSec->symArr_[j].name_,pSec->symArr_[j]) );
		}

		libSection_.push_back(pSec);
	}

//	PrintLibSymbol();

	LinkLibSymbol();

	if( needed_.size() != 0 )
	{
		cout << "error:无法找到下列符号定义" << endl;
		map<string,Symbol>::iterator it = needed_.begin();
		while( it != needed_.end() )
		{
			cout << it->first << endl;
			++it;
		}
#ifdef DEBUG
		cout << "已找到的符号" << endl;
		it = finded_.begin();
		while( it != finded_.end() )
		{
			cout << it->first << endl;
			++it;
		}
#endif
		return false;
	}

	return true;
}