bool PinyinPhraseLib::load_lib (const char *libfile, const char *pylibfile, const char *idxfile) { std::ifstream is_lib(libfile); std::ifstream is_pylib (pylibfile); std::ifstream is_idx (idxfile); if (!is_lib) return false; input (is_lib, is_pylib, is_idx); compact_memory (); return number_of_phrases () != 0; }
std::pair<string_set *, string_set *> *WorkerBee::linked_libraries(const std::string executable) { /* open the offending executable */ int fl = open(executable.c_str(), O_RDONLY); if (-1 == fl) { fprintf(stderr, "Could not open %s\n", executable.c_str()); return NULL; } /* Make sure we are working with a version of libelf */ if (EV_NONE == elf_version(EV_CURRENT)) { fprintf(stderr, "ELF libs failed to initialize\n"); return NULL; } // Start the elf interrogation Elf *elf = elf_begin(fl, ELF_C_READ, NULL); if (NULL == elf) { fprintf(stderr, "elf_begin failed because %s\n", elf_errmsg(-1)); return NULL; } // Show deps GElf_Ehdr ehdr; if (!gelf_getehdr(elf, &ehdr)) { fprintf(stderr, "elf_getehdr failed from %s\n", elf_errmsg(-1)); return NULL; } // scanning the headers string_set *libs = new string_set(), *paths = new string_set(); // include the standard library paths paths->insert(""); // So we get full-path libraries included paths->insert("/lib"); paths->insert("/usr/lib"); paths->insert("/usr/local/lib"); // include standard libraries to copy libs->insert("libdl.so.2"); libs->insert("libm.so.2"); libs->insert("libpthread.so.0"); libs->insert("libattr.so.1"); // Start scanning the header Elf_Scn *scn = elf_nextscn(elf, NULL); GElf_Shdr shdr; while (scn) { if (NULL == gelf_getshdr(scn, &shdr)) { fprintf(stderr, "getshdr() failed from %s\n", elf_errmsg(-1)); return NULL; } // get the name of the section (could optimize) char * nm = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name); if (NULL == nm) { fprintf(stderr, "elf_strptr() failed from %s\n", elf_errmsg(-1)); return NULL; } // look through the headers for the .dynstr and .interp headers if (strcmp(nm, ".bss")) { Elf_Data *data = NULL; size_t n = 0; // for each header, find the name if it matches the library name regex while (n < shdr.sh_size && (data = elf_getdata(scn, data)) ) { char *bfr = static_cast<char *>(data->d_buf); char *p = bfr + 1; while (p < bfr + data->d_size) { if (is_lib(p)) { libs->insert(p); } size_t lngth = strlen(p) + 1; n += lngth; p += lngth; } } } scn = elf_nextscn(elf, scn); } return new std::pair<string_set *, string_set*> (libs, paths); }