コード例 #1
0
ファイル: elf.cpp プロジェクト: geomsaek/CSCI212_Ass2
int main(int argc, char **argv){
	Elf *elf;
	Elf_Scn *scn = NULL;
	GElf_Shdr shdr;
	Elf_Data *data;
	int fd, ii, count;
	GElf_Dyn sym;
	char* custcustname = NULL;
	char curLib[1000];
	int lib_count = 0, z = 0;

	// do arguement checking

	if (argc != 2){
		cerr << "Usage is: elf filecustname " << endl;
		exit(1);
	}

	// open the file in argv[1] and produce the elf descriptor
	elf_version(EV_CURRENT);

	if ((fd = open(argv[1], O_RDONLY)) == -1){
		cerr << "Could not open file: " << argv[1] << endl;
		exit(1);
	}

	elf = elf_begin(fd, ELF_C_READ, NULL);
	
	// scan through the sections in the file

	while ((scn = elf_nextscn(elf, scn)) != NULL) 
	{
		// get the section header and check to see if the it the
		// .dynamic section
		gelf_getshdr(scn, &shdr);
        if (shdr.sh_type == SHT_DYNAMIC) 
		{
			data = elf_getdata(scn, NULL);
			ii=0;
			do
			{	
				gelf_getdyn(data, ii++, &sym);
				if (sym.d_tag == DT_NEEDED){
					// if we find a element in the section
					// that is a curLib we pull it out
					custname = elf_strptr(elf,shdr.sh_link,sym.d_un.d_ptr);
					if (custname != NULL){
						strcat(curLib,custname); //curLib custnames are added separated by ":"
						strcat(curLib,":");
						lib_count++; //count the number of libraries read
					} 
				}

				if (sym.d_tag == DT_RPATH) {
					custname = elf_strptr(elf,shdr.sh_link,sym.d_un.d_ptr);
					if (custname != NULL){
						char complete_list[5000];
						char lib[100];
						int count = 0;
						//for each curLib, resolve its address
						
						for(int i = 0; i < lib_count; i++) {
							int counter = 0;
							while(curLib[count] != ':'){
								lib[counter++] = curLib[count++];
							}
							lib[counter] = '\0';
							count++;
							
							//if by any chance a curLib cannot be found, exit program
							if(get_replace(complete_list,lib,custname) == false){
								cerr << "Error some of the libraries not found!" << endl;
								exit(1);
							}
						}
						cout<< endl << complete_list << endl; //output all the libraries with their absolute paths
					} 

				}
         	} while (sym.d_tag != DT_NULL);
		}
	}
	elf_end(elf);
	return 0;
}
コード例 #2
0
ファイル: sm_net.cpp プロジェクト: repinel/SimpleMan
const bool SM_Net::download_file ( void )
{
	if( ! define_info( sm_download->option == OPTION_NOT_ASKED ) )
		return false;
	if( sm_download->option == OPTION_NOT_ASKED )
	{
		sm_download->size      = get_totalSize();
		sm_download->completed = get_comSize();
		sm_download->option    = get_replace();
	}
	else
	{
		set_totalSize ( sm_download->size );
		set_replace   ( sm_download->option );
	}

	if( isHTTP() )
	{
		sm_httpSocket = new SM_HTTP ();
		if( ! sm_httpSocket )
		{
			SM_Util::msg_err( "Error: could not allocate http socket." );
			delete sm_httpSocket;
			sm_httpSocket = NULL;
			return false;
		}
		if( ! sm_httpSocket->configure( sm_address, sm_port ) )
		{
			SM_Util::msg_err( "Error: could not connect to server." );
			delete sm_httpSocket;
			sm_httpSocket = NULL;
			return false;
		}
		if( ! donwload )
		{
			running = true;

			sm_httpSocket->set_speedLimit( sm_speedLimit );

			bool get_return;
			if( replace && get_comSize() == 0 )
				get_return = sm_httpSocket->get_file( sm_download, sm_fileName, sm_path, get_totalSize(), replace );
			else
				get_return = sm_httpSocket->get_file( sm_download, sm_fileName, sm_path, get_totalSize() - sm_size, replace );

			running = false;
			delete sm_httpSocket;
			sm_httpSocket = NULL;

			if( get_return )
			{
				if( isCompleted() )
					sm_signal_completed( this );
				else
					sm_signal_paused( this );
				return true;
			}
			else
			{
				SM_Util::msg_err( "Error: could not get file." );
				return false;
			}
		}
		delete sm_httpSocket;
		sm_httpSocket = NULL;
		return true;
	}
	else if ( isFTP() )
	{
		sm_ftpSocket = new SM_FTP();

		if( ! sm_ftpSocket )
		{
			SM_Util::msg_err( "Error: could not allocate ftp socket." );
			delete sm_ftpSocket;
			sm_ftpSocket = NULL;
			return false;
		}
		if( ! sm_ftpSocket->configure( sm_address, sm_port ) )
		{
			SM_Util::msg_err( "Error: could not connect to server." );
			delete sm_ftpSocket;
			sm_ftpSocket = NULL;
			return false;
		}
		if( ! sm_ftpSocket->user_conf( sm_download->user, sm_download->pass ) )
		{
			SM_Util::msg_err( "Error: could not authenticate." );
			delete sm_ftpSocket;
			sm_ftpSocket = NULL;
			return false;
		}
		if( ! sm_ftpSocket->set_type( true ) )
		{
			SM_Util::msg_err( "Error: could not set connection type." );
			delete sm_ftpSocket;
			sm_ftpSocket = NULL;
			return false;
		}
		if( ! donwload )
		{
			running = true;

			sm_ftpSocket->set_speedLimit( sm_speedLimit );

			bool get_return;
			if( replace && get_comSize() == 0 )
				get_return = sm_ftpSocket->get_file( sm_download, sm_fileName, sm_path, 0, replace );
			else
				get_return = sm_ftpSocket->get_file( sm_download, sm_fileName, sm_path, sm_size, replace );

			running = false;

			if( get_return )
			{
				delete sm_ftpSocket;
				sm_ftpSocket = NULL;

				if( isCompleted() )
					sm_signal_completed( this );
				else
					sm_signal_paused( this );
				return true;
			}
			else
			{
				SM_Util::msg_err( "Error: could not make download." );

				if( ! sm_ftpSocket->quit() )
					SM_Util::msg_err( "Error: could not quit connection." );

				delete sm_ftpSocket;
				sm_ftpSocket = NULL;

				return false;
			}
		}
		delete sm_ftpSocket;
		sm_ftpSocket = NULL;
		return true;
	}
	else
	{
		SM_Util::msg_err( "Error: network protocol not supported." );
		return false;
	}	
}