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