| 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> |
| 26 | #include <gelf.h> // XXX |
| 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. */ |
| 34 | LS_TOPLT_POINT /* PLT for this symbol is a non-executable. */ |
| 35 | }; |
| 36 | |
| 37 | /* We should in general be able to trace 64-bit processes with 32-bit |
| 38 | * ltrace. (At least PPC has several PTRACE requests related to |
| 39 | * tracing 64-on-32, so presumably it should be possible.) But ltrace |
| 40 | * is currently hopelessly infested with using void* for host address. |
| 41 | * So keep with it, for now. */ |
| 42 | typedef void *target_address_t; |
| 43 | |
| 44 | struct library_symbol { |
| 45 | struct library_symbol *next; |
| 46 | const char *name; |
| 47 | target_address_t enter_addr; |
| 48 | enum toplt plt_type; |
| 49 | char needs_init; |
| 50 | char is_weak; |
| 51 | char own_name; |
| 52 | }; |
| 53 | |
| 54 | /* Init LIBSYM. NAME will be freed when LIBSYM is destroyed if |
| 55 | * OWN_NAME. */ |
| 56 | /* XXX note that we shouldn't use GElf_Addr for ADDR either. The fact |
| 57 | * that Elf is used is a back-end detail. At least ltrace pretends |
| 58 | * that it would like to be cross-platform like that one day. */ |
| 59 | void library_symbol_init(struct library_symbol *libsym, |
| 60 | GElf_Addr addr, const char *name, int own_name, |
| 61 | enum toplt type_of_plt, int is_weak); |
| 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 | |
| 80 | /* A function that can be used as library_each_symbol callback. Looks |
| 81 | * for a symbol SYM for which library_symbol_cmp(SYM, STANDARD) |
| 82 | * returns 0. */ |
| 83 | enum callback_status library_symbol_equal_cb(struct library_symbol *libsym, |
| 84 | void *standard); |
| 85 | |
| 86 | /* XXX we might consider sharing libraries across processes. Things |
| 87 | * like libc will be opened by every single process, no point cloning |
| 88 | * these everywhere. But for now, keep the ownership structure |
| 89 | * simple. */ |
| 90 | struct library { |
| 91 | struct library *next; |
| 92 | |
| 93 | /* Address where the library is mapped. Two library objects |
| 94 | * are considered equal, if they have the same base. */ |
| 95 | target_address_t base; |
| 96 | |
| 97 | /* Absolute address of the entry point. Useful for main |
| 98 | * binary, though I the value might be useful for the dynamic |
| 99 | * linker, too (in case we ever want to do early process |
| 100 | * tracing). */ |
| 101 | target_address_t entry; |
| 102 | |
| 103 | /* Symbols associated with the library. */ |
| 104 | struct library_symbol *symbols; |
| 105 | const char *name; |
| 106 | char own_name; |
| 107 | }; |
| 108 | |
| 109 | /* Init LIB. NAME will be freed when LIB is destroyed if |
| 110 | * OWN_NAME. */ |
| 111 | void library_init(struct library *lib, const char *name, int own_name); |
| 112 | |
| 113 | /* Initialize RETP to a library identical to LIB. Symbols are not |
| 114 | * shared, but copied over. Returns 0 on success and a negative value |
| 115 | * in case of failure. */ |
| 116 | int library_clone(struct library *retp, struct library *lib); |
| 117 | |
| 118 | /* Destroy library. Doesn't free LIB itself. */ |
| 119 | void library_destroy(struct library *lib); |
| 120 | |
| 121 | /* Set library name. Frees the old name if necessary. */ |
| 122 | void library_set_name(struct library *lib, const char *new_name, int own_name); |
| 123 | |
| 124 | /* Iterate through list of symbols of library LIB. Restarts are |
| 125 | * supported via START (see each_process for details of iteration |
| 126 | * interface). */ |
| 127 | struct library_symbol *library_each_symbol |
| 128 | (struct library *lib, struct library_symbol *start, |
| 129 | enum callback_status (*cb)(struct library_symbol *, void *), |
| 130 | void *data); |
| 131 | |
| 132 | /* Add a new symbol SYM to LIB. SYM is assumed owned, we need to |
| 133 | * overwrite SYM->next. */ |
| 134 | void library_add_symbol(struct library *lib, struct library_symbol *sym); |
| 135 | |
| 136 | /* A function that can be used as proc_each_library callback. Looks |
| 137 | * for a library with the name passed in DATA. PROC is ignored. */ |
| 138 | enum callback_status library_named_cb(struct Process *proc, |
| 139 | struct library *lib, void *name); |
| 140 | |
| 141 | /* A function that can be used as proc_each_library callback. Looks |
| 142 | * for a library with given base. |
| 143 | * |
| 144 | * NOTE: The base is passed as a POINTER to target_address_t (that |
| 145 | * because in general, target_address_t doesn't fit in void*). */ |
| 146 | enum callback_status library_with_base_cb(struct Process *proc, |
| 147 | struct library *lib, void *basep); |
| 148 | |
| 149 | #endif /* _LIBRARY_H_ */ |