library.c revision b1ab2aefa199ea8e14e29bac78ae71030c2d4863
1/* 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 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 93int 94strdup_if(const char **retp, const char *str, int whether) 95{ 96 if (whether && str != NULL) { 97 str = strdup(str); 98 if (str == NULL) 99 return -1; 100 } 101 102 *retp = str; 103 return 0; 104} 105 106static void 107private_library_symbol_init(struct library_symbol *libsym, 108 arch_addr_t addr, 109 const char *name, int own_name, 110 enum toplt type_of_plt, 111 int latent, int delayed) 112{ 113 libsym->next = NULL; 114 libsym->lib = NULL; 115 libsym->plt_type = type_of_plt; 116 libsym->name = name; 117 libsym->own_name = own_name; 118 libsym->latent = latent; 119 libsym->delayed = delayed; 120 libsym->enter_addr = (void *)(uintptr_t)addr; 121} 122 123static void 124private_library_symbol_destroy(struct library_symbol *libsym) 125{ 126 library_symbol_set_name(libsym, NULL, 0); 127} 128 129int 130library_symbol_init(struct library_symbol *libsym, 131 arch_addr_t addr, const char *name, int own_name, 132 enum toplt type_of_plt) 133{ 134 private_library_symbol_init(libsym, addr, name, own_name, 135 type_of_plt, 0, 0); 136 137 /* If arch init fails, we've already set libsym->name and 138 * own_name. But we return failure, and the client code isn't 139 * supposed to call library_symbol_destroy in such a case. */ 140 return arch_library_symbol_init(libsym); 141} 142 143void 144library_symbol_destroy(struct library_symbol *libsym) 145{ 146 if (libsym != NULL) { 147 private_library_symbol_destroy(libsym); 148 arch_library_symbol_destroy(libsym); 149 } 150} 151 152int 153library_symbol_clone(struct library_symbol *retp, struct library_symbol *libsym) 154{ 155 /* Make lifetimes of name stored at original independent of 156 * the one at the clone. */ 157 const char *name; 158 if (strdup_if(&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 lib->protolib = NULL; 229 230 lib->soname = NULL; 231 lib->own_soname = 0; 232 233 lib->pathname = NULL; 234 lib->own_pathname = 0; 235 236 lib->symbols = NULL; 237 lib->exported_names = NULL; 238 lib->type = type; 239} 240 241void 242library_init(struct library *lib, enum library_type type) 243{ 244 private_library_init(lib, type); 245 arch_library_init(lib); 246} 247 248static int 249library_exported_name_clone(struct library_exported_name *retp, 250 struct library_exported_name *exnm) 251{ 252 char *name = exnm->own_name ? strdup(exnm->name) : (char *)exnm->name; 253 if (name == NULL) 254 return -1; 255 retp->name = name; 256 retp->own_name = exnm->own_name; 257 return 0; 258} 259 260int 261library_clone(struct library *retp, struct library *lib) 262{ 263 const char *soname = NULL; 264 const char *pathname; 265 266 /* Make lifetimes of strings stored at original independent of 267 * those at the clone. */ 268 if (strdup_if(&soname, lib->soname, lib->own_soname) < 0 269 || strdup_if(&pathname, lib->pathname, lib->own_pathname) < 0) { 270 if (lib->own_soname) 271 free((char *)soname); 272 return -1; 273 } 274 275 private_library_init(retp, lib->type); 276 library_set_soname(retp, soname, lib->own_soname); 277 library_set_pathname(retp, pathname, lib->own_pathname); 278 arch_library_clone(retp, lib); 279 280 retp->key = lib->key; 281 282 /* Clone symbols. */ 283 { 284 struct library_symbol *it; 285 struct library_symbol **nsymp = &retp->symbols; 286 for (it = lib->symbols; it != NULL; it = it->next) { 287 *nsymp = malloc(sizeof(**nsymp)); 288 if (*nsymp == NULL 289 || library_symbol_clone(*nsymp, it) < 0) { 290 free(*nsymp); 291 fail: 292 /* Release what we managed to allocate. */ 293 library_destroy(retp); 294 return -1; 295 } 296 297 (*nsymp)->lib = retp; 298 nsymp = &(*nsymp)->next; 299 } 300 *nsymp = NULL; 301 } 302 303 /* Clone exported names. */ 304 { 305 struct library_exported_name *it; 306 struct library_exported_name **nnamep = &retp->exported_names; 307 for (it = lib->exported_names; it != NULL; it = it->next) { 308 *nnamep = malloc(sizeof(**nnamep)); 309 if (*nnamep == NULL 310 || library_exported_name_clone(*nnamep, it) < 0) { 311 free(*nnamep); 312 goto fail; 313 } 314 nnamep = &(*nnamep)->next; 315 } 316 *nnamep = NULL; 317 } 318 319 return 0; 320} 321 322void 323library_destroy(struct library *lib) 324{ 325 if (lib == NULL) 326 return; 327 328 arch_library_destroy(lib); 329 library_set_soname(lib, NULL, 0); 330 library_set_pathname(lib, NULL, 0); 331 332 struct library_symbol *sym; 333 for (sym = lib->symbols; sym != NULL; ) { 334 struct library_symbol *next = sym->next; 335 library_symbol_destroy(sym); 336 free(sym); 337 sym = next; 338 } 339 340 /* Release exported names. */ 341 struct library_exported_name *it; 342 for (it = lib->exported_names; it != NULL; ) { 343 struct library_exported_name *next = it->next; 344 if (it->own_name) 345 free((char *)it->name); 346 free(it); 347 it = next; 348 } 349} 350 351void 352library_set_soname(struct library *lib, const char *new_name, int own_name) 353{ 354 if (lib->own_soname) 355 free((char *)lib->soname); 356 lib->soname = new_name; 357 lib->own_soname = own_name; 358} 359 360void 361library_set_pathname(struct library *lib, const char *new_name, int own_name) 362{ 363 if (lib->own_pathname) 364 free((char *)lib->pathname); 365 lib->pathname = new_name; 366 lib->own_pathname = own_name; 367} 368 369struct library_symbol * 370library_each_symbol(struct library *lib, struct library_symbol *start_after, 371 enum callback_status (*cb)(struct library_symbol *, void *), 372 void *data) 373{ 374 struct library_symbol *it = start_after == NULL ? lib->symbols 375 : start_after->next; 376 377 while (it != NULL) { 378 struct library_symbol *next = it->next; 379 380 switch ((*cb)(it, data)) { 381 case CBS_FAIL: 382 /* XXX handle me */ 383 case CBS_STOP: 384 return it; 385 case CBS_CONT: 386 break; 387 } 388 389 it = next; 390 } 391 392 return NULL; 393} 394 395void 396library_add_symbol(struct library *lib, struct library_symbol *first) 397{ 398 struct library_symbol *last; 399 for (last = first; last != NULL; ) { 400 last->lib = lib; 401 if (last->next != NULL) 402 last = last->next; 403 else 404 break; 405 } 406 407 assert(last->next == NULL); 408 last->next = lib->symbols; 409 lib->symbols = first; 410} 411 412enum callback_status 413library_named_cb(struct process *proc, struct library *lib, void *name) 414{ 415 if (name == lib->soname 416 || strcmp(lib->soname, (char *)name) == 0) 417 return CBS_STOP; 418 else 419 return CBS_CONT; 420} 421 422enum callback_status 423library_with_key_cb(struct process *proc, struct library *lib, void *keyp) 424{ 425 return lib->key == *(arch_addr_t *)keyp ? CBS_STOP : CBS_CONT; 426} 427