2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2001,2009 Juan Cespedes
5 * Copyright (C) 2006 Ian Wienand
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
33 #include "backend.h" // for arch_library_symbol_init, arch_library_init
36 library_exported_names_init(struct library_exported_names *names);
38 #ifndef OS_HAVE_LIBRARY_DATA
40 os_library_init(struct library *lib)
46 os_library_destroy(struct library *lib)
51 os_library_clone(struct library *retp, struct library *lib)
57 #ifndef ARCH_HAVE_LIBRARY_DATA
59 arch_library_init(struct library *lib)
65 arch_library_destroy(struct library *lib)
70 arch_library_clone(struct library *retp, struct library *lib)
76 #ifndef OS_HAVE_LIBRARY_SYMBOL_DATA
78 os_library_symbol_init(struct library_symbol *libsym)
84 os_library_symbol_destroy(struct library_symbol *libsym)
89 os_library_symbol_clone(struct library_symbol *retp,
90 struct library_symbol *libsym)
96 #ifndef ARCH_HAVE_LIBRARY_SYMBOL_DATA
98 arch_library_symbol_init(struct library_symbol *libsym)
104 arch_library_symbol_destroy(struct library_symbol *libsym)
109 arch_library_symbol_clone(struct library_symbol *retp,
110 struct library_symbol *libsym)
117 arch_addr_hash(const arch_addr_t *addr)
121 int ints[sizeof(arch_addr_t)
122 / sizeof(unsigned int)];
123 } u = { .addr = *addr };
127 for (i = 0; i < sizeof(u.ints) / sizeof(*u.ints); ++i)
128 h ^= dict_hash_int(&u.ints[i]);
133 arch_addr_eq(const arch_addr_t *addr1, const arch_addr_t *addr2)
135 return *addr1 == *addr2;
139 strdup_if(const char **retp, const char *str, int whether)
141 if (whether && str != NULL) {
152 private_library_symbol_init(struct library_symbol *libsym,
154 const char *name, int own_name,
155 enum toplt type_of_plt,
156 int latent, int delayed)
160 libsym->plt_type = type_of_plt;
162 libsym->own_name = own_name;
163 libsym->latent = latent;
164 libsym->delayed = delayed;
165 libsym->enter_addr = (void *)(uintptr_t)addr;
166 libsym->proto = NULL;
170 private_library_symbol_destroy(struct library_symbol *libsym)
172 library_symbol_set_name(libsym, NULL, 0);
176 library_symbol_init(struct library_symbol *libsym,
177 arch_addr_t addr, const char *name, int own_name,
178 enum toplt type_of_plt)
180 private_library_symbol_init(libsym, addr, name, own_name,
183 if (os_library_symbol_init(libsym) < 0)
184 /* We've already set libsym->name and own_name. But
185 * we return failure, and the client code isn't
186 * supposed to call library_symbol_destroy in such
190 if (arch_library_symbol_init(libsym) < 0) {
191 os_library_symbol_destroy(libsym);
199 library_symbol_destroy(struct library_symbol *libsym)
201 if (libsym != NULL) {
202 arch_library_symbol_destroy(libsym);
203 os_library_symbol_destroy(libsym);
204 private_library_symbol_destroy(libsym);
209 library_symbol_clone(struct library_symbol *retp, struct library_symbol *libsym)
211 /* Make lifetimes of name stored at original independent of
212 * the one at the clone. */
214 if (strdup_if(&name, libsym->name, libsym->own_name) < 0)
217 private_library_symbol_init(retp, libsym->enter_addr,
218 name, libsym->own_name, libsym->plt_type,
219 libsym->latent, libsym->delayed);
221 if (os_library_symbol_clone(retp, libsym) < 0) {
223 private_library_symbol_destroy(retp);
227 if (arch_library_symbol_clone(retp, libsym) < 0) {
228 os_library_symbol_destroy(retp);
236 library_symbol_cmp(struct library_symbol *a, struct library_symbol *b)
238 if (a->enter_addr < b->enter_addr)
240 if (a->enter_addr > b->enter_addr)
242 if (a->name != NULL && b->name != NULL)
243 return strcmp(a->name, b->name);
244 if (a->name == NULL) {
253 library_symbol_set_name(struct library_symbol *libsym,
254 const char *name, int own_name)
256 if (libsym->own_name)
257 free((char *)libsym->name);
259 libsym->own_name = own_name;
263 library_symbol_equal_cb(struct library_symbol *libsym, void *u)
265 struct library_symbol *standard = u;
266 return library_symbol_cmp(libsym, standard) == 0 ? CBS_STOP : CBS_CONT;
270 library_symbol_named_cb(struct library_symbol *libsym, void *name)
272 return strcmp(libsym->name, name) == 0 ? CBS_STOP : CBS_CONT;
276 library_symbol_delayed_cb(struct library_symbol *libsym, void *unused)
278 return libsym->delayed ? CBS_STOP : CBS_CONT;
282 private_library_init(struct library *lib, enum library_type type)
290 lib->protolib = NULL;
295 lib->pathname = NULL;
296 lib->own_pathname = 0;
299 library_exported_names_init(&lib->exported_names);
300 lib->should_activate_latent = false;
303 #if defined(HAVE_LIBDW)
304 lib->dwfl_module = NULL;
309 library_init(struct library *lib, enum library_type type)
311 private_library_init(lib, type);
313 if (os_library_init(lib) < 0)
316 if (arch_library_init(lib) < 0) {
317 os_library_destroy(lib);
328 static void dtor_string(const char **tgt, void *data)
332 static int clone_vect(struct vect **to, const struct vect **from, void *data)
334 *to = malloc(sizeof(struct vect));
339 VECT_CLONE(*to, *from, const char*,
344 static void dtor_vect(struct vect **tgt, void *data)
346 VECT_DESTROY(*tgt, const char*, dtor_string, NULL);
351 library_exported_names_init(struct library_exported_names *names)
353 DICT_INIT(&names->names,
354 const char*, uint64_t,
355 dict_hash_string, dict_eq_string, NULL);
356 DICT_INIT(&names->addrs,
357 uint64_t, struct vect*,
358 dict_hash_uint64, dict_eq_uint64, NULL);
362 library_exported_names_destroy(struct library_exported_names *names)
364 DICT_DESTROY(&names->names,
365 const char*, uint64_t,
366 dtor_string, NULL, NULL);
367 DICT_DESTROY(&names->addrs,
368 uint64_t, struct vect*,
369 NULL, dtor_vect, NULL);
373 library_exported_names_clone(struct library_exported_names *retp,
374 const struct library_exported_names *names)
376 return (DICT_CLONE(&retp->names, &names->names,
377 const char*, uint64_t,
378 dict_clone_string, dtor_string,
381 DICT_CLONE(&retp->addrs, &names->addrs,
382 uint64_t, struct vect*,
384 clone_vect, dtor_vect,
388 int library_exported_names_push(struct library_exported_names *names,
389 uint64_t addr, char *name,
392 // first, take ownership of the name, if it's not yet ours
398 // push to the name->addr map
399 int result = DICT_INSERT(&names->names, &name, &addr);
401 // This symbol is already present in the table. This library has
402 // multiple copies of this symbol (probably corresponding to
403 // different symbol versions). I should handle this gracefully
404 // at some point, but for now I simply ignore later instances of
405 // any particular symbol
413 // push to the addr->names map
414 // I get the alias vector. If it doesn't yet exist, I make it
415 struct vect *aliases;
416 struct vect **paliases = DICT_FIND_REF(&names->addrs,
417 &addr, struct vect*);
419 if (paliases == NULL) {
420 aliases = malloc(sizeof(struct vect));
423 VECT_INIT(aliases, const char*);
424 result = DICT_INSERT(&names->addrs, &addr, &aliases);
432 char *namedup = strdup(name);
436 result = vect_pushback(aliases, &namedup);
445 struct library_exported_names_each_alias_context
447 enum callback_status (*inner_cb)(const char *, void *);
448 const char *origname;
451 static enum callback_status
452 library_exported_names_each_alias_cb(const char **name, void *data)
454 struct library_exported_names_each_alias_context *context =
455 (struct library_exported_names_each_alias_context*)data;
457 // I do not report the original name we were asked about. Otherwise, any
458 // time the caller asks for aliases of symbol "sym", I'll always report
459 // "sym" along with any actual aliases
460 if (strcmp(*name, context->origname) == 0)
463 return context->inner_cb(*name, context->data);
466 const char** library_exported_names_each_alias(
467 struct library_exported_names *names,
468 const char *aliasname,
469 const char **name_start_after,
470 enum callback_status (*cb)(const char *,
474 // I have a symbol name. I look up its address, then get the list of
476 uint64_t *addr = DICT_FIND_REF(&names->names,
477 &aliasname, uint64_t);
481 // OK. I have an address. Get the list of symbols at this address
482 struct vect **aliases = DICT_FIND_REF(&names->addrs,
484 assert(aliases != NULL);
486 struct library_exported_names_each_alias_context context =
488 .origname = aliasname,
490 return VECT_EACH(*aliases, const char*, name_start_after,
491 library_exported_names_each_alias_cb, &context);
494 int library_exported_names_contains(struct library_exported_names* names,
495 const char* queryname)
497 uint64_t *addr = DICT_FIND_REF(&names->names,
498 &queryname, uint64_t);
499 return (addr == NULL) ? 0 : 1;
507 library_clone(struct library *retp, struct library *lib)
509 const char *soname = NULL;
510 const char *pathname;
512 /* Make lifetimes of strings stored at original independent of
513 * those at the clone. */
514 if (strdup_if(&soname, lib->soname, lib->own_soname) < 0
515 || strdup_if(&pathname, lib->pathname, lib->own_pathname) < 0) {
517 free((char *)soname);
521 private_library_init(retp, lib->type);
522 library_set_soname(retp, soname, lib->own_soname);
523 library_set_pathname(retp, pathname, lib->own_pathname);
525 retp->key = lib->key;
529 struct library_symbol *it;
530 struct library_symbol **nsymp = &retp->symbols;
531 for (it = lib->symbols; it != NULL; it = it->next) {
532 *nsymp = malloc(sizeof(**nsymp));
534 || library_symbol_clone(*nsymp, it) < 0) {
538 /* Release what we managed to allocate. */
539 library_destroy(retp);
543 (*nsymp)->lib = retp;
544 nsymp = &(*nsymp)->next;
549 /* Clone exported names. */
550 if (library_exported_names_clone(&retp->exported_names,
551 &lib->exported_names) != 0)
554 if (os_library_clone(retp, lib) < 0)
557 if (arch_library_clone(retp, lib) < 0) {
558 os_library_destroy(retp);
566 library_destroy(struct library *lib)
571 arch_library_destroy(lib);
572 os_library_destroy(lib);
574 library_set_soname(lib, NULL, 0);
575 library_set_pathname(lib, NULL, 0);
577 struct library_symbol *sym;
578 for (sym = lib->symbols; sym != NULL; ) {
579 struct library_symbol *next = sym->next;
580 library_symbol_destroy(sym);
585 /* Release exported names. */
586 library_exported_names_destroy(&lib->exported_names);
590 library_set_soname(struct library *lib, const char *new_name, int own_name)
593 free((char *)lib->soname);
594 lib->soname = new_name;
595 lib->own_soname = own_name;
599 library_set_pathname(struct library *lib, const char *new_name, int own_name)
601 if (lib->own_pathname)
602 free((char *)lib->pathname);
603 lib->pathname = new_name;
604 lib->own_pathname = own_name;
607 struct library_symbol *
608 library_each_symbol(struct library *lib, struct library_symbol *start_after,
609 enum callback_status (*cb)(struct library_symbol *, void *),
612 struct library_symbol *it = start_after == NULL ? lib->symbols
616 struct library_symbol *next = it->next;
618 switch ((*cb)(it, data)) {
634 library_add_symbol(struct library *lib, struct library_symbol *first)
636 struct library_symbol *last;
637 for (last = first; last != NULL; ) {
639 if (last->next != NULL)
645 assert(last->next == NULL);
646 last->next = lib->symbols;
647 lib->symbols = first;
651 library_named_cb(struct process *proc, struct library *lib, void *name)
653 if (name == lib->soname
654 || strcmp(lib->soname, (char *)name) == 0)
661 library_with_key_cb(struct process *proc, struct library *lib, void *keyp)
663 return lib->key == *(arch_addr_t *)keyp ? CBS_STOP : CBS_CONT;