| 1 |
/* |
| 2 |
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved. |
| 3 |
* Copyright (c) 1997 by Silicon Graphics. All rights reserved. |
| 4 |
* Copyright (c) 2009-2021 Ivan Maidanski |
| 5 |
* |
| 6 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 7 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 8 |
* |
| 9 |
* Permission is hereby granted to use or copy this program |
| 10 |
* for any purpose, provided the above notices are retained on all copies. |
| 11 |
* Permission to modify the code and to distribute modified code is granted, |
| 12 |
* provided the above notices are retained, and a notice that the code was |
| 13 |
* modified is included with the above copyright notice. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "private/gc_priv.h" |
| 17 |
|
| 18 |
/* |
| 19 |
* This is incredibly OS specific code for tracking down data sections in |
| 20 |
* dynamic libraries. There appears to be no way of doing this quickly |
| 21 |
* without groveling through undocumented data structures. We would argue |
| 22 |
* that this is a bug in the design of the dlopen interface. THIS CODE |
| 23 |
* MAY BREAK IN FUTURE OS RELEASES. If this matters to you, don't hesitate |
| 24 |
* to let your vendor know ... |
| 25 |
* |
| 26 |
* None of this is safe with dlclose and incremental collection. |
| 27 |
* But then not much of anything is safe in the presence of dlclose. |
| 28 |
*/ |
| 29 |
|
| 30 |
#if !defined(MACOS) && !defined(GC_NO_TYPES) && !defined(SN_TARGET_PSP2) \ |
| 31 |
&& !defined(_WIN32_WCE) && !defined(__CC_ARM) |
| 32 |
# include <sys/types.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
/* BTL: avoid circular redefinition of dlopen if GC_SOLARIS_THREADS defined */ |
| 36 |
#undef GC_MUST_RESTORE_REDEFINED_DLOPEN |
| 37 |
#if defined(GC_PTHREADS) && !defined(GC_NO_DLOPEN) \ |
| 38 |
&& !defined(GC_NO_THREAD_REDIRECTS) && !defined(GC_USE_LD_WRAP) |
| 39 |
/* To support threads in Solaris, gc.h interposes on dlopen by */ |
| 40 |
/* defining "dlopen" to be "GC_dlopen", which is implemented below. */ |
| 41 |
/* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the */ |
| 42 |
/* real system dlopen() in their implementation. We first remove */ |
| 43 |
/* gc.h's dlopen definition and restore it later, after GC_dlopen(). */ |
| 44 |
# undef dlopen |
| 45 |
# define GC_MUST_RESTORE_REDEFINED_DLOPEN |
| 46 |
#endif /* !GC_NO_DLOPEN */ |
| 47 |
|
| 48 |
/* A user-supplied routine (custom filter) that might be called to */ |
| 49 |
/* determine whether a DSO really needs to be scanned by the GC. */ |
| 50 |
/* 0 means no filter installed. May be unused on some platforms. */ |
| 51 |
/* FIXME: Add filter support for more platforms. */ |
| 52 |
STATIC GC_has_static_roots_func GC_has_static_roots = 0; |
| 53 |
|
| 54 |
#if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \ |
| 55 |
|| defined(CYGWIN32)) && !defined(PCR) |
| 56 |
|
| 57 |
#if !defined(DARWIN) && !defined(SCO_ELF) && !defined(SOLARISDL) \ |
| 58 |
&& !defined(AIX) && !defined(DGUX) && !defined(IRIX5) && !defined(HPUX) \ |
| 59 |
&& !defined(CYGWIN32) && !defined(MSWIN32) && !defined(MSWINCE) \ |
| 60 |
&& !(defined(ALPHA) && defined(OSF1)) \ |
| 61 |
&& !(defined(FREEBSD) && defined(__ELF__)) \ |
| 62 |
&& !(defined(LINUX) && defined(__ELF__)) \ |
| 63 |
&& !(defined(NETBSD) && defined(__ELF__)) \ |
| 64 |
&& !(defined(OPENBSD) && (defined(__ELF__) || defined(M68K))) \ |
| 65 |
&& !defined(HAIKU) && !defined(HURD) && !defined(NACL) \ |
| 66 |
&& !defined(CPPCHECK) |
| 67 |
# error We only know how to find data segments of dynamic libraries for above. |
| 68 |
# error Additional SVR4 variants might not be too hard to add. |
| 69 |
#endif |
| 70 |
|
| 71 |
#include <stdio.h> |
| 72 |
#ifdef SOLARISDL |
| 73 |
# include <sys/elf.h> |
| 74 |
# include <dlfcn.h> |
| 75 |
# include <link.h> |
| 76 |
#endif |
| 77 |
|
| 78 |
#if defined(NETBSD) |
| 79 |
# include <sys/param.h> |
| 80 |
# include <dlfcn.h> |
| 81 |
# include <machine/elf_machdep.h> |
| 82 |
# define ELFSIZE ARCH_ELFSIZE |
| 83 |
#endif |
| 84 |
|
| 85 |
#if defined(OPENBSD) |
| 86 |
# include <sys/param.h> |
| 87 |
# if (OpenBSD >= 200519) && !defined(HAVE_DL_ITERATE_PHDR) |
| 88 |
# define HAVE_DL_ITERATE_PHDR |
| 89 |
# endif |
| 90 |
#endif /* OPENBSD */ |
| 91 |
|
| 92 |
#if defined(SCO_ELF) || defined(DGUX) || defined(HURD) || defined(NACL) \ |
| 93 |
|| (defined(__ELF__) && (defined(LINUX) || defined(FREEBSD) \ |
| 94 |
|| defined(NETBSD) || defined(OPENBSD))) |
| 95 |
# include <stddef.h> |
| 96 |
# if !defined(OPENBSD) && !defined(HOST_ANDROID) |
| 97 |
/* OpenBSD does not have elf.h file; link.h below is sufficient. */ |
| 98 |
/* Exclude Android because linker.h below includes its own version. */ |
| 99 |
# include <elf.h> |
| 100 |
# endif |
| 101 |
# ifdef HOST_ANDROID |
| 102 |
/* If you don't need the "dynamic loading" feature, you may build */ |
| 103 |
/* the collector with -D IGNORE_DYNAMIC_LOADING. */ |
| 104 |
# ifdef BIONIC_ELFDATA_REDEF_BUG |
| 105 |
/* Workaround a problem in Bionic (as of Android 4.2) which has */ |
| 106 |
/* mismatching ELF_DATA definitions in sys/exec_elf.h and */ |
| 107 |
/* asm/elf.h included from linker.h file (similar to EM_ALPHA). */ |
| 108 |
# include <asm/elf.h> |
| 109 |
# include <linux/elf-em.h> |
| 110 |
# undef ELF_DATA |
| 111 |
# undef EM_ALPHA |
| 112 |
# endif |
| 113 |
# include <link.h> |
| 114 |
# if !defined(GC_DONT_DEFINE_LINK_MAP) && !(__ANDROID_API__ >= 21) |
| 115 |
/* link_map and r_debug are defined in link.h of NDK r10+. */ |
| 116 |
/* bionic/linker/linker.h defines them too but the header */ |
| 117 |
/* itself is a C++ one starting from Android 4.3. */ |
| 118 |
struct link_map { |
| 119 |
uintptr_t l_addr; |
| 120 |
char* l_name; |
| 121 |
uintptr_t l_ld; |
| 122 |
struct link_map* l_next; |
| 123 |
struct link_map* l_prev; |
| 124 |
}; |
| 125 |
struct r_debug { |
| 126 |
int32_t r_version; |
| 127 |
struct link_map* r_map; |
| 128 |
void (*r_brk)(void); |
| 129 |
int32_t r_state; |
| 130 |
uintptr_t r_ldbase; |
| 131 |
}; |
| 132 |
# endif |
| 133 |
# else |
| 134 |
EXTERN_C_BEGIN /* Workaround missing extern "C" around _DYNAMIC */ |
| 135 |
/* symbol in link.h of some Linux hosts. */ |
| 136 |
# include <link.h> |
| 137 |
EXTERN_C_END |
| 138 |
# endif |
| 139 |
#endif |
| 140 |
|
| 141 |
/* Newer versions of GNU/Linux define this macro. We |
| 142 |
* define it similarly for any ELF systems that don't. */ |
| 143 |
# ifndef ElfW |
| 144 |
# if defined(FREEBSD) |
| 145 |
# if __ELF_WORD_SIZE == 32 |
| 146 |
# define ElfW(type) Elf32_##type |
| 147 |
# else |
| 148 |
# define ElfW(type) Elf64_##type |
| 149 |
# endif |
| 150 |
# elif defined(NETBSD) || defined(OPENBSD) |
| 151 |
# if ELFSIZE == 32 |
| 152 |
# define ElfW(type) Elf32_##type |
| 153 |
# elif ELFSIZE == 64 |
| 154 |
# define ElfW(type) Elf64_##type |
| 155 |
# else |
| 156 |
# error Missing ELFSIZE define |
| 157 |
# endif |
| 158 |
# else |
| 159 |
# if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32 |
| 160 |
# define ElfW(type) Elf32_##type |
| 161 |
# else |
| 162 |
# define ElfW(type) Elf64_##type |
| 163 |
# endif |
| 164 |
# endif |
| 165 |
# endif |
| 166 |
|
| 167 |
#if defined(SOLARISDL) && !defined(USE_PROC_FOR_LIBRARIES) |
| 168 |
|
| 169 |
EXTERN_C_BEGIN |
| 170 |
extern ElfW(Dyn) _DYNAMIC; |
| 171 |
EXTERN_C_END |
| 172 |
|
| 173 |
STATIC struct link_map * |
| 174 |
GC_FirstDLOpenedLinkMap(void) |
| 175 |
{ |
| 176 |
ElfW(Dyn) *dp; |
| 177 |
static struct link_map * cachedResult = 0; |
| 178 |
static ElfW(Dyn) *dynStructureAddr = 0; |
| 179 |
/* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */ |
| 180 |
|
| 181 |
# ifdef SUNOS53_SHARED_LIB |
| 182 |
/* BTL: Avoid the Solaris 5.3 bug that _DYNAMIC isn't being set */ |
| 183 |
/* up properly in dynamically linked .so's. This means we have */ |
| 184 |
/* to use its value in the set of original object files loaded */ |
| 185 |
/* at program startup. */ |
| 186 |
if( dynStructureAddr == 0 ) { |
| 187 |
void* startupSyms = dlopen(0, RTLD_LAZY); |
| 188 |
dynStructureAddr = (ElfW(Dyn)*)(word)dlsym(startupSyms, "_DYNAMIC"); |
| 189 |
} |
| 190 |
# else |
| 191 |
dynStructureAddr = &_DYNAMIC; |
| 192 |
# endif |
| 193 |
|
| 194 |
if (0 == COVERT_DATAFLOW(dynStructureAddr)) { |
| 195 |
/* _DYNAMIC symbol not resolved. */ |
| 196 |
return(0); |
| 197 |
} |
| 198 |
if (cachedResult == 0) { |
| 199 |
int tag; |
| 200 |
for( dp = ((ElfW(Dyn) *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) { |
| 201 |
if (tag == DT_DEBUG) { |
| 202 |
struct r_debug *rd = (struct r_debug *)dp->d_un.d_ptr; |
| 203 |
if (rd != NULL) { |
| 204 |
struct link_map *lm = rd->r_map; |
| 205 |
if (lm != NULL) |
| 206 |
cachedResult = lm->l_next; /* might be NULL */ |
| 207 |
} |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
return cachedResult; |
| 213 |
} |
| 214 |
|
| 215 |
#endif /* SOLARISDL ... */ |
| 216 |
|
| 217 |
/* BTL: added to fix circular dlopen definition if GC_SOLARIS_THREADS defined */ |
| 218 |
# ifdef GC_MUST_RESTORE_REDEFINED_DLOPEN |
| 219 |
# define dlopen GC_dlopen |
| 220 |
# endif |
| 221 |
|
| 222 |
# if defined(SOLARISDL) |
| 223 |
|
| 224 |
/* Add dynamic library data sections to the root set. */ |
| 225 |
# if !defined(PCR) && !defined(GC_SOLARIS_THREADS) && defined(THREADS) \ |
| 226 |
&& !defined(CPPCHECK) |
| 227 |
# error Fix mutual exclusion with dlopen |
| 228 |
# endif |
| 229 |
|
| 230 |
# ifndef USE_PROC_FOR_LIBRARIES |
| 231 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 232 |
{ |
| 233 |
struct link_map *lm; |
| 234 |
|
| 235 |
for (lm = GC_FirstDLOpenedLinkMap(); lm != 0; lm = lm->l_next) { |
| 236 |
ElfW(Ehdr) * e; |
| 237 |
ElfW(Phdr) * p; |
| 238 |
unsigned long offset; |
| 239 |
char * start; |
| 240 |
int i; |
| 241 |
|
| 242 |
e = (ElfW(Ehdr) *) lm->l_addr; |
| 243 |
p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff)); |
| 244 |
offset = ((unsigned long)(lm->l_addr)); |
| 245 |
for( i = 0; i < (int)e->e_phnum; i++, p++ ) { |
| 246 |
switch( p->p_type ) { |
| 247 |
case PT_LOAD: |
| 248 |
{ |
| 249 |
if( !(p->p_flags & PF_W) ) break; |
| 250 |
start = ((char *)(p->p_vaddr)) + offset; |
| 251 |
GC_add_roots_inner(start, start + p->p_memsz, TRUE); |
| 252 |
} |
| 253 |
break; |
| 254 |
default: |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
# endif /* !USE_PROC_FOR_LIBRARIES */ |
| 262 |
# endif /* SOLARISDL */ |
| 263 |
|
| 264 |
#if defined(SCO_ELF) || defined(DGUX) || defined(HURD) || defined(NACL) \ |
| 265 |
|| (defined(__ELF__) && (defined(LINUX) || defined(FREEBSD) \ |
| 266 |
|| defined(NETBSD) || defined(OPENBSD))) |
| 267 |
|
| 268 |
#ifdef USE_PROC_FOR_LIBRARIES |
| 269 |
|
| 270 |
#include <string.h> |
| 271 |
|
| 272 |
#include <sys/stat.h> |
| 273 |
#include <fcntl.h> |
| 274 |
#include <unistd.h> |
| 275 |
|
| 276 |
#define MAPS_BUF_SIZE (32*1024) |
| 277 |
|
| 278 |
/* Sort an array of HeapSects by start address. */ |
| 279 |
/* Unfortunately at least some versions of */ |
| 280 |
/* Linux qsort end up calling malloc by way of sysconf, and hence can't */ |
| 281 |
/* be used in the collector. Hence we roll our own. Should be */ |
| 282 |
/* reasonably fast if the array is already mostly sorted, as we expect */ |
| 283 |
/* it to be. */ |
| 284 |
static void sort_heap_sects(struct HeapSect *base, size_t number_of_elements) |
| 285 |
{ |
| 286 |
signed_word n = (signed_word)number_of_elements; |
| 287 |
signed_word nsorted = 1; |
| 288 |
|
| 289 |
while (nsorted < n) { |
| 290 |
signed_word i; |
| 291 |
|
| 292 |
while (nsorted < n && |
| 293 |
(word)base[nsorted-1].hs_start < (word)base[nsorted].hs_start) |
| 294 |
++nsorted; |
| 295 |
if (nsorted == n) break; |
| 296 |
GC_ASSERT((word)base[nsorted-1].hs_start > (word)base[nsorted].hs_start); |
| 297 |
i = nsorted - 1; |
| 298 |
while (i >= 0 && (word)base[i].hs_start > (word)base[i+1].hs_start) { |
| 299 |
struct HeapSect tmp = base[i]; |
| 300 |
base[i] = base[i+1]; |
| 301 |
base[i+1] = tmp; |
| 302 |
--i; |
| 303 |
} |
| 304 |
GC_ASSERT((word)base[nsorted-1].hs_start < (word)base[nsorted].hs_start); |
| 305 |
++nsorted; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
STATIC void GC_register_map_entries(const char *maps) |
| 310 |
{ |
| 311 |
const char *prot; |
| 312 |
ptr_t start, end; |
| 313 |
unsigned int maj_dev; |
| 314 |
ptr_t least_ha, greatest_ha; |
| 315 |
unsigned i; |
| 316 |
|
| 317 |
GC_ASSERT(I_HOLD_LOCK()); |
| 318 |
sort_heap_sects(GC_our_memory, GC_n_memory); |
| 319 |
least_ha = GC_our_memory[0].hs_start; |
| 320 |
greatest_ha = GC_our_memory[GC_n_memory-1].hs_start |
| 321 |
+ GC_our_memory[GC_n_memory-1].hs_bytes; |
| 322 |
|
| 323 |
for (;;) { |
| 324 |
maps = GC_parse_map_entry(maps, &start, &end, &prot, &maj_dev, 0); |
| 325 |
if (NULL == maps) break; |
| 326 |
|
| 327 |
if (prot[1] == 'w') { |
| 328 |
/* This is a writable mapping. Add it to */ |
| 329 |
/* the root set unless it is already otherwise */ |
| 330 |
/* accounted for. */ |
| 331 |
if ((word)start <= (word)GC_stackbottom |
| 332 |
&& (word)end >= (word)GC_stackbottom) { |
| 333 |
/* Stack mapping; discard */ |
| 334 |
continue; |
| 335 |
} |
| 336 |
# ifdef THREADS |
| 337 |
/* This may fail, since a thread may already be */ |
| 338 |
/* unregistered, but its thread stack may still be there. */ |
| 339 |
/* That can fail because the stack may disappear while */ |
| 340 |
/* we're marking. Thus the marker is, and has to be */ |
| 341 |
/* prepared to recover from segmentation faults. */ |
| 342 |
|
| 343 |
if (GC_segment_is_thread_stack(start, end)) continue; |
| 344 |
|
| 345 |
/* FIXME: NPTL squirrels */ |
| 346 |
/* away pointers in pieces of the stack segment that we */ |
| 347 |
/* don't scan. We work around this */ |
| 348 |
/* by treating anything allocated by libpthread as */ |
| 349 |
/* uncollectible, as we do in some other cases. */ |
| 350 |
/* A specifically identified problem is that */ |
| 351 |
/* thread stacks contain pointers to dynamic thread */ |
| 352 |
/* vectors, which may be reused due to thread caching. */ |
| 353 |
/* They may not be marked if the thread is still live. */ |
| 354 |
/* This specific instance should be addressed by */ |
| 355 |
/* INCLUDE_LINUX_THREAD_DESCR, but that doesn't quite */ |
| 356 |
/* seem to suffice. */ |
| 357 |
/* We currently trace entire thread stacks, if they are */ |
| 358 |
/* are currently cached but unused. This is */ |
| 359 |
/* very suboptimal for performance reasons. */ |
| 360 |
# endif |
| 361 |
/* We no longer exclude the main data segment. */ |
| 362 |
if ((word)end <= (word)least_ha |
| 363 |
|| (word)start >= (word)greatest_ha) { |
| 364 |
/* The easy case; just trace entire segment */ |
| 365 |
GC_add_roots_inner(start, end, TRUE); |
| 366 |
continue; |
| 367 |
} |
| 368 |
/* Add sections that don't belong to us. */ |
| 369 |
i = 0; |
| 370 |
while ((word)(GC_our_memory[i].hs_start |
| 371 |
+ GC_our_memory[i].hs_bytes) < (word)start) |
| 372 |
++i; |
| 373 |
GC_ASSERT(i < GC_n_memory); |
| 374 |
if ((word)GC_our_memory[i].hs_start <= (word)start) { |
| 375 |
start = GC_our_memory[i].hs_start |
| 376 |
+ GC_our_memory[i].hs_bytes; |
| 377 |
++i; |
| 378 |
} |
| 379 |
while (i < GC_n_memory |
| 380 |
&& (word)GC_our_memory[i].hs_start < (word)end |
| 381 |
&& (word)start < (word)end) { |
| 382 |
if ((word)start < (word)GC_our_memory[i].hs_start) |
| 383 |
GC_add_roots_inner(start, |
| 384 |
GC_our_memory[i].hs_start, TRUE); |
| 385 |
start = GC_our_memory[i].hs_start |
| 386 |
+ GC_our_memory[i].hs_bytes; |
| 387 |
++i; |
| 388 |
} |
| 389 |
if ((word)start < (word)end) |
| 390 |
GC_add_roots_inner(start, end, TRUE); |
| 391 |
} else if (prot[0] == '-' && prot[1] == '-' && prot[2] == '-') { |
| 392 |
/* Even roots added statically might disappear partially */ |
| 393 |
/* (e.g. the roots added by INCLUDE_LINUX_THREAD_DESCR). */ |
| 394 |
GC_remove_roots_subregion(start, end); |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 400 |
{ |
| 401 |
GC_register_map_entries(GC_get_maps()); |
| 402 |
} |
| 403 |
|
| 404 |
/* We now take care of the main data segment ourselves: */ |
| 405 |
GC_INNER GC_bool GC_register_main_static_data(void) |
| 406 |
{ |
| 407 |
return FALSE; |
| 408 |
} |
| 409 |
|
| 410 |
# define HAVE_REGISTER_MAIN_STATIC_DATA |
| 411 |
|
| 412 |
#else /* !USE_PROC_FOR_LIBRARIES */ |
| 413 |
|
| 414 |
/* The following is the preferred way to walk dynamic libraries */ |
| 415 |
/* for glibc 2.2.4+. Unfortunately, it doesn't work for older */ |
| 416 |
/* versions. Thanks to Jakub Jelinek for most of the code. */ |
| 417 |
|
| 418 |
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \ |
| 419 |
|| (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)) \ |
| 420 |
|| defined(HOST_ANDROID) /* Are others OK here, too? */ |
| 421 |
# ifndef HAVE_DL_ITERATE_PHDR |
| 422 |
# define HAVE_DL_ITERATE_PHDR |
| 423 |
# endif |
| 424 |
# ifdef HOST_ANDROID |
| 425 |
/* Android headers might have no such definition for some targets. */ |
| 426 |
EXTERN_C_BEGIN |
| 427 |
extern int dl_iterate_phdr(int (*cb)(struct dl_phdr_info *, |
| 428 |
size_t, void *), |
| 429 |
void *data); |
| 430 |
EXTERN_C_END |
| 431 |
# endif |
| 432 |
#endif /* __GLIBC__ >= 2 || HOST_ANDROID */ |
| 433 |
|
| 434 |
#if defined(__DragonFly__) || defined(__FreeBSD_kernel__) \ |
| 435 |
|| (defined(FREEBSD) && __FreeBSD__ >= 7) |
| 436 |
/* On the FreeBSD system, any target system at major version 7 shall */ |
| 437 |
/* have dl_iterate_phdr; therefore, we need not make it weak as below. */ |
| 438 |
# ifndef HAVE_DL_ITERATE_PHDR |
| 439 |
# define HAVE_DL_ITERATE_PHDR |
| 440 |
# endif |
| 441 |
# define DL_ITERATE_PHDR_STRONG |
| 442 |
#elif defined(HAVE_DL_ITERATE_PHDR) |
| 443 |
/* We have the header files for a glibc that includes dl_iterate_phdr.*/ |
| 444 |
/* It may still not be available in the library on the target system. */ |
| 445 |
/* Thus we also treat it as a weak symbol. */ |
| 446 |
EXTERN_C_BEGIN |
| 447 |
# pragma weak dl_iterate_phdr |
| 448 |
EXTERN_C_END |
| 449 |
#endif |
| 450 |
|
| 451 |
#if defined(HAVE_DL_ITERATE_PHDR) |
| 452 |
|
| 453 |
# ifdef PT_GNU_RELRO |
| 454 |
/* Instead of registering PT_LOAD sections directly, we keep them */ |
| 455 |
/* in a temporary list, and filter them by excluding PT_GNU_RELRO */ |
| 456 |
/* segments. Processing PT_GNU_RELRO sections with */ |
| 457 |
/* GC_exclude_static_roots instead would be superficially cleaner. But */ |
| 458 |
/* it runs into trouble if a client registers an overlapping segment, */ |
| 459 |
/* which unfortunately seems quite possible. */ |
| 460 |
|
| 461 |
# define MAX_LOAD_SEGS MAX_ROOT_SETS |
| 462 |
|
| 463 |
static struct load_segment { |
| 464 |
ptr_t start; |
| 465 |
ptr_t end; |
| 466 |
/* Room for a second segment if we remove a RELRO segment */ |
| 467 |
/* from the middle. */ |
| 468 |
ptr_t start2; |
| 469 |
ptr_t end2; |
| 470 |
} load_segs[MAX_LOAD_SEGS]; |
| 471 |
|
| 472 |
static int n_load_segs; |
| 473 |
static GC_bool load_segs_overflow; |
| 474 |
# endif /* PT_GNU_RELRO */ |
| 475 |
|
| 476 |
STATIC int GC_register_dynlib_callback(struct dl_phdr_info * info, |
| 477 |
size_t size, void * ptr) |
| 478 |
{ |
| 479 |
const ElfW(Phdr) * p; |
| 480 |
ptr_t start, end; |
| 481 |
int i; |
| 482 |
|
| 483 |
/* Make sure struct dl_phdr_info is at least as big as we need. */ |
| 484 |
if (size < offsetof (struct dl_phdr_info, dlpi_phnum) |
| 485 |
+ sizeof (info->dlpi_phnum)) |
| 486 |
return -1; |
| 487 |
|
| 488 |
p = info->dlpi_phdr; |
| 489 |
for (i = 0; i < (int)info->dlpi_phnum; i++, p++) { |
| 490 |
if (p->p_type == PT_LOAD) { |
| 491 |
GC_has_static_roots_func callback = GC_has_static_roots; |
| 492 |
if ((p->p_flags & PF_W) == 0) continue; |
| 493 |
|
| 494 |
start = (ptr_t)p->p_vaddr + info->dlpi_addr; |
| 495 |
end = start + p->p_memsz; |
| 496 |
if (callback != 0 && !callback(info->dlpi_name, start, p->p_memsz)) |
| 497 |
continue; |
| 498 |
# ifdef PT_GNU_RELRO |
| 499 |
# if CPP_WORDSZ == 64 |
| 500 |
/* TODO: GC_push_all eventually does the correct */ |
| 501 |
/* rounding to the next multiple of ALIGNMENT, so, most */ |
| 502 |
/* probably, we should remove the corresponding assertion */ |
| 503 |
/* check in GC_add_roots_inner along with this code line. */ |
| 504 |
/* start pointer value may require aligning. */ |
| 505 |
start = (ptr_t)((word)start & ~(word)(sizeof(word) - 1)); |
| 506 |
# endif |
| 507 |
if (n_load_segs >= MAX_LOAD_SEGS) { |
| 508 |
if (!load_segs_overflow) { |
| 509 |
WARN("Too many PT_LOAD segments;" |
| 510 |
" registering as roots directly...\n", 0); |
| 511 |
load_segs_overflow = TRUE; |
| 512 |
} |
| 513 |
GC_add_roots_inner(start, end, TRUE); |
| 514 |
} else { |
| 515 |
load_segs[n_load_segs].start = start; |
| 516 |
load_segs[n_load_segs].end = end; |
| 517 |
load_segs[n_load_segs].start2 = 0; |
| 518 |
load_segs[n_load_segs].end2 = 0; |
| 519 |
++n_load_segs; |
| 520 |
} |
| 521 |
# else |
| 522 |
GC_add_roots_inner(start, end, TRUE); |
| 523 |
# endif /* !PT_GNU_RELRO */ |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
# ifdef PT_GNU_RELRO |
| 528 |
p = info->dlpi_phdr; |
| 529 |
for (i = 0; i < (int)info->dlpi_phnum; i++, p++) { |
| 530 |
if (p->p_type == PT_GNU_RELRO) { |
| 531 |
/* This entry is known to be constant and will eventually be */ |
| 532 |
/* remapped as read-only. However, the address range covered */ |
| 533 |
/* by this entry is typically a subset of a previously */ |
| 534 |
/* encountered "LOAD" segment, so we need to exclude it. */ |
| 535 |
int j; |
| 536 |
|
| 537 |
start = (ptr_t)p->p_vaddr + info->dlpi_addr; |
| 538 |
end = start + p->p_memsz; |
| 539 |
for (j = n_load_segs; --j >= 0; ) { |
| 540 |
if ((word)start >= (word)load_segs[j].start |
| 541 |
&& (word)start < (word)load_segs[j].end) { |
| 542 |
if (load_segs[j].start2 != 0) { |
| 543 |
WARN("More than one GNU_RELRO segment per load one\n",0); |
| 544 |
} else { |
| 545 |
GC_ASSERT((word)end <= |
| 546 |
(((word)load_segs[j].end + GC_page_size - 1) & |
| 547 |
~(word)(GC_page_size - 1))); |
| 548 |
/* Remove from the existing load segment */ |
| 549 |
load_segs[j].end2 = load_segs[j].end; |
| 550 |
load_segs[j].end = start; |
| 551 |
load_segs[j].start2 = end; |
| 552 |
/* Note that start2 may be greater than end2 because of */ |
| 553 |
/* p->p_memsz value multiple of page size. */ |
| 554 |
} |
| 555 |
break; |
| 556 |
} |
| 557 |
if (0 == j && 0 == GC_has_static_roots) |
| 558 |
WARN("Failed to find PT_GNU_RELRO segment" |
| 559 |
" inside PT_LOAD region\n", 0); |
| 560 |
/* No warning reported in case of the callback is present */ |
| 561 |
/* because most likely the segment has been excluded. */ |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
# endif |
| 566 |
|
| 567 |
*(int *)ptr = 1; /* Signal that we were called */ |
| 568 |
return 0; |
| 569 |
} |
| 570 |
|
| 571 |
/* Do we need to separately register the main static data segment? */ |
| 572 |
GC_INNER GC_bool GC_register_main_static_data(void) |
| 573 |
{ |
| 574 |
# ifdef DL_ITERATE_PHDR_STRONG |
| 575 |
/* If dl_iterate_phdr is not a weak symbol then don't test against */ |
| 576 |
/* zero (otherwise a compiler might issue a warning). */ |
| 577 |
return FALSE; |
| 578 |
# else |
| 579 |
return 0 == COVERT_DATAFLOW(dl_iterate_phdr); |
| 580 |
# endif |
| 581 |
} |
| 582 |
|
| 583 |
/* Return TRUE if we succeed, FALSE if dl_iterate_phdr wasn't there. */ |
| 584 |
STATIC GC_bool GC_register_dynamic_libraries_dl_iterate_phdr(void) |
| 585 |
{ |
| 586 |
int did_something; |
| 587 |
if (GC_register_main_static_data()) |
| 588 |
return FALSE; |
| 589 |
|
| 590 |
# ifdef PT_GNU_RELRO |
| 591 |
{ |
| 592 |
static GC_bool excluded_segs = FALSE; |
| 593 |
n_load_segs = 0; |
| 594 |
load_segs_overflow = FALSE; |
| 595 |
if (!EXPECT(excluded_segs, TRUE)) { |
| 596 |
GC_exclude_static_roots_inner((ptr_t)load_segs, |
| 597 |
(ptr_t)load_segs + sizeof(load_segs)); |
| 598 |
excluded_segs = TRUE; |
| 599 |
} |
| 600 |
} |
| 601 |
# endif |
| 602 |
|
| 603 |
did_something = 0; |
| 604 |
dl_iterate_phdr(GC_register_dynlib_callback, &did_something); |
| 605 |
if (did_something) { |
| 606 |
# ifdef PT_GNU_RELRO |
| 607 |
int i; |
| 608 |
|
| 609 |
for (i = 0; i < n_load_segs; ++i) { |
| 610 |
if ((word)load_segs[i].end > (word)load_segs[i].start) { |
| 611 |
GC_add_roots_inner(load_segs[i].start, load_segs[i].end, TRUE); |
| 612 |
} |
| 613 |
if ((word)load_segs[i].end2 > (word)load_segs[i].start2) { |
| 614 |
GC_add_roots_inner(load_segs[i].start2, load_segs[i].end2, TRUE); |
| 615 |
} |
| 616 |
} |
| 617 |
# endif |
| 618 |
} else { |
| 619 |
ptr_t datastart, dataend; |
| 620 |
# ifdef DATASTART_IS_FUNC |
| 621 |
static ptr_t datastart_cached = (ptr_t)GC_WORD_MAX; |
| 622 |
|
| 623 |
/* Evaluate DATASTART only once. */ |
| 624 |
if (datastart_cached == (ptr_t)GC_WORD_MAX) { |
| 625 |
datastart_cached = DATASTART; |
| 626 |
} |
| 627 |
datastart = datastart_cached; |
| 628 |
# else |
| 629 |
datastart = DATASTART; |
| 630 |
# endif |
| 631 |
# ifdef DATAEND_IS_FUNC |
| 632 |
{ |
| 633 |
static ptr_t dataend_cached = 0; |
| 634 |
/* Evaluate DATAEND only once. */ |
| 635 |
if (dataend_cached == 0) { |
| 636 |
dataend_cached = DATAEND; |
| 637 |
} |
| 638 |
dataend = dataend_cached; |
| 639 |
} |
| 640 |
# else |
| 641 |
dataend = DATAEND; |
| 642 |
# endif |
| 643 |
if (NULL == *(char * volatile *)&datastart |
| 644 |
|| (word)datastart > (word)dataend) |
| 645 |
ABORT_ARG2("Wrong DATASTART/END pair", |
| 646 |
": %p .. %p", (void *)datastart, (void *)dataend); |
| 647 |
|
| 648 |
/* dl_iterate_phdr may forget the static data segment in */ |
| 649 |
/* statically linked executables. */ |
| 650 |
GC_add_roots_inner(datastart, dataend, TRUE); |
| 651 |
# ifdef GC_HAVE_DATAREGION2 |
| 652 |
if ((word)DATASTART2 - 1U >= (word)DATAEND2) { |
| 653 |
/* Subtract one to check also for NULL */ |
| 654 |
/* without a compiler warning. */ |
| 655 |
ABORT_ARG2("Wrong DATASTART/END2 pair", |
| 656 |
": %p .. %p", (void *)DATASTART2, (void *)DATAEND2); |
| 657 |
} |
| 658 |
GC_add_roots_inner(DATASTART2, DATAEND2, TRUE); |
| 659 |
# endif |
| 660 |
} |
| 661 |
return TRUE; |
| 662 |
} |
| 663 |
|
| 664 |
# define HAVE_REGISTER_MAIN_STATIC_DATA |
| 665 |
|
| 666 |
#else /* !HAVE_DL_ITERATE_PHDR */ |
| 667 |
|
| 668 |
/* Dynamic loading code for Linux running ELF. Somewhat tested on |
| 669 |
* Linux/x86, untested but hopefully should work on Linux/Alpha. |
| 670 |
* This code was derived from the Solaris/ELF support. Thanks to |
| 671 |
* whatever kind soul wrote that. - Patrick Bridges */ |
| 672 |
|
| 673 |
/* This doesn't necessarily work in all cases, e.g. with preloaded |
| 674 |
* dynamic libraries. */ |
| 675 |
|
| 676 |
# if defined(NETBSD) || defined(OPENBSD) |
| 677 |
# include <sys/exec_elf.h> |
| 678 |
/* for compatibility with 1.4.x */ |
| 679 |
# ifndef DT_DEBUG |
| 680 |
# define DT_DEBUG 21 |
| 681 |
# endif |
| 682 |
# ifndef PT_LOAD |
| 683 |
# define PT_LOAD 1 |
| 684 |
# endif |
| 685 |
# ifndef PF_W |
| 686 |
# define PF_W 2 |
| 687 |
# endif |
| 688 |
# elif !defined(HOST_ANDROID) |
| 689 |
# include <elf.h> |
| 690 |
# endif |
| 691 |
|
| 692 |
# ifndef HOST_ANDROID |
| 693 |
# include <link.h> |
| 694 |
# endif |
| 695 |
|
| 696 |
#endif /* !HAVE_DL_ITERATE_PHDR */ |
| 697 |
|
| 698 |
EXTERN_C_BEGIN |
| 699 |
#ifdef __GNUC__ |
| 700 |
# pragma weak _DYNAMIC |
| 701 |
#endif |
| 702 |
extern ElfW(Dyn) _DYNAMIC[]; |
| 703 |
EXTERN_C_END |
| 704 |
|
| 705 |
STATIC struct link_map * |
| 706 |
GC_FirstDLOpenedLinkMap(void) |
| 707 |
{ |
| 708 |
static struct link_map *cachedResult = 0; |
| 709 |
|
| 710 |
if (0 == COVERT_DATAFLOW(_DYNAMIC)) { |
| 711 |
/* _DYNAMIC symbol not resolved. */ |
| 712 |
return(0); |
| 713 |
} |
| 714 |
if( cachedResult == 0 ) { |
| 715 |
# if defined(NETBSD) && defined(RTLD_DI_LINKMAP) |
| 716 |
# if defined(CPPCHECK) |
| 717 |
# define GC_RTLD_DI_LINKMAP 2 |
| 718 |
# else |
| 719 |
# define GC_RTLD_DI_LINKMAP RTLD_DI_LINKMAP |
| 720 |
# endif |
| 721 |
struct link_map *lm = NULL; |
| 722 |
if (!dlinfo(RTLD_SELF, GC_RTLD_DI_LINKMAP, &lm) && lm != NULL) { |
| 723 |
/* Now lm points link_map object of libgc. Since it */ |
| 724 |
/* might not be the first dynamically linked object, */ |
| 725 |
/* try to find it (object next to the main object). */ |
| 726 |
while (lm->l_prev != NULL) { |
| 727 |
lm = lm->l_prev; |
| 728 |
} |
| 729 |
cachedResult = lm->l_next; |
| 730 |
} |
| 731 |
# else |
| 732 |
ElfW(Dyn) *dp; |
| 733 |
int tag; |
| 734 |
|
| 735 |
for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) { |
| 736 |
if (tag == DT_DEBUG) { |
| 737 |
struct r_debug *rd = (struct r_debug *)dp->d_un.d_ptr; |
| 738 |
/* d_ptr could be null if libs are linked statically. */ |
| 739 |
if (rd != NULL) { |
| 740 |
struct link_map *lm = rd->r_map; |
| 741 |
if (lm != NULL) |
| 742 |
cachedResult = lm->l_next; /* might be NULL */ |
| 743 |
} |
| 744 |
break; |
| 745 |
} |
| 746 |
} |
| 747 |
# endif /* !NETBSD || !RTLD_DI_LINKMAP */ |
| 748 |
} |
| 749 |
return cachedResult; |
| 750 |
} |
| 751 |
|
| 752 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 753 |
{ |
| 754 |
struct link_map *lm; |
| 755 |
|
| 756 |
# ifdef HAVE_DL_ITERATE_PHDR |
| 757 |
if (GC_register_dynamic_libraries_dl_iterate_phdr()) { |
| 758 |
return; |
| 759 |
} |
| 760 |
# endif |
| 761 |
for (lm = GC_FirstDLOpenedLinkMap(); lm != 0; lm = lm->l_next) |
| 762 |
{ |
| 763 |
ElfW(Ehdr) * e; |
| 764 |
ElfW(Phdr) * p; |
| 765 |
unsigned long offset; |
| 766 |
char * start; |
| 767 |
int i; |
| 768 |
|
| 769 |
e = (ElfW(Ehdr) *) lm->l_addr; |
| 770 |
# ifdef HOST_ANDROID |
| 771 |
if (e == NULL) |
| 772 |
continue; |
| 773 |
# endif |
| 774 |
p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff)); |
| 775 |
offset = ((unsigned long)(lm->l_addr)); |
| 776 |
for( i = 0; i < (int)e->e_phnum; i++, p++ ) { |
| 777 |
switch( p->p_type ) { |
| 778 |
case PT_LOAD: |
| 779 |
{ |
| 780 |
if( !(p->p_flags & PF_W) ) break; |
| 781 |
start = ((char *)(p->p_vaddr)) + offset; |
| 782 |
GC_add_roots_inner(start, start + p->p_memsz, TRUE); |
| 783 |
} |
| 784 |
break; |
| 785 |
default: |
| 786 |
break; |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
#endif /* !USE_PROC_FOR_LIBRARIES */ |
| 793 |
|
| 794 |
#endif /* LINUX */ |
| 795 |
|
| 796 |
#if defined(IRIX5) || (defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX)) |
| 797 |
|
| 798 |
#include <sys/procfs.h> |
| 799 |
#include <sys/stat.h> |
| 800 |
#include <fcntl.h> |
| 801 |
#include <elf.h> |
| 802 |
#include <errno.h> |
| 803 |
#include <signal.h> /* Only for the following test. */ |
| 804 |
#ifndef _sigargs |
| 805 |
# define IRIX6 |
| 806 |
#endif |
| 807 |
|
| 808 |
/* We use /proc to track down all parts of the address space that are */ |
| 809 |
/* mapped by the process, and throw out regions we know we shouldn't */ |
| 810 |
/* worry about. This may also work under other SVR4 variants. */ |
| 811 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 812 |
{ |
| 813 |
static int fd = -1; |
| 814 |
char buf[30]; |
| 815 |
static prmap_t * addr_map = 0; |
| 816 |
static int current_sz = 0; /* Number of records currently in addr_map */ |
| 817 |
int needed_sz = 0; /* Required size of addr_map */ |
| 818 |
int i; |
| 819 |
long flags; |
| 820 |
ptr_t start; |
| 821 |
ptr_t limit; |
| 822 |
ptr_t heap_start = HEAP_START; |
| 823 |
ptr_t heap_end = heap_start; |
| 824 |
|
| 825 |
# ifdef SOLARISDL |
| 826 |
# define MA_PHYS 0 |
| 827 |
# endif /* SOLARISDL */ |
| 828 |
|
| 829 |
if (fd < 0) { |
| 830 |
(void)snprintf(buf, sizeof(buf), "/proc/%ld", (long)getpid()); |
| 831 |
buf[sizeof(buf) - 1] = '\0'; |
| 832 |
fd = open(buf, O_RDONLY); |
| 833 |
if (fd < 0) { |
| 834 |
ABORT("/proc open failed"); |
| 835 |
} |
| 836 |
} |
| 837 |
if (ioctl(fd, PIOCNMAP, &needed_sz) < 0) { |
| 838 |
ABORT_ARG2("/proc PIOCNMAP ioctl failed", |
| 839 |
": fd= %d, errno= %d", fd, errno); |
| 840 |
} |
| 841 |
if (needed_sz >= current_sz) { |
| 842 |
GC_scratch_recycle_no_gww(addr_map, |
| 843 |
(size_t)current_sz * sizeof(prmap_t)); |
| 844 |
current_sz = needed_sz * 2 + 1; |
| 845 |
/* Expansion, plus room for 0 record */ |
| 846 |
addr_map = (prmap_t *)GC_scratch_alloc( |
| 847 |
(size_t)current_sz * sizeof(prmap_t)); |
| 848 |
if (addr_map == NULL) |
| 849 |
ABORT("Insufficient memory for address map"); |
| 850 |
} |
| 851 |
if (ioctl(fd, PIOCMAP, addr_map) < 0) { |
| 852 |
ABORT_ARG3("/proc PIOCMAP ioctl failed", |
| 853 |
": errcode= %d, needed_sz= %d, addr_map= %p", |
| 854 |
errno, needed_sz, (void *)addr_map); |
| 855 |
} |
| 856 |
if (GC_n_heap_sects > 0) { |
| 857 |
heap_end = GC_heap_sects[GC_n_heap_sects-1].hs_start |
| 858 |
+ GC_heap_sects[GC_n_heap_sects-1].hs_bytes; |
| 859 |
if ((word)heap_end < (word)GC_scratch_last_end_ptr) |
| 860 |
heap_end = GC_scratch_last_end_ptr; |
| 861 |
} |
| 862 |
for (i = 0; i < needed_sz; i++) { |
| 863 |
flags = addr_map[i].pr_mflags; |
| 864 |
if ((flags & (MA_BREAK | MA_STACK | MA_PHYS |
| 865 |
| MA_FETCHOP | MA_NOTCACHED)) != 0) goto irrelevant; |
| 866 |
if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE)) |
| 867 |
goto irrelevant; |
| 868 |
/* The latter test is empirically useless in very old Irix */ |
| 869 |
/* versions. Other than the */ |
| 870 |
/* main data and stack segments, everything appears to be */ |
| 871 |
/* mapped readable, writable, executable, and shared(!!). */ |
| 872 |
/* This makes no sense to me. - HB */ |
| 873 |
start = (ptr_t)(addr_map[i].pr_vaddr); |
| 874 |
if (GC_roots_present(start)) goto irrelevant; |
| 875 |
if ((word)start < (word)heap_end && (word)start >= (word)heap_start) |
| 876 |
goto irrelevant; |
| 877 |
|
| 878 |
limit = start + addr_map[i].pr_size; |
| 879 |
/* The following seemed to be necessary for very old versions */ |
| 880 |
/* of Irix, but it has been reported to discard relevant */ |
| 881 |
/* segments under Irix 6.5. */ |
| 882 |
# ifndef IRIX6 |
| 883 |
if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) { |
| 884 |
/* Discard text segments, i.e. 0-offset mappings against */ |
| 885 |
/* executable files which appear to have ELF headers. */ |
| 886 |
caddr_t arg; |
| 887 |
int obj; |
| 888 |
# define MAP_IRR_SZ 10 |
| 889 |
static ptr_t map_irr[MAP_IRR_SZ]; |
| 890 |
/* Known irrelevant map entries */ |
| 891 |
static int n_irr = 0; |
| 892 |
struct stat buf; |
| 893 |
int j; |
| 894 |
|
| 895 |
for (j = 0; j < n_irr; j++) { |
| 896 |
if (map_irr[j] == start) goto irrelevant; |
| 897 |
} |
| 898 |
arg = (caddr_t)start; |
| 899 |
obj = ioctl(fd, PIOCOPENM, &arg); |
| 900 |
if (obj >= 0) { |
| 901 |
fstat(obj, &buf); |
| 902 |
close(obj); |
| 903 |
if ((buf.st_mode & 0111) != 0) { |
| 904 |
if (n_irr < MAP_IRR_SZ) { |
| 905 |
map_irr[n_irr++] = start; |
| 906 |
} |
| 907 |
goto irrelevant; |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
# endif /* !IRIX6 */ |
| 912 |
GC_add_roots_inner(start, limit, TRUE); |
| 913 |
irrelevant: ; |
| 914 |
} |
| 915 |
/* Don't keep cached descriptor, for now. Some kernels don't like us */ |
| 916 |
/* to keep a /proc file descriptor around during kill -9. */ |
| 917 |
/* Otherwise, it should also require FD_CLOEXEC and proper handling */ |
| 918 |
/* at fork (i.e. close because of the pid change). */ |
| 919 |
if (close(fd) < 0) ABORT("Couldn't close /proc file"); |
| 920 |
fd = -1; |
| 921 |
} |
| 922 |
|
| 923 |
# endif /* USE_PROC_FOR_LIBRARIES || IRIX5 */ |
| 924 |
|
| 925 |
# if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32) |
| 926 |
|
| 927 |
# include <stdlib.h> |
| 928 |
|
| 929 |
/* We traverse the entire address space and register all segments */ |
| 930 |
/* that could possibly have been written to. */ |
| 931 |
STATIC void GC_cond_add_roots(char *base, char * limit) |
| 932 |
{ |
| 933 |
# ifdef GC_WIN32_THREADS |
| 934 |
char * curr_base = base; |
| 935 |
char * next_stack_lo; |
| 936 |
char * next_stack_hi; |
| 937 |
|
| 938 |
if (base == limit) return; |
| 939 |
for(;;) { |
| 940 |
GC_get_next_stack(curr_base, limit, &next_stack_lo, &next_stack_hi); |
| 941 |
if ((word)next_stack_lo >= (word)limit) break; |
| 942 |
if ((word)next_stack_lo > (word)curr_base) |
| 943 |
GC_add_roots_inner(curr_base, next_stack_lo, TRUE); |
| 944 |
curr_base = next_stack_hi; |
| 945 |
} |
| 946 |
if ((word)curr_base < (word)limit) |
| 947 |
GC_add_roots_inner(curr_base, limit, TRUE); |
| 948 |
# else |
| 949 |
char * stack_top |
| 950 |
= (char *)((word)GC_approx_sp() & |
| 951 |
~(word)(GC_sysinfo.dwAllocationGranularity - 1)); |
| 952 |
|
| 953 |
if (base == limit) return; |
| 954 |
if ((word)limit > (word)stack_top |
| 955 |
&& (word)base < (word)GC_stackbottom) { |
| 956 |
/* Part of the stack; ignore it. */ |
| 957 |
return; |
| 958 |
} |
| 959 |
GC_add_roots_inner(base, limit, TRUE); |
| 960 |
# endif |
| 961 |
} |
| 962 |
|
| 963 |
#ifdef DYNAMIC_LOADING |
| 964 |
/* GC_register_main_static_data is not needed unless DYNAMIC_LOADING. */ |
| 965 |
GC_INNER GC_bool GC_register_main_static_data(void) |
| 966 |
{ |
| 967 |
# if defined(MSWINCE) || defined(CYGWIN32) |
| 968 |
/* Do we need to separately register the main static data segment? */ |
| 969 |
return FALSE; |
| 970 |
# else |
| 971 |
return GC_no_win32_dlls; |
| 972 |
# endif |
| 973 |
} |
| 974 |
# define HAVE_REGISTER_MAIN_STATIC_DATA |
| 975 |
#endif /* DYNAMIC_LOADING */ |
| 976 |
|
| 977 |
# ifdef DEBUG_VIRTUALQUERY |
| 978 |
void GC_dump_meminfo(MEMORY_BASIC_INFORMATION *buf) |
| 979 |
{ |
| 980 |
GC_printf("BaseAddress= 0x%lx, AllocationBase= 0x%lx," |
| 981 |
" RegionSize= 0x%lx(%lu)\n", |
| 982 |
buf -> BaseAddress, buf -> AllocationBase, |
| 983 |
buf -> RegionSize, buf -> RegionSize); |
| 984 |
GC_printf("\tAllocationProtect= 0x%lx, State= 0x%lx, Protect= 0x%lx, " |
| 985 |
"Type= 0x%lx\n", buf -> AllocationProtect, buf -> State, |
| 986 |
buf -> Protect, buf -> Type); |
| 987 |
} |
| 988 |
# endif /* DEBUG_VIRTUALQUERY */ |
| 989 |
|
| 990 |
# if defined(MSWINCE) || defined(CYGWIN32) |
| 991 |
/* FIXME: Should we really need to scan MEM_PRIVATE sections? */ |
| 992 |
/* For now, we don't add MEM_PRIVATE sections to the data roots for */ |
| 993 |
/* WinCE because otherwise SEGV fault sometimes happens to occur in */ |
| 994 |
/* GC_mark_from() (and, even if we use WRAP_MARK_SOME, WinCE prints */ |
| 995 |
/* a "Data Abort" message to the debugging console). */ |
| 996 |
/* To workaround that, use -DGC_REGISTER_MEM_PRIVATE. */ |
| 997 |
# define GC_wnt TRUE |
| 998 |
# endif |
| 999 |
|
| 1000 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 1001 |
{ |
| 1002 |
MEMORY_BASIC_INFORMATION buf; |
| 1003 |
DWORD protect; |
| 1004 |
LPVOID p; |
| 1005 |
char * base; |
| 1006 |
char * limit; |
| 1007 |
|
| 1008 |
# ifdef MSWIN32 |
| 1009 |
if (GC_no_win32_dlls) return; |
| 1010 |
# endif |
| 1011 |
p = GC_sysinfo.lpMinimumApplicationAddress; |
| 1012 |
base = limit = (char *)p; |
| 1013 |
while ((word)p < (word)GC_sysinfo.lpMaximumApplicationAddress) { |
| 1014 |
size_t result = VirtualQuery(p, &buf, sizeof(buf)); |
| 1015 |
|
| 1016 |
# ifdef MSWINCE |
| 1017 |
if (result == 0) { |
| 1018 |
if ((word)p > GC_WORD_MAX - GC_sysinfo.dwAllocationGranularity) |
| 1019 |
break; /* overflow */ |
| 1020 |
/* Page is free; advance to the next possible allocation base. */ |
| 1021 |
p = (LPVOID)(((DWORD)p + GC_sysinfo.dwAllocationGranularity) |
| 1022 |
& ~(GC_sysinfo.dwAllocationGranularity-1)); |
| 1023 |
} else |
| 1024 |
# endif |
| 1025 |
/* else */ { |
| 1026 |
if (result != sizeof(buf)) { |
| 1027 |
ABORT("Weird VirtualQuery result"); |
| 1028 |
} |
| 1029 |
if ((word)p > GC_WORD_MAX - buf.RegionSize) break; /* overflow */ |
| 1030 |
|
| 1031 |
protect = buf.Protect; |
| 1032 |
if (buf.State == MEM_COMMIT |
| 1033 |
&& (protect == PAGE_EXECUTE_READWRITE |
| 1034 |
|| protect == PAGE_EXECUTE_WRITECOPY |
| 1035 |
|| protect == PAGE_READWRITE |
| 1036 |
|| protect == PAGE_WRITECOPY) |
| 1037 |
&& (buf.Type == MEM_IMAGE |
| 1038 |
# ifdef GC_REGISTER_MEM_PRIVATE |
| 1039 |
|| (protect == PAGE_READWRITE && buf.Type == MEM_PRIVATE) |
| 1040 |
# else |
| 1041 |
/* There is some evidence that we cannot always */ |
| 1042 |
/* ignore MEM_PRIVATE sections under Windows ME */ |
| 1043 |
/* and predecessors. Hence we now also check for */ |
| 1044 |
/* that case. */ |
| 1045 |
|| (!GC_wnt && buf.Type == MEM_PRIVATE) |
| 1046 |
# endif |
| 1047 |
) |
| 1048 |
&& !GC_is_heap_base(buf.AllocationBase)) { |
| 1049 |
# ifdef DEBUG_VIRTUALQUERY |
| 1050 |
GC_dump_meminfo(&buf); |
| 1051 |
# endif |
| 1052 |
if ((char *)p != limit) { |
| 1053 |
GC_cond_add_roots(base, limit); |
| 1054 |
base = (char *)p; |
| 1055 |
} |
| 1056 |
limit = (char *)p + buf.RegionSize; |
| 1057 |
} |
| 1058 |
p = (char *)p + buf.RegionSize; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
GC_cond_add_roots(base, limit); |
| 1062 |
} |
| 1063 |
|
| 1064 |
#endif /* MSWIN32 || MSWINCE || CYGWIN32 */ |
| 1065 |
|
| 1066 |
#if defined(ALPHA) && defined(OSF1) |
| 1067 |
|
| 1068 |
#include <loader.h> |
| 1069 |
|
| 1070 |
EXTERN_C_BEGIN |
| 1071 |
extern char *sys_errlist[]; |
| 1072 |
extern int sys_nerr; |
| 1073 |
extern int errno; |
| 1074 |
EXTERN_C_END |
| 1075 |
|
| 1076 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 1077 |
{ |
| 1078 |
ldr_module_t moduleid = LDR_NULL_MODULE; |
| 1079 |
ldr_process_t mypid = ldr_my_process(); /* obtain id of this process */ |
| 1080 |
|
| 1081 |
/* For each module */ |
| 1082 |
while (TRUE) { |
| 1083 |
ldr_module_info_t moduleinfo; |
| 1084 |
size_t modulereturnsize; |
| 1085 |
ldr_region_t region; |
| 1086 |
ldr_region_info_t regioninfo; |
| 1087 |
size_t regionreturnsize; |
| 1088 |
int status = ldr_next_module(mypid, &moduleid); |
| 1089 |
/* Get the next (first) module */ |
| 1090 |
|
| 1091 |
/* Any more modules? */ |
| 1092 |
if (moduleid == LDR_NULL_MODULE) |
| 1093 |
break; /* No more modules */ |
| 1094 |
|
| 1095 |
/* Check status AFTER checking moduleid because */ |
| 1096 |
/* of a bug in the non-shared ldr_next_module stub. */ |
| 1097 |
if (status != 0) { |
| 1098 |
ABORT_ARG3("ldr_next_module failed", |
| 1099 |
": status= %d, errcode= %d (%s)", status, errno, |
| 1100 |
errno < sys_nerr ? sys_errlist[errno] : ""); |
| 1101 |
} |
| 1102 |
|
| 1103 |
/* Get the module information */ |
| 1104 |
status = ldr_inq_module(mypid, moduleid, &moduleinfo, |
| 1105 |
sizeof(moduleinfo), &modulereturnsize); |
| 1106 |
if (status != 0 ) |
| 1107 |
ABORT("ldr_inq_module failed"); |
| 1108 |
|
| 1109 |
/* is module for the main program (i.e. nonshared portion)? */ |
| 1110 |
if (moduleinfo.lmi_flags & LDR_MAIN) |
| 1111 |
continue; /* skip the main module */ |
| 1112 |
|
| 1113 |
# ifdef DL_VERBOSE |
| 1114 |
GC_log_printf("---Module---\n"); |
| 1115 |
GC_log_printf("Module ID: %ld\n", moduleinfo.lmi_modid); |
| 1116 |
GC_log_printf("Count of regions: %d\n", moduleinfo.lmi_nregion); |
| 1117 |
GC_log_printf("Flags for module: %016lx\n", moduleinfo.lmi_flags); |
| 1118 |
GC_log_printf("Module pathname: \"%s\"\n", moduleinfo.lmi_name); |
| 1119 |
# endif |
| 1120 |
|
| 1121 |
/* For each region in this module */ |
| 1122 |
for (region = 0; region < moduleinfo.lmi_nregion; region++) { |
| 1123 |
/* Get the region information */ |
| 1124 |
status = ldr_inq_region(mypid, moduleid, region, ®ioninfo, |
| 1125 |
sizeof(regioninfo), ®ionreturnsize); |
| 1126 |
if (status != 0 ) |
| 1127 |
ABORT("ldr_inq_region failed"); |
| 1128 |
|
| 1129 |
/* only process writable (data) regions */ |
| 1130 |
if (! (regioninfo.lri_prot & LDR_W)) |
| 1131 |
continue; |
| 1132 |
|
| 1133 |
# ifdef DL_VERBOSE |
| 1134 |
GC_log_printf("--- Region ---\n"); |
| 1135 |
GC_log_printf("Region number: %ld\n", regioninfo.lri_region_no); |
| 1136 |
GC_log_printf("Protection flags: %016x\n", regioninfo.lri_prot); |
| 1137 |
GC_log_printf("Virtual address: %p\n", regioninfo.lri_vaddr); |
| 1138 |
GC_log_printf("Mapped address: %p\n", regioninfo.lri_mapaddr); |
| 1139 |
GC_log_printf("Region size: %ld\n", regioninfo.lri_size); |
| 1140 |
GC_log_printf("Region name: \"%s\"\n", regioninfo.lri_name); |
| 1141 |
# endif |
| 1142 |
|
| 1143 |
/* register region as a garbage collection root */ |
| 1144 |
GC_add_roots_inner((char *)regioninfo.lri_mapaddr, |
| 1145 |
(char *)regioninfo.lri_mapaddr + regioninfo.lri_size, |
| 1146 |
TRUE); |
| 1147 |
|
| 1148 |
} |
| 1149 |
} |
| 1150 |
} |
| 1151 |
#endif |
| 1152 |
|
| 1153 |
#if defined(HPUX) |
| 1154 |
|
| 1155 |
#include <errno.h> |
| 1156 |
#include <dl.h> |
| 1157 |
|
| 1158 |
EXTERN_C_BEGIN |
| 1159 |
extern char *sys_errlist[]; |
| 1160 |
extern int sys_nerr; |
| 1161 |
EXTERN_C_END |
| 1162 |
|
| 1163 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 1164 |
{ |
| 1165 |
int index = 1; /* Ordinal position in shared library search list */ |
| 1166 |
|
| 1167 |
/* For each dynamic library loaded */ |
| 1168 |
while (TRUE) { |
| 1169 |
struct shl_descriptor *shl_desc; /* Shared library info, see dl.h */ |
| 1170 |
int status = shl_get(index, &shl_desc); |
| 1171 |
/* Get info about next shared library */ |
| 1172 |
|
| 1173 |
/* Check if this is the end of the list or if some error occurred */ |
| 1174 |
if (status != 0) { |
| 1175 |
# ifdef GC_HPUX_THREADS |
| 1176 |
/* I've seen errno values of 0. The man page is not clear */ |
| 1177 |
/* as to whether errno should get set on a -1 return. */ |
| 1178 |
break; |
| 1179 |
# else |
| 1180 |
if (errno == EINVAL) { |
| 1181 |
break; /* Moved past end of shared library list --> finished */ |
| 1182 |
} else { |
| 1183 |
ABORT_ARG3("shl_get failed", |
| 1184 |
": status= %d, errcode= %d (%s)", status, errno, |
| 1185 |
errno < sys_nerr ? sys_errlist[errno] : ""); |
| 1186 |
} |
| 1187 |
# endif |
| 1188 |
} |
| 1189 |
|
| 1190 |
# ifdef DL_VERBOSE |
| 1191 |
GC_log_printf("---Shared library---\n"); |
| 1192 |
GC_log_printf("filename= \"%s\"\n", shl_desc->filename); |
| 1193 |
GC_log_printf("index= %d\n", index); |
| 1194 |
GC_log_printf("handle= %08x\n", (unsigned long) shl_desc->handle); |
| 1195 |
GC_log_printf("text seg.start= %08x\n", shl_desc->tstart); |
| 1196 |
GC_log_printf("text seg.end= %08x\n", shl_desc->tend); |
| 1197 |
GC_log_printf("data seg.start= %08x\n", shl_desc->dstart); |
| 1198 |
GC_log_printf("data seg.end= %08x\n", shl_desc->dend); |
| 1199 |
GC_log_printf("ref.count= %lu\n", shl_desc->ref_count); |
| 1200 |
# endif |
| 1201 |
|
| 1202 |
/* register shared library's data segment as a garbage collection root */ |
| 1203 |
GC_add_roots_inner((char *) shl_desc->dstart, |
| 1204 |
(char *) shl_desc->dend, TRUE); |
| 1205 |
|
| 1206 |
index++; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
#endif /* HPUX */ |
| 1210 |
|
| 1211 |
#ifdef AIX |
| 1212 |
# include <alloca.h> |
| 1213 |
# include <sys/ldr.h> |
| 1214 |
# include <sys/errno.h> |
| 1215 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 1216 |
{ |
| 1217 |
int ldibuflen = 8192; |
| 1218 |
|
| 1219 |
for (;;) { |
| 1220 |
int len; |
| 1221 |
struct ld_info *ldi; |
| 1222 |
# if defined(CPPCHECK) |
| 1223 |
char ldibuf[ldibuflen]; |
| 1224 |
# else |
| 1225 |
char *ldibuf = alloca(ldibuflen); |
| 1226 |
# endif |
| 1227 |
|
| 1228 |
len = loadquery(L_GETINFO, ldibuf, ldibuflen); |
| 1229 |
if (len < 0) { |
| 1230 |
if (errno != ENOMEM) { |
| 1231 |
ABORT("loadquery failed"); |
| 1232 |
} |
| 1233 |
ldibuflen *= 2; |
| 1234 |
continue; |
| 1235 |
} |
| 1236 |
|
| 1237 |
ldi = (struct ld_info *)ldibuf; |
| 1238 |
while (ldi) { |
| 1239 |
len = ldi->ldinfo_next; |
| 1240 |
GC_add_roots_inner( |
| 1241 |
ldi->ldinfo_dataorg, |
| 1242 |
(ptr_t)(unsigned long)ldi->ldinfo_dataorg |
| 1243 |
+ ldi->ldinfo_datasize, |
| 1244 |
TRUE); |
| 1245 |
ldi = len ? (struct ld_info *)((char *)ldi + len) : 0; |
| 1246 |
} |
| 1247 |
break; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
#endif /* AIX */ |
| 1251 |
|
| 1252 |
#ifdef DARWIN |
| 1253 |
|
| 1254 |
/* __private_extern__ hack required for pre-3.4 gcc versions. */ |
| 1255 |
#ifndef __private_extern__ |
| 1256 |
# define __private_extern__ extern |
| 1257 |
# include <mach-o/dyld.h> |
| 1258 |
# undef __private_extern__ |
| 1259 |
#else |
| 1260 |
# include <mach-o/dyld.h> |
| 1261 |
#endif |
| 1262 |
#include <mach-o/getsect.h> |
| 1263 |
|
| 1264 |
/*#define DARWIN_DEBUG*/ |
| 1265 |
|
| 1266 |
/* Writable sections generally available on Darwin. */ |
| 1267 |
STATIC const struct dyld_sections_s { |
| 1268 |
const char *seg; |
| 1269 |
const char *sect; |
| 1270 |
} GC_dyld_sections[] = { |
| 1271 |
{ SEG_DATA, SECT_DATA }, |
| 1272 |
/* Used by FSF GCC, but not by OS X system tools, so far. */ |
| 1273 |
{ SEG_DATA, "__static_data" }, |
| 1274 |
{ SEG_DATA, SECT_BSS }, |
| 1275 |
{ SEG_DATA, SECT_COMMON }, |
| 1276 |
/* FSF GCC - zero-sized object sections for targets */ |
| 1277 |
/*supporting section anchors. */ |
| 1278 |
{ SEG_DATA, "__zobj_data" }, |
| 1279 |
{ SEG_DATA, "__zobj_bss" } |
| 1280 |
}; |
| 1281 |
|
| 1282 |
/* Additional writable sections: */ |
| 1283 |
/* GCC on Darwin constructs aligned sections "on demand", where */ |
| 1284 |
/* the alignment size is embedded in the section name. */ |
| 1285 |
/* Furthermore, there are distinctions between sections */ |
| 1286 |
/* containing private vs. public symbols. It also constructs */ |
| 1287 |
/* sections specifically for zero-sized objects, when the */ |
| 1288 |
/* target supports section anchors. */ |
| 1289 |
STATIC const char * const GC_dyld_add_sect_fmts[] = { |
| 1290 |
"__bss%u", |
| 1291 |
"__pu_bss%u", |
| 1292 |
"__zo_bss%u", |
| 1293 |
"__zo_pu_bss%u" |
| 1294 |
}; |
| 1295 |
|
| 1296 |
/* Currently, mach-o will allow up to the max of 2^15 alignment */ |
| 1297 |
/* in an object file. */ |
| 1298 |
#ifndef L2_MAX_OFILE_ALIGNMENT |
| 1299 |
# define L2_MAX_OFILE_ALIGNMENT 15 |
| 1300 |
#endif |
| 1301 |
|
| 1302 |
STATIC const char *GC_dyld_name_for_hdr(const struct GC_MACH_HEADER *hdr) |
| 1303 |
{ |
| 1304 |
unsigned long i, c; |
| 1305 |
c = _dyld_image_count(); |
| 1306 |
for (i = 0; i < c; i++) |
| 1307 |
if ((const struct GC_MACH_HEADER *)_dyld_get_image_header(i) == hdr) |
| 1308 |
return _dyld_get_image_name(i); |
| 1309 |
return NULL; |
| 1310 |
} |
| 1311 |
|
| 1312 |
/* This should never be called by a thread holding the lock. */ |
| 1313 |
STATIC void GC_dyld_image_add(const struct GC_MACH_HEADER *hdr, |
| 1314 |
intptr_t slide) |
| 1315 |
{ |
| 1316 |
unsigned long start, end; |
| 1317 |
unsigned i, j; |
| 1318 |
const struct GC_MACH_SECTION *sec; |
| 1319 |
const char *name; |
| 1320 |
GC_has_static_roots_func callback = GC_has_static_roots; |
| 1321 |
DCL_LOCK_STATE; |
| 1322 |
|
| 1323 |
if (GC_no_dls) return; |
| 1324 |
# ifdef DARWIN_DEBUG |
| 1325 |
name = GC_dyld_name_for_hdr(hdr); |
| 1326 |
# else |
| 1327 |
name = callback != 0 ? GC_dyld_name_for_hdr(hdr) : NULL; |
| 1328 |
# endif |
| 1329 |
for (i = 0; i < sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]); i++) { |
| 1330 |
sec = GC_GETSECTBYNAME(hdr, GC_dyld_sections[i].seg, |
| 1331 |
GC_dyld_sections[i].sect); |
| 1332 |
if (sec == NULL || sec->size < sizeof(word)) |
| 1333 |
continue; |
| 1334 |
start = slide + sec->addr; |
| 1335 |
end = start + sec->size; |
| 1336 |
LOCK(); |
| 1337 |
/* The user callback is called holding the lock. */ |
| 1338 |
if (callback == 0 || callback(name, (void*)start, (size_t)sec->size)) { |
| 1339 |
# ifdef DARWIN_DEBUG |
| 1340 |
GC_log_printf( |
| 1341 |
"Adding section __DATA,%s at %p-%p (%lu bytes) from image %s\n", |
| 1342 |
GC_dyld_sections[i].sect, (void*)start, (void*)end, |
| 1343 |
(unsigned long)sec->size, name); |
| 1344 |
# endif |
| 1345 |
GC_add_roots_inner((ptr_t)start, (ptr_t)end, FALSE); |
| 1346 |
} |
| 1347 |
UNLOCK(); |
| 1348 |
} |
| 1349 |
|
| 1350 |
/* Sections constructed on demand. */ |
| 1351 |
for (j = 0; j < sizeof(GC_dyld_add_sect_fmts) / sizeof(char *); j++) { |
| 1352 |
const char *fmt = GC_dyld_add_sect_fmts[j]; |
| 1353 |
|
| 1354 |
/* Add our manufactured aligned BSS sections. */ |
| 1355 |
for (i = 0; i <= L2_MAX_OFILE_ALIGNMENT; i++) { |
| 1356 |
char secnam[16]; |
| 1357 |
|
| 1358 |
(void)snprintf(secnam, sizeof(secnam), fmt, (unsigned)i); |
| 1359 |
secnam[sizeof(secnam) - 1] = '\0'; |
| 1360 |
sec = GC_GETSECTBYNAME(hdr, SEG_DATA, secnam); |
| 1361 |
if (sec == NULL || sec->size == 0) |
| 1362 |
continue; |
| 1363 |
start = slide + sec->addr; |
| 1364 |
end = start + sec->size; |
| 1365 |
# ifdef DARWIN_DEBUG |
| 1366 |
GC_log_printf("Adding on-demand section __DATA,%s at" |
| 1367 |
" %p-%p (%lu bytes) from image %s\n", |
| 1368 |
secnam, (void*)start, (void*)end, |
| 1369 |
(unsigned long)sec->size, name); |
| 1370 |
# endif |
| 1371 |
GC_add_roots((char*)start, (char*)end); |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
# if defined(DARWIN_DEBUG) && !defined(NO_DEBUGGING) |
| 1376 |
LOCK(); |
| 1377 |
GC_print_static_roots(); |
| 1378 |
UNLOCK(); |
| 1379 |
# endif |
| 1380 |
} |
| 1381 |
|
| 1382 |
/* This should never be called by a thread holding the lock. */ |
| 1383 |
STATIC void GC_dyld_image_remove(const struct GC_MACH_HEADER *hdr, |
| 1384 |
intptr_t slide) |
| 1385 |
{ |
| 1386 |
unsigned long start, end; |
| 1387 |
unsigned i, j; |
| 1388 |
const struct GC_MACH_SECTION *sec; |
| 1389 |
# if defined(DARWIN_DEBUG) && !defined(NO_DEBUGGING) |
| 1390 |
DCL_LOCK_STATE; |
| 1391 |
# endif |
| 1392 |
|
| 1393 |
for (i = 0; i < sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]); i++) { |
| 1394 |
sec = GC_GETSECTBYNAME(hdr, GC_dyld_sections[i].seg, |
| 1395 |
GC_dyld_sections[i].sect); |
| 1396 |
if (sec == NULL || sec->size == 0) |
| 1397 |
continue; |
| 1398 |
start = slide + sec->addr; |
| 1399 |
end = start + sec->size; |
| 1400 |
# ifdef DARWIN_DEBUG |
| 1401 |
GC_log_printf( |
| 1402 |
"Removing section __DATA,%s at %p-%p (%lu bytes) from image %s\n", |
| 1403 |
GC_dyld_sections[i].sect, (void*)start, (void*)end, |
| 1404 |
(unsigned long)sec->size, GC_dyld_name_for_hdr(hdr)); |
| 1405 |
# endif |
| 1406 |
GC_remove_roots((char*)start, (char*)end); |
| 1407 |
} |
| 1408 |
|
| 1409 |
/* Remove our on-demand sections. */ |
| 1410 |
for (j = 0; j < sizeof(GC_dyld_add_sect_fmts) / sizeof(char *); j++) { |
| 1411 |
const char *fmt = GC_dyld_add_sect_fmts[j]; |
| 1412 |
|
| 1413 |
for (i = 0; i <= L2_MAX_OFILE_ALIGNMENT; i++) { |
| 1414 |
char secnam[16]; |
| 1415 |
|
| 1416 |
(void)snprintf(secnam, sizeof(secnam), fmt, (unsigned)i); |
| 1417 |
secnam[sizeof(secnam) - 1] = '\0'; |
| 1418 |
sec = GC_GETSECTBYNAME(hdr, SEG_DATA, secnam); |
| 1419 |
if (sec == NULL || sec->size == 0) |
| 1420 |
continue; |
| 1421 |
start = slide + sec->addr; |
| 1422 |
end = start + sec->size; |
| 1423 |
# ifdef DARWIN_DEBUG |
| 1424 |
GC_log_printf("Removing on-demand section __DATA,%s at" |
| 1425 |
" %p-%p (%lu bytes) from image %s\n", secnam, |
| 1426 |
(void*)start, (void*)end, (unsigned long)sec->size, |
| 1427 |
GC_dyld_name_for_hdr(hdr)); |
| 1428 |
# endif |
| 1429 |
GC_remove_roots((char*)start, (char*)end); |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
# if defined(DARWIN_DEBUG) && !defined(NO_DEBUGGING) |
| 1434 |
LOCK(); |
| 1435 |
GC_print_static_roots(); |
| 1436 |
UNLOCK(); |
| 1437 |
# endif |
| 1438 |
} |
| 1439 |
|
| 1440 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 1441 |
{ |
| 1442 |
/* Currently does nothing. The callbacks are setup by GC_init_dyld() |
| 1443 |
The dyld library takes it from there. */ |
| 1444 |
} |
| 1445 |
|
| 1446 |
/* The _dyld_* functions have an internal lock so no _dyld functions |
| 1447 |
can be called while the world is stopped without the risk of a deadlock. |
| 1448 |
Because of this we MUST setup callbacks BEFORE we ever stop the world. |
| 1449 |
This should be called BEFORE any thread is created and WITHOUT the |
| 1450 |
allocation lock held. */ |
| 1451 |
|
| 1452 |
GC_INNER void GC_init_dyld(void) |
| 1453 |
{ |
| 1454 |
static GC_bool initialized = FALSE; |
| 1455 |
|
| 1456 |
if (initialized) return; |
| 1457 |
|
| 1458 |
# ifdef DARWIN_DEBUG |
| 1459 |
GC_log_printf("Registering dyld callbacks...\n"); |
| 1460 |
# endif |
| 1461 |
|
| 1462 |
/* Apple's Documentation: |
| 1463 |
When you call _dyld_register_func_for_add_image, the dynamic linker |
| 1464 |
runtime calls the specified callback (func) once for each of the images |
| 1465 |
that is currently loaded into the program. When a new image is added to |
| 1466 |
the program, your callback is called again with the mach_header for the |
| 1467 |
new image, and the virtual memory slide amount of the new image. |
| 1468 |
|
| 1469 |
This WILL properly register already linked libraries and libraries |
| 1470 |
linked in the future. |
| 1471 |
*/ |
| 1472 |
_dyld_register_func_for_add_image( |
| 1473 |
(void (*)(const struct mach_header*, intptr_t))GC_dyld_image_add); |
| 1474 |
_dyld_register_func_for_remove_image( |
| 1475 |
(void (*)(const struct mach_header*, intptr_t))GC_dyld_image_remove); |
| 1476 |
/* Structure mach_header_64 has the same fields */ |
| 1477 |
/* as mach_header except for the reserved one */ |
| 1478 |
/* at the end, so these casts are OK. */ |
| 1479 |
|
| 1480 |
/* Set this early to avoid reentrancy issues. */ |
| 1481 |
initialized = TRUE; |
| 1482 |
|
| 1483 |
# ifdef NO_DYLD_BIND_FULLY_IMAGE |
| 1484 |
/* FIXME: What should we do in this case? */ |
| 1485 |
# else |
| 1486 |
if (GC_no_dls) return; /* skip main data segment registration */ |
| 1487 |
|
| 1488 |
/* When the environment variable is set, the dynamic linker binds */ |
| 1489 |
/* all undefined symbols the application needs at launch time. */ |
| 1490 |
/* This includes function symbols that are normally bound lazily at */ |
| 1491 |
/* the time of their first invocation. */ |
| 1492 |
if (GETENV("DYLD_BIND_AT_LAUNCH") == 0) { |
| 1493 |
/* The environment variable is unset, so we should bind manually. */ |
| 1494 |
# ifdef DARWIN_DEBUG |
| 1495 |
GC_log_printf("Forcing full bind of GC code...\n"); |
| 1496 |
# endif |
| 1497 |
/* FIXME: '_dyld_bind_fully_image_containing_address' is deprecated. */ |
| 1498 |
if (!_dyld_bind_fully_image_containing_address( |
| 1499 |
(unsigned long *)GC_malloc)) |
| 1500 |
ABORT("_dyld_bind_fully_image_containing_address failed"); |
| 1501 |
} |
| 1502 |
# endif |
| 1503 |
} |
| 1504 |
|
| 1505 |
#define HAVE_REGISTER_MAIN_STATIC_DATA |
| 1506 |
GC_INNER GC_bool GC_register_main_static_data(void) |
| 1507 |
{ |
| 1508 |
/* Already done through dyld callbacks */ |
| 1509 |
return FALSE; |
| 1510 |
} |
| 1511 |
|
| 1512 |
#endif /* DARWIN */ |
| 1513 |
|
| 1514 |
#if defined(HAIKU) |
| 1515 |
# include <kernel/image.h> |
| 1516 |
|
| 1517 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 1518 |
{ |
| 1519 |
image_info info; |
| 1520 |
int32 cookie = 0; |
| 1521 |
|
| 1522 |
while (get_next_image_info(0, &cookie, &info) == B_OK) { |
| 1523 |
ptr_t data = (ptr_t)info.data; |
| 1524 |
GC_add_roots_inner(data, data + info.data_size, TRUE); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
#endif /* HAIKU */ |
| 1528 |
|
| 1529 |
#elif defined(PCR) |
| 1530 |
|
| 1531 |
# include "il/PCR_IL.h" |
| 1532 |
# include "th/PCR_ThCtl.h" |
| 1533 |
# include "mm/PCR_MM.h" |
| 1534 |
|
| 1535 |
GC_INNER void GC_register_dynamic_libraries(void) |
| 1536 |
{ |
| 1537 |
/* Add new static data areas of dynamically loaded modules. */ |
| 1538 |
PCR_IL_LoadedFile * p = PCR_IL_GetLastLoadedFile(); |
| 1539 |
PCR_IL_LoadedSegment * q; |
| 1540 |
|
| 1541 |
/* Skip uncommitted files */ |
| 1542 |
while (p != NIL && !(p -> lf_commitPoint)) { |
| 1543 |
/* The loading of this file has not yet been committed */ |
| 1544 |
/* Hence its description could be inconsistent. */ |
| 1545 |
/* Furthermore, it hasn't yet been run. Hence its data */ |
| 1546 |
/* segments can't possibly reference heap allocated */ |
| 1547 |
/* objects. */ |
| 1548 |
p = p -> lf_prev; |
| 1549 |
} |
| 1550 |
for (; p != NIL; p = p -> lf_prev) { |
| 1551 |
for (q = p -> lf_ls; q != NIL; q = q -> ls_next) { |
| 1552 |
if ((q -> ls_flags & PCR_IL_SegFlags_Traced_MASK) |
| 1553 |
== PCR_IL_SegFlags_Traced_on) { |
| 1554 |
GC_add_roots_inner((ptr_t)q->ls_addr, |
| 1555 |
(ptr_t)q->ls_addr + q->ls_bytes, TRUE); |
| 1556 |
} |
| 1557 |
} |
| 1558 |
} |
| 1559 |
} |
| 1560 |
#endif /* PCR && !DYNAMIC_LOADING && !MSWIN32 */ |
| 1561 |
|
| 1562 |
#if !defined(HAVE_REGISTER_MAIN_STATIC_DATA) && defined(DYNAMIC_LOADING) |
| 1563 |
/* Do we need to separately register the main static data segment? */ |
| 1564 |
GC_INNER GC_bool GC_register_main_static_data(void) |
| 1565 |
{ |
| 1566 |
return TRUE; |
| 1567 |
} |
| 1568 |
#endif /* HAVE_REGISTER_MAIN_STATIC_DATA */ |
| 1569 |
|
| 1570 |
/* Register a routine to filter dynamic library registration. */ |
| 1571 |
GC_API void GC_CALL GC_register_has_static_roots_callback( |
| 1572 |
GC_has_static_roots_func callback) |
| 1573 |
{ |
| 1574 |
GC_has_static_roots = callback; |
| 1575 |
} |