summaryrefslogtreecommitdiff
path: root/library.c
blob: 562b52a2fcdf53d768a3ddb1142274b6bcac8a62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
 * This file is part of ltrace.
 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
 * Copyright (C) 2001,2009 Juan Cespedes
 * Copyright (C) 2006 Ian Wienand
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>

#include "library.h"
#include "callback.h"
#include "debug.h"
#include "dict.h"
#include "vect.h"
#include "backend.h" // for arch_library_symbol_init, arch_library_init

static void
library_exported_names_init(struct library_exported_names *names);

#ifndef OS_HAVE_LIBRARY_DATA
int
os_library_init(struct library *lib)
{
	return 0;
}

void
os_library_destroy(struct library *lib)
{
}

int
os_library_clone(struct library *retp, struct library *lib)
{
	return 0;
}
#endif

#ifndef ARCH_HAVE_LIBRARY_DATA
int
arch_library_init(struct library *lib)
{
	return 0;
}

void
arch_library_destroy(struct library *lib)
{
}

int
arch_library_clone(struct library *retp, struct library *lib)
{
	return 0;
}
#endif

#ifndef OS_HAVE_LIBRARY_SYMBOL_DATA
int
os_library_symbol_init(struct library_symbol *libsym)
{
	return 0;
}

void
os_library_symbol_destroy(struct library_symbol *libsym)
{
}

int
os_library_symbol_clone(struct library_symbol *retp,
			struct library_symbol *libsym)
{
	return 0;
}
#endif

#ifndef ARCH_HAVE_LIBRARY_SYMBOL_DATA
int
arch_library_symbol_init(struct library_symbol *libsym)
{
	return 0;
}

void
arch_library_symbol_destroy(struct library_symbol *libsym)
{
}

int
arch_library_symbol_clone(struct library_symbol *retp,
			  struct library_symbol *libsym)
{
	return 0;
}
#endif

size_t
arch_addr_hash(const arch_addr_t *addr)
{
	union {
		arch_addr_t addr;
		int ints[sizeof(arch_addr_t)
			 / sizeof(unsigned int)];
	} u = { .addr = *addr };

	size_t i;
	size_t h = 0;
	for (i = 0; i < sizeof(u.ints) / sizeof(*u.ints); ++i)
		h ^= dict_hash_int(&u.ints[i]);
	return h;
}

int
arch_addr_eq(const arch_addr_t *addr1, const arch_addr_t *addr2)
{
	return *addr1 == *addr2;
}

int
strdup_if(const char **retp, const char *str, int whether)
{
	if (whether && str != NULL) {
		str = strdup(str);
		if (str == NULL)
			return -1;
	}

	*retp = str;
	return 0;
}

static void
private_library_symbol_init(struct library_symbol *libsym,
			    arch_addr_t addr,
			    const char *name, int own_name,
			    enum toplt type_of_plt,
			    int latent, int delayed)
{
	libsym->next = NULL;
	libsym->lib = NULL;
	libsym->plt_type = type_of_plt;
	libsym->name = name;
	libsym->own_name = own_name;
	libsym->latent = latent;
	libsym->delayed = delayed;
	libsym->enter_addr = (void *)(uintptr_t)addr;
	libsym->proto = NULL;
}

static void
private_library_symbol_destroy(struct library_symbol *libsym)
{
	library_symbol_set_name(libsym, NULL, 0);
}

int
library_symbol_init(struct library_symbol *libsym,
		    arch_addr_t addr, const char *name, int own_name,
		    enum toplt type_of_plt)
{
	private_library_symbol_init(libsym, addr, name, own_name,
				    type_of_plt, 0, 0);

	if (os_library_symbol_init(libsym) < 0)
		/* We've already set libsym->name and own_name.  But
		 * we return failure, and the client code isn't
		 * supposed to call library_symbol_destroy in such
		 * case.  */
		return -1;

	if (arch_library_symbol_init(libsym) < 0) {
		os_library_symbol_destroy(libsym);
		return -1;
	}

	return 0;
}

void
library_symbol_destroy(struct library_symbol *libsym)
{
	if (libsym != NULL) {
		arch_library_symbol_destroy(libsym);
		os_library_symbol_destroy(libsym);
		private_library_symbol_destroy(libsym);
	}
}

int
library_symbol_clone(struct library_symbol *retp, struct library_symbol *libsym)
{
	/* Make lifetimes of name stored at original independent of
	 * the one at the clone.  */
	const char *name;
	if (strdup_if(&name, libsym->name, libsym->own_name) < 0)
		return -1;

	private_library_symbol_init(retp, libsym->enter_addr,
				    name, libsym->own_name, libsym->plt_type,
				    libsym->latent, libsym->delayed);

	if (os_library_symbol_clone(retp, libsym) < 0) {
	fail:
		private_library_symbol_destroy(retp);
		return -1;
	}

	if (arch_library_symbol_clone(retp, libsym) < 0) {
		os_library_symbol_destroy(retp);
		goto fail;
	}

	return 0;
}

int
library_symbol_cmp(struct library_symbol *a, struct library_symbol *b)
{
	if (a->enter_addr < b->enter_addr)
		return -1;
	if (a->enter_addr > b->enter_addr)
		return 1;
	if (a->name != NULL && b->name != NULL)
		return strcmp(a->name, b->name);
	if (a->name == NULL) {
		if (b->name == NULL)
			return 0;
		return -1;
	}
	return 1;
}

void
library_symbol_set_name(struct library_symbol *libsym,
			const char *name, int own_name)
{
	if (libsym->own_name)
		free((char *)libsym->name);
	libsym->name = name;
	libsym->own_name = own_name;
}

enum callback_status
library_symbol_equal_cb(struct library_symbol *libsym, void *u)
{
	struct library_symbol *standard = u;
	return library_symbol_cmp(libsym, standard) == 0 ? CBS_STOP : CBS_CONT;
}

enum callback_status
library_symbol_named_cb(struct library_symbol *libsym, void *name)
{
	return strcmp(libsym->name, name) == 0 ? CBS_STOP : CBS_CONT;
}

enum callback_status
library_symbol_delayed_cb(struct library_symbol *libsym, void *unused)
{
	return libsym->delayed ? CBS_STOP : CBS_CONT;
}

static void
private_library_init(struct library *lib, enum library_type type)
{
	lib->next = NULL;

	lib->key = 0;
	lib->base = 0;
	lib->entry = 0;
	lib->dyn_addr = 0;
	lib->protolib = NULL;

	lib->soname = NULL;
	lib->own_soname = 0;

	lib->pathname = NULL;
	lib->own_pathname = 0;

	lib->symbols = NULL;
	library_exported_names_init(&lib->exported_names);
	lib->type = type;

#if defined(HAVE_LIBDW)
	lib->dwfl_module = NULL;
#endif
}

int
library_init(struct library *lib, enum library_type type)
{
	private_library_init(lib, type);

	if (os_library_init(lib) < 0)
		return -1;

	if (arch_library_init(lib) < 0) {
		os_library_destroy(lib);
		return -1;
	}

	return 0;
}





static void dtor_string(const char **tgt, void *data)
{
	free((char*)*tgt);
}
static int clone_vect(struct vect **to, const struct vect **from, void *data)
{
	*to = malloc(sizeof(struct vect));
	if (*to == NULL)
		return -1;

	return
		VECT_CLONE(*to, *from, const char*,
			   dict_clone_string,
			   dtor_string,
			   NULL);
}
static void dtor_vect(const struct vect **tgt, void *data)
{
	VECT_DESTROY(*tgt, const char*, dtor_string, NULL);
	free(*tgt);
}

static void
library_exported_names_init(struct library_exported_names *names)
{
	DICT_INIT(&names->names,
		  const char*, uint64_t,
		  dict_hash_string, dict_eq_string, NULL);
	DICT_INIT(&names->addrs,
		  uint64_t, struct vect*,
		  dict_hash_uint64, dict_eq_uint64, NULL);
}

static void
library_exported_names_destroy(struct library_exported_names *names)
{
	DICT_DESTROY(&names->names,
		     const char*, uint64_t,
		     dtor_string, NULL, NULL);
	DICT_DESTROY(&names->addrs,
		     uint64_t, struct vect*,
		     NULL, dtor_vect, NULL);
}

static int
library_exported_names_clone(struct library_exported_names *retp,
			     const struct library_exported_names *names)
{
	return (DICT_CLONE(&retp->names, &names->names,
			   const char*, uint64_t,
			   dict_clone_string, dtor_string,
			   NULL, NULL,
			   NULL) < 0  ||
		DICT_CLONE(&retp->addrs, &names->addrs,
			   uint64_t, struct vect*,
			   NULL, NULL,
			   clone_vect, dtor_vect,
			   NULL) < 0) ? -1 : 0;
}

int library_exported_names_push(struct library_exported_names *names,
				uint64_t addr, const char *name,
				int own_name )
{
	// first, take ownership of the name, if it's not yet ours
	if (!own_name)
		name = strdup(name);
	if (name == NULL)
		return -1;

	// push to the name->addr map
	int result = DICT_INSERT(&names->names, &name, &addr);
	if (result > 0) {
		// This symbol is already present in the table. This library has
		// multiple copies of this symbol (probably corresponding to
		// different symbol versions). I should handle this gracefully
		// at some point, but for now I simply ignore later instances of
		// any particular symbol
		free(name);
		return 0;
	}

	if (result != 0)
		return result;

	// push to the addr->names map
	// I get the alias vector. If it doesn't yet exist, I make it
	struct vect *aliases;
	struct vect **paliases = DICT_FIND_REF(&names->addrs,
					       &addr, struct vect*);

	if (paliases == NULL) {
		aliases = malloc(sizeof(struct vect));
		if (aliases == NULL)
			return -1;
		VECT_INIT(aliases, const char*);
		result = DICT_INSERT(&names->addrs, &addr, &aliases);
		assert(result <= 0);
		if (result < 0)
			return result;
	}
	else
		aliases = *paliases;

	const char *namedup = strdup(name);
	if (namedup == NULL)
		return -1;

	result = vect_pushback(aliases, &namedup);
	if (result != 0)
		return result;

	return 0;
}

struct library_exported_names_each_context
{
	enum callback_status (*inner_cb)(const char *, void *);
	void *data;
	bool failure : 1;
};
static enum callback_status
library_exported_names_each_cb(const char **key, uint64_t *value, void *data)
{
	struct library_exported_names_each_context *context =
		(struct library_exported_names_each_context*)data;
	enum callback_status status = context->inner_cb(*key, context->data);
	if (status == CBS_FAIL)
		context->failure = true;
	return status;
}
bool library_exported_names_each(const struct library_exported_names *names,
				 enum callback_status (*cb)(const char *,
							    void *),
				 void *data)
{
	struct library_exported_names_each_context context =
		{.inner_cb = cb,
		 .data = data,
		 .failure = false};
	DICT_EACH(&names->names,
		  const char*, uint64_t,
		  NULL, library_exported_names_each_cb, &context);
	return !context.failure;
}

struct library_exported_names_each_alias_context
{
	enum callback_status (*inner_cb)(const char *, void *);
	const char *origname;
	void *data;
	bool failure : 1;
};
static enum callback_status
library_exported_names_each_alias_cb(const char **name, void *data)
{
	struct library_exported_names_each_alias_context *context =
		(struct library_exported_names_each_alias_context*)data;

	// I do not report the original name we were asked about. Otherwise, any
	// time the caller asks for aliases of symbol "sym", I'll always report
	// "sym" along with any actual aliases
	if (strcmp(*name, context->origname) == 0)
		return CBS_CONT;

	enum callback_status status = context->inner_cb(*name, context->data);
	if (status == CBS_FAIL)
		context->failure = true;
	return status;
}

bool library_exported_names_each_alias(
	const struct library_exported_names *names,
	const char *aliasname,
	enum callback_status (*cb)(const char *,
				   void *),
	void *data)
{
	// I have a symbol name. I look up its address, then get the list of
	// aliased names
	uint64_t *addr = DICT_FIND_REF(&names->names,
				       &aliasname, uint64_t);
	if (addr == NULL)
		return false;

	// OK. I have an address. Get the list of symbols at this address
	struct vect **aliases = DICT_FIND_REF(&names->addrs,
					     addr, struct vect*);
	if (aliases == NULL)
		return false;

	struct library_exported_names_each_alias_context context =
		{.inner_cb = cb,
		 .origname = aliasname,
		 .data = data,
		 .failure = false};
	VECT_EACH(*aliases, const char*, NULL,
		  library_exported_names_each_alias_cb, &context);
}




int
library_clone(struct library *retp, struct library *lib)
{
	const char *soname = NULL;
	const char *pathname;

	/* Make lifetimes of strings stored at original independent of
	 * those at the clone.  */
	if (strdup_if(&soname, lib->soname, lib->own_soname) < 0
	    || strdup_if(&pathname, lib->pathname, lib->own_pathname) < 0) {
		if (lib->own_soname)
			free((char *)soname);
		return -1;
	}

	private_library_init(retp, lib->type);
	library_set_soname(retp, soname, lib->own_soname);
	library_set_pathname(retp, pathname, lib->own_pathname);

	retp->key = lib->key;

	/* Clone symbols.  */
	{
		struct library_symbol *it;
		struct library_symbol **nsymp = &retp->symbols;
		for (it = lib->symbols; it != NULL; it = it->next) {
			*nsymp = malloc(sizeof(**nsymp));
			if (*nsymp == NULL
			    || library_symbol_clone(*nsymp, it) < 0) {
				free(*nsymp);
				*nsymp = NULL;
			fail:
				/* Release what we managed to allocate.  */
				library_destroy(retp);
				return -1;
			}

			(*nsymp)->lib = retp;
			nsymp = &(*nsymp)->next;
		}
		*nsymp = NULL;
	}

	/* Clone exported names.  */
	if (library_exported_names_clone(&retp->exported_names,
					 &lib->exported_names) != 0)
		goto fail;

	if (os_library_clone(retp, lib) < 0)
		goto fail;

	if (arch_library_clone(retp, lib) < 0) {
		os_library_destroy(retp);
		goto fail;
	}

	return 0;
}

void
library_destroy(struct library *lib)
{
	if (lib == NULL)
		return;

	arch_library_destroy(lib);
	os_library_destroy(lib);

	library_set_soname(lib, NULL, 0);
	library_set_pathname(lib, NULL, 0);

	struct library_symbol *sym;
	for (sym = lib->symbols; sym != NULL; ) {
		struct library_symbol *next = sym->next;
		library_symbol_destroy(sym);
		free(sym);
		sym = next;
	}

	/* Release exported names.  */
	library_exported_names_destroy(&lib->exported_names);
}

void
library_set_soname(struct library *lib, const char *new_name, int own_name)
{
	if (lib->own_soname)
		free((char *)lib->soname);
	lib->soname = new_name;
	lib->own_soname = own_name;
}

void
library_set_pathname(struct library *lib, const char *new_name, int own_name)
{
	if (lib->own_pathname)
		free((char *)lib->pathname);
	lib->pathname = new_name;
	lib->own_pathname = own_name;
}

struct library_symbol *
library_each_symbol(struct library *lib, struct library_symbol *start_after,
		    enum callback_status (*cb)(struct library_symbol *, void *),
		    void *data)
{
	struct library_symbol *it = start_after == NULL ? lib->symbols
		: start_after->next;

	while (it != NULL) {
		struct library_symbol *next = it->next;

		switch ((*cb)(it, data)) {
		case CBS_FAIL:
			/* XXX handle me  */
		case CBS_STOP:
			return it;
		case CBS_CONT:
			break;
		}

		it = next;
	}

	return NULL;
}

void
library_add_symbol(struct library *lib, struct library_symbol *first)
{
	struct library_symbol *last;
	for (last = first; last != NULL; ) {
		last->lib = lib;
		if (last->next != NULL)
			last = last->next;
		else
			break;
	}

	assert(last->next == NULL);
	last->next = lib->symbols;
	lib->symbols = first;
}

enum callback_status
library_named_cb(struct process *proc, struct library *lib, void *name)
{
	if (name == lib->soname
	    || strcmp(lib->soname, (char *)name) == 0)
		return CBS_STOP;
	else
		return CBS_CONT;
}

enum callback_status
library_with_key_cb(struct process *proc, struct library *lib, void *keyp)
{
	return lib->key == *(arch_addr_t *)keyp ? CBS_STOP : CBS_CONT;
}