linux c 技术

#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
//#include <unistd.h>
#include <math.h>

//源程序:http://blog.chinaunix.net/uid-26874207-id-4936260.html

//linux c动态调用 
/*
; Needed Library 'libstdc++.so'
.plt:000085CC                         ; Needed Library 'libm.so'
.plt:000085CC                         ; Needed Library 'libc.so'
.plt:000085CC                         ; Needed Library 'libdl.so'
.plt:000085CC                         ;

typedef UINT (WINAPI *URLDownloadToFileAT)
(
LPUNKNOWN,LPCSTR,LPCSTR,DWORD,LPBINDSTATUSCALLBACK
); 

URLDownloadToFileAT pURLDownloadToFileA = (URLDownloadToFileAT)GetProcAddress(LoadLibraryA("Urlmon.dll"),"URLDownloadToFileA");

*/


//http://blog.csdn.net/jernymy/article/details/6903683
// -ldl
int main(int argc, char *argv[])
{
	char * dlso="libc.so";
	typedef int (* getpidT)();
	//typedef __pid_t (* getpidT)();
	getpidT pgetpid=(getpidT)dlsym(dlopen(dlso, RTLD_NOW),"getpid");
	//typedef uid_t (* getuidT)();
	typedef int (* getuidT)();
	getuidT pgetuid=(getuidT)dlsym(dlopen(dlso, RTLD_NOW),"getuid");

	typedef int (* execlpT)(__const char *__file, __const char *__arg, ...);
	execlpT pexeclp=(execlpT)dlsym(dlopen("xxx.so", RTLD_NOW),"execlp");


	char *dlmath="libm.so";
	typedef double (* cosT)(double );
	cosT pcos=(cosT)dlsym(dlopen(dlmath, RTLD_NOW),"cos");

	//double sin(double x);
	typedef double (* sinT)(double x);
	sinT psin=(sinT)dlsym(dlopen(dlmath, RTLD_NOW),"sin");
	//开方 double sqrt (double x);
	typedef double (* sqrtT)(double x);
	sqrtT psqrt=(sqrtT)dlsym(dlopen(dlmath, RTLD_NOW),"sqrt");
	//double pow (double base, double exponent);
	typedef double (* powT)(double base, double exponent);
	powT ppow=(powT)dlsym(dlopen(dlmath, RTLD_NOW),"pow");

	printf("pgetpid=%d\n",pgetpid());
	printf("pgetuid=%d\n",pgetuid());
	printf("pcos=%f psin=%f psqrt=%f ppow=%f \n",pcos(2.0),psin(90.0),psqrt(1024.0),ppow(25,2.0));


	pexeclp("firefox", "firefox", NULL);
	pexeclp("/bin/sh", "sh", NULL); //在ubuntu运行
	//printf("pid=%d uid=%d\n",getpid(),getuid());

	return 0;
}
#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
	void *handle;
	double (*cosine)(double);
	char *error;

	handle = dlopen ("libm.so", RTLD_LAZY);
	if (!handle) {
		fprintf (stderr, "%s\n", dlerror());
		exit(1);
	}

	cosine = dlsym(handle, "cos");
	if ((error = dlerror()) != NULL)  {
		fprintf (stderr, "%s\n", error);
		exit(1);
	}

	printf ("%f\n", (*cosine)(2.0));
	dlclose(handle);
	return 0;
}

查看库文件的导出函数
在ubuntu下:
cd /lib/i386-linux-gnu/
nm -D libc-2.15.so  |grep getpid
nm -D libc-2.15.so  |grep getuid

先查找库文件的全名:
ls -l |grep libm
nm -D libm-2.15.so  |grep sqrt
nm -D libm-2.15.so  |grep pow

或者使用objdump 命令
objdump -tT libm-2.15.so |grep pow
objdump -tT libm-2.15.so |grep sqrt
nm -D libc-2.15.so  |grep exec
从而判断dlopen调用的库文件是否正确。

http://www.cplusplus.com/reference/algorithm/

在/lib/i386-linux-gnu 执行:ls  |grep lib >1.txt

附上linux 常用库文件demo:

libacl.so.1
libacl.so.1.1.0
libaio.so.1
libaio.so.1.0.1
libanl-2.15.so
libanl.so.1
libattr.so.1
libattr.so.1.1.0
libBrokenLocale-2.15.so
libBrokenLocale.so.1
libbz2.so.1
libbz2.so.1.0
libbz2.so.1.0.4
libc-2.15.so
libcap.so.2
libcap.so.2.22
libcidn-2.15.so
libcidn.so.1
libcom_err.so.2
libcom_err.so.2.1
libcrypt-2.15.so
libcrypto.so.0.9.8
libcrypto.so.1.0.0
libcrypt.so.1
libc.so.6
libdbus-1.so.3
libdbus-1.so.3.5.8
libdl-2.15.so
libdl.so.2
libexpat.so.1
libexpat.so.1.5.2
libgcc_s.so.1
libgcrypt.so.11
libgcrypt.so.11.7.0
libglib-2.0.so.0
libglib-2.0.so.0.3200.4
libgpg-error.so.0
libgpg-error.so.0.8.0
libkeyutils.so.1
libkeyutils.so.1.4
libm-2.15.so
libmemusage.so
libm.so.6
libncurses.so.5
libncurses.so.5.9
libncursesw.so.5
libncursesw.so.5.9
libnsl-2.15.so
libnsl.so.1
libnss_compat-2.15.so
libnss_compat.so.2
libnss_dns-2.15.so
libnss_dns.so.2
libnss_files-2.15.so
libnss_files.so.2
libnss_hesiod-2.15.so
libnss_hesiod.so.2
libnss_nis-2.15.so
libnss_nisplus-2.15.so
libnss_nisplus.so.2
libnss_nis.so.2
libpcprofile.so
libpcre.so.3
libpcre.so.3.12.1
libpng12.so.0
libpng12.so.0.46.0
libpthread-2.15.so
libpthread.so.0
libresolv-2.15.so
libresolv.so.2
librt-2.15.so
librt.so.1
libSegFault.so
libselinux.so.1
libslang.so.2
libslang.so.2.2.4
libssl.so.0.9.8
libssl.so.1.0.0
libthread_db-1.0.so
libthread_db.so.1
libtinfo.so.5
libtinfo.so.5.9
libudev.so.0
libudev.so.0.13.0
libusb-0.1.so.4
libusb-0.1.so.4.4.4
libutil-2.15.so
libutil.so.1
libuuid.so.1
libuuid.so.1.3.0
libwrap.so.0
libwrap.so.0.7.6
libz.so.1
libz.so.1.2.3.4


你可能感兴趣的:(linux c 技术)