dyn_load.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. /*
  2. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  3. * Copyright (c) 1997 by Silicon Graphics. All rights reserved.
  4. *
  5. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  6. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  7. *
  8. * Permission is hereby granted to use or copy this program
  9. * for any purpose, provided the above notices are retained on all copies.
  10. * Permission to modify the code and to distribute modified code is granted,
  11. * provided the above notices are retained, and a notice that the code was
  12. * modified is included with the above copyright notice.
  13. *
  14. * Original author: Bill Janssen
  15. * Heavily modified by Hans Boehm and others
  16. */
  17. /*
  18. * This is incredibly OS specific code for tracking down data sections in
  19. * dynamic libraries. There appears to be no way of doing this quickly
  20. * without groveling through undocumented data structures. We would argue
  21. * that this is a bug in the design of the dlopen interface. THIS CODE
  22. * MAY BREAK IN FUTURE OS RELEASES. If this matters to you, don't hesitate
  23. * to let your vendor know ...
  24. *
  25. * None of this is safe with dlclose and incremental collection.
  26. * But then not much of anything is safe in the presence of dlclose.
  27. */
  28. #if (defined(__linux__) || defined(__GLIBC__) || defined(__GNU__)) \
  29. && !defined(_GNU_SOURCE)
  30. /* Can't test LINUX, since this must be define before other includes */
  31. # define _GNU_SOURCE
  32. #endif
  33. #if !defined(MACOS) && !defined(_WIN32_WCE)
  34. # include <sys/types.h>
  35. #endif
  36. #include "private/gc_priv.h"
  37. /* BTL: avoid circular redefinition of dlopen if GC_SOLARIS_THREADS defined */
  38. # if (defined(GC_PTHREADS) || defined(GC_SOLARIS_THREADS)) \
  39. && defined(dlopen) && !defined(GC_USE_LD_WRAP)
  40. /* To support threads in Solaris, gc.h interposes on dlopen by */
  41. /* defining "dlopen" to be "GC_dlopen", which is implemented below. */
  42. /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the */
  43. /* real system dlopen() in their implementation. We first remove */
  44. /* gc.h's dlopen definition and restore it later, after GC_dlopen(). */
  45. # undef dlopen
  46. # define GC_must_restore_redefined_dlopen
  47. # else
  48. # undef GC_must_restore_redefined_dlopen
  49. # endif
  50. #if (defined(DYNAMIC_LOADING) \
  51. || defined(MSWIN32) \
  52. || defined(MSWINCE) \
  53. || defined(CYGWIN32)) \
  54. && !defined(PCR)
  55. #if !defined(SUNOS4) && !defined(SUNOS5DL) && !defined(IRIX5) && \
  56. !defined(MSWIN32) && !defined(MSWINCE) && !defined(CYGWIN32) && \
  57. !(defined(ALPHA) && defined(OSF1)) && \
  58. !defined(HPUX) && !(defined(LINUX) && defined(__ELF__)) && \
  59. !defined(RS6000) && !defined(SCO_ELF) && !defined(DGUX) && \
  60. !(defined(FREEBSD) && defined(__ELF__)) && \
  61. !(defined(NETBSD) && defined(__ELF__)) && !defined(HURD) && \
  62. !defined(DARWIN)
  63. --> We only know how to find data segments of dynamic libraries for the
  64. --> above. Additional SVR4 variants might not be too
  65. --> hard to add.
  66. #endif
  67. #include <stdio.h>
  68. #ifdef SUNOS5DL
  69. # include <sys/elf.h>
  70. # include <dlfcn.h>
  71. # include <link.h>
  72. #endif
  73. #ifdef SUNOS4
  74. # include <dlfcn.h>
  75. # include <link.h>
  76. # include <a.out.h>
  77. /* struct link_map field overrides */
  78. # define l_next lm_next
  79. # define l_addr lm_addr
  80. # define l_name lm_name
  81. #endif
  82. #ifdef IRIX5
  83. # include <elf.h>
  84. # if _MIPS_SIM == _MIPS_SIM_ABI32 /* O32 ABI */
  85. /* Don't include <obj_list.h> here. */
  86. # include <obj.h>
  87. # else /* N32 or N64 ABIs */
  88. # include <objlist.h>
  89. # endif
  90. #endif
  91. #if defined(NETBSD)
  92. # include <machine/elf_machdep.h>
  93. # define ELFSIZE ARCH_ELFSIZE
  94. #endif
  95. #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
  96. (defined(FREEBSD) && defined(__ELF__)) || defined(DGUX) || \
  97. (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
  98. # include <stddef.h>
  99. # include <elf.h>
  100. # include <link.h>
  101. #endif
  102. /* Newer versions of GNU/Linux define this macro. We
  103. * define it similarly for any ELF systems that don't. */
  104. # ifndef ElfW
  105. # if defined(FREEBSD)
  106. # if __ELF_WORD_SIZE == 32
  107. # define ElfW(type) Elf32_##type
  108. # else
  109. # define ElfW(type) Elf64_##type
  110. # endif
  111. # else
  112. # ifdef NETBSD
  113. # if ELFSIZE == 32
  114. # define ElfW(type) Elf32_##type
  115. # else
  116. # define ElfW(type) Elf64_##type
  117. # endif
  118. # else
  119. # if !defined(ELF_CLASS) || ELF_CLASS == ELFCLASS32
  120. # define ElfW(type) Elf32_##type
  121. # else
  122. # define ElfW(type) Elf64_##type
  123. # endif
  124. # endif
  125. # endif
  126. # endif
  127. /* An user-supplied routine that is called to determine if a DSO must
  128. be scanned by the gc. */
  129. static int (*GC_has_static_roots)(const char *, void *, size_t);
  130. /* Register the routine. */
  131. void
  132. GC_register_has_static_roots_callback
  133. (int (*callback)(const char *, void *, size_t))
  134. {
  135. GC_has_static_roots = callback;
  136. }
  137. #if defined(SUNOS5DL) && !defined(USE_PROC_FOR_LIBRARIES)
  138. #ifdef LINT
  139. Elf32_Dyn _DYNAMIC;
  140. #endif
  141. #define obj_offset(lm) ((unsigned long)(lm->l_addr))
  142. static struct link_map *
  143. GC_FirstDLOpenedLinkMap()
  144. {
  145. extern ElfW(Dyn) _DYNAMIC;
  146. ElfW(Dyn) *dp;
  147. struct r_debug *r;
  148. static struct link_map * cachedResult = 0;
  149. static ElfW(Dyn) *dynStructureAddr = 0;
  150. /* BTL: added to avoid Solaris 5.3 ld.so _DYNAMIC bug */
  151. # ifdef SUNOS53_SHARED_LIB
  152. /* BTL: Avoid the Solaris 5.3 bug that _DYNAMIC isn't being set */
  153. /* up properly in dynamically linked .so's. This means we have */
  154. /* to use its value in the set of original object files loaded */
  155. /* at program startup. */
  156. if( dynStructureAddr == 0 ) {
  157. void* startupSyms = dlopen(0, RTLD_LAZY);
  158. dynStructureAddr = (ElfW(Dyn)*)dlsym(startupSyms, "_DYNAMIC");
  159. }
  160. # else
  161. dynStructureAddr = &_DYNAMIC;
  162. # endif
  163. if( dynStructureAddr == 0) {
  164. return(0);
  165. }
  166. if( cachedResult == 0 ) {
  167. int tag;
  168. for( dp = ((ElfW(Dyn) *)(&_DYNAMIC)); (tag = dp->d_tag) != 0; dp++ ) {
  169. if( tag == DT_DEBUG ) {
  170. struct link_map *lm
  171. = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
  172. if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
  173. break;
  174. }
  175. }
  176. }
  177. return cachedResult;
  178. }
  179. #endif /* SUNOS5DL ... */
  180. /* BTL: added to fix circular dlopen definition if GC_SOLARIS_THREADS defined */
  181. # if defined(GC_must_restore_redefined_dlopen)
  182. # define dlopen GC_dlopen
  183. # endif
  184. #if defined(SUNOS4) && !defined(USE_PROC_FOR_LIBRARIES)
  185. #ifdef LINT
  186. struct link_dynamic _DYNAMIC;
  187. #endif
  188. #define obj_offset(lm) ((unsigned long)(lm->l_addr))
  189. static struct link_map *
  190. GC_FirstDLOpenedLinkMap()
  191. {
  192. extern struct link_dynamic _DYNAMIC;
  193. if( &_DYNAMIC == 0) {
  194. return(0);
  195. }
  196. return(_DYNAMIC.ld_un.ld_1->ld_loaded);
  197. }
  198. /* Return the address of the ld.so allocated common symbol */
  199. /* with the least address, or 0 if none. */
  200. static ptr_t GC_first_common()
  201. {
  202. ptr_t result = 0;
  203. extern struct link_dynamic _DYNAMIC;
  204. struct rtc_symb * curr_symbol;
  205. if( &_DYNAMIC == 0) {
  206. return(0);
  207. }
  208. curr_symbol = _DYNAMIC.ldd -> ldd_cp;
  209. for (; curr_symbol != 0; curr_symbol = curr_symbol -> rtc_next) {
  210. if (result == 0
  211. || (ptr_t)(curr_symbol -> rtc_sp -> n_value) < result) {
  212. result = (ptr_t)(curr_symbol -> rtc_sp -> n_value);
  213. }
  214. }
  215. return(result);
  216. }
  217. #endif /* SUNOS4 ... */
  218. #if defined(IRIX5) && !defined(USE_PROC_FOR_LIBRARIES)
  219. /* Provide struct link map. */
  220. # if _MIPS_SIM == _MIPS_SIM_ABI32 /* O32 ABI */
  221. /* Provide our own version of struct obj_list in <obj_list.h> with
  222. correctly typed data member. */
  223. struct obj_list {
  224. struct obj *data;
  225. struct obj_list *next;
  226. struct obj_list *prev;
  227. } objList;
  228. struct link_map {
  229. objList l_ol;
  230. };
  231. extern objList *__rld_obj_head;
  232. /* Map field names */
  233. # define l_next l_ol.next
  234. # define l_addr l_ol.data->o_pelfhdr
  235. # define obj_offset(lm) \
  236. ((unsigned long)(lm->l_ol.o_praw - (char *)lm->l_ol.o_base_address))
  237. # else /* N32 or N64 ABIs */
  238. struct link_map {
  239. ElfW(Obj_Info) l_oi;
  240. };
  241. extern ElfW(Obj_Info) *__rld_obj_head;
  242. /* Map field names */
  243. # define l_next l_oi.oi_next
  244. # define l_addr l_oi.oi_ehdr
  245. /* See gdb/solib-irix.c (fetch_lm_info). */
  246. # define obj_offset(lm) \
  247. ((unsigned long)(lm->l_oi.oi_ehdr - lm->l_oi.oi_orig_ehdr))
  248. # endif
  249. static struct link_map *
  250. GC_FirstDLOpenedLinkMap()
  251. {
  252. return (struct link_map *)__rld_obj_head;
  253. }
  254. #endif /* IRIX5 ... */
  255. # if defined(SUNOS4) || defined(SUNOS5DL) || defined(IRIX5)
  256. /* Add dynamic library data sections to the root set. */
  257. # if !defined(PCR) \
  258. && !defined(GC_SOLARIS_PTHREADS) && !defined(GC_IRIX_THREADS) \
  259. && defined(THREADS)
  260. # ifndef SRC_M3
  261. --> fix mutual exclusion with dlopen
  262. # endif /* We assume M3 programs don't call dlopen for now */
  263. # endif
  264. # ifndef USE_PROC_FOR_LIBRARIES
  265. void GC_register_dynamic_libraries()
  266. {
  267. struct link_map *lm = GC_FirstDLOpenedLinkMap();
  268. for (lm = GC_FirstDLOpenedLinkMap();
  269. lm != (struct link_map *) 0; lm = (struct link_map *) lm->l_next)
  270. {
  271. # ifdef SUNOS4
  272. struct exec *e;
  273. e = (struct exec *) lm->lm_addr;
  274. GC_add_roots_inner(
  275. ((char *) (N_DATOFF(*e) + lm->lm_addr)),
  276. ((char *) (N_BSSADDR(*e) + e->a_bss + lm->lm_addr)),
  277. TRUE);
  278. # endif
  279. # if defined(SUNOS5DL) || defined(IRIX5)
  280. ElfW(Ehdr) * e;
  281. ElfW(Phdr) * p;
  282. unsigned long offset;
  283. char * start;
  284. register int i;
  285. e = (ElfW(Ehdr) *) lm->l_addr;
  286. p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
  287. offset = obj_offset(lm);
  288. for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
  289. switch( p->p_type ) {
  290. case PT_LOAD:
  291. {
  292. if( !(p->p_flags & PF_W) ) break;
  293. start = ((char *)(p->p_vaddr)) + offset;
  294. GC_add_roots_inner(
  295. start,
  296. start + p->p_memsz,
  297. TRUE
  298. );
  299. }
  300. break;
  301. default:
  302. break;
  303. }
  304. }
  305. # endif
  306. }
  307. # ifdef SUNOS4
  308. {
  309. static ptr_t common_start = 0;
  310. ptr_t common_end;
  311. extern ptr_t GC_find_limit();
  312. if (common_start == 0) common_start = GC_first_common();
  313. if (common_start != 0) {
  314. common_end = GC_find_limit(common_start, TRUE);
  315. GC_add_roots_inner((char *)common_start, (char *)common_end, TRUE);
  316. }
  317. }
  318. # endif
  319. }
  320. # endif /* !USE_PROC ... */
  321. # endif /* SUNOS */
  322. #if defined(LINUX) && defined(__ELF__) || defined(SCO_ELF) || \
  323. (defined(FREEBSD) && defined(__ELF__)) || defined(DGUX) || \
  324. (defined(NETBSD) && defined(__ELF__)) || defined(HURD)
  325. #ifdef USE_PROC_FOR_LIBRARIES
  326. #include <string.h>
  327. #include <sys/stat.h>
  328. #include <fcntl.h>
  329. #include <unistd.h>
  330. #define MAPS_BUF_SIZE (32*1024)
  331. extern ssize_t GC_repeat_read(int fd, char *buf, size_t count);
  332. /* Repeatedly read until buffer is filled, or EOF is encountered */
  333. /* Defined in os_dep.c. */
  334. char *GC_parse_map_entry(char *buf_ptr, word *start, word *end,
  335. char *prot_buf, unsigned int *maj_dev);
  336. word GC_apply_to_maps(word (*fn)(char *));
  337. /* From os_dep.c */
  338. word GC_register_map_entries(char *maps)
  339. {
  340. char prot_buf[5];
  341. char *buf_ptr = maps;
  342. int count;
  343. word start, end;
  344. unsigned int maj_dev;
  345. word least_ha, greatest_ha;
  346. unsigned i;
  347. word datastart = (word)(DATASTART);
  348. /* Compute heap bounds. FIXME: Should be done by add_to_heap? */
  349. least_ha = (word)(-1);
  350. greatest_ha = 0;
  351. for (i = 0; i < GC_n_heap_sects; ++i) {
  352. word sect_start = (word)GC_heap_sects[i].hs_start;
  353. word sect_end = sect_start + GC_heap_sects[i].hs_bytes;
  354. if (sect_start < least_ha) least_ha = sect_start;
  355. if (sect_end > greatest_ha) greatest_ha = sect_end;
  356. }
  357. if (greatest_ha < (word)GC_scratch_last_end_ptr)
  358. greatest_ha = (word)GC_scratch_last_end_ptr;
  359. for (;;) {
  360. buf_ptr = GC_parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev);
  361. if (buf_ptr == NULL) return 1;
  362. if (prot_buf[1] == 'w') {
  363. /* This is a writable mapping. Add it to */
  364. /* the root set unless it is already otherwise */
  365. /* accounted for. */
  366. if (start <= (word)GC_stackbottom && end >= (word)GC_stackbottom) {
  367. /* Stack mapping; discard */
  368. continue;
  369. }
  370. # ifdef THREADS
  371. if (GC_segment_is_thread_stack(start, end)) continue;
  372. # endif
  373. /* We no longer exclude the main data segment. */
  374. if (start < least_ha && end > least_ha) {
  375. end = least_ha;
  376. }
  377. if (start < greatest_ha && end > greatest_ha) {
  378. start = greatest_ha;
  379. }
  380. if (start >= least_ha && end <= greatest_ha) continue;
  381. GC_add_roots_inner((char *)start, (char *)end, TRUE);
  382. }
  383. }
  384. return 1;
  385. }
  386. void GC_register_dynamic_libraries()
  387. {
  388. if (!GC_apply_to_maps(GC_register_map_entries))
  389. ABORT("Failed to read /proc for library registration.");
  390. }
  391. /* We now take care of the main data segment ourselves: */
  392. GC_bool GC_register_main_static_data()
  393. {
  394. return FALSE;
  395. }
  396. # define HAVE_REGISTER_MAIN_STATIC_DATA
  397. #endif /* USE_PROC_FOR_LIBRARIES */
  398. #if !defined(USE_PROC_FOR_LIBRARIES)
  399. /* The following is the preferred way to walk dynamic libraries */
  400. /* For glibc 2.2.4+. Unfortunately, it doesn't work for older */
  401. /* versions. Thanks to Jakub Jelinek for most of the code. */
  402. # if (defined(LINUX) || defined (__GLIBC__)) /* Are others OK here, too? */ \
  403. && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2) \
  404. || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && defined(DT_CONFIG)))
  405. /* We have the header files for a glibc that includes dl_iterate_phdr. */
  406. /* It may still not be available in the library on the target system. */
  407. /* Thus we also treat it as a weak symbol. */
  408. #define HAVE_DL_ITERATE_PHDR
  409. #pragma weak dl_iterate_phdr
  410. #endif
  411. # if (defined(FREEBSD) && __FreeBSD__ >= 7)
  412. /* On the FreeBSD system, any target system at major version 7 shall */
  413. /* have dl_iterate_phdr; therefore, we need not make it weak as above. */
  414. #define HAVE_DL_ITERATE_PHDR
  415. #endif
  416. #if defined(HAVE_DL_ITERATE_PHDR)
  417. static int GC_register_dynlib_callback(info, size, ptr)
  418. struct dl_phdr_info * info;
  419. size_t size;
  420. void * ptr;
  421. {
  422. const ElfW(Phdr) * p;
  423. char * start;
  424. register int i;
  425. /* Make sure struct dl_phdr_info is at least as big as we need. */
  426. if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
  427. + sizeof (info->dlpi_phnum))
  428. return -1;
  429. p = info->dlpi_phdr;
  430. for( i = 0; i < (int)(info->dlpi_phnum); ((i++),(p++)) ) {
  431. switch( p->p_type ) {
  432. case PT_LOAD:
  433. {
  434. if( !(p->p_flags & PF_W) ) break;
  435. start = ((char *)(p->p_vaddr)) + info->dlpi_addr;
  436. if (GC_has_static_roots
  437. && !GC_has_static_roots(info->dlpi_name, start, p->p_memsz))
  438. break;
  439. GC_add_roots_inner(start, start + p->p_memsz, TRUE);
  440. }
  441. break;
  442. default:
  443. break;
  444. }
  445. }
  446. * (int *)ptr = 1; /* Signal that we were called */
  447. return 0;
  448. }
  449. /* Return TRUE if we succeed, FALSE if dl_iterate_phdr wasn't there. */
  450. GC_bool GC_register_dynamic_libraries_dl_iterate_phdr()
  451. {
  452. if (dl_iterate_phdr) {
  453. int did_something = 0;
  454. dl_iterate_phdr(GC_register_dynlib_callback, &did_something);
  455. if (!did_something) {
  456. /* dl_iterate_phdr may forget the static data segment in */
  457. /* statically linked executables. */
  458. GC_add_roots_inner(DATASTART, (char *)(DATAEND), TRUE);
  459. # if defined(DATASTART2)
  460. GC_add_roots_inner(DATASTART2, (char *)(DATAEND2), TRUE);
  461. # endif
  462. }
  463. return TRUE;
  464. } else {
  465. return FALSE;
  466. }
  467. }
  468. /* Do we need to separately register the main static data segment? */
  469. GC_bool GC_register_main_static_data()
  470. {
  471. return (dl_iterate_phdr == 0);
  472. }
  473. #define HAVE_REGISTER_MAIN_STATIC_DATA
  474. # else /* !LINUX || version(glibc) < 2.2.4 */
  475. /* Dynamic loading code for Linux running ELF. Somewhat tested on
  476. * Linux/x86, untested but hopefully should work on Linux/Alpha.
  477. * This code was derived from the Solaris/ELF support. Thanks to
  478. * whatever kind soul wrote that. - Patrick Bridges */
  479. /* This doesn't necessarily work in all cases, e.g. with preloaded
  480. * dynamic libraries. */
  481. #if defined(NETBSD)
  482. # include <sys/exec_elf.h>
  483. /* for compatibility with 1.4.x */
  484. # ifndef DT_DEBUG
  485. # define DT_DEBUG 21
  486. # endif
  487. # ifndef PT_LOAD
  488. # define PT_LOAD 1
  489. # endif
  490. # ifndef PF_W
  491. # define PF_W 2
  492. # endif
  493. #else
  494. # include <elf.h>
  495. #endif
  496. #include <link.h>
  497. # endif
  498. #ifdef __GNUC__
  499. # pragma weak _DYNAMIC
  500. #endif
  501. extern ElfW(Dyn) _DYNAMIC[];
  502. static struct link_map *
  503. GC_FirstDLOpenedLinkMap()
  504. {
  505. ElfW(Dyn) *dp;
  506. static struct link_map *cachedResult = 0;
  507. if( _DYNAMIC == 0) {
  508. return(0);
  509. }
  510. if( cachedResult == 0 ) {
  511. int tag;
  512. for( dp = _DYNAMIC; (tag = dp->d_tag) != 0; dp++ ) {
  513. /* FIXME: The DT_DEBUG header is not mandated by the */
  514. /* ELF spec. This code appears to be dependent on */
  515. /* idiosynchracies of older GNU tool chains. If this code */
  516. /* fails for you, the real problem is probably that it is */
  517. /* being used at all. You should be getting the */
  518. /* dl_iterate_phdr version. */
  519. if( tag == DT_DEBUG ) {
  520. struct link_map *lm
  521. = ((struct r_debug *)(dp->d_un.d_ptr))->r_map;
  522. if( lm != 0 ) cachedResult = lm->l_next; /* might be NIL */
  523. break;
  524. }
  525. }
  526. }
  527. return cachedResult;
  528. }
  529. void GC_register_dynamic_libraries()
  530. {
  531. struct link_map *lm;
  532. # ifdef HAVE_DL_ITERATE_PHDR
  533. if (GC_register_dynamic_libraries_dl_iterate_phdr()) {
  534. return;
  535. }
  536. # endif
  537. lm = GC_FirstDLOpenedLinkMap();
  538. for (lm = GC_FirstDLOpenedLinkMap();
  539. lm != (struct link_map *) 0; lm = lm->l_next)
  540. {
  541. ElfW(Ehdr) * e;
  542. ElfW(Phdr) * p;
  543. unsigned long offset;
  544. char * start;
  545. register int i;
  546. e = (ElfW(Ehdr) *) lm->l_addr;
  547. p = ((ElfW(Phdr) *)(((char *)(e)) + e->e_phoff));
  548. offset = ((unsigned long)(lm->l_addr));
  549. for( i = 0; i < (int)(e->e_phnum); ((i++),(p++)) ) {
  550. switch( p->p_type ) {
  551. case PT_LOAD:
  552. {
  553. if( !(p->p_flags & PF_W) ) break;
  554. start = ((char *)(p->p_vaddr)) + offset;
  555. GC_add_roots_inner(start, start + p->p_memsz, TRUE);
  556. }
  557. break;
  558. default:
  559. break;
  560. }
  561. }
  562. }
  563. }
  564. #endif /* !USE_PROC_FOR_LIBRARIES */
  565. #endif /* LINUX */
  566. #if defined(USE_PROC_FOR_LIBRARIES) && !defined(LINUX)
  567. #include <sys/procfs.h>
  568. #include <sys/stat.h>
  569. #include <fcntl.h>
  570. #include <elf.h>
  571. #include <errno.h>
  572. #include <signal.h> /* Only for the following test. */
  573. #ifndef _sigargs
  574. # define IRIX6
  575. #endif
  576. extern void * GC_roots_present();
  577. /* The type is a lie, since the real type doesn't make sense here, */
  578. /* and we only test for NULL. */
  579. /* We use /proc to track down all parts of the address space that are */
  580. /* mapped by the process, and throw out regions we know we shouldn't */
  581. /* worry about. This may also work under other SVR4 variants. */
  582. void GC_register_dynamic_libraries()
  583. {
  584. static int fd = -1;
  585. char buf[30];
  586. static prmap_t * addr_map = 0;
  587. static int current_sz = 0; /* Number of records currently in addr_map */
  588. static int needed_sz; /* Required size of addr_map */
  589. register int i;
  590. register long flags;
  591. register ptr_t start;
  592. register ptr_t limit;
  593. ptr_t heap_start = (ptr_t)HEAP_START;
  594. ptr_t heap_end = heap_start;
  595. # ifdef SUNOS5DL
  596. # define MA_PHYS 0
  597. # endif /* SUNOS5DL */
  598. if (fd < 0) {
  599. sprintf(buf, "/proc/%d", getpid());
  600. /* The above generates a lint complaint, since pid_t varies. */
  601. /* It's unclear how to improve this. */
  602. fd = open(buf, O_RDONLY);
  603. if (fd < 0) {
  604. ABORT("/proc open failed");
  605. }
  606. }
  607. if (ioctl(fd, PIOCNMAP, &needed_sz) < 0) {
  608. GC_err_printf2("fd = %d, errno = %d\n", fd, errno);
  609. ABORT("/proc PIOCNMAP ioctl failed");
  610. }
  611. if (needed_sz >= current_sz) {
  612. current_sz = needed_sz * 2 + 1;
  613. /* Expansion, plus room for 0 record */
  614. addr_map = (prmap_t *)GC_scratch_alloc((word)
  615. (current_sz * sizeof(prmap_t)));
  616. }
  617. if (ioctl(fd, PIOCMAP, addr_map) < 0) {
  618. GC_err_printf4("fd = %d, errno = %d, needed_sz = %d, addr_map = 0x%X\n",
  619. fd, errno, needed_sz, addr_map);
  620. ABORT("/proc PIOCMAP ioctl failed");
  621. };
  622. if (GC_n_heap_sects > 0) {
  623. heap_end = GC_heap_sects[GC_n_heap_sects-1].hs_start
  624. + GC_heap_sects[GC_n_heap_sects-1].hs_bytes;
  625. if (heap_end < GC_scratch_last_end_ptr) heap_end = GC_scratch_last_end_ptr;
  626. }
  627. for (i = 0; i < needed_sz; i++) {
  628. flags = addr_map[i].pr_mflags;
  629. if ((flags & (MA_BREAK | MA_STACK | MA_PHYS
  630. | MA_FETCHOP | MA_NOTCACHED)) != 0) goto irrelevant;
  631. if ((flags & (MA_READ | MA_WRITE)) != (MA_READ | MA_WRITE))
  632. goto irrelevant;
  633. /* The latter test is empirically useless in very old Irix */
  634. /* versions. Other than the */
  635. /* main data and stack segments, everything appears to be */
  636. /* mapped readable, writable, executable, and shared(!!). */
  637. /* This makes no sense to me. - HB */
  638. start = (ptr_t)(addr_map[i].pr_vaddr);
  639. if (GC_roots_present(start)) goto irrelevant;
  640. if (start < heap_end && start >= heap_start)
  641. goto irrelevant;
  642. # ifdef MMAP_STACKS
  643. if (GC_is_thread_stack(start)) goto irrelevant;
  644. # endif /* MMAP_STACKS */
  645. limit = start + addr_map[i].pr_size;
  646. /* The following seemed to be necessary for very old versions */
  647. /* of Irix, but it has been reported to discard relevant */
  648. /* segments under Irix 6.5. */
  649. # ifndef IRIX6
  650. if (addr_map[i].pr_off == 0 && strncmp(start, ELFMAG, 4) == 0) {
  651. /* Discard text segments, i.e. 0-offset mappings against */
  652. /* executable files which appear to have ELF headers. */
  653. caddr_t arg;
  654. int obj;
  655. # define MAP_IRR_SZ 10
  656. static ptr_t map_irr[MAP_IRR_SZ];
  657. /* Known irrelevant map entries */
  658. static int n_irr = 0;
  659. struct stat buf;
  660. register int i;
  661. for (i = 0; i < n_irr; i++) {
  662. if (map_irr[i] == start) goto irrelevant;
  663. }
  664. arg = (caddr_t)start;
  665. obj = ioctl(fd, PIOCOPENM, &arg);
  666. if (obj >= 0) {
  667. fstat(obj, &buf);
  668. close(obj);
  669. if ((buf.st_mode & 0111) != 0) {
  670. if (n_irr < MAP_IRR_SZ) {
  671. map_irr[n_irr++] = start;
  672. }
  673. goto irrelevant;
  674. }
  675. }
  676. }
  677. # endif /* !IRIX6 */
  678. GC_add_roots_inner(start, limit, TRUE);
  679. irrelevant: ;
  680. }
  681. /* Dont keep cached descriptor, for now. Some kernels don't like us */
  682. /* to keep a /proc file descriptor around during kill -9. */
  683. if (close(fd) < 0) ABORT("Couldnt close /proc file");
  684. fd = -1;
  685. }
  686. # endif /* USE_PROC */
  687. # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
  688. # define WIN32_LEAN_AND_MEAN
  689. # define NOSERVICE
  690. # include <windows.h>
  691. # include <stdlib.h>
  692. /* We traverse the entire address space and register all segments */
  693. /* that could possibly have been written to. */
  694. extern GC_bool GC_is_heap_base (ptr_t p);
  695. # ifdef GC_WIN32_THREADS
  696. extern void GC_get_next_stack(char *start, char **lo, char **hi);
  697. void GC_cond_add_roots(char *base, char * limit)
  698. {
  699. char * curr_base = base;
  700. char * next_stack_lo;
  701. char * next_stack_hi;
  702. if (base == limit) return;
  703. for(;;) {
  704. GC_get_next_stack(curr_base, &next_stack_lo, &next_stack_hi);
  705. if (next_stack_lo >= limit) break;
  706. GC_add_roots_inner(curr_base, next_stack_lo, TRUE);
  707. curr_base = next_stack_hi;
  708. }
  709. if (curr_base < limit) GC_add_roots_inner(curr_base, limit, TRUE);
  710. }
  711. # else
  712. void GC_cond_add_roots(char *base, char * limit)
  713. {
  714. char dummy;
  715. char * stack_top
  716. = (char *) ((word)(&dummy) & ~(GC_sysinfo.dwAllocationGranularity-1));
  717. if (base == limit) return;
  718. if (limit > stack_top && base < GC_stackbottom) {
  719. /* Part of the stack; ignore it. */
  720. return;
  721. }
  722. GC_add_roots_inner(base, limit, TRUE);
  723. }
  724. # endif
  725. # if defined(MSWINCE) || defined(CYGWIN32)
  726. /* Do we need to separately register the main static data segment? */
  727. GC_bool GC_register_main_static_data()
  728. {
  729. return FALSE;
  730. }
  731. # else /* win32 */
  732. extern GC_bool GC_no_win32_dlls;
  733. GC_bool GC_register_main_static_data()
  734. {
  735. return GC_no_win32_dlls;
  736. }
  737. # endif /* win32 */
  738. # define HAVE_REGISTER_MAIN_STATIC_DATA
  739. /* The frame buffer testing code is dead in this version. */
  740. /* We leave it here temporarily in case the switch to just */
  741. /* testing for MEM_IMAGE sections causes un expected */
  742. /* problems. */
  743. GC_bool GC_warn_fb = TRUE; /* Warn about traced likely */
  744. /* graphics memory. */
  745. GC_bool GC_disallow_ignore_fb = FALSE;
  746. int GC_ignore_fb_mb; /* Ignore mappings bigger than the */
  747. /* specified number of MB. */
  748. GC_bool GC_ignore_fb = FALSE; /* Enable frame buffer */
  749. /* checking. */
  750. /* Issue warning if tracing apparent framebuffer. */
  751. /* This limits us to one warning, and it's a back door to */
  752. /* disable that. */
  753. /* Should [start, start+len) be treated as a frame buffer */
  754. /* and ignored? */
  755. /* Unfortunately, we currently are not quite sure how to tell */
  756. /* this automatically, and rely largely on user input. */
  757. /* We expect that any mapping with type MEM_MAPPED (which */
  758. /* apparently excludes library data sections) can be safely */
  759. /* ignored. But we're too chicken to do that in this */
  760. /* version. */
  761. /* Based on a very limited sample, it appears that: */
  762. /* - Frame buffer mappings appear as mappings of large */
  763. /* length, usually a bit less than a power of two. */
  764. /* - The definition of "a bit less" in the above cannot */
  765. /* be made more precise. */
  766. /* - Have a starting address at best 64K aligned. */
  767. /* - Have type == MEM_MAPPED. */
  768. static GC_bool is_frame_buffer(ptr_t start, size_t len, DWORD tp)
  769. {
  770. static GC_bool initialized = FALSE;
  771. # define MB (1024*1024)
  772. # define DEFAULT_FB_MB 15
  773. # define MIN_FB_MB 3
  774. if (GC_disallow_ignore_fb || tp != MEM_MAPPED) return FALSE;
  775. if (!initialized) {
  776. char * ignore_fb_string = GETENV("GC_IGNORE_FB");
  777. if (0 != ignore_fb_string) {
  778. while (*ignore_fb_string == ' ' || *ignore_fb_string == '\t')
  779. ++ignore_fb_string;
  780. if (*ignore_fb_string == '\0') {
  781. GC_ignore_fb_mb = DEFAULT_FB_MB;
  782. } else {
  783. GC_ignore_fb_mb = atoi(ignore_fb_string);
  784. if (GC_ignore_fb_mb < MIN_FB_MB) {
  785. WARN("Bad GC_IGNORE_FB value. Using %ld\n", DEFAULT_FB_MB);
  786. GC_ignore_fb_mb = DEFAULT_FB_MB;
  787. }
  788. }
  789. GC_ignore_fb = TRUE;
  790. } else {
  791. GC_ignore_fb_mb = DEFAULT_FB_MB; /* For warning */
  792. }
  793. initialized = TRUE;
  794. }
  795. if (len >= ((size_t)GC_ignore_fb_mb << 20)) {
  796. if (GC_ignore_fb) {
  797. return TRUE;
  798. } else {
  799. if (GC_warn_fb) {
  800. WARN("Possible frame buffer mapping at 0x%lx: \n"
  801. "\tConsider setting GC_IGNORE_FB to improve performance.\n",
  802. start);
  803. GC_warn_fb = FALSE;
  804. }
  805. return FALSE;
  806. }
  807. } else {
  808. return FALSE;
  809. }
  810. }
  811. # ifdef DEBUG_VIRTUALQUERY
  812. void GC_dump_meminfo(MEMORY_BASIC_INFORMATION *buf)
  813. {
  814. GC_printf4("BaseAddress = %lx, AllocationBase = %lx, RegionSize = %lx(%lu)\n",
  815. buf -> BaseAddress, buf -> AllocationBase, buf -> RegionSize,
  816. buf -> RegionSize);
  817. GC_printf4("\tAllocationProtect = %lx, State = %lx, Protect = %lx, "
  818. "Type = %lx\n",
  819. buf -> AllocationProtect, buf -> State, buf -> Protect,
  820. buf -> Type);
  821. }
  822. # endif /* DEBUG_VIRTUALQUERY */
  823. # ifdef CYGWIN32
  824. # define GC_wnt (TRUE)
  825. # else
  826. extern GC_bool GC_wnt; /* Is Windows NT derivative. */
  827. /* Defined and set in os_dep.c. */
  828. # endif
  829. void GC_register_dynamic_libraries()
  830. {
  831. MEMORY_BASIC_INFORMATION buf;
  832. DWORD result;
  833. DWORD protect;
  834. LPVOID p;
  835. char * base;
  836. char * limit, * new_limit;
  837. # ifdef MSWIN32
  838. if (GC_no_win32_dlls) return;
  839. # endif
  840. base = limit = p = GC_sysinfo.lpMinimumApplicationAddress;
  841. # if defined(MSWINCE) && !defined(_WIN32_WCE_EMULATION)
  842. /* Only the first 32 MB of address space belongs to the current process */
  843. while (p < (LPVOID)0x02000000) {
  844. result = VirtualQuery(p, &buf, sizeof(buf));
  845. if (result == 0) {
  846. /* Page is free; advance to the next possible allocation base */
  847. new_limit = (char *)
  848. (((DWORD) p + GC_sysinfo.dwAllocationGranularity)
  849. & ~(GC_sysinfo.dwAllocationGranularity-1));
  850. } else
  851. # else
  852. while (p < GC_sysinfo.lpMaximumApplicationAddress) {
  853. result = VirtualQuery(p, &buf, sizeof(buf));
  854. # endif
  855. {
  856. if (result != sizeof(buf)) {
  857. ABORT("Weird VirtualQuery result");
  858. }
  859. new_limit = (char *)p + buf.RegionSize;
  860. protect = buf.Protect;
  861. if (buf.State == MEM_COMMIT
  862. && (protect == PAGE_EXECUTE_READWRITE
  863. || protect == PAGE_READWRITE)
  864. && !GC_is_heap_base(buf.AllocationBase)
  865. /* This used to check for
  866. * !is_frame_buffer(p, buf.RegionSize, buf.Type)
  867. * instead of just checking for MEM_IMAGE.
  868. * If something breaks, change it back. */
  869. /* There is some evidence that we cannot always
  870. * ignore MEM_PRIVATE sections under Windows ME
  871. * and predecessors. Hence we now also check for
  872. * that case. */
  873. && (buf.Type == MEM_IMAGE ||
  874. !GC_wnt && buf.Type == MEM_PRIVATE)) {
  875. # ifdef DEBUG_VIRTUALQUERY
  876. GC_dump_meminfo(&buf);
  877. # endif
  878. if ((char *)p != limit) {
  879. GC_cond_add_roots(base, limit);
  880. base = p;
  881. }
  882. limit = new_limit;
  883. }
  884. }
  885. if (p > (LPVOID)new_limit /* overflow */) break;
  886. p = (LPVOID)new_limit;
  887. }
  888. GC_cond_add_roots(base, limit);
  889. }
  890. #endif /* MSWIN32 || MSWINCE || CYGWIN32 */
  891. #if defined(ALPHA) && defined(OSF1)
  892. #include <loader.h>
  893. void GC_register_dynamic_libraries()
  894. {
  895. int status;
  896. ldr_process_t mypid;
  897. /* module */
  898. ldr_module_t moduleid = LDR_NULL_MODULE;
  899. ldr_module_info_t moduleinfo;
  900. size_t moduleinfosize = sizeof(moduleinfo);
  901. size_t modulereturnsize;
  902. /* region */
  903. ldr_region_t region;
  904. ldr_region_info_t regioninfo;
  905. size_t regioninfosize = sizeof(regioninfo);
  906. size_t regionreturnsize;
  907. /* Obtain id of this process */
  908. mypid = ldr_my_process();
  909. /* For each module */
  910. while (TRUE) {
  911. /* Get the next (first) module */
  912. status = ldr_next_module(mypid, &moduleid);
  913. /* Any more modules? */
  914. if (moduleid == LDR_NULL_MODULE)
  915. break; /* No more modules */
  916. /* Check status AFTER checking moduleid because */
  917. /* of a bug in the non-shared ldr_next_module stub */
  918. if (status != 0 ) {
  919. GC_printf1("dynamic_load: status = %ld\n", (long)status);
  920. {
  921. extern char *sys_errlist[];
  922. extern int sys_nerr;
  923. extern int errno;
  924. if (errno <= sys_nerr) {
  925. GC_printf1("dynamic_load: %s\n", (long)sys_errlist[errno]);
  926. } else {
  927. GC_printf1("dynamic_load: %d\n", (long)errno);
  928. }
  929. }
  930. ABORT("ldr_next_module failed");
  931. }
  932. /* Get the module information */
  933. status = ldr_inq_module(mypid, moduleid, &moduleinfo,
  934. moduleinfosize, &modulereturnsize);
  935. if (status != 0 )
  936. ABORT("ldr_inq_module failed");
  937. /* is module for the main program (i.e. nonshared portion)? */
  938. if (moduleinfo.lmi_flags & LDR_MAIN)
  939. continue; /* skip the main module */
  940. # ifdef VERBOSE
  941. GC_printf("---Module---\n");
  942. GC_printf("Module ID = %16ld\n", moduleinfo.lmi_modid);
  943. GC_printf("Count of regions = %16d\n", moduleinfo.lmi_nregion);
  944. GC_printf("flags for module = %16lx\n", moduleinfo.lmi_flags);
  945. GC_printf("pathname of module = \"%s\"\n", moduleinfo.lmi_name);
  946. # endif
  947. /* For each region in this module */
  948. for (region = 0; region < moduleinfo.lmi_nregion; region++) {
  949. /* Get the region information */
  950. status = ldr_inq_region(mypid, moduleid, region, &regioninfo,
  951. regioninfosize, &regionreturnsize);
  952. if (status != 0 )
  953. ABORT("ldr_inq_region failed");
  954. /* only process writable (data) regions */
  955. if (! (regioninfo.lri_prot & LDR_W))
  956. continue;
  957. # ifdef VERBOSE
  958. GC_printf("--- Region ---\n");
  959. GC_printf("Region number = %16ld\n",
  960. regioninfo.lri_region_no);
  961. GC_printf("Protection flags = %016x\n", regioninfo.lri_prot);
  962. GC_printf("Virtual address = %16p\n", regioninfo.lri_vaddr);
  963. GC_printf("Mapped address = %16p\n", regioninfo.lri_mapaddr);
  964. GC_printf("Region size = %16ld\n", regioninfo.lri_size);
  965. GC_printf("Region name = \"%s\"\n", regioninfo.lri_name);
  966. # endif
  967. /* register region as a garbage collection root */
  968. GC_add_roots_inner (
  969. (char *)regioninfo.lri_mapaddr,
  970. (char *)regioninfo.lri_mapaddr + regioninfo.lri_size,
  971. TRUE);
  972. }
  973. }
  974. }
  975. #endif
  976. #if defined(HPUX)
  977. #include <errno.h>
  978. #include <dl.h>
  979. extern int errno;
  980. extern char *sys_errlist[];
  981. extern int sys_nerr;
  982. void GC_register_dynamic_libraries()
  983. {
  984. int status;
  985. int index = 1; /* Ordinal position in shared library search list */
  986. struct shl_descriptor *shl_desc; /* Shared library info, see dl.h */
  987. /* For each dynamic library loaded */
  988. while (TRUE) {
  989. /* Get info about next shared library */
  990. status = shl_get(index, &shl_desc);
  991. /* Check if this is the end of the list or if some error occured */
  992. if (status != 0) {
  993. # ifdef GC_HPUX_THREADS
  994. /* I've seen errno values of 0. The man page is not clear */
  995. /* as to whether errno should get set on a -1 return. */
  996. break;
  997. # else
  998. if (errno == EINVAL) {
  999. break; /* Moved past end of shared library list --> finished */
  1000. } else {
  1001. if (errno <= sys_nerr) {
  1002. GC_printf1("dynamic_load: %s\n", (long) sys_errlist[errno]);
  1003. } else {
  1004. GC_printf1("dynamic_load: %d\n", (long) errno);
  1005. }
  1006. ABORT("shl_get failed");
  1007. }
  1008. # endif
  1009. }
  1010. # ifdef VERBOSE
  1011. GC_printf0("---Shared library---\n");
  1012. GC_printf1("\tfilename = \"%s\"\n", shl_desc->filename);
  1013. GC_printf1("\tindex = %d\n", index);
  1014. GC_printf1("\thandle = %08x\n",
  1015. (unsigned long) shl_desc->handle);
  1016. GC_printf1("\ttext seg. start = %08x\n", shl_desc->tstart);
  1017. GC_printf1("\ttext seg. end = %08x\n", shl_desc->tend);
  1018. GC_printf1("\tdata seg. start = %08x\n", shl_desc->dstart);
  1019. GC_printf1("\tdata seg. end = %08x\n", shl_desc->dend);
  1020. GC_printf1("\tref. count = %lu\n", shl_desc->ref_count);
  1021. # endif
  1022. /* register shared library's data segment as a garbage collection root */
  1023. GC_add_roots_inner((char *) shl_desc->dstart,
  1024. (char *) shl_desc->dend, TRUE);
  1025. index++;
  1026. }
  1027. }
  1028. #endif /* HPUX */
  1029. #ifdef RS6000
  1030. #pragma alloca
  1031. #include <sys/ldr.h>
  1032. #include <sys/errno.h>
  1033. void GC_register_dynamic_libraries()
  1034. {
  1035. int len;
  1036. char *ldibuf;
  1037. int ldibuflen;
  1038. struct ld_info *ldi;
  1039. ldibuf = alloca(ldibuflen = 8192);
  1040. while ( (len = loadquery(L_GETINFO,ldibuf,ldibuflen)) < 0) {
  1041. if (errno != ENOMEM) {
  1042. ABORT("loadquery failed");
  1043. }
  1044. ldibuf = alloca(ldibuflen *= 2);
  1045. }
  1046. ldi = (struct ld_info *)ldibuf;
  1047. while (ldi) {
  1048. len = ldi->ldinfo_next;
  1049. GC_add_roots_inner(
  1050. ldi->ldinfo_dataorg,
  1051. (ptr_t)(unsigned long)ldi->ldinfo_dataorg
  1052. + ldi->ldinfo_datasize,
  1053. TRUE);
  1054. ldi = len ? (struct ld_info *)((char *)ldi + len) : 0;
  1055. }
  1056. }
  1057. #endif /* RS6000 */
  1058. #ifdef DARWIN
  1059. /* __private_extern__ hack required for pre-3.4 gcc versions. */
  1060. #ifndef __private_extern__
  1061. # define __private_extern__ extern
  1062. # include <mach-o/dyld.h>
  1063. # undef __private_extern__
  1064. #else
  1065. # include <mach-o/dyld.h>
  1066. #endif
  1067. #include <mach-o/getsect.h>
  1068. /*#define DARWIN_DEBUG*/
  1069. /* Writeable sections generally available on Darwin. */
  1070. const static struct {
  1071. const char *seg;
  1072. const char *sect;
  1073. } GC_dyld_sections[] = {
  1074. { SEG_DATA, SECT_DATA },
  1075. /* Used by FSF GCC, but not by OSX system tools, so far. */
  1076. { SEG_DATA, "__static_data" },
  1077. { SEG_DATA, SECT_BSS },
  1078. { SEG_DATA, SECT_COMMON },
  1079. /* FSF GCC - zero-sized object sections for targets supporting section
  1080. anchors. */
  1081. { SEG_DATA, "__zobj_data" },
  1082. { SEG_DATA, "__zobj_bss" }
  1083. };
  1084. /* Additional writeable sections:
  1085. GCC on Darwin constucts aligned sections "on demand", where the alignment
  1086. size is embedded in the section name. Furthermore, there are distintions
  1087. between sections containing private vs. public symbols.
  1088. It also constructs sections specifically for zero-sized objects, when the
  1089. target supports section anchors. */
  1090. const char * GC_dyld_add_sect_fmts[] =
  1091. {
  1092. "__bss%u",
  1093. "__pu_bss%u",
  1094. "__zo_bss%u",
  1095. "__zo_pu_bss%u",
  1096. NULL
  1097. } ;
  1098. /* Currently, mach-o will allow up to a max of 2^15 alignment in an
  1099. object file. */
  1100. #define L2_MAX_OFILE_ALIGNMENT 15
  1101. #ifdef DARWIN_DEBUG
  1102. static const char *
  1103. GC_dyld_name_for_hdr (const struct GC_MACH_HEADER *hdr)
  1104. {
  1105. unsigned long i,c;
  1106. c = _dyld_image_count();
  1107. for (i=0;i<c;i++)
  1108. if(_dyld_get_image_header(i) == hdr)
  1109. return _dyld_get_image_name(i);
  1110. return NULL;
  1111. }
  1112. #endif
  1113. /* This should never be called by a thread holding the lock */
  1114. static void
  1115. GC_dyld_image_add (const struct GC_MACH_HEADER *hdr, intptr_t slide)
  1116. {
  1117. char secnam[16];
  1118. unsigned long start,end,i,j;
  1119. const struct GC_MACH_SECTION *sec;
  1120. const char *fmt;
  1121. if (GC_no_dls)
  1122. return;
  1123. for (i=0; i<sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]); i++)
  1124. {
  1125. sec = GC_GETSECTBYNAME (hdr, GC_dyld_sections[i].seg,
  1126. GC_dyld_sections[i].sect);
  1127. if(sec == NULL || sec->size == 0)
  1128. continue;
  1129. start = slide + sec->addr;
  1130. end = start + sec->size;
  1131. # ifdef DARWIN_DEBUG
  1132. GC_printf5("Adding section __DATA,%s at %p-%p (%lu bytes) from image %s\n",
  1133. GC_dyld_sections[i].sect, start,end,sec->size,GC_dyld_name_for_hdr(hdr));
  1134. # endif
  1135. GC_add_roots((char*)start,(char*)end);
  1136. }
  1137. /* Sections constructed on demand. */
  1138. j=0;
  1139. while ((fmt = GC_dyld_add_sect_fmts[j]) != NULL)
  1140. {
  1141. /* Add our manufactured aligned BSS sections. */
  1142. for (i=0; i<=L2_MAX_OFILE_ALIGNMENT; i++)
  1143. {
  1144. snprintf (secnam, 16, fmt, (unsigned)i);
  1145. sec = GC_GETSECTBYNAME (hdr, SEG_DATA, secnam);
  1146. if (sec == NULL || sec->size == 0)
  1147. continue;
  1148. start = slide + sec->addr;
  1149. end = start + sec->size;
  1150. # ifdef DARWIN_DEBUG
  1151. GC_printf5("Adding section __DATA,%s at %p-%p (%lu bytes) from image %s\n",
  1152. secnam, start,end,sec->size,GC_dyld_name_for_hdr(hdr));
  1153. # endif
  1154. GC_add_roots((char*)start,(char*)end);
  1155. }
  1156. j++;
  1157. }
  1158. # ifdef DARWIN_DEBUG
  1159. GC_print_static_roots();
  1160. # endif
  1161. }
  1162. /* This should never be called by a thread holding the lock */
  1163. static void
  1164. GC_dyld_image_remove (const struct GC_MACH_HEADER *hdr, intptr_t slide)
  1165. {
  1166. char secnam[16];
  1167. unsigned long start,end,i,j;
  1168. const struct GC_MACH_SECTION *sec;
  1169. const char *fmt;
  1170. for (i=0; i<sizeof(GC_dyld_sections)/sizeof(GC_dyld_sections[0]); i++)
  1171. {
  1172. sec = GC_GETSECTBYNAME (hdr, GC_dyld_sections[i].seg,
  1173. GC_dyld_sections[i].sect);
  1174. if(sec == NULL || sec->size == 0)
  1175. continue;
  1176. start = slide + sec->addr;
  1177. end = start + sec->size;
  1178. # ifdef DARWIN_DEBUG
  1179. GC_printf5("Removing section __DATA,%s at %p-%p (%lu bytes) from image %s\n",
  1180. GC_dyld_sections[i].sect, start,end,sec->size,GC_dyld_name_for_hdr(hdr));
  1181. # endif
  1182. GC_remove_roots((char*)start,(char*)end);
  1183. }
  1184. /* Remove our on-demand sections. */
  1185. j=0;
  1186. while ((fmt = GC_dyld_add_sect_fmts[j]) != NULL)
  1187. {
  1188. for (i=0; i<=L2_MAX_OFILE_ALIGNMENT; i++)
  1189. {
  1190. snprintf (secnam, 16, fmt, (unsigned)i);
  1191. sec = GC_GETSECTBYNAME (hdr, SEG_DATA, secnam);
  1192. if (sec == NULL || sec->size == 0)
  1193. continue;
  1194. start = slide + sec->addr;
  1195. end = start + sec->size;
  1196. # ifdef DARWIN_DEBUG
  1197. GC_printf5("Removing section __DATA,%s at %p-%p (%lu bytes) from image %s\n",
  1198. secnam, start,end,sec->size,GC_dyld_name_for_hdr(hdr));
  1199. # endif
  1200. GC_remove_roots((char*)start,(char*)end);
  1201. }
  1202. j++;
  1203. }
  1204. # ifdef DARWIN_DEBUG
  1205. GC_print_static_roots();
  1206. # endif
  1207. }
  1208. void
  1209. GC_register_dynamic_libraries()
  1210. {
  1211. /* Currently does nothing. The callbacks are setup by GC_init_dyld()
  1212. The dyld library takes it from there. */
  1213. }
  1214. /* The _dyld_* functions have an internal lock so no _dyld functions
  1215. can be called while the world is stopped without the risk of a deadlock.
  1216. Because of this we MUST setup callbacks BEFORE we ever stop the world.
  1217. This should be called BEFORE any thread in created and WITHOUT the
  1218. allocation lock held. */
  1219. void
  1220. GC_init_dyld()
  1221. {
  1222. static GC_bool initialized = FALSE;
  1223. char *bind_fully_env = NULL;
  1224. if(initialized)
  1225. return;
  1226. # ifdef DARWIN_DEBUG
  1227. GC_printf0("Registering dyld callbacks...\n");
  1228. # endif
  1229. /* Apple's Documentation:
  1230. When you call _dyld_register_func_for_add_image, the dynamic linker runtime
  1231. calls the specified callback (func) once for each of the images that is
  1232. currently loaded into the program. When a new image is added to the program,
  1233. your callback is called again with the mach_header for the new image, and the
  1234. virtual memory slide amount of the new image.
  1235. This WILL properly register already linked libraries and libraries
  1236. linked in the future
  1237. */
  1238. _dyld_register_func_for_add_image(GC_dyld_image_add);
  1239. _dyld_register_func_for_remove_image(GC_dyld_image_remove);
  1240. /* Set this early to avoid reentrancy issues. */
  1241. initialized = TRUE;
  1242. bind_fully_env = getenv("DYLD_BIND_AT_LAUNCH");
  1243. if (bind_fully_env == NULL)
  1244. {
  1245. # ifdef DARWIN_DEBUG
  1246. GC_printf0("Forcing full bind of GC code...\n");
  1247. # endif
  1248. if (!_dyld_bind_fully_image_containing_address((unsigned long*)GC_malloc))
  1249. GC_abort("_dyld_bind_fully_image_containing_address failed");
  1250. }
  1251. }
  1252. #define HAVE_REGISTER_MAIN_STATIC_DATA
  1253. GC_bool
  1254. GC_register_main_static_data (void)
  1255. {
  1256. /* Already done through dyld callbacks */
  1257. return FALSE;
  1258. }
  1259. #endif /* DARWIN */
  1260. #else /* !DYNAMIC_LOADING */
  1261. #ifdef PCR
  1262. # include "il/PCR_IL.h"
  1263. # include "th/PCR_ThCtl.h"
  1264. # include "mm/PCR_MM.h"
  1265. void GC_register_dynamic_libraries()
  1266. {
  1267. /* Add new static data areas of dynamically loaded modules. */
  1268. {
  1269. PCR_IL_LoadedFile * p = PCR_IL_GetLastLoadedFile();
  1270. PCR_IL_LoadedSegment * q;
  1271. /* Skip uncommited files */
  1272. while (p != NIL && !(p -> lf_commitPoint)) {
  1273. /* The loading of this file has not yet been committed */
  1274. /* Hence its description could be inconsistent. */
  1275. /* Furthermore, it hasn't yet been run. Hence its data */
  1276. /* segments can't possibly reference heap allocated */
  1277. /* objects. */
  1278. p = p -> lf_prev;
  1279. }
  1280. for (; p != NIL; p = p -> lf_prev) {
  1281. for (q = p -> lf_ls; q != NIL; q = q -> ls_next) {
  1282. if ((q -> ls_flags & PCR_IL_SegFlags_Traced_MASK)
  1283. == PCR_IL_SegFlags_Traced_on) {
  1284. GC_add_roots_inner
  1285. ((char *)(q -> ls_addr),
  1286. (char *)(q -> ls_addr) + q -> ls_bytes,
  1287. TRUE);
  1288. }
  1289. }
  1290. }
  1291. }
  1292. }
  1293. #else /* !PCR */
  1294. void GC_register_dynamic_libraries(){}
  1295. int GC_no_dynamic_loading;
  1296. #endif /* !PCR */
  1297. #endif /* !DYNAMIC_LOADING */
  1298. #ifndef HAVE_REGISTER_MAIN_STATIC_DATA
  1299. /* Do we need to separately register the main static data segment? */
  1300. GC_bool GC_register_main_static_data()
  1301. {
  1302. return TRUE;
  1303. }
  1304. #endif /* HAVE_REGISTER_MAIN_STATIC_DATA */