library.h revision d7e4ca82e1cf20bb2605befb1da74dd1688c706e
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2012 Petr Machata, Red Hat Inc. 4 * Copyright (C) 2006 Paul Gilliam, IBM Corporation 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of the 9 * License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 19 * 02110-1301 USA 20 */ 21 22#ifndef _LIBRARY_H_ 23#define _LIBRARY_H_ 24 25#include <stdint.h> 26 27#include "callback.h" 28#include "forward.h" 29#include "sysdep.h" 30 31enum toplt { 32 LS_TOPLT_NONE = 0, /* PLT not used for this symbol. */ 33 LS_TOPLT_EXEC, /* PLT for this symbol is executable. */ 34}; 35 36/* Dict interface. */ 37size_t arch_addr_hash(const arch_addr_t *addr); 38int arch_addr_eq(const arch_addr_t *addr1, const arch_addr_t *addr2); 39 40/* For handling -l. */ 41struct library_exported_name { 42 struct library_exported_name *next; 43 const char *name; 44 int own_name : 1; 45}; 46 47struct library_symbol { 48 struct library_symbol *next; 49 struct library *lib; 50 const char *name; 51 arch_addr_t enter_addr; 52 enum toplt plt_type; 53 int own_name : 1; 54 55 /* This is relevant for PLT symbols. Latent PLT symbols are 56 * those that don't match any of the -e rules, but that might 57 * potentially become active if a library implementing them 58 * appears that matches a -l rule. Ltrace core is responsible 59 * for clearing latent flag. */ 60 int latent : 1; 61 62 /* Delayed symbols are those for which a breakpoint shouldn't 63 * be enabled yet. They are similar to latent symbols, but 64 * backend is responsible for clearing the delayed flag. See 65 * proc_activate_delayed_symbol. */ 66 int delayed : 1; 67 68 struct arch_library_symbol_data arch; 69}; 70 71/* Init LIBSYM. NAME will be freed when LIBSYM is destroyed if 72 * OWN_NAME. ARCH has to be initialized by a separate call. */ 73int library_symbol_init(struct library_symbol *libsym, 74 arch_addr_t addr, const char *name, int own_name, 75 enum toplt type_of_plt); 76 77/* Copy library symbol SYM into the area pointed-to by RETP. Return 0 78 * on success or a negative value on failure. */ 79int library_symbol_clone(struct library_symbol *retp, 80 struct library_symbol *sym); 81 82/* Destroy library symbol. This essentially just frees name if it's 83 * owned. It doesn't free the memory associated with SYM pointer 84 * itself. Returns 0 on success or a negative value in case of an 85 * error (which would be an out of memory condition). */ 86void library_symbol_destroy(struct library_symbol *sym); 87 88/* Compare two library symbols. Returns a negative value, 0, or a 89 * positive value, much like strcmp. The function compares symbol 90 * addresses, and if those are equal, it compares symbol names. If 91 * those are equal, too, the symbols are considered equal. */ 92int library_symbol_cmp(struct library_symbol *a, struct library_symbol *b); 93 94/* Set a name for library symbol. This frees the old name, if 95 * that is owned. */ 96void library_symbol_set_name(struct library_symbol *libsym, 97 const char *name, int own_name); 98 99/* A function that can be used as library_each_symbol callback. Looks 100 * for a symbol SYM for which library_symbol_cmp(SYM, STANDARD) 101 * returns 0. */ 102enum callback_status library_symbol_equal_cb(struct library_symbol *libsym, 103 void *standard); 104 105/* A function that can be used as library_each_symbol callback. Looks 106 * for a symbol SYM for which strcmp(SYM->name, NAME) == 0. */ 107enum callback_status library_symbol_named_cb(struct library_symbol *libsym, 108 void *name); 109 110/* A function that can be used as library_each_symbol callback. Looks 111 * for a delayed symbol. */ 112enum callback_status library_symbol_delayed_cb(struct library_symbol *libsym, 113 void *unused); 114 115enum library_type { 116 LT_LIBTYPE_MAIN, 117 LT_LIBTYPE_DSO, 118}; 119 120/* XXX we might consider sharing libraries across processes. Things 121 * like libc will be opened by every single process, no point cloning 122 * these everywhere. But for now, keep the ownership structure 123 * simple. */ 124struct library { 125 struct library *next; 126 127 /* Unique key. Two library objects are considered equal, if 128 * they have the same key. */ 129 arch_addr_t key; 130 131 /* Address where the library is mapped. */ 132 arch_addr_t base; 133 134 /* Absolute address of the entry point. Useful for main 135 * binary, though I suppose the value might be useful for the 136 * dynamic linker, too (in case we ever want to do early 137 * process tracing). */ 138 arch_addr_t entry; 139 140 /* Address of PT_DYNAMIC segment. */ 141 arch_addr_t dyn_addr; 142 143 /* Symbols associated with the library. This includes a 144 * symbols that don't have a breakpoint attached (yet). */ 145 struct library_symbol *symbols; 146 147 /* List of names that this library implements, and that match 148 * -l filter. Each time a new library is mapped, its list of 149 * exports is examined, and corresponding PLT slots are 150 * enabled. */ 151 struct library_exported_name *exported_names; 152 153 const char *soname; 154 const char *pathname; 155 156 enum library_type type; 157 158 char own_soname : 1; 159 char own_pathname : 1; 160 161 struct arch_library_data arch; 162}; 163 164/* Init LIB. */ 165void library_init(struct library *lib, enum library_type type); 166 167/* Initialize RETP to a library identical to LIB. Symbols are not 168 * shared, but copied over. Returns 0 on success and a negative value 169 * in case of failure. */ 170int library_clone(struct library *retp, struct library *lib); 171 172/* Destroy library. Doesn't free LIB itself. Symbols are destroyed 173 * and freed. */ 174void library_destroy(struct library *lib); 175 176/* Set library soname. Frees the old name if necessary. */ 177void library_set_soname(struct library *lib, 178 const char *new_name, int own_name); 179 180/* Set library pathname. Frees the old name if necessary. */ 181void library_set_pathname(struct library *lib, 182 const char *new_name, int own_name); 183 184/* Iterate through list of symbols of library LIB. See callback.h for 185 * notes on this interface. */ 186struct library_symbol *library_each_symbol 187 (struct library *lib, struct library_symbol *start_after, 188 enum callback_status (*cb)(struct library_symbol *, void *), 189 void *data); 190 191/* Add a new symbol SYM to LIB. SYM is assumed owned, we need to 192 * overwrite SYM->next. */ 193void library_add_symbol(struct library *lib, struct library_symbol *sym); 194 195/* A function that can be used as proc_each_library callback. Looks 196 * for a library with the name passed in DATA. PROC is ignored. */ 197enum callback_status library_named_cb(struct process *proc, 198 struct library *lib, void *name); 199 200/* A function that can be used as proc_each_library callback. Looks 201 * for a library with given base. 202 * 203 * NOTE: The key is passed as a POINTER to arch_addr_t (that 204 * because in general, arch_addr_t doesn't fit in void*). */ 205enum callback_status library_with_key_cb(struct process *proc, 206 struct library *lib, void *keyp); 207 208/* XXX this should really be in backend.h (as on pmachata/revamp 209 * branch), or, on this branch, in common.h. But we need 210 * arch_addr_t (which should also be in backend.h, I reckon), so 211 * stuff it here for the time being. */ 212/* This function is implemented in the back end. It is called for all 213 * raw addresses as read from symbol tables etc. If necessary on 214 * given architecture, this function should translate the address 215 * according to .opd or other indirection mechanism. Returns 0 on 216 * success and a negative value on failure. */ 217struct ltelf; 218int arch_translate_address(struct ltelf *lte, 219 arch_addr_t addr, arch_addr_t *ret); 220/* This is the same function as arch_translate_address, except it's 221 * used at the point that we don't have ELF available anymore. */ 222int arch_translate_address_dyn(struct process *proc, 223 arch_addr_t addr, arch_addr_t *ret); 224 225#endif /* _LIBRARY_H_ */ 226