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