diff options
Diffstat (limited to 'dyn_load.c')
-rw-r--r-- | dyn_load.c | 143 |
1 files changed, 20 insertions, 123 deletions
@@ -283,56 +283,23 @@ extern ssize_t GC_repeat_read(int fd, char *buf, size_t count); /* Repeatedly read until buffer is filled, or EOF is encountered */ /* Defined in os_dep.c. */ -static char *parse_map_entry(char *buf_ptr, word *start, word *end, - char *prot_buf, unsigned int *maj_dev); +char *GC_parse_map_entry(char *buf_ptr, word *start, word *end, + char *prot_buf, unsigned int *maj_dev); +word GC_apply_to_maps(word (*fn)(char *)); + /* From os_dep.c */ -void GC_register_dynamic_libraries() +word GC_register_map_entries(char *maps) { - int f; - int result; char prot_buf[5]; - int maps_size; - char maps_temp[32768]; - char *maps_buf; - char *buf_ptr; + char *buf_ptr = maps; int count; word start, end; - unsigned int maj_dev, min_dev; + unsigned int maj_dev; word least_ha, greatest_ha; unsigned i; word datastart = (word)(DATASTART); - /* Read /proc/self/maps */ - /* Note that we may not allocate, and thus can't use stdio. */ - f = open("/proc/self/maps", O_RDONLY); - if (-1 == f) ABORT("Couldn't open /proc/self/maps"); - /* stat() doesn't work for /proc/self/maps, so we have to - read it to find out how large it is... */ - maps_size = 0; - do { - result = GC_repeat_read(f, maps_temp, sizeof(maps_temp)); - if (result <= 0) ABORT("Couldn't read /proc/self/maps"); - maps_size += result; - } while (result == sizeof(maps_temp)); - - if (maps_size > sizeof(maps_temp)) { - /* If larger than our buffer, close and re-read it. */ - close(f); - f = open("/proc/self/maps", O_RDONLY); - if (-1 == f) ABORT("Couldn't open /proc/self/maps"); - maps_buf = alloca(maps_size); - if (NULL == maps_buf) ABORT("/proc/self/maps alloca failed"); - result = GC_repeat_read(f, maps_buf, maps_size); - if (result <= 0) ABORT("Couldn't read /proc/self/maps"); - } else { - /* Otherwise use the fixed size buffer */ - maps_buf = maps_temp; - } - - close(f); - maps_buf[result] = '\0'; - buf_ptr = maps_buf; - /* Compute heap bounds. Should be done by add_to_heap? */ + /* Compute heap bounds. FIXME: Should be done by add_to_heap? */ least_ha = (word)(-1); greatest_ha = 0; for (i = 0; i < GC_n_heap_sects; ++i) { @@ -343,11 +310,10 @@ void GC_register_dynamic_libraries() } if (greatest_ha < (word)GC_scratch_last_end_ptr) greatest_ha = (word)GC_scratch_last_end_ptr; - for (;;) { - - buf_ptr = parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev); - if (buf_ptr == NULL) return; + for (;;) { + buf_ptr = GC_parse_map_entry(buf_ptr, &start, &end, prot_buf, &maj_dev); + if (buf_ptr == NULL) return 1; if (prot_buf[1] == 'w') { /* This is a writable mapping. Add it to */ /* the root set unless it is already otherwise */ @@ -359,16 +325,7 @@ void GC_register_dynamic_libraries() # ifdef THREADS if (GC_segment_is_thread_stack(start, end)) continue; # endif - /* The rest of this assumes that there is no mapping */ - /* spanning the beginning of the data segment, or extending */ - /* beyond the entire heap at both ends. */ - /* Empirically these assumptions hold. */ - - if (start < (word)DATAEND && end > (word)DATAEND) { - /* Rld may use space at the end of the main data */ - /* segment. Thus we add that in. */ - start = (word)DATAEND; - } + /* We no longer exclude the main data segment. */ if (start < least_ha && end > least_ha) { end = least_ha; } @@ -378,7 +335,14 @@ void GC_register_dynamic_libraries() if (start >= least_ha && end <= greatest_ha) continue; GC_add_roots_inner((char *)start, (char *)end, TRUE); } - } + } + return 1; +} + +void GC_register_dynamic_libraries() +{ + if (!GC_apply_to_maps(GC_register_map_entries)) + ABORT("Failed to read /proc for library registration."); } /* We now take care of the main data segment ourselves: */ @@ -388,73 +352,6 @@ GC_bool GC_register_main_static_data() } # define HAVE_REGISTER_MAIN_STATIC_DATA -// -// parse_map_entry parses an entry from /proc/self/maps so we can -// locate all writable data segments that belong to shared libraries. -// The format of one of these entries and the fields we care about -// is as follows: -// XXXXXXXX-XXXXXXXX r-xp 00000000 30:05 260537 name of mapping...\n -// ^^^^^^^^ ^^^^^^^^ ^^^^ ^^ -// start end prot maj_dev -// 0 9 18 32 -// -// For 64 bit ABIs: -// 0 17 34 56 -// -// The parser is called with a pointer to the entry and the return value -// is either NULL or is advanced to the next entry(the byte after the -// trailing '\n'.) -// -#if CPP_WORDSZ == 32 -# define OFFSET_MAP_START 0 -# define OFFSET_MAP_END 9 -# define OFFSET_MAP_PROT 18 -# define OFFSET_MAP_MAJDEV 32 -# define ADDR_WIDTH 8 -#endif - -#if CPP_WORDSZ == 64 -# define OFFSET_MAP_START 0 -# define OFFSET_MAP_END 17 -# define OFFSET_MAP_PROT 34 -# define OFFSET_MAP_MAJDEV 56 -# define ADDR_WIDTH 16 -#endif - -static char *parse_map_entry(char *buf_ptr, word *start, word *end, - char *prot_buf, unsigned int *maj_dev) -{ - int i; - char *tok; - - if (buf_ptr == NULL || *buf_ptr == '\0') { - return NULL; - } - - memcpy(prot_buf, buf_ptr+OFFSET_MAP_PROT, 4); // do the protections first - prot_buf[4] = '\0'; - - if (prot_buf[1] == 'w') { // we can skip all of this if it's not writable - - tok = buf_ptr; - buf_ptr[OFFSET_MAP_START+ADDR_WIDTH] = '\0'; - *start = strtoul(tok, NULL, 16); - - tok = buf_ptr+OFFSET_MAP_END; - buf_ptr[OFFSET_MAP_END+ADDR_WIDTH] = '\0'; - *end = strtoul(tok, NULL, 16); - - buf_ptr += OFFSET_MAP_MAJDEV; - tok = buf_ptr; - while (*buf_ptr != ':') buf_ptr++; - *buf_ptr++ = '\0'; - *maj_dev = strtoul(tok, NULL, 16); - } - - while (*buf_ptr && *buf_ptr++ != '\n'); - - return buf_ptr; -} #endif /* USE_PROC_FOR_LIBRARIES */ |