本文共 3229 字,大约阅读时间需要 10 分钟。
#include#include #include #include bool GetModuleBase(long long &ulModBase,pid_t pid,const char * pszModName){ bool bRet = false; FILE * fp = NULL; char szMapFilePath[32]={0}; char szMapFileLine[1024]={0}; if(pszModName == NULL) { return bRet; } if(pid < 0) { sprintf(szMapFilePath,"/proc/self/maps"); }else{ sprintf(szMapFilePath,"/proc/%d/maps",pid); } fp = fopen(szMapFilePath,"r"); if(fp != NULL) { while(fgets(szMapFileLine,sizeof(szMapFileLine),fp)!=NULL) { if(strstr(szMapFileLine,pszModName)) { char * pszModAddrStart = strtok(szMapFileLine,"-"); if(pszModAddrStart) { ulModBase = strtoul(pszModAddrStart,NULL,16); if(ulModBase == 0x8000) ulModBase =0; bRet = true; break; } } } fclose(fp); } return bRet;}bool GetModuleFullName(pid_t pid,const char * pszModName,char *pszFullModName,int nBuffSize){ bool bRet = false; FILE * fp = NULL; char szMapFilePath[32]={0}; char szMapFileLine[1024]={0}; char * pszFullName = NULL; if(pszModName == NULL|| pszFullModName == NULL||nBuffSize<=0) { return bRet; } if(pid<0) { sprintf(szMapFilePath,"/proc/self/maps"); } else { sprintf(szMapFilePath,"/proc/%d/maps",pid); } fp = fopen(szMapFilePath,"r"); if(fp!=NULL) { while(fgets(szMapFileLine,sizeof(szMapFileLine),fp)!=NULL) { if(strstr(szMapFileLine,pszModName)) { if(szMapFileLine[strlen(szMapFileLine) -1]=='\n') { szMapFileLine[strlen(szMapFileLine) -1]=0; } pszFullName = strchr(szMapFileLine,'/'); if(pszFullName == NULL) { continue; } strncpy(pszFullModName,pszFullName,nBuffSize -1); bRet = true; } } fclose(fp); } return bRet;}int main(int argc,char ** argv){ long long ulCModBase = 0; char szCModPath[256] = {0}; if(GetModuleBase(ulCModBase,getpid(),"libc-2.23.so")) { printf("c mod base:0x%llx\n", ulCModBase); } if (GetModuleFullName(getpid(), "libc-2.23.so", szCModPath, 256)) { printf("c mod full path:%s\n", szCModPath); } printf("finish \n"); return 0;}
linux
修改对应的模块androidLOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_ARM_MODE := armLOCAL_CFLAGS += -pie -fPIELOCAL_LDFLAGS += -pie -fPIELOCAL_MODULE := test1LOCAL_SRC_FILES := test.cppinclude $(BUILD_EXECUTABLE)
.......int main(int argc,char ** argv){ long long ulCModBase = 0; char szCModPath[256] = {0}; if(GetModuleBase(ulCModBase,getpid(),"libc.so")) { printf("c mod base:0x%llx\n", ulCModBase); } if (GetModuleFullName(getpid(), "libc.so", szCModPath, 256)) { printf("c mod full path:%s\n", szCModPath); } printf("finish \n"); return 0;}
转载于:https://blog.51cto.com/haidragon/2388328