I now always build the export list
[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->should_activate_latent = false;
301         lib->type = type;
302
303 #if defined(HAVE_LIBDW)
304         lib->dwfl_module = NULL;
305 #endif
306 }
307
308 int
309 library_init(struct library *lib, enum library_type type)
310 {
311         private_library_init(lib, type);
312
313         if (os_library_init(lib) < 0)
314                 return -1;
315
316         if (arch_library_init(lib) < 0) {
317                 os_library_destroy(lib);
318                 return -1;
319         }
320
321         return 0;
322 }
323
324
325
326
327
328 static void dtor_string(const char **tgt, void *data)
329 {
330         free((char*)*tgt);
331 }
332 static int clone_vect(struct vect **to, const struct vect **from, void *data)
333 {
334         *to = malloc(sizeof(struct vect));
335         if (*to == NULL)
336                 return -1;
337
338         return
339                 VECT_CLONE(*to, *from, const char*,
340                            dict_clone_string,
341                            dtor_string,
342                            NULL);
343 }
344 static void dtor_vect(struct vect **tgt, void *data)
345 {
346         VECT_DESTROY(*tgt, const char*, dtor_string, NULL);
347         free(*tgt);
348 }
349
350 static void
351 library_exported_names_init(struct library_exported_names *names)
352 {
353         DICT_INIT(&names->names,
354                   const char*, uint64_t,
355                   dict_hash_string, dict_eq_string, NULL);
356         DICT_INIT(&names->addrs,
357                   uint64_t, struct vect*,
358                   dict_hash_uint64, dict_eq_uint64, NULL);
359 }
360
361 static void
362 library_exported_names_destroy(struct library_exported_names *names)
363 {
364         DICT_DESTROY(&names->names,
365                      const char*, uint64_t,
366                      dtor_string, NULL, NULL);
367         DICT_DESTROY(&names->addrs,
368                      uint64_t, struct vect*,
369                      NULL, dtor_vect, NULL);
370 }
371
372 static int
373 library_exported_names_clone(struct library_exported_names *retp,
374                              const struct library_exported_names *names)
375 {
376         return (DICT_CLONE(&retp->names, &names->names,
377                            const char*, uint64_t,
378                            dict_clone_string, dtor_string,
379                            NULL, NULL,
380                            NULL) < 0  ||
381                 DICT_CLONE(&retp->addrs, &names->addrs,
382                            uint64_t, struct vect*,
383                            NULL, NULL,
384                            clone_vect, dtor_vect,
385                            NULL) < 0) ? -1 : 0;
386 }
387
388 int library_exported_names_push(struct library_exported_names *names,
389                                 uint64_t addr, 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 > 0) {
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                 assert(result <= 0);
426                 if (result < 0)
427                         return result;
428         }
429         else
430                 aliases = *paliases;
431
432         char *namedup = strdup(name);
433         if (namedup == NULL)
434                 return -1;
435
436         result = vect_pushback(aliases, &namedup);
437         if (result != 0) {
438                 free(namedup);
439                 return result;
440         }
441
442         return 0;
443 }
444
445 struct library_exported_names_each_alias_context
446 {
447         enum callback_status (*inner_cb)(const char *, void *);
448         const char *origname;
449         void *data;
450 };
451 static enum callback_status
452 library_exported_names_each_alias_cb(const char **name, void *data)
453 {
454         struct library_exported_names_each_alias_context *context =
455                 (struct library_exported_names_each_alias_context*)data;
456
457         // I do not report the original name we were asked about. Otherwise, any
458         // time the caller asks for aliases of symbol "sym", I'll always report
459         // "sym" along with any actual aliases
460         if (strcmp(*name, context->origname) == 0)
461                 return CBS_CONT;
462
463         return context->inner_cb(*name, context->data);
464 }
465
466 const char** library_exported_names_each_alias(
467         struct library_exported_names *names,
468         const char *aliasname,
469         const char **name_start_after,
470         enum callback_status (*cb)(const char *,
471                                    void *),
472         void *data)
473 {
474         // I have a symbol name. I look up its address, then get the list of
475         // aliased names
476         uint64_t *addr = DICT_FIND_REF(&names->names,
477                                        &aliasname, uint64_t);
478         if (addr == NULL)
479                 return NULL;
480
481         // OK. I have an address. Get the list of symbols at this address
482         struct vect **aliases = DICT_FIND_REF(&names->addrs,
483                                               addr, struct vect*);
484         assert(aliases != NULL);
485
486         struct library_exported_names_each_alias_context context =
487                 {.inner_cb = cb,
488                  .origname = aliasname,
489                  .data = data};
490         return VECT_EACH(*aliases, const char*, name_start_after,
491                          library_exported_names_each_alias_cb, &context);
492 }
493
494 int library_exported_names_contains(struct library_exported_names* names,
495                                     const char* queryname)
496 {
497         uint64_t *addr = DICT_FIND_REF(&names->names,
498                                        &queryname, uint64_t);
499         return (addr == NULL) ? 0 : 1;
500 }
501
502
503
504
505
506 int
507 library_clone(struct library *retp, struct library *lib)
508 {
509         const char *soname = NULL;
510         const char *pathname;
511
512         /* Make lifetimes of strings stored at original independent of
513          * those at the clone.  */
514         if (strdup_if(&soname, lib->soname, lib->own_soname) < 0
515             || strdup_if(&pathname, lib->pathname, lib->own_pathname) < 0) {
516                 if (lib->own_soname)
517                         free((char *)soname);
518                 return -1;
519         }
520
521         private_library_init(retp, lib->type);
522         library_set_soname(retp, soname, lib->own_soname);
523         library_set_pathname(retp, pathname, lib->own_pathname);
524
525         retp->key = lib->key;
526
527         /* Clone symbols.  */
528         {
529                 struct library_symbol *it;
530                 struct library_symbol **nsymp = &retp->symbols;
531                 for (it = lib->symbols; it != NULL; it = it->next) {
532                         *nsymp = malloc(sizeof(**nsymp));
533                         if (*nsymp == NULL
534                             || library_symbol_clone(*nsymp, it) < 0) {
535                                 free(*nsymp);
536                                 *nsymp = NULL;
537                         fail:
538                                 /* Release what we managed to allocate.  */
539                                 library_destroy(retp);
540                                 return -1;
541                         }
542
543                         (*nsymp)->lib = retp;
544                         nsymp = &(*nsymp)->next;
545                 }
546                 *nsymp = NULL;
547         }
548
549         /* Clone exported names.  */
550         if (library_exported_names_clone(&retp->exported_names,
551                                          &lib->exported_names) != 0)
552                 goto fail;
553
554         if (os_library_clone(retp, lib) < 0)
555                 goto fail;
556
557         if (arch_library_clone(retp, lib) < 0) {
558                 os_library_destroy(retp);
559                 goto fail;
560         }
561
562         return 0;
563 }
564
565 void
566 library_destroy(struct library *lib)
567 {
568         if (lib == NULL)
569                 return;
570
571         arch_library_destroy(lib);
572         os_library_destroy(lib);
573
574         library_set_soname(lib, NULL, 0);
575         library_set_pathname(lib, NULL, 0);
576
577         struct library_symbol *sym;
578         for (sym = lib->symbols; sym != NULL; ) {
579                 struct library_symbol *next = sym->next;
580                 library_symbol_destroy(sym);
581                 free(sym);
582                 sym = next;
583         }
584
585         /* Release exported names.  */
586         library_exported_names_destroy(&lib->exported_names);
587 }
588
589 void
590 library_set_soname(struct library *lib, const char *new_name, int own_name)
591 {
592         if (lib->own_soname)
593                 free((char *)lib->soname);
594         lib->soname = new_name;
595         lib->own_soname = own_name;
596 }
597
598 void
599 library_set_pathname(struct library *lib, const char *new_name, int own_name)
600 {
601         if (lib->own_pathname)
602                 free((char *)lib->pathname);
603         lib->pathname = new_name;
604         lib->own_pathname = own_name;
605 }
606
607 struct library_symbol *
608 library_each_symbol(struct library *lib, struct library_symbol *start_after,
609                     enum callback_status (*cb)(struct library_symbol *, void *),
610                     void *data)
611 {
612         struct library_symbol *it = start_after == NULL ? lib->symbols
613                 : start_after->next;
614
615         while (it != NULL) {
616                 struct library_symbol *next = it->next;
617
618                 switch ((*cb)(it, data)) {
619                 case CBS_FAIL:
620                         /* XXX handle me  */
621                 case CBS_STOP:
622                         return it;
623                 case CBS_CONT:
624                         break;
625                 }
626
627                 it = next;
628         }
629
630         return NULL;
631 }
632
633 void
634 library_add_symbol(struct library *lib, struct library_symbol *first)
635 {
636         struct library_symbol *last;
637         for (last = first; last != NULL; ) {
638                 last->lib = lib;
639                 if (last->next != NULL)
640                         last = last->next;
641                 else
642                         break;
643         }
644
645         assert(last->next == NULL);
646         last->next = lib->symbols;
647         lib->symbols = first;
648 }
649
650 enum callback_status
651 library_named_cb(struct process *proc, struct library *lib, void *name)
652 {
653         if (name == lib->soname
654             || strcmp(lib->soname, (char *)name) == 0)
655                 return CBS_STOP;
656         else
657                 return CBS_CONT;
658 }
659
660 enum callback_status
661 library_with_key_cb(struct process *proc, struct library *lib, void *keyp)
662 {
663         return lib->key == *(arch_addr_t *)keyp ? CBS_STOP : CBS_CONT;
664 }