summaryrefslogtreecommitdiffstats
path: root/scripts/ksymoops/object.c
blob: 7a44e4c350bd2717f83e59a1f06e372cbbad0297 (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
/*
	object.c.

	object handling routines for ksymoops.  Read modules, vmlinux, etc. 

	Copyright Keith Owens <kaos@ocs.com.au>.
	Released under the GNU Public Licence, Version 2.

	Wed Oct 28 13:47:23 EST 1998
	Version 0.4
	Split into separate sources.
 */

#include "ksymoops.h"
#include <malloc.h>
#include <string.h>
#include <sys/stat.h>

/* Extract all symbols definitions from an object using nm */
static void read_nm_symbols(SYMBOL_SET *ss, const char *file)
{
	FILE *f;
	char *cmd, *line = NULL, **string = NULL;
	int i, size = 0;
	static char const procname[] = "read_nm_symbols";

	if (!regular_file(file, procname))
		return;

	cmd = malloc(strlen(path_nm)+strlen(file)+2);
	if (!cmd)
		malloc_error("nm command");
	strcpy(cmd, path_nm);
	strcat(cmd, " ");
	strcat(cmd, file);
	if (debug > 1)
		fprintf(stderr, "DEBUG: %s command '%s'\n", procname, cmd);
	if (!(f = popen_local(cmd, procname)))
		return;
	free(cmd);

	while (fgets_local(&line, &size, f, procname)) {
		i = regexec(&re_nm, line, re_nm.re_nsub+1, re_nm_pmatch, 0);
		if (debug > 3)
			fprintf(stderr, "DEBUG: %s regexec %d\n", procname, i);
		if (i == 0) {
			re_strings(&re_nm, line, re_nm_pmatch, &string);
			add_symbol(ss, string[1], *string[2], 1, string[3]);
		}
	}

	pclose_local(f, procname);
	re_strings_free(&re_nm, &string);
	free(line);
	if (debug > 1)
		fprintf(stderr,
			"DEBUG: %s %s used %d out of %d entries\n",
			procname, ss->source, ss->used, ss->alloc);
}

/* Read the symbols from vmlinux */
void read_vmlinux(const char *vmlinux)
{
	if (!vmlinux)
		return;
	ss_init(&ss_vmlinux, "vmlinux");
	read_nm_symbols(&ss_vmlinux, vmlinux);
	if (ss_vmlinux.used) {
		ss_sort_na(&ss_vmlinux);
		extract_Version(&ss_vmlinux);
	}
	else {
		fprintf(stderr,
			"Warning, no kernel symbols in vmlinux, is %s a valid "
			"vmlinux file?\n",
			vmlinux);
		++warnings;
	}
}


/* Read the symbols from one object (module) */
void read_object(const char *object, int i)
{
	ss_init(ss_object+i, object);
	read_nm_symbols(ss_object+i, object);
	if ((ss_object+i)->used) {
		ss_sort_na(ss_object+i);
		extract_Version(ss_object+i);
	}
	else {
		fprintf(stderr, "Warning, no symbols in %s\n", object);
		++warnings;
	}
}

/* Add a new entry to the list of objects */
static void add_ss_object(const char *file)
{
	++ss_objects;
	ss_object = realloc(ss_object, ss_objects*sizeof(*ss_object));
	if (!ss_object)
		malloc_error("realloc ss_object");
	ss_init(ss_object+ss_objects-1, file);
}

/* Run a directory and its subdirectories, looking for *.o files */
static void find_objects(const char *dir)
{
	FILE *f;
	char *cmd, *line = NULL;
	int size = 0, files = 0;
	static char const procname[] = "find_objects";
	static char const options[] = " -follow -name '*.o' -print";

	cmd = malloc(strlen(path_find)+1+strlen(dir)+strlen(options)+1);
	if (!cmd)
		malloc_error("find command");
	strcpy(cmd, path_find);
	strcat(cmd, " ");
	strcat(cmd, dir);
	strcat(cmd, options);
	if (debug > 1)
		fprintf(stderr, "DEBUG: %s command '%s'\n", procname, cmd);
	if (!(f = popen_local(cmd, procname)))
		return;
	free(cmd);

	while (fgets_local(&line, &size, f, procname)) {
		if (debug > 1)
			fprintf(stderr, "DEBUG: %s - %s\n", procname, line);
		add_ss_object(line);
		++files;
	}

	pclose_local(f, procname);
	if (!files) {
		fprintf(stderr,
			"Warning: no *.o files in %s.  "
			"Is %s a valid module directory?\n",
			dir, dir);
		++warnings;
	}
}

/* Take the user supplied list of objects which can include directories.
 * Expand directories into any *.o files.  The results are stored in
 * ss_object, leaving the user supplied options untouched.
 */
void expand_objects(char * const *object, int objects)
{
	struct stat statbuf;
	int i;
	const char *file;
	static char const procname[] = "expand_objects";

	for (i = 0; i < objects; ++i) {
		file = object[i];
		if (debug > 1)
			fprintf(stderr, "DEBUG: %s checking '%s' - ",
				procname, file);
		if (!stat(file, &statbuf) && S_ISDIR(statbuf.st_mode)) {
			if (debug > 1)
				fprintf(stderr, "directory, expanding\n");
			find_objects(file);
		}
		else {
			if (debug > 1)
				fprintf(stderr, "not directory\n");
			add_ss_object(file);
		}
	}
}

/* Map a symbol type to a section code. 0 - text, 1 - data, 2 - read only data,
 * 3 - C (cannot relocate), 4 - the rest.
 */
static int section(char type)
{
	switch (type) {
	case 'T':
	case 't':
		return 0;
	case 'D':
	case 'd':
		return 1;
	case 'R':
	case 'r':
		return 2;
	case 'C':
		return 3;
	default:
		return 4;
	}
}

/* Given ksyms module data which has a related object, create a copy of the
 * object data, adjusting the offsets to match where the module was loaded.
 */
SYMBOL_SET *adjust_object_offsets(SYMBOL_SET *ss)
{
	int i;
	elf_addr_t adjust[] = {0, 0, 0, 0, 0};
	SYMBOL *sk, *so;
	SYMBOL_SET *ssc;

	if (debug > 1)
		fprintf(stderr,
			"DEBUG: adjust_object_offsets %s\n", ss->source);

	ssc = ss_copy(ss->related);

	/* For common symbols, calculate the adjustment */
	for (i = 0; i < ss->used; ++i) {
		sk = ss->symbol+i;
		if ((so = find_symbol_name(ssc, sk->name, NULL)))
			adjust[section(so->type)] = sk->address - so->address;
	}
	for (i = 0; i < ssc->used; ++i) {
		so = ssc->symbol+i;
		/* Type C does not relocate well, silently ignore */
		if (so->type != 'C' && adjust[section(so->type)])
			so->address += adjust[section(so->type)];
		else
			so->keep = 0;  /* do not merge into final map */
	}

	ss->related = ssc;	/* map using adjusted copy */
	return(ssc);
}