⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dyn_load.c

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Do we need to separately register the main static data segment? */GC_bool GC_register_main_static_data(){  return (dl_iterate_phdr == 0);}#define HAVE_REGISTER_MAIN_STATIC_DATA# else /* !LINUX || version(glibc) < 2.2.4 *//* Dynamic loading code for Linux running ELF. Somewhat tested on * Linux/x86, untested but hopefully should work on Linux/Alpha.  * This code was derived from the Solaris/ELF support. Thanks to * whatever kind soul wrote that.  - Patrick Bridges *//* This doesn't necessarily work in all cases, e.g. with preloaded * dynamic libraries.						*/#if defined(NETBSD)#  include <sys/exec_elf.h>/* for compatibility with 1.4.x */#  ifndef DT_DEBUG#  define DT_DEBUG     21#  endif#  ifndef PT_LOAD#  define PT_LOAD      1#  endif#  ifndef PF_W#  define PF_W         2#  endif#else#  include <elf.h>#endif#include <link.h># endif#ifdef __GNUC__# pragma weak _DYNAMIC#endifextern ElfW(Dyn) _DYNAMIC[];static struct link_map *GC_FirstDLOpenedLinkMap(){    ElfW(Dyn) *dp;    static struct link_map *cachedResult = 0;    if( _DYNAMIC == 0) {        return(0);    }    if( cachedResult == 0 ) {        int tag;        for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) {            if( tag == DT_DEBUG ) {                struct link_map *lm                        = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;                if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */                break;            }        }    }    return cachedResult;}void GC_register_dynamic_libraries(){  struct link_map *lm;  # ifdef HAVE_DL_ITERATE_PHDR    if (GC_register_dynamic_libraries_dl_iterate_phdr()) {	return;    }# endif  lm = GC_FirstDLOpenedLinkMap();  for (lm = GC_FirstDLOpenedLinkMap();       lm != (struct link_map *) 0;  lm = lm->l_next)    {	ElfW(Ehdr) * e;        ElfW(Phdr) * p;        unsigned long offset;        char * start;        register int i;        	e = (ElfW(Ehdr) *) lm->l_addr;        p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));        offset = ((unsigned long)(lm->l_addr));        for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {          switch( p->p_type ) {            case PT_LOAD:              {                if( !(p->p_flags & PF_W) ) break;                start = ((char *)(p->p_vaddr)) + offset;                GC_add_roots_inner(start, start + p->p_memsz, TRUE);              }              break;            default:              break;          }	}    }}#endif /* !USE_PROC_FOR_LIBRARIES */#endif /* LINUX */#if defined(IRIX5) || (defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX))#include <sys/procfs.h>#include <sys/stat.h>#include <fcntl.h>#include <elf.h>#include <errno.h>#include <signal.h>  /* Only for the following test. */#ifndef _sigargs# define IRIX6#endifextern void * GC_roots_present();	/* The type is a lie, since the real type doesn't make sense here, */	/* and we only test for NULL.					   *//* We use /proc to track down all parts of the address space that are	*//* mapped by the process, and throw out regions we know we shouldn't	*//* worry about.  This may also work under other SVR4 variants.		*/void GC_register_dynamic_libraries(){    static int fd = -1;    char buf[30];    static prmap_t * addr_map = 0;    static int current_sz = 0;	/* Number of records currently in addr_map */    static int needed_sz;	/* Required size of addr_map		*/    int i;    long flags;    ptr_t start;    ptr_t limit;    ptr_t heap_start = (ptr_t)HEAP_START;    ptr_t heap_end = heap_start;#   ifdef SOLARISDL#     define MA_PHYS 0#   endif /* SOLARISDL */    if (fd < 0) {      sprintf(buf, "/proc/%d", getpid());	/* The above generates a lint complaint, since pid_t varies.	*/	/* It's unclear how to improve this.				*/      fd = open(buf, O_RDONLY);      if (fd < 0) {    	ABORT("/proc open failed");      }    }    if (ioctl(fd, PIOCNMAP, &needed_sz) < 0) {	GC_err_printf("fd = %d, errno = %d\n", fd, errno);    	ABORT("/proc PIOCNMAP ioctl failed");    }    if (needed_sz >= current_sz) {        current_sz = needed_sz * 2 + 1;        		/* Expansion, plus room for 0 record */        addr_map = (prmap_t *)GC_scratch_alloc((word)						(current_sz * sizeof(prmap_t)));    }    if (ioctl(fd, PIOCMAP, addr_map) < 0) {        GC_err_printf("fd = %d, errno = %d, needed_sz = %d, addr_map = 0x%X\n",                        fd, errno, needed_sz, addr_map);    	ABORT("/proc PIOCMAP ioctl failed");    };    if (GC_n_heap_sects > 0) {    	heap_end = GC_heap_sects[GC_n_heap_sects-1].hs_start    			+ GC_heap_sects[GC_n_heap_sects-1].hs_bytes;    	if (heap_end < GC_scratch_last_end_ptr) heap_end = GC_scratch_last_end_ptr;     }    for (i = 0; i < needed_sz; i++) {        flags = addr_map[i].pr_mflags;	if ((flags & (MA_BREAK | MA_STACK | MA_PHYS		      | MA_FETCHOP | MA_NOTCACHED)) != 0) goto irrelevant;        if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))            goto irrelevant;          /* The latter test is empirically useless in very old Irix	*/	  /* versions.  Other than the					*/          /* main data and stack segments, everything appears to be	*/          /* mapped readable, writable, executable, and shared(!!).	*/          /* This makes no sense to me.	- HB				*/        start = (ptr_t)(addr_map[i].pr_vaddr);        if (GC_roots_present(start)) goto irrelevant;        if (start < heap_end && start >= heap_start)        	goto irrelevant;#	ifdef MMAP_STACKS	  if (GC_is_thread_stack(start)) goto irrelevant;#	endif /* MMAP_STACKS */        limit = start + addr_map[i].pr_size;	/* The following seemed to be necessary for very old versions 	*/	/* of Irix, but it has been reported to discard relevant	*/	/* segments under Irix 6.5.  					*/#	ifndef IRIX6	  if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {	    /* Discard text segments, i.e. 0-offset mappings against	*/	    /* executable files which appear to have ELF headers.	*/	    caddr_t arg;	    int obj;#	    define MAP_IRR_SZ 10	    static ptr_t map_irr[MAP_IRR_SZ];	    				/* Known irrelevant map entries	*/	    static int n_irr = 0;	    struct stat buf;	    register int i;	    	    for (i = 0; i < n_irr; i++) {	        if (map_irr[i] == start) goto irrelevant;	    }	    arg = (caddr_t)start;	    obj = ioctl(fd, PIOCOPENM, &arg);	    if (obj >= 0) {	        fstat(obj, &buf);	        close(obj);	        if ((buf.st_mode & 0111) != 0) {	            if (n_irr < MAP_IRR_SZ) {	                map_irr[n_irr++] = start;	            }	            goto irrelevant;	        }	    }	  }#	endif /* !IRIX6 */        GC_add_roots_inner(start, limit, TRUE);      irrelevant: ;    }    /* Dont keep cached descriptor, for now.  Some kernels don't like us */    /* to keep a /proc file descriptor around during kill -9.		 */    	if (close(fd) < 0) ABORT("Couldnt close /proc file");	fd = -1;}# endif /* USE_PROC || IRIX5 */# if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)# define WIN32_LEAN_AND_MEAN# define NOSERVICE# include <windows.h># include <stdlib.h>  /* We traverse the entire address space and register all segments 	*/  /* that could possibly have been written to.				*/    extern GC_bool GC_is_heap_base (ptr_t p);# ifdef GC_WIN32_THREADS    extern void GC_get_next_stack(char *start, char **lo, char **hi);    void GC_cond_add_roots(char *base, char * limit)    {      char * curr_base = base;      char * next_stack_lo;      char * next_stack_hi;         if (base == limit) return;      for(;;) {	  GC_get_next_stack(curr_base, &next_stack_lo, &next_stack_hi);	  if (next_stack_lo >= limit) break;	  GC_add_roots_inner(curr_base, next_stack_lo, TRUE);	  curr_base = next_stack_hi;      }      if (curr_base < limit) GC_add_roots_inner(curr_base, limit, TRUE);    }# else    void GC_cond_add_roots(char *base, char * limit)    {      char dummy;      char * stack_top	 = (char *) ((word)(&dummy) & ~(GC_sysinfo.dwAllocationGranularity-1));      if (base == limit) return;      if (limit > stack_top && base < GC_stackbottom) {    	  /* Part of the stack; ignore it. */    	  return;      }      GC_add_roots_inner(base, limit, TRUE);    }# endif# ifdef MSWINCE  /* Do we need to separately register the main static data segment? */  GC_bool GC_register_main_static_data()  {    return FALSE;  }# else /* win32 */  extern GC_bool GC_no_win32_dlls;  GC_bool GC_register_main_static_data()  {    return GC_no_win32_dlls;  }# endif /* win32 */  # define HAVE_REGISTER_MAIN_STATIC_DATA# ifdef DEBUG_VIRTUALQUERY  void GC_dump_meminfo(MEMORY_BASIC_INFORMATION *buf)  {    GC_printf("BaseAddress = %lx, AllocationBase = %lx, RegionSize = %lx(%lu)\n",	       buf -> BaseAddress, buf -> AllocationBase, buf -> RegionSize,	       buf -> RegionSize);    GC_printf("\tAllocationProtect = %lx, State = %lx, Protect = %lx, "	       "Type = %lx\n",	       buf -> AllocationProtect, buf -> State, buf -> Protect,	       buf -> Type);  }# endif /* DEBUG_VIRTUALQUERY */  extern GC_bool GC_wnt;  /* Is Windows NT derivative.		*/  			  /* Defined and set in os_dep.c.	*/  void GC_register_dynamic_libraries()  {    MEMORY_BASIC_INFORMATION buf;    size_t result;    DWORD protect;    LPVOID p;    char * base;    char * limit, * new_limit;#   ifdef MSWIN32      if (GC_no_win32_dlls) return;#   endif    base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;#   if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)      /* Only the first 32 MB of address space belongs to the current process */      while (p < (LPVOID)0x02000000) {        result = VirtualQuery(p, &buf, sizeof(buf));	if (result == 0) {	    /* Page is free; advance to the next possible allocation base */	    new_limit = (char *)		(((DWORD) p + GC_sysinfo.dwAllocationGranularity)		 & ~(GC_sysinfo.dwAllocationGranularity-1));	} else#   else      while (p < GC_sysinfo.lpMaximumApplicationAddress) {        result = VirtualQuery(p, &buf, sizeof(buf));#   endif	{	    if (result != sizeof(buf)) {		ABORT("Weird VirtualQuery result");	    }	    new_limit = (char *)p + buf.RegionSize;	    protect = buf.Protect;	    if (buf.State == MEM_COMMIT		&& (protect == PAGE_EXECUTE_READWRITE		    || protect == PAGE_READWRITE)		&& !GC_is_heap_base(buf.AllocationBase) 		/* There is some evidence that we cannot always 		 * ignore MEM_PRIVATE sections under Windows ME 		 * and predecessors.  Hence we now also check for 		 * that case.	*/ 		&& (buf.Type == MEM_IMAGE || 		    !GC_wnt && buf.Type == MEM_PRIVATE)) {#	        ifdef DEBUG_VIRTUALQUERY	          GC_dump_meminfo(&buf);#	        endif		if ((char *)p != limit) {		    GC_cond_add_roots(base, limit);		    base = p;		}		limit = new_limit;	    }	}        if (p > (LPVOID)new_limit /* overflow */) break;        p = (LPVOID)new_limit;    }    GC_cond_add_roots(base, limit);  }#endif /* MSWIN32 || MSWINCE || CYGWIN32 */  #if defined(ALPHA) && defined(OSF1)#include <loader.h>void GC_register_dynamic_libraries(){  int status;  ldr_process_t mypid;  /* module */    ldr_module_t moduleid = LDR_NULL_MODULE;    ldr_module_info_t moduleinfo;    size_t moduleinfosize = sizeof(moduleinfo);    size_t modulereturnsize;      /* region */    ldr_region_t region;     ldr_region_info_t regioninfo;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -