File: codegen.c

package info (click to toggle)
libfiu 1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: ansic: 2,633; python: 973; makefile: 599; sh: 309
file content (57 lines) | stat: -rw-r--r-- 1,375 bytes parent folder | download | duplicates (2)
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

#include "codegen.h"
#include "build-env.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

/* Recursion counter, per-thread */
int __thread _fiu_called = 0;

/* Let the user know if there is no constructor priorities support, just in
 * case there are bugs when building/running without them */
#ifdef NO_CONSTRUCTOR_PRIORITIES
#warning "Building without using constructor priorities"
#endif

/* Get a symbol from libc.
 * This function is a wrapper around dlsym(libc, ...), that we use to abstract
 * away how we get the libc wrapper, because on some platforms there are
 * better shortcuts. */
void *libc_symbol(const char *symbol)
{
#ifdef RTLD_NEXT
	return dlsym(RTLD_NEXT, symbol);
#else
	/* We don't want to get this over and over again, so we set it once
	 * and reuse it afterwards. */
	static void *_fiu_libc = NULL;

	if (_fiu_libc == NULL) {
		_fiu_libc = dlopen(LIBC_SONAME, RTLD_NOW);
		if (_fiu_libc == NULL) {
			fprintf(stderr, "Error loading libc: %s\n", dlerror());
			exit(1);
		}
	}

	return dlsym(_fiu_libc, symbol);
#endif
}

/* this runs after all function-specific constructors */
static void constructor_attr(250) _fiu_init_final(void)
{
	struct timeval tv;

	rec_inc();

	fiu_init(0);

	/* since we use random() in the wrappers, we need to seed it */
	gettimeofday(&tv, NULL);
	srandom(tv.tv_usec);

	rec_dec();
}