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