| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 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> |
| Petr Machata | e4e693b | 2012-03-27 03:10:53 +0200 | [diff] [blame] | 26 | #include "sysdep.h" |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 27 | |
| 28 | struct Process; |
| 29 | struct library; |
| 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 | |
| 36 | /* We should in general be able to trace 64-bit processes with 32-bit |
| 37 | * ltrace. (At least PPC has several PTRACE requests related to |
| 38 | * tracing 64-on-32, so presumably it should be possible.) But ltrace |
| 39 | * is currently hopelessly infested with using void* for host address. |
| 40 | * So keep with it, for now. */ |
| 41 | typedef void *target_address_t; |
| 42 | |
| Petr Machata | ecb082f | 2012-04-05 02:10:10 +0200 | [diff] [blame] | 43 | /* Dict interface. */ |
| 44 | unsigned int target_address_hash(const void *key); |
| 45 | int target_address_cmp(const void *key1, const void *key2); |
| 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; |
| 51 | target_address_t enter_addr; |
| 52 | enum toplt plt_type; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 53 | char own_name; |
| Petr Machata | e4e693b | 2012-03-27 03:10:53 +0200 | [diff] [blame] | 54 | struct arch_library_symbol_data arch; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /* Init LIBSYM. NAME will be freed when LIBSYM is destroyed if |
| Petr Machata | e4e693b | 2012-03-27 03:10:53 +0200 | [diff] [blame] | 58 | * OWN_NAME. ARCH has to be initialized by a separate call. */ |
| Petr Machata | e8d9076 | 2012-04-15 04:28:31 +0200 | [diff] [blame] | 59 | int library_symbol_init(struct library_symbol *libsym, |
| 60 | target_address_t addr, const char *name, int own_name, |
| 61 | enum toplt type_of_plt); |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 62 | |
| 63 | /* Copy library symbol SYM into the area pointed-to by RETP. Return 0 |
| 64 | * on success or a negative value on failure. */ |
| 65 | int library_symbol_clone(struct library_symbol *retp, |
| 66 | struct library_symbol *sym); |
| 67 | |
| 68 | /* Destroy library symbol. This essentially just frees name if it's |
| 69 | * owned. It doesn't free the memory associated with SYM pointer |
| 70 | * itself. Returns 0 on success or a negative value in case of an |
| 71 | * error (which would be an out of memory condition). */ |
| 72 | void library_symbol_destroy(struct library_symbol *sym); |
| 73 | |
| 74 | /* Compare two library symbols. Returns a negative value, 0, or a |
| 75 | * positive value, much like strcmp. The function compares symbol |
| 76 | * addresses, and if those are equal, it compares symbol names. If |
| 77 | * those are equal, too, the symbols are considered equal. */ |
| 78 | int library_symbol_cmp(struct library_symbol *a, struct library_symbol *b); |
| 79 | |
| Petr Machata | 157cc4d | 2012-04-04 19:00:34 +0200 | [diff] [blame] | 80 | /* Set a name for library symbol. This frees the old name, if |
| 81 | * that is owned. */ |
| 82 | void library_symbol_set_name(struct library_symbol *libsym, |
| 83 | const char *name, int own_name); |
| 84 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 85 | /* A function that can be used as library_each_symbol callback. Looks |
| 86 | * for a symbol SYM for which library_symbol_cmp(SYM, STANDARD) |
| 87 | * returns 0. */ |
| 88 | enum callback_status library_symbol_equal_cb(struct library_symbol *libsym, |
| 89 | void *standard); |
| 90 | |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 91 | enum library_type { |
| 92 | LT_LIBTYPE_MAIN, |
| 93 | LT_LIBTYPE_DSO, |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 96 | /* XXX we might consider sharing libraries across processes. Things |
| 97 | * like libc will be opened by every single process, no point cloning |
| 98 | * these everywhere. But for now, keep the ownership structure |
| 99 | * simple. */ |
| 100 | struct library { |
| 101 | struct library *next; |
| 102 | |
| Petr Machata | 89ac039 | 2012-04-03 02:30:48 +0200 | [diff] [blame] | 103 | /* Unique key. Two library objects are considered equal, if |
| 104 | * they have the same key. */ |
| 105 | target_address_t key; |
| 106 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 107 | /* Address where the library is mapped. Two library objects |
| 108 | * are considered equal, if they have the same base. */ |
| 109 | target_address_t base; |
| 110 | |
| 111 | /* Absolute address of the entry point. Useful for main |
| Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 112 | * binary, though I suppose the value might be useful for the |
| 113 | * dynamic linker, too (in case we ever want to do early |
| 114 | * process tracing). */ |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 115 | target_address_t entry; |
| 116 | |
| Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 117 | /* Address of PT_DYNAMIC segment. */ |
| 118 | target_address_t dyn_addr; |
| 119 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 120 | /* Symbols associated with the library. */ |
| 121 | struct library_symbol *symbols; |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 122 | |
| 123 | const char *soname; |
| 124 | const char *pathname; |
| 125 | |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 126 | enum library_type type; |
| 127 | |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 128 | char own_soname : 1; |
| 129 | char own_pathname : 1; |
| Petr Machata | 994ad6d | 2012-04-17 03:07:04 +0200 | [diff] [blame] | 130 | |
| 131 | struct arch_library_data arch; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 132 | }; |
| 133 | |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 134 | /* Init LIB. */ |
| Petr Machata | b5f80ac | 2012-04-04 01:46:18 +0200 | [diff] [blame] | 135 | void library_init(struct library *lib, enum library_type type); |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 136 | |
| 137 | /* Initialize RETP to a library identical to LIB. Symbols are not |
| 138 | * shared, but copied over. Returns 0 on success and a negative value |
| 139 | * in case of failure. */ |
| 140 | int library_clone(struct library *retp, struct library *lib); |
| 141 | |
| Petr Machata | 0fe76c6 | 2012-04-14 02:29:30 +0200 | [diff] [blame] | 142 | /* Destroy library. Doesn't free LIB itself. Symbols are destroyed |
| 143 | * and freed. */ |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 144 | void library_destroy(struct library *lib); |
| 145 | |
| Petr Machata | 0b55b58 | 2012-04-02 00:38:46 +0200 | [diff] [blame] | 146 | /* Set library soname. Frees the old name if necessary. */ |
| 147 | void library_set_soname(struct library *lib, |
| 148 | const char *new_name, int own_name); |
| 149 | |
| 150 | /* Set library pathname. Frees the old name if necessary. */ |
| 151 | void library_set_pathname(struct library *lib, |
| 152 | const char *new_name, int own_name); |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 153 | |
| 154 | /* Iterate through list of symbols of library LIB. Restarts are |
| Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 155 | * supported via START_AFTER (see each_process for details of |
| 156 | * iteration interface). */ |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 157 | struct library_symbol *library_each_symbol |
| Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 158 | (struct library *lib, struct library_symbol *start_after, |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 159 | enum callback_status (*cb)(struct library_symbol *, void *), |
| 160 | void *data); |
| 161 | |
| 162 | /* Add a new symbol SYM to LIB. SYM is assumed owned, we need to |
| 163 | * overwrite SYM->next. */ |
| 164 | void library_add_symbol(struct library *lib, struct library_symbol *sym); |
| 165 | |
| 166 | /* A function that can be used as proc_each_library callback. Looks |
| 167 | * for a library with the name passed in DATA. PROC is ignored. */ |
| 168 | enum callback_status library_named_cb(struct Process *proc, |
| 169 | struct library *lib, void *name); |
| 170 | |
| 171 | /* A function that can be used as proc_each_library callback. Looks |
| 172 | * for a library with given base. |
| 173 | * |
| Petr Machata | 89ac039 | 2012-04-03 02:30:48 +0200 | [diff] [blame] | 174 | * NOTE: The key is passed as a POINTER to target_address_t (that |
| 175 | * because in general, target_address_t doesn't fit in void*). */ |
| 176 | enum callback_status library_with_key_cb(struct Process *proc, |
| 177 | struct library *lib, void *keyp); |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 178 | |
| Petr Machata | b88fd6e | 2012-03-21 05:00:56 +0100 | [diff] [blame] | 179 | /* XXX this should really be in backend.h (as on pmachata/revamp |
| 180 | * branch), or, on this branch, in common.h. But we need |
| 181 | * target_address_t (which should also be in backend.h, I reckon), so |
| 182 | * stuff it here for the time being. */ |
| 183 | /* 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] | 184 | * raw addresses as read from symbol tables etc. If necessary on |
| Petr Machata | b88fd6e | 2012-03-21 05:00:56 +0100 | [diff] [blame] | 185 | * given architecture, this function should translate the address |
| 186 | * according to .opd or other indirection mechanism. Returns 0 on |
| 187 | * success and a negative value on failure. */ |
| Petr Machata | b1492df | 2012-04-30 21:01:40 +0200 | [diff] [blame] | 188 | struct ltelf; |
| 189 | int arch_translate_address(struct ltelf *lte, |
| Petr Machata | b88fd6e | 2012-03-21 05:00:56 +0100 | [diff] [blame] | 190 | target_address_t addr, target_address_t *ret); |
| Petr Machata | b1492df | 2012-04-30 21:01:40 +0200 | [diff] [blame] | 191 | /* This is the same function as arch_translate_address, except it's |
| 192 | * used at the point that we don't have ELF available anymore. */ |
| 193 | int arch_translate_address_dyn(struct Process *proc, |
| 194 | target_address_t addr, target_address_t *ret); |
| Petr Machata | b88fd6e | 2012-03-21 05:00:56 +0100 | [diff] [blame] | 195 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 196 | #endif /* _LIBRARY_H_ */ |