📄 dyn_load.c
字号:
if (!did_something) { /* dl_iterate_phdr may forget the static data segment in */ /* statically linked executables. */ GC_add_roots_inner(DATASTART, (char *)(DATAEND), TRUE);# if defined(DATASTART2) GC_add_roots_inner(DATASTART2, (char *)(DATAEND2), TRUE);# endif } return TRUE; } else { return FALSE; }}/* 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++ ) { /* FIXME: The DT_DEBUG header is not mandated by the */ /* ELF spec. This code appears to be dependent on */ /* idiosynchracies of older GNU tool chains. If this code */ /* fails for you, the real problem is probably that it is */ /* being used at all. You should be getting the */ /* dl_iterate_phdr version. */ 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 */ register int i; register long flags; register ptr_t start; register ptr_t limit; ptr_t heap_start = (ptr_t)HEAP_START; ptr_t heap_end = heap_start;# ifdef SUNOS5DL# define MA_PHYS 0# endif /* SUNOS5DL */ 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_printf2("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_printf4("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)# 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 /* The frame buffer testing code is dead in this version. */ /* We leave it here temporarily in case the switch to just */ /* testing for MEM_IMAGE sections causes un expected */ /* problems. */ GC_bool GC_warn_fb = TRUE; /* Warn about traced likely */ /* graphics memory. */ GC_bool GC_disallow_ignore_fb = FALSE; int GC_ignore_fb_mb; /* Ignore mappings bigger than the */ /* specified number of MB. */ GC_bool GC_ignore_fb = FALSE; /* Enable frame buffer */ /* checking. */ /* Issue warning if tracing apparent framebuffer. */ /* This limits us to one warning, and it's a back door to */ /* disable that. */ /* Should [start, start+len) be treated as a frame buffer */ /* and ignored? */ /* Unfortunately, we currently are not quite sure how to tell */ /* this automatically, and rely largely on user input. */ /* We expect that any mapping with type MEM_MAPPED (which */ /* apparently excludes library data sections) can be safely */ /* ignored. But we're too chicken to do that in this */ /* version. */ /* Based on a very limited sample, it appears that: */ /* - Frame buffer mappings appear as mappings of large */ /* length, usually a bit less than a power of two. */ /* - The definition of "a bit less" in the above cannot */ /* be made more precise. */ /* - Have a starting address at best 64K aligned. */ /* - Have type == MEM_MAPPED. */ static GC_bool is_frame_buffer(ptr_t start, size_t len, DWORD tp) { static GC_bool initialized = FALSE;# define MB (1024*1024)# define DEFAULT_FB_MB 15# define MIN_FB_MB 3 if (GC_disallow_ignore_fb || tp != MEM_MAPPED) return FALSE; if (!initialized) { char * ignore_fb_string = GETENV("GC_IGNORE_FB"); if (0 != ignore_fb_string) { while (*ignore_fb_string == ' ' || *ignore_fb_string == '\t') ++ignore_fb_string; if (*ignore_fb_string == '\0') { GC_ignore_fb_mb = DEFAULT_FB_MB; } else { GC_ignore_fb_mb = atoi(ignore_fb_string); if (GC_ignore_fb_mb < MIN_FB_MB) { WARN("Bad GC_IGNORE_FB value. Using %ld\n", DEFAULT_FB_MB); GC_ignore_fb_mb = DEFAULT_FB_MB; } } GC_ignore_fb = TRUE; } else { GC_ignore_fb_mb = DEFAULT_FB_MB; /* For warning */ } initialized = TRUE; } if (len >= ((size_t)GC_ignore_fb_mb << 20)) { if (GC_ignore_fb) { return TRUE; } else { if (GC_warn_fb) { WARN("Possible frame buffer mapping at 0x%lx: \n" "\tConsider setting GC_IGNORE_FB to improve performance.\n", start); GC_warn_fb = FALSE; } return FALSE; } } else { return FALSE; } }# ifdef DEBUG_VIRTUALQUERY void GC_dump_meminfo(MEMORY_BASIC_INFORMATION *buf) { GC_printf4("BaseAddress = %lx, AllocationBase = %lx, RegionSize = %lx(%lu)\n", buf -> BaseAddress, buf -> AllocationBase, buf -> RegionSize, buf -> RegionSize); GC_printf4("\tAllocationProtect = %lx, State = %lx, Protect = %lx, " "Type = %lx\n", buf -> AllocationProtect, buf -> State, buf -> Protect, buf -> Type); }# endif /* DEBUG_VIRTUALQUERY */ void GC_register_dynamic_libraries() { MEMORY_BASIC_INFORMATION buf; DWORD 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));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -