removed needless #include
[platform/upstream/ltrace.git] / library.h
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2012,2013 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 #if defined(HAVE_LIBDW)
28 # include <elfutils/libdwfl.h>
29 #endif
30
31 #include "dict.h"
32 #include "callback.h"
33 #include "forward.h"
34 #include "sysdep.h"
35
36 enum toplt {
37         LS_TOPLT_NONE = 0,      /* PLT not used for this symbol. */
38         LS_TOPLT_EXEC,          /* PLT for this symbol is executable. */
39 };
40
41 /* Dict interface.  */
42 size_t arch_addr_hash(const arch_addr_t *addr);
43 int arch_addr_eq(const arch_addr_t *addr1, const arch_addr_t *addr2);
44
45
46 /* For handling -l and for handling library export aliases (different symbol
47  * name, same address)
48  *
49  * This structure needs to
50  * - store (addr, name) tuples
51  * - be searchable by addr (when populating)
52  * - be searchable by name (when looking for aliases)
53  * - be enumeratable (by activate_latent_in())
54  */
55 struct library_exported_names {
56         // I store the data in several structures to facilitate different types
57         // of access
58         struct dict names; // maps a name to an address
59         struct dict addrs; // maps an address to a vect of names
60 };
61
62 struct library_symbol {
63         struct library_symbol *next;
64         struct library *lib;
65         const char *name;
66         arch_addr_t enter_addr;
67         enum toplt plt_type;
68
69         /* If this is non-NULL, this prototype is used instead of
70          * looking up one in LIB->protolib.  */
71         struct prototype *proto;
72
73         int own_name : 1;
74
75         /* This is relevant for PLT symbols.  Latent PLT symbols are
76          * those that don't match any of the -e rules, but that might
77          * potentially become active if a library implementing them
78          * appears that matches a -l rule.  Ltrace core is responsible
79          * for clearing latent flag.  */
80         int latent : 1;
81
82         /* Delayed symbols are those for which a breakpoint shouldn't
83          * be enabled yet.  They are similar to latent symbols, but
84          * backend is responsible for clearing the delayed flag.  See
85          * proc_activate_delayed_symbol.  */
86         int delayed : 1;
87
88         struct arch_library_symbol_data arch;
89         struct os_library_symbol_data os;
90 };
91
92 /* Init LIBSYM.  NAME will be freed when LIBSYM is destroyed if
93  * OWN_NAME.  ARCH has to be initialized by a separate call.  */
94 int library_symbol_init(struct library_symbol *libsym,
95                         arch_addr_t addr, const char *name, int own_name,
96                         enum toplt type_of_plt);
97
98 /* Copy library symbol SYM into the area pointed-to by RETP.  Return 0
99  * on success or a negative value on failure.  */
100 int library_symbol_clone(struct library_symbol *retp,
101                          struct library_symbol *sym);
102
103 /* Destroy library symbol.  This essentially just frees name if it's
104  * owned.  It doesn't free the memory associated with SYM pointer
105  * itself.  Returns 0 on success or a negative value in case of an
106  * error (which would be an out of memory condition).  */
107 void library_symbol_destroy(struct library_symbol *sym);
108
109 /* Compare two library symbols.  Returns a negative value, 0, or a
110  * positive value, much like strcmp.  The function compares symbol
111  * addresses, and if those are equal, it compares symbol names.  If
112  * those are equal, too, the symbols are considered equal.  */
113 int library_symbol_cmp(struct library_symbol *a, struct library_symbol *b);
114
115 /* Set a name for library symbol.  This frees the old name, if
116  * that is owned.  */
117 void library_symbol_set_name(struct library_symbol *libsym,
118                              const char *name, int own_name);
119
120 /* A function that can be used as library_each_symbol callback.  Looks
121  * for a symbol SYM for which library_symbol_cmp(SYM, STANDARD)
122  * returns 0.  */
123 enum callback_status library_symbol_equal_cb(struct library_symbol *libsym,
124                                              void *standard);
125
126 /* A function that can be used as library_each_symbol callback.  Looks
127  * for a symbol SYM for which strcmp(SYM->name, NAME) == 0.  */
128 enum callback_status library_symbol_named_cb(struct library_symbol *libsym,
129                                              void *name);
130
131 /* A function that can be used as library_each_symbol callback.  Looks
132  * for a delayed symbol.  */
133 enum callback_status library_symbol_delayed_cb(struct library_symbol *libsym,
134                                                void *unused);
135
136 enum library_type {
137         LT_LIBTYPE_MAIN,
138         LT_LIBTYPE_DSO,
139         LT_LIBTYPE_SYSCALL,
140 };
141
142 /* XXX we might consider sharing libraries across processes.  Things
143  * like libc will be opened by every single process, no point cloning
144  * these everywhere.  But for now, keep the ownership structure
145  * simple.  */
146 struct library {
147         struct library *next;
148
149         /* Unique key. Two library objects are considered equal, if
150          * they have the same key.  */
151         arch_addr_t key;
152
153         /* Address where the library is mapped.  */
154         arch_addr_t base;
155
156         /* Absolute address of the entry point.  Useful for main
157          * binary, though I suppose the value might be useful for the
158          * dynamic linker, too (in case we ever want to do early
159          * process tracing).  */
160         arch_addr_t entry;
161
162         /* Address of PT_DYNAMIC segment.  */
163         arch_addr_t dyn_addr;
164
165         /* Symbols associated with the library.  This includes a
166          * symbols that don't have a breakpoint attached (yet).  */
167         struct library_symbol *symbols;
168
169         /* List of names that this library implements, and that match
170          * -l filter.  Each time a new library is mapped, its list of
171          * exports is examined, and corresponding PLT slots are
172          * enabled. This data structure also keeps track of export
173          * addresses to find symbols with different names, but same
174          * addresses */
175         struct library_exported_names exported_names;
176
177         /* Prototype library associated with this library.  */
178         struct protolib *protolib;
179
180         const char *soname;
181         const char *pathname;
182
183         enum library_type type;
184
185         char own_soname : 1;
186         char own_pathname : 1;
187
188         struct arch_library_data arch;
189         struct os_library_data os;
190
191 #if defined(HAVE_LIBDW)
192         Dwfl_Module *dwfl_module;
193 #endif
194 };
195
196 /* Init LIB.  */
197 int library_init(struct library *lib, enum library_type type);
198
199 /* Initialize RETP to a library identical to LIB.  Symbols are not
200  * shared, but copied over.  Returns 0 on success and a negative value
201  * in case of failure.  */
202 int library_clone(struct library *retp, struct library *lib);
203
204 /* Destroy library.  Doesn't free LIB itself.  Symbols are destroyed
205  * and freed.  */
206 void library_destroy(struct library *lib);
207
208 /* Set library soname.  Frees the old name if necessary.  */
209 void library_set_soname(struct library *lib,
210                         const char *new_name, int own_name);
211
212 /* Set library pathname.  Frees the old name if necessary.  */
213 void library_set_pathname(struct library *lib,
214                           const char *new_name, int own_name);
215
216 /* Iterate through list of symbols of library LIB.  See callback.h for
217  * notes on this interface.  */
218 struct library_symbol *library_each_symbol
219         (struct library *lib, struct library_symbol *start_after,
220          enum callback_status (*cb)(struct library_symbol *, void *),
221          void *data);
222
223 /* Add a new symbol SYM to LIB.  SYM is assumed owned, we need to
224  * overwrite SYM->next.  */
225 void library_add_symbol(struct library *lib, struct library_symbol *sym);
226
227 /* A function that can be used as proc_each_library callback.  Looks
228  * for a library with the name passed in DATA.  PROC is ignored.  */
229 enum callback_status library_named_cb(struct process *proc,
230                                       struct library *lib, void *name);
231
232 /* A function that can be used as proc_each_library callback.  Looks
233  * for a library with given base.
234  *
235  * NOTE: The key is passed as a POINTER to arch_addr_t (that
236  * because in general, arch_addr_t doesn't fit in void*).  */
237 enum callback_status library_with_key_cb(struct process *proc,
238                                          struct library *lib, void *keyp);
239
240 /* XXX this should really be in backend.h (as on pmachata/revamp
241  * branch), or, on this branch, in common.h.  But we need
242  * arch_addr_t (which should also be in backend.h, I reckon), so
243  * stuff it here for the time being.  */
244 /* This function is implemented in the back end.  It is called for all
245  * raw addresses as read from symbol tables etc.  If necessary on
246  * given architecture, this function should translate the address
247  * according to .opd or other indirection mechanism.  Returns 0 on
248  * success and a negative value on failure.  */
249 struct ltelf;
250 int arch_translate_address(struct ltelf *lte,
251                            arch_addr_t addr, arch_addr_t *ret);
252 /* This is the same function as arch_translate_address, except it's
253  * used at the point that we don't have ELF available anymore.  */
254 int arch_translate_address_dyn(struct process *proc,
255                                arch_addr_t addr, arch_addr_t *ret);
256
257
258 /* Pushes a name/address tuple to the list of a library's exports. Returns 0 on
259  * success
260  */
261 int library_exported_names_push(struct library_exported_names *names,
262                                 uint64_t addr, const char *name,
263                                 int own_name );
264
265 /* Iterates through the a library's export list. The callback is called for
266  * every symbol a library exports. Symbol aliases do not apply here. If multiple
267  * symbols are defined at the same address, each is reported here.
268  *
269  * If we want to iterate through the whole list, set name_start_after=NULL. If
270  * we want to start iterating immediately past a particular symbol name, pass a
271  * pointer to this symbol name in name_start_after. This must be a pointer in
272  * the internal dict, preferably returned by an earlier call to this function
273  *
274  * If the callback fails at any point, a pointer to the failing key is returned.
275  * On success, returns NULL. The returned pointer can be passed back to this
276  * function in name_start_after to resume skipping this element
277  */
278 const char** library_exported_names_each(
279         const struct library_exported_names *names,
280         const char **name_start_after,
281         enum callback_status (*cb)(const char *,
282                                    void *),
283         void *data);
284
285 /* Iterates through the a library's export list, reporting each symbol that is
286  * an alias of the given 'aliasname' symbol. This 'aliasname' symbol itself is
287  * NOT reported, so if this symbol is unique, the callback is not called at all.
288  *
289  * If we want to iterate through the whole alias list, set
290  * name_start_after=NULL. If we want to start iterating immediately past a
291  * particular symbol name, pass a pointer to this symbol name in
292  * name_start_after. This must be a pointer in the internal dict, preferably
293  * returned by an earlier call to this function
294  *
295  * If the callback fails at any point, a pointer to the failing key is returned.
296  * On success, returns NULL. The returned pointer can be passed back to this
297  * function in name_start_after to resume skipping this element
298  */
299 const char** library_exported_names_each_alias(
300         const struct library_exported_names *names,
301         const char *aliasname,
302         const char **name_start_after,
303         enum callback_status (*cb)(const char *,
304                                    void *),
305         void *data);
306
307 #endif /* _LIBRARY_H_ */