aboutsummaryrefslogtreecommitdiff
path: root/src/libtrace/init_libtrace.c
blob: 131c31f732662ff6694f092133d2d2af309fd495 (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
#include <fcntl.h>
#include <sys/types.h>
#include <linux/elf.h>
#include <hck/syscall.h>
#include <sys/mman.h>

static int fd = 2;

static char ax_jmp[] = {
	0xb8, 1, 2, 3, 4,		/* movl $0x04030201, %eax	*/
	0xa3, 5, 6, 7, 8,		/* movl %eax, 0x08070605	*/
	0xff, 0x25, 1, 2, 3, 4,		/* jmp *0x04030201		*/
	0, 0, 0, 0,			/* real_func			*/
	0, 0, 0, 0,			/* name				*/
	0, 0, 0, 0			/* got				*/
};

struct ax_jmp_t {
	char tmp1;
	void * src __attribute__ ((packed));
	char tmp2;
	void * dst __attribute__ ((packed));
	char tmp3,tmp4;
	void * new_func __attribute__ ((packed));
	void * real_func __attribute__ ((packed));
	char * name __attribute__ ((packed));
	u_long * got __attribute__ ((packed));
};

static struct ax_jmp_t * pointer_tmp;
static struct ax_jmp_t * pointer;

static void new_func(void);
static void * new_func_ptr = NULL;

static int current_pid;

static void init_libtrace(void)
{
	int i,j;
	void * k;
	struct elf32_sym * symtab = NULL;
	size_t symtab_len = 0;
	char * strtab = NULL;
	char * tmp;
	int nsymbols = 0;
	long buffer[6];
	struct ax_jmp_t * table;

	if (new_func_ptr) {
		return;
	}
	new_func_ptr = new_func;

	current_pid = _sys_getpid();

	tmp = getenv("LTRACE_FILENAME");
	if (tmp) {
		int fd_old;

		fd_old = _sys_open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0666);
		if (fd_old<0) {
			_sys_write(2, "Can't open LTRACE_FILENAME\n", 27);
			return;
		}
		fd = _sys_dup2(fd_old, 234);
		_sys_close(fd_old);
		if (fd<0) {
			_sys_write(2, "Not enough fd's?\n", 17);
			return;
		}
	}

	tmp = getenv("LTRACE_SYMTAB");
	if (!tmp) {
		_sys_write(fd, "No LTRACE_SYMTAB...\n", 20);
		return;
	}
	symtab = (struct elf32_sym *)atoi(tmp);

	tmp = getenv("LTRACE_SYMTAB_LEN");
	if (!tmp) {
		_sys_write(fd, "No LTRACE_SYMTAB_LEN...\n", 24);
		return;
	}
	symtab_len = atoi(tmp);

	tmp = getenv("LTRACE_STRTAB");
	if (!tmp) {
		_sys_write(fd, "No LTRACE_STRTAB...\n", 20);
		return;
	}
	strtab = (char *)atoi(tmp);

	unsetenv("LD_PRELOAD");
	unsetenv("LTRACE_SYMTAB");
	unsetenv("LTRACE_SYMTAB_LEN");
	unsetenv("LTRACE_STRTAB");

	for(i=0; i<symtab_len/sizeof(struct elf32_sym); i++) {
		if (!((symtab+i)->st_shndx) && (symtab+i)->st_value) {
			nsymbols++;
#if 0
			_sys_write(fd, "New symbol: ", 12);
			_sys_write(fd,strtab+(symtab+i)->st_name,strlen(strtab+(symtab+i)->st_name));
			_sys_write(fd, "\n", 1);
#endif
		}
	}

	buffer[0] = 0;
	buffer[1] = nsymbols * sizeof(struct ax_jmp_t);
	buffer[2] = PROT_READ | PROT_WRITE | PROT_EXEC;
	buffer[3] = MAP_PRIVATE | MAP_ANON;
	buffer[4] = 0;
	buffer[5] = 0;
	table = (struct ax_jmp_t *)_sys_mmap(buffer);
	if (!table) {
		_sys_write(fd,"Cannot mmap?\n",13);
		return;
	}

	j=0;
	for(i=0; i<symtab_len/sizeof(struct elf32_sym); i++) {
		if (!((symtab+i)->st_shndx) && (symtab+i)->st_value) {
			bcopy(ax_jmp, (char *)&table[j], sizeof(struct ax_jmp_t));
			table[j].src = (char *)&table[j];
			table[j].dst = &pointer_tmp;
			table[j].new_func = &new_func_ptr;
			table[j].name = strtab+(symtab+i)->st_name;
			table[j].got = (u_long *)*(long *)(((int)((symtab+i)->st_value))+2);
			table[j].real_func = (void *)*(table[j].got);
			k = &table[j];
			bcopy((char*)&k, (char *)table[j].got, 4);
			j++;
		}
	}

	_sys_write(fd,"ltrace starting...\n",19);
}

static u_long where_to_return;
static u_long returned_value;
static u_long where_to_jump;

static int reentrant=0;

static void print_results(u_long arg);

static void kk(void)
{
	char buf[1024];
	sprintf(buf, "\n%s(", pointer_tmp->name);
	_sys_write(fd,buf,strlen(buf));
}

static void new_func(void)
{
#if 1
	kk();
#endif
	if (reentrant) {
#if 0
_sys_write(fd,"reentrant\n",10);
#endif
		__asm__ __volatile__(
			"movl %%ebp, %%esp\n\t"
			"popl %%ebp\n\t"
			"jmp *%%eax\n"
			: : "a" (pointer_tmp->real_func)
		);
	}
	reentrant=1;
	pointer = pointer_tmp;
	where_to_jump = (u_long)pointer->real_func;

	/* This is only to avoid a GCC warning; shouldn't be here:
	 */
	where_to_return = (long)print_results;

	/* HCK: Is all these stuff about 'ebp' buggy? */
	__asm__ __volatile__(
		"movl %ebp, %esp\n\t"
		"popl %ebp\n\t"
		"popl %eax\n\t"
		"movl %eax, where_to_return\n\t"
		"movl where_to_jump, %eax\n\t"
		"call *%eax\n\t"
		"movl %eax, returned_value\n\t"
		"call print_results\n\t"
		"movl where_to_return, %eax\n\t"
		"pushl %eax\n\t"
		"movl returned_value, %eax\n\t"
		"movl $0, reentrant\n\t"
		"ret\n"
	);
}