void plugin_load(struct uct_pluginset *ps, char *path, char *args) { ps->plugins = realloc(ps->plugins, ++ps->n_plugins * sizeof(ps->plugins[0])); struct plugin *p = &ps->plugins[ps->n_plugins - 1]; p->path = strdup(path); p->args = args ? strdup(args) : args; p->dlh = dlopen(path, RTLD_NOW); if (!p->dlh) { fprintf(stderr, "Cannot load plugin %s: %s\n", path, dlerror()); exit(EXIT_FAILURE); } #define loadsym(s_) do {\ p->s_ = dlsym(p->dlh, "pachi_plugin_" #s_); \ if (!p->s_) { \ fprintf(stderr, "Cannot find pachi_plugin_%s in plugin %s: %s\n", #s_, path, dlerror()); \ exit(EXIT_FAILURE); \ } \ } while (0) loadsym(init); loadsym(prior); loadsym(done); p->data = p->init(p->args, ps->b, fast_random(65536)); }
void socket_lib_init() { socket_fn.accept = loadsym(libc_dl_handle, "accept"); socket_fn.send = loadsym(libc_dl_handle, "send"); socket_fn.recv = loadsym(libc_dl_handle, "recv"); }
void elf_mod_symload(int strtablen) { Elf_Ehdr ehdr; char *shstrtab; struct elf_section *head, *s; char *symbuf, *strbuf; /* * Seek to the text offset to start loading... */ if (lseek(modfd, 0, SEEK_SET) == -1) err(12, "lseek"); if (read_elf_header(modfd, &ehdr) < 0) return; shstrtab = read_shstring_table(modfd, &ehdr); read_sections(modfd, &ehdr, shstrtab, &head); for (s = head; s; s = s->next) { struct elf_section *p = s; if ((p->type == SHT_SYMTAB) || (p->type == SHT_DYNSYM)) { if (debug) fprintf(stderr, "loading `%s': addr = %p, " "size = %#lx\n", s->name, s->addr, (u_long)s->size); /* * Seek to the file offset to start loading it... */ if (lseek(modfd, p->offset, SEEK_SET) == -1) err(12, "lseek"); symbuf = malloc(p->size); if (symbuf == 0) err(13, "malloc"); if (read(modfd, symbuf, p->size) != p->size) err(14, "read"); loadsym(symbuf, p->size); free(symbuf); } } for (s = head; s; s = s->next) { struct elf_section *p = s; if ((p->type == SHT_STRTAB) && (strcmp(p->name, ".strtab") == 0 )) { if (debug) fprintf(stderr, "loading `%s': addr = %p, " "size = %#lx\n", s->name, s->addr, (u_long)s->size); /* * Seek to the file offset to start loading it... */ if (lseek(modfd, p->offset, SEEK_SET) == -1) err(12, "lseek"); strbuf = malloc(p->size); if (strbuf == 0) err(13, "malloc"); if (read(modfd, strbuf, p->size) != p->size) err(14, "read"); loadsym(strbuf, p->size); free(strbuf); } } free(shstrtab); free_sections(head); return; }