library.c revision d7e4ca82e1cf20bb2605befb1da74dd1688c706e
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc. 4 * Copyright (C) 2001,2009 Juan Cespedes 5 * Copyright (C) 2006 Ian Wienand 6 * 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. 11 * 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. 16 * 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 20 * 02110-1301 USA 21 */ 22 23#include <stdlib.h> 24#include <string.h> 25#include <assert.h> 26#include <stdio.h> 27 28#include "library.h" 29#include "callback.h" 30#include "debug.h" 31#include "dict.h" 32#include "backend.h" // for arch_library_symbol_init, arch_library_init 33 34#ifndef ARCH_HAVE_LIBRARY_DATA 35void 36arch_library_init(struct library *lib) 37{ 38} 39 40void 41arch_library_destroy(struct library *lib) 42{ 43} 44 45void 46arch_library_clone(struct library *retp, struct library *lib) 47{ 48} 49#endif 50 51#ifndef ARCH_HAVE_LIBRARY_SYMBOL_DATA 52int 53arch_library_symbol_init(struct library_symbol *libsym) 54{ 55 return 0; 56} 57 58void 59arch_library_symbol_destroy(struct library_symbol *libsym) 60{ 61} 62 63int 64arch_library_symbol_clone(struct library_symbol *retp, 65 struct library_symbol *libsym) 66{ 67 return 0; 68} 69#endif 70 71size_t 72arch_addr_hash(const arch_addr_t *addr) 73{ 74 union { 75 arch_addr_t addr; 76 int ints[sizeof(arch_addr_t) 77 / sizeof(unsigned int)]; 78 } u = { .addr = *addr }; 79 80 size_t i; 81 size_t h = 0; 82 for (i = 0; i < sizeof(u.ints) / sizeof(*u.ints); ++i) 83 h ^= dict_hash_int(&u.ints[i]); 84 return h; 85} 86 87int 88arch_addr_eq(const arch_addr_t *addr1, const arch_addr_t *addr2) 89{ 90 return *addr1 == *addr2; 91} 92 93/* If the other symbol owns the name, we need to make the copy, so 94 * that the life-times of the two symbols are not dependent on each 95 * other. */ 96static int 97strdup_if_owned(const char **retp, const char *str, int owned) 98{ 99 if (!owned || str == NULL) { 100 *retp = str; 101 return 0; 102 } else { 103 *retp = strdup(str); 104 return *retp != NULL ? 0 : -1; 105 } 106} 107 108static void 109private_library_symbol_init(struct library_symbol *libsym, 110 arch_addr_t addr, 111 const char *name, int own_name, 112 enum toplt type_of_plt, 113 int latent, int delayed) 114{ 115 libsym->next = NULL; 116 libsym->lib = NULL; 117 libsym->plt_type = type_of_plt; 118 libsym->name = name; 119 libsym->own_name = own_name; 120 libsym->latent = latent; 121 libsym->delayed = delayed; 122 libsym->enter_addr = (void *)(uintptr_t)addr; 123} 124 125static void 126private_library_symbol_destroy(struct library_symbol *libsym) 127{ 128 library_symbol_set_name(libsym, NULL, 0); 129} 130 131int 132library_symbol_init(struct library_symbol *libsym, 133 arch_addr_t addr, const char *name, int own_name, 134 enum toplt type_of_plt) 135{ 136 private_library_symbol_init(libsym, addr, name, own_name, 137 type_of_plt, 0, 0); 138 139 /* If arch init fails, we've already set libsym->name and 140 * own_name. But we return failure, and the client code isn't 141 * supposed to call library_symbol_destroy in such a case. */ 142 return arch_library_symbol_init(libsym); 143} 144 145void 146library_symbol_destroy(struct library_symbol *libsym) 147{ 148 if (libsym != NULL) { 149 private_library_symbol_destroy(libsym); 150 arch_library_symbol_destroy(libsym); 151 } 152} 153 154int 155library_symbol_clone(struct library_symbol *retp, struct library_symbol *libsym) 156{ 157 const char *name; 158 if (strdup_if_owned(&name, libsym->name, libsym->own_name) < 0) 159 return -1; 160 161 private_library_symbol_init(retp, libsym->enter_addr, 162 name, libsym->own_name, libsym->plt_type, 163 libsym->latent, libsym->delayed); 164 165 if (arch_library_symbol_clone(retp, libsym) < 0) { 166 private_library_symbol_destroy(retp); 167 return -1; 168 } 169 170 return 0; 171} 172 173int 174library_symbol_cmp(struct library_symbol *a, struct library_symbol *b) 175{ 176 if (a->enter_addr < b->enter_addr) 177 return -1; 178 if (a->enter_addr > b->enter_addr) 179 return 1; 180 if (a->name != NULL && b->name != NULL) 181 return strcmp(a->name, b->name); 182 if (a->name == NULL) { 183 if (b->name == NULL) 184 return 0; 185 return -1; 186 } 187 return 1; 188} 189 190void 191library_symbol_set_name(struct library_symbol *libsym, 192 const char *name, int own_name) 193{ 194 if (libsym->own_name) 195 free((char *)libsym->name); 196 libsym->name = name; 197 libsym->own_name = own_name; 198} 199 200enum callback_status 201library_symbol_equal_cb(struct library_symbol *libsym, void *u) 202{ 203 struct library_symbol *standard = u; 204 return library_symbol_cmp(libsym, standard) == 0 ? CBS_STOP : CBS_CONT; 205} 206 207enum callback_status 208library_symbol_named_cb(struct library_symbol *libsym, void *name) 209{ 210 return strcmp(libsym->name, name) == 0 ? CBS_STOP : CBS_CONT; 211} 212 213enum callback_status 214library_symbol_delayed_cb(struct library_symbol *libsym, void *unused) 215{ 216 return libsym->delayed ? CBS_STOP : CBS_CONT; 217} 218 219static void 220private_library_init(struct library *lib, enum library_type type) 221{ 222 lib->next = NULL; 223 224 lib->key = 0; 225 lib->base = 0; 226 lib->entry = 0; 227 lib->dyn_addr = 0; 228 229 lib->soname = NULL; 230 lib->own_soname = 0; 231 232 lib->pathname = NULL; 233 lib->own_pathname = 0; 234 235 lib->symbols = NULL; 236 lib->exported_names = NULL; 237 lib->type = type; 238} 239 240void 241library_init(struct library *lib, enum library_type type) 242{ 243 private_library_init(lib, type); 244 arch_library_init(lib); 245} 246 247static int 248library_exported_name_clone(struct library_exported_name *retp, 249 struct library_exported_name *exnm) 250{ 251 char *name = exnm->own_name ? strdup(exnm->name) : (char *)exnm->name; 252 if (name == NULL) 253 return -1; 254 retp->name = name; 255 retp->own_name = exnm->own_name; 256 return 0; 257} 258 259int 260library_clone(struct library *retp, struct library *lib) 261{ 262 const char *soname = NULL; 263 const char *pathname; 264 if (strdup_if_owned(&soname, lib->soname, lib->own_soname) < 0 265 || strdup_if_owned(&pathname, 266 lib->pathname, lib->own_pathname) < 0) { 267 if (lib->own_soname) 268 free((char *)soname); 269 return -1; 270 } 271 272 private_library_init(retp, lib->type); 273 library_set_soname(retp, soname, lib->own_soname); 274 library_set_pathname(retp, pathname, lib->own_pathname); 275 arch_library_clone(retp, lib); 276 277 retp->key = lib->key; 278 279 /* Clone symbols. */ 280 { 281 struct library_symbol *it; 282 struct library_symbol **nsymp = &retp->symbols; 283 for (it = lib->symbols; it != NULL; it = it->next) { 284 *nsymp = malloc(sizeof(**nsymp)); 285 if (*nsymp == NULL 286 || library_symbol_clone(*nsymp, it) < 0) { 287 free(*nsymp); 288 fail: 289 /* Release what we managed to allocate. */ 290 library_destroy(retp); 291 return -1; 292 } 293 294 (*nsymp)->lib = retp; 295 nsymp = &(*nsymp)->next; 296 } 297 *nsymp = NULL; 298 } 299 300 /* Clone exported names. */ 301 { 302 struct library_exported_name *it; 303 struct library_exported_name **nnamep = &retp->exported_names; 304 for (it = lib->exported_names; it != NULL; it = it->next) { 305 *nnamep = malloc(sizeof(**nnamep)); 306 if (*nnamep == NULL 307 || library_exported_name_clone(*nnamep, it) < 0) { 308 free(*nnamep); 309 goto fail; 310 } 311 nnamep = &(*nnamep)->next; 312 } 313 *nnamep = NULL; 314 } 315 316 return 0; 317} 318 319void 320library_destroy(struct library *lib) 321{ 322 if (lib == NULL) 323 return; 324 325 arch_library_destroy(lib); 326 library_set_soname(lib, NULL, 0); 327 library_set_pathname(lib, NULL, 0); 328 329 struct library_symbol *sym; 330 for (sym = lib->symbols; sym != NULL; ) { 331 struct library_symbol *next = sym->next; 332 library_symbol_destroy(sym); 333 free(sym); 334 sym = next; 335 } 336 337 /* Release exported names. */ 338 struct library_exported_name *it; 339 for (it = lib->exported_names; it != NULL; ) { 340 struct library_exported_name *next = it->next; 341 if (it->own_name) 342 free((char *)it->name); 343 free(it); 344 it = next; 345 } 346} 347 348void 349library_set_soname(struct library *lib, const char *new_name, int own_name) 350{ 351 if (lib->own_soname) 352 free((char *)lib->soname); 353 lib->soname = new_name; 354 lib->own_soname = own_name; 355} 356 357void 358library_set_pathname(struct library *lib, const char *new_name, int own_name) 359{ 360 if (lib->own_pathname) 361 free((char *)lib->pathname); 362 lib->pathname = new_name; 363 lib->own_pathname = own_name; 364} 365 366struct library_symbol * 367library_each_symbol(struct library *lib, struct library_symbol *start_after, 368 enum callback_status (*cb)(struct library_symbol *, void *), 369 void *data) 370{ 371 struct library_symbol *it = start_after == NULL ? lib->symbols 372 : start_after->next; 373 374 while (it != NULL) { 375 struct library_symbol *next = it->next; 376 377 switch ((*cb)(it, data)) { 378 case CBS_FAIL: 379 /* XXX handle me */ 380 case CBS_STOP: 381 return it; 382 case CBS_CONT: 383 break; 384 } 385 386 it = next; 387 } 388 389 return NULL; 390} 391 392void 393library_add_symbol(struct library *lib, struct library_symbol *first) 394{ 395 struct library_symbol *last; 396 for (last = first; last != NULL; ) { 397 last->lib = lib; 398 if (last->next != NULL) 399 last = last->next; 400 else 401 break; 402 } 403 404 assert(last->next == NULL); 405 last->next = lib->symbols; 406 lib->symbols = first; 407} 408 409enum callback_status 410library_named_cb(struct process *proc, struct library *lib, void *name) 411{ 412 if (name == lib->soname 413 || strcmp(lib->soname, (char *)name) == 0) 414 return CBS_STOP; 415 else 416 return CBS_CONT; 417} 418 419enum callback_status 420library_with_key_cb(struct process *proc, struct library *lib, void *keyp) 421{ 422 return lib->key == *(arch_addr_t *)keyp ? CBS_STOP : CBS_CONT; 423} 424