static elf_t * open_object(const char * path) { if (!path) { _main_obj->loaded = 1; return _main_obj; } if (hashmap_has(objects_map, (void*)path)) { elf_t * object = hashmap_get(objects_map, (void*)path); object->loaded = 1; return object; } char * file = find_lib(path); if (!file) return NULL; FILE * f = fopen(file, "r"); free(file); if (!f) { return NULL; } elf_t * object = calloc(1, sizeof(elf_t)); hashmap_set(objects_map, (void*)path, object); if (!object) { return NULL; } object->file = f; size_t r = fread(&object->header, sizeof(Elf32_Header), 1, object->file); if (!r) { free(object); return NULL; } if (object->header.e_ident[0] != ELFMAG0 || object->header.e_ident[1] != ELFMAG1 || object->header.e_ident[2] != ELFMAG2 || object->header.e_ident[3] != ELFMAG3) { free(object); return NULL; } object->dependencies = list_create(); return object; }
int check_path(char **pathsep, char *cmd, char **str, t_inp p) { int ret; char *filepath; ret = 1; filepath = NULL; if (pathsep != NULL) filepath = find_lib(pathsep, cmd); ret = execute(filepath, cmd, str, p); free(str); free(filepath); free(pathsep); return (ret); }
bool read_lib_info(pid_t pid) { char fname[32]; char buf[256]; FILE *fp = NULL; char *execfile = ""; guarantee(debuggee.pid = pid, "wrong pid value"); sprintf(fname, "/proc/%d/maps", pid); fp = fopen(fname, "r"); if (fp == NULL) { fatal("can't open maps file"); } while(fgets_no_cr(buf, 256, fp)){ char * word[6]; int nwords = split_n_str(buf, 6, word, ' ', '\0'); # define EXE_LOC "08048000-" if (strncmp(buf, EXE_LOC, strlen(EXE_LOC)) == 0){ execfile = strdup(word[5]); } else if (nwords > 5 && strcmp(word[5], execfile)!=0 && find_lib(word[5]) == NULL) { lib_info *lib = malloc(sizeof(lib_info)); lib->symtab = build_symtab(word[5]); if (lib->symtab) { sscanf(word[0], "%x", &lib->base); strcpy(lib->name, word[5]); lib->next = debuggee.libs; debuggee.libs = lib; } else { // not an elf file // FIXME: we could also reach here if library does not have // symbol table. free(lib); } } } fclose(fp); }
int main (int argc, char **argv) { int x = 0, y = 0, result[MAXBUFSIZE]; char buf[MAXBUFSIZE]; if (!(config_mak = fopen ("config.mak", "wb"))) return -1; if (!(config_h = fopen ("config.h", "wb"))) { fclose (config_mak); remove ("config.mak"); return -1; } for (x = 0; configure_in[x].config_mak || configure_in[x].config_h || configure_in[x].success || configure_in[x].failure; x++) { st_configure_in_t *c = &configure_in[x]; result[x] = 1; if (c->lib[0] || c->header[0] || c->file[0]) { if (c->lib) for (y = 0; c->lib[y]; y++) if (find_lib (c->lib[y]) == -1) { result[x] = 0; break; } if (result[x]) if (c->header) for (y = 0; c->header[y]; y++) if (find_header (c->header[y]) == -1) { result[x] = 0; break; } if (result[x]) if (c->file) for (y = 0; c->file[y]; y++) if (find_file (c->file[y]) == -1) { result[x] = 0; break; } } if (result[x]) { if (c->config_mak) fprintf (config_mak, "%s\n", c->config_mak); if (c->config_h) fputs (c->config_h, config_h); } #if 0 else { if (c->config_mak) fprintf (config_mak, "#%s\n", c->config_mak); if (c->config_h) fprintf (config_h, "/*\n%s\n*/\n", c->config_h); } #endif } puts ("config.status: creating config.mak\n" "config.status: creating config.h\n"); fclose (config_h); fclose (config_mak); for (x = 0; configure_in[x].config_mak || configure_in[x].config_h || configure_in[x].success || configure_in[x].failure; x++) { st_configure_in_t *c = &configure_in[x]; if (result[x] && c->success) puts (c->success); if (!result[x] && c->failure) puts (c->failure); } puts ("\n"); // ("\nYou may edit config.mak and/or config.h by hand to disable features\n"); return 0; }
static bool read_lib_info(struct ps_prochandle* ph) { char fname[32]; char buf[PATH_MAX]; FILE *fp = NULL; sprintf(fname, "/proc/%d/maps", ph->pid); fp = fopen(fname, "r"); if (fp == NULL) { print_debug("can't open /proc/%d/maps file\n", ph->pid); return false; } while(fgets_no_cr(buf, PATH_MAX, fp)){ char * word[7]; int nwords = split_n_str(buf, 7, word, ' ', '\0'); if (nwords < 6) { // not a shared library entry. ignore. continue; } // SA does not handle the lines with patterns: // "[stack]", "[heap]", "[vdso]", "[vsyscall]", etc. if (word[5][0] == '[') { // not a shared library entry. ignore. continue; } if (nwords > 6) { // prelink altered mapfile when the program is running. // Entries like one below have to be skipped // /lib64/libc-2.15.so (deleted) // SO name in entries like one below have to be stripped. // /lib64/libpthread-2.15.so.#prelink#.EECVts char *s = strstr(word[5],".#prelink#"); if (s == NULL) { // No prelink keyword. skip deleted library print_debug("skip shared object %s deleted by prelink\n", word[5]); continue; } // Fall through print_debug("rectifying shared object name %s changed by prelink\n", word[5]); *s = 0; } if (find_lib(ph, word[5]) == false) { intptr_t base; lib_info* lib; #ifdef _LP64 sscanf(word[0], "%lx", &base); #else sscanf(word[0], "%x", &base); #endif if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL) continue; // ignore, add_lib_info prints error // we don't need to keep the library open, symtab is already // built. Only for core dump we need to keep the fd open. close(lib->fd); lib->fd = -1; } } fclose(fp); return true; }
static bool read_lib_info(struct ps_prochandle* ph) { #if defined(__FreeBSD__) && __FreeBSD_version >= 701000 struct kinfo_vmentry *freep, *kve; int i, cnt; freep = kinfo_getvmmap(ph->pid, &cnt); if (freep == NULL) { print_debug("can't get vm map for pid\n", ph->pid); return false; } for (i = 0; i < cnt; i++) { kve = &freep[i]; if ((kve->kve_flags & KVME_FLAG_COW) && kve->kve_path != NULL && strlen(kve->kve_path) > 0) { if (find_lib(ph, kve->kve_path) == false) { lib_info* lib; if ((lib = add_lib_info(ph, kve->kve_path, (uintptr_t) kve->kve_start)) == NULL) continue; // ignore, add_lib_info prints error // we don't need to keep the library open, symtab is already // built. Only for core dump we need to keep the fd open. close(lib->fd); lib->fd = -1; } } } free(freep); return true; #else char *l_name; struct link_map *lmap; uintptr_t lmap_addr; if ((l_name = malloc(BUF_SIZE)) == NULL) return false; if ((lmap = malloc(sizeof(*lmap))) == NULL) { free(l_name); return false; } lmap_addr = linkmap_addr(ph); if (lmap_addr == 0) { free(l_name); free(lmap); return false; } do { if (process_read_data(ph, lmap_addr, (char *)lmap, sizeof(*lmap)) != true) { print_debug("process_read_data failed for lmap_addr %p\n", lmap_addr); free (l_name); free (lmap); return false; } if (process_read_data(ph, (uintptr_t)lmap->l_name, l_name, BUF_SIZE) != true) { print_debug("process_read_data failed for lmap->l_name %p\n", lmap->l_name); free (l_name); free (lmap); return false; } if (find_lib(ph, l_name) == false) { lib_info* lib; if ((lib = add_lib_info(ph, l_name, (uintptr_t) lmap->l_addr)) == NULL) continue; // ignore, add_lib_info prints error // we don't need to keep the library open, symtab is already // built. Only for core dump we need to keep the fd open. close(lib->fd); lib->fd = -1; } lmap_addr = (uintptr_t)lmap->l_next; } while (lmap->l_next != NULL); free (l_name); free (lmap); return true; #endif }