whitespace 'if(' -> 'if ('
[platform/upstream/ltrace.git] / library.c
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
4  * Copyright (C) 2001,2009 Juan Cespedes
5  * Copyright (C) 2006 Ian Wienand
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <stdio.h>
27
28 #include "library.h"
29 #include "callback.h"
30 #include "debug.h"
31 #include "dict.h"
32 #include "vect.h"
33 #include "backend.h" // for arch_library_symbol_init, arch_library_init
34
35 static void
36 library_exported_names_init(struct library_exported_names *names);
37
38 #ifndef OS_HAVE_LIBRARY_DATA
39 int
40 os_library_init(struct library *lib)
41 {
42         return 0;
43 }
44
45 void
46 os_library_destroy(struct library *lib)
47 {
48 }
49
50 int
51 os_library_clone(struct library *retp, struct library *lib)
52 {
53         return 0;
54 }
55 #endif
56
57 #ifndef ARCH_HAVE_LIBRARY_DATA
58 int
59 arch_library_init(struct library *lib)
60 {
61         return 0;
62 }
63
64 void
65 arch_library_destroy(struct library *lib)
66 {
67 }
68
69 int
70 arch_library_clone(struct library *retp, struct library *lib)
71 {
72         return 0;
73 }
74 #endif
75
76 #ifndef OS_HAVE_LIBRARY_SYMBOL_DATA
77 int
78 os_library_symbol_init(struct library_symbol *libsym)
79 {
80         return 0;
81 }
82
83 void
84 os_library_symbol_destroy(struct library_symbol *libsym)
85 {
86 }
87
88 int
89 os_library_symbol_clone(struct library_symbol *retp,
90                         struct library_symbol *libsym)
91 {
92         return 0;
93 }
94 #endif
95
96 #ifndef ARCH_HAVE_LIBRARY_SYMBOL_DATA
97 int
98 arch_library_symbol_init(struct library_symbol *libsym)
99 {
100         return 0;
101 }
102
103 void
104 arch_library_symbol_destroy(struct library_symbol *libsym)
105 {
106 }
107
108 int
109 arch_library_symbol_clone(struct library_symbol *retp,
110                           struct library_symbol *libsym)
111 {
112         return 0;
113 }
114 #endif
115
116 size_t
117 arch_addr_hash(const arch_addr_t *addr)
118 {
119         union {
120                 arch_addr_t addr;
121                 int ints[sizeof(arch_addr_t)
122                          / sizeof(unsigned int)];
123         } u = { .addr = *addr };
124
125         size_t i;
126         size_t h = 0;
127         for (i = 0; i < sizeof(u.ints) / sizeof(*u.ints); ++i)
128                 h ^= dict_hash_int(&u.ints[i]);
129         return h;
130 }
131
132 int
133 arch_addr_eq(const arch_addr_t *addr1, const arch_addr_t *addr2)
134 {
135         return *addr1 == *addr2;
136 }
137
138 int
139 strdup_if(const char **retp, const char *str, int whether)
140 {
141         if (whether && str != NULL) {
142                 str = strdup(str);
143                 if (str == NULL)
144                         return -1;
145         }
146
147         *retp = str;
148         return 0;
149 }
150
151 static void
152 private_library_symbol_init(struct library_symbol *libsym,
153                             arch_addr_t addr,
154                             const char *name, int own_name,
155                             enum toplt type_of_plt,
156                             int latent, int delayed)
157 {
158         libsym->next = NULL;
159         libsym->lib = NULL;
160         libsym->plt_type = type_of_plt;
161         libsym->name = name;
162         libsym->own_name = own_name;
163         libsym->latent = latent;
164         libsym->delayed = delayed;
165         libsym->enter_addr = (void *)(uintptr_t)addr;
166         libsym->proto = NULL;
167 }
168
169 static void
170 private_library_symbol_destroy(struct library_symbol *libsym)
171 {
172         library_symbol_set_name(libsym, NULL, 0);
173 }
174
175 int
176 library_symbol_init(struct library_symbol *libsym,
177                     arch_addr_t addr, const char *name, int own_name,
178                     enum toplt type_of_plt)
179 {
180         private_library_symbol_init(libsym, addr, name, own_name,
181                                     type_of_plt, 0, 0);
182
183         if (os_library_symbol_init(libsym) < 0)
184                 /* We've already set libsym->name and own_name.  But
185                  * we return failure, and the client code isn't
186                  * supposed to call library_symbol_destroy in such
187                  * case.  */
188                 return -1;
189
190         if (arch_library_symbol_init(libsym) < 0) {
191                 os_library_symbol_destroy(libsym);
192                 return -1;
193         }
194
195         return 0;
196 }
197
198 void
199 library_symbol_destroy(struct library_symbol *libsym)
200 {
201         if (libsym != NULL) {
202                 arch_library_symbol_destroy(libsym);
203                 os_library_symbol_destroy(libsym);
204                 private_library_symbol_destroy(libsym);
205         }
206 }
207
208 int
209 library_symbol_clone(struct library_symbol *retp, struct library_symbol *libsym)
210 {
211         /* Make lifetimes of name stored at original independent of
212          * the one at the clone.  */
213         const char *name;
214         if (strdup_if(&name, libsym->name, libsym->own_name) < 0)
215                 return -1;
216
217         private_library_symbol_init(retp, libsym->enter_addr,
218                                     name, libsym->own_name, libsym->plt_type,
219                                     libsym->latent, libsym->delayed);
220
221         if (os_library_symbol_clone(retp, libsym) < 0) {
222         fail:
223                 private_library_symbol_destroy(retp);
224                 return -1;
225         }
226
227         if (arch_library_symbol_clone(retp, libsym) < 0) {
228                 os_library_symbol_destroy(retp);
229                 goto fail;
230         }
231
232         return 0;
233 }
234
235 int
236 library_symbol_cmp(struct library_symbol *a, struct library_symbol *b)
237 {
238         if (a->enter_addr < b->enter_addr)
239                 return -1;
240         if (a->enter_addr > b->enter_addr)
241                 return 1;
242         if (a->name != NULL && b->name != NULL)
243                 return strcmp(a->name, b->name);
244         if (a->name == NULL) {
245                 if (b->name == NULL)
246                         return 0;
247                 return -1;
248         }
249         return 1;
250 }
251
252 void
253 library_symbol_set_name(struct library_symbol *libsym,
254                         const char *name, int own_name)
255 {
256         if (libsym->own_name)
257                 free((char *)libsym->name);
258         libsym->name = name;
259         libsym->own_name = own_name;
260 }
261
262 enum callback_status
263 library_symbol_equal_cb(struct library_symbol *libsym, void *u)
264 {
265         struct library_symbol *standard = u;
266         return library_symbol_cmp(libsym, standard) == 0 ? CBS_STOP : CBS_CONT;
267 }
268
269 enum callback_status
270 library_symbol_named_cb(struct library_symbol *libsym, void *name)
271 {
272         return strcmp(libsym->name, name) == 0 ? CBS_STOP : CBS_CONT;
273 }
274
275 enum callback_status
276 library_symbol_delayed_cb(struct library_symbol *libsym, void *unused)
277 {
278         return libsym->delayed ? CBS_STOP : CBS_CONT;
279 }
280
281 static void
282 private_library_init(struct library *lib, enum library_type type)
283 {
284         lib->next = NULL;
285
286         lib->key = 0;
287         lib->base = 0;
288         lib->entry = 0;
289         lib->dyn_addr = 0;
290         lib->protolib = NULL;
291
292         lib->soname = NULL;
293         lib->own_soname = 0;
294
295         lib->pathname = NULL;
296         lib->own_pathname = 0;
297
298         lib->symbols = NULL;
299         library_exported_names_init(&lib->exported_names);
300         lib->type = type;
301
302 #if defined(HAVE_LIBDW)
303         lib->dwfl_module = NULL;
304 #endif
305 }
306
307 int
308 library_init(struct library *lib, enum library_type type)
309 {
310         private_library_init(lib, type);
311
312         if (os_library_init(lib) < 0)
313                 return -1;
314
315         if (arch_library_init(lib) < 0) {
316                 os_library_destroy(lib);
317                 return -1;
318         }
319
320         return 0;
321 }
322
323
324
325
326
327 static void dtor_string(const char **tgt, void *data)
328 {
329         free((char*)*tgt);
330 }
331 static int clone_vect(struct vect **to, const struct vect **from, void *data)
332 {
333         *to = malloc(sizeof(struct vect));
334         if (*to == NULL)
335                 return -1;
336
337         return
338                 VECT_CLONE(*to, *from, const char*,
339                            dict_clone_string,
340                            dtor_string,
341                            NULL);
342 }
343 static void dtor_vect(const struct vect **tgt, void *data)
344 {
345         VECT_DESTROY(*tgt, const char*, dtor_string, NULL);
346         free(*tgt);
347 }
348
349 static void
350 library_exported_names_init(struct library_exported_names *names)
351 {
352         DICT_INIT(&names->names,
353                   const char*, uint64_t,
354                   dict_hash_string, dict_eq_string, NULL);
355         DICT_INIT(&names->addrs,
356                   uint64_t, struct vect*,
357                   dict_hash_uint64, dict_eq_uint64, NULL);
358 }
359
360 static void
361 library_exported_names_destroy(struct library_exported_names *names)
362 {
363         DICT_DESTROY(&names->names,
364                      const char*, uint64_t,
365                      dtor_string, NULL, NULL);
366         DICT_DESTROY(&names->addrs,
367                      uint64_t, struct vect*,
368                      NULL, dtor_vect, NULL);
369 }
370
371 static int
372 library_exported_names_clone(struct library_exported_names *retp,
373                              const struct library_exported_names *names)
374 {
375         return
376                 DICT_CLONE(&retp->names, &names->names,
377                            const char*, uint64_t,
378                            dict_clone_string, dtor_string,
379                            NULL, NULL,
380                            NULL) ||
381                 DICT_CLONE(&retp->addrs, &names->addrs,
382                            uint64_t, struct vect*,
383                            NULL, NULL,
384                            clone_vect, dtor_vect,
385                            NULL);
386 }
387
388 int library_exported_names_push(struct library_exported_names *names,
389                                 uint64_t addr, const char *name,
390                                 int own_name )
391 {
392         // first, take ownership of the name, if it's not yet ours
393         if (!own_name)
394                 name = strdup(name);
395         if (name == NULL)
396                 return -1;
397
398         // push to the name->addr map
399         int result = DICT_INSERT(&names->names, &name, &addr);
400         if (result == 1) {
401                 // This symbol is already present in the table. This library has
402                 // multiple copies of this symbol (probably corresponding to
403                 // different symbol versions). I should handle this gracefully
404                 // at some point, but for now I simply ignore later instances of
405                 // any particular symbol
406                 free(name);
407                 return 0;
408         }
409
410         if (result != 0)
411                 return result;
412
413         // push to the addr->names map
414         // I get the alias vector. If it doesn't yet exist, I make it
415         struct vect *aliases;
416         struct vect **paliases = DICT_FIND_REF(&names->addrs,
417                                                &addr, struct vect*);
418
419         if (paliases == NULL) {
420                 aliases = malloc(sizeof(struct vect));
421                 if (aliases == NULL)
422                         return -1;
423                 VECT_INIT(aliases, const char*);
424                 result = DICT_INSERT(&names->addrs, &addr, &aliases);
425                 if (result != 0)
426                         return result;
427         }
428         else
429                 aliases = *paliases;
430
431         const char *namedup = strdup(name);
432         if (namedup == NULL)
433                 return -1;
434
435         result = vect_pushback(aliases, &namedup);
436         if (result != 0)
437                 return result;
438
439         return 0;
440 }
441
442 struct library_exported_names_each_context
443 {
444         enum callback_status (*inner_cb)(const char *, void *);
445         void *data;
446         bool failure : 1;
447 };
448 static enum callback_status
449 library_exported_names_each_cb(const char **key, uint64_t *value, void *data)
450 {
451         struct library_exported_names_each_context *context =
452                 (struct library_exported_names_each_context*)data;
453         enum callback_status status = context->inner_cb(*key, context->data);
454         if (status == CBS_FAIL)
455                 context->failure = true;
456         return status;
457 }
458 bool library_exported_names_each(const struct library_exported_names *names,
459                                  enum callback_status (*cb)(const char *,
460                                                             void *),
461                                  void *data)
462 {
463         struct library_exported_names_each_context context =
464                 {.inner_cb = cb,
465                  .data = data,
466                  .failure = false};
467         DICT_EACH(&names->names,
468                   const char*, uint64_t,
469                   NULL, library_exported_names_each_cb, &context);
470         return !context.failure;
471 }
472
473 struct library_exported_names_each_alias_context
474 {
475         enum callback_status (*inner_cb)(const char *, void *);
476         const char *origname;
477         void *data;
478         bool failure : 1;
479 };
480 static enum callback_status
481 library_exported_names_each_alias_cb(const char **name, void *data)
482 {
483         struct library_exported_names_each_alias_context *context =
484                 (struct library_exported_names_each_alias_context*)data;
485
486         // I do not report the original name we were asked about. Otherwise, any
487         // time the caller asks for aliases of symbol "sym", I'll always report
488         // "sym" along with any actual aliases
489         if (strcmp(*name, context->origname) == 0)
490                 return CBS_CONT;
491
492         enum callback_status status = context->inner_cb(*name, context->data);
493         if (status == CBS_FAIL)
494                 context->failure = true;
495         return status;
496 }
497
498 bool library_exported_names_each_alias(
499         const struct library_exported_names *names,
500         const char *aliasname,
501         enum callback_status (*cb)(const char *,
502                                    void *),
503         void *data)
504 {
505         // I have a symbol name. I look up its address, then get the list of
506         // aliased names
507         uint64_t *addr = DICT_FIND_REF(&names->names,
508                                        &aliasname, uint64_t);
509         if (addr == NULL)
510                 return false;
511
512         // OK. I have an address. Get the list of symbols at this address
513         struct vect **aliases = DICT_FIND_REF(&names->addrs,
514                                              addr, struct vect*);
515         if (aliases == NULL)
516                 return false;
517
518         struct library_exported_names_each_alias_context context =
519                 {.inner_cb = cb,
520                  .origname = aliasname,
521                  .data = data,
522                  .failure = false};
523         VECT_EACH(*aliases, const char*, NULL,
524                   library_exported_names_each_alias_cb, &context);
525 }
526
527
528
529
530 int
531 library_clone(struct library *retp, struct library *lib)
532 {
533         const char *soname = NULL;
534         const char *pathname;
535
536         /* Make lifetimes of strings stored at original independent of
537          * those at the clone.  */
538         if (strdup_if(&soname, lib->soname, lib->own_soname) < 0
539             || strdup_if(&pathname, lib->pathname, lib->own_pathname) < 0) {
540                 if (lib->own_soname)
541                         free((char *)soname);
542                 return -1;
543         }
544
545         private_library_init(retp, lib->type);
546         library_set_soname(retp, soname, lib->own_soname);
547         library_set_pathname(retp, pathname, lib->own_pathname);
548
549         retp->key = lib->key;
550
551         /* Clone symbols.  */
552         {
553                 struct library_symbol *it;
554                 struct library_symbol **nsymp = &retp->symbols;
555                 for (it = lib->symbols; it != NULL; it = it->next) {
556                         *nsymp = malloc(sizeof(**nsymp));
557                         if (*nsymp == NULL
558                             || library_symbol_clone(*nsymp, it) < 0) {
559                                 free(*nsymp);
560                                 *nsymp = NULL;
561                         fail:
562                                 /* Release what we managed to allocate.  */
563                                 library_destroy(retp);
564                                 return -1;
565                         }
566
567                         (*nsymp)->lib = retp;
568                         nsymp = &(*nsymp)->next;
569                 }
570                 *nsymp = NULL;
571         }
572
573         /* Clone exported names.  */
574         if (library_exported_names_clone(&retp->exported_names,
575                                          &lib->exported_names) != 0)
576                 goto fail;
577
578         if (os_library_clone(retp, lib) < 0)
579                 goto fail;
580
581         if (arch_library_clone(retp, lib) < 0) {
582                 os_library_destroy(retp);
583                 goto fail;
584         }
585
586         return 0;
587 }
588
589 void
590 library_destroy(struct library *lib)
591 {
592         if (lib == NULL)
593                 return;
594
595         arch_library_destroy(lib);
596         os_library_destroy(lib);
597
598         library_set_soname(lib, NULL, 0);
599         library_set_pathname(lib, NULL, 0);
600
601         struct library_symbol *sym;
602         for (sym = lib->symbols; sym != NULL; ) {
603                 struct library_symbol *next = sym->next;
604                 library_symbol_destroy(sym);
605                 free(sym);
606                 sym = next;
607         }
608
609         /* Release exported names.  */
610         library_exported_names_destroy(&lib->exported_names);
611 }
612
613 void
614 library_set_soname(struct library *lib, const char *new_name, int own_name)
615 {
616         if (lib->own_soname)
617                 free((char *)lib->soname);
618         lib->soname = new_name;
619         lib->own_soname = own_name;
620 }
621
622 void
623 library_set_pathname(struct library *lib, const char *new_name, int own_name)
624 {
625         if (lib->own_pathname)
626                 free((char *)lib->pathname);
627         lib->pathname = new_name;
628         lib->own_pathname = own_name;
629 }
630
631 struct library_symbol *
632 library_each_symbol(struct library *lib, struct library_symbol *start_after,
633                     enum callback_status (*cb)(struct library_symbol *, void *),
634                     void *data)
635 {
636         struct library_symbol *it = start_after == NULL ? lib->symbols
637                 : start_after->next;
638
639         while (it != NULL) {
640                 struct library_symbol *next = it->next;
641
642                 switch ((*cb)(it, data)) {
643                 case CBS_FAIL:
644                         /* XXX handle me  */
645                 case CBS_STOP:
646                         return it;
647                 case CBS_CONT:
648                         break;
649                 }
650
651                 it = next;
652         }
653
654         return NULL;
655 }
656
657 void
658 library_add_symbol(struct library *lib, struct library_symbol *first)
659 {
660         struct library_symbol *last;
661         for (last = first; last != NULL; ) {
662                 last->lib = lib;
663                 if (last->next != NULL)
664                         last = last->next;
665                 else
666                         break;
667         }
668
669         assert(last->next == NULL);
670         last->next = lib->symbols;
671         lib->symbols = first;
672 }
673
674 enum callback_status
675 library_named_cb(struct process *proc, struct library *lib, void *name)
676 {
677         if (name == lib->soname
678             || strcmp(lib->soname, (char *)name) == 0)
679                 return CBS_STOP;
680         else
681                 return CBS_CONT;
682 }
683
684 enum callback_status
685 library_with_key_cb(struct process *proc, struct library *lib, void *keyp)
686 {
687         return lib->key == *(arch_addr_t *)keyp ? CBS_STOP : CBS_CONT;
688 }