File: object.c

package info (click to toggle)
ksymoops 2.4.11-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 280 kB
  • ctags: 251
  • sloc: ansic: 4,330; makefile: 117
file content (352 lines) | stat: -rw-r--r-- 9,708 bytes parent folder | download
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
/*
    object.c.

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

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

 */

#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, const OPTIONS *options)
{
    FILE *f;
    char *cmd, *line = NULL, **string = NULL;
    int i, cmd_strlen, size = 0;
    static char const procname[] = "read_nm_symbols";
    static char const nm_options[] = "--target=";

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

    cmd_strlen = strlen(path_nm)+1 + strlen(file)+1;
    if (options->target)
	cmd_strlen += sizeof(nm_options) + strlen(options->target) + 1;
    cmd = malloc(cmd_strlen);
    if (!cmd)
	malloc_error("nm command");
    strcpy(cmd, path_nm);
    strcat(cmd, " ");
    if (options->target) {
	strcat(cmd, nm_options);
	strcat(cmd, options->target);
	strcat(cmd, " ");
    }
    strcat(cmd, file);
    DEBUG(2, "command '%s'", 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);
	DEBUG(4, "regexec %d", 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);
    DEBUG(2, "%s used %d out of %d entries", ss->source, ss->used, ss->alloc);
}

/* Read the symbols from vmlinux */
void read_vmlinux(const OPTIONS *options)
{
    static char procname[] = "read_vmlinux";
    const char *vmlinux = options->vmlinux;
    if (!vmlinux)
	return;
    ss_init(&ss_vmlinux, "vmlinux");
    read_nm_symbols(&ss_vmlinux, vmlinux, options);
    if (ss_vmlinux.used) {
	ss_sort_na(&ss_vmlinux);
	extract_Version(&ss_vmlinux);
    }
    else
	WARNING("no kernel symbols in vmlinux, is %s a valid vmlinux file?",
	    vmlinux);
}

/* Read the symbols from one object (module) */
void read_object(const char *object, int i, const OPTIONS *options)
{
    static char procname[] = "read_object";
    ss_init(ss_object[i], object);
    read_nm_symbols(ss_object[i], object, options);
    if ((ss_object[i])->used) {
	ss_sort_na(ss_object[i]);
	extract_Version(ss_object[i]);
    }
    else
	WARNING("no symbols in %s", object);
}

/* Add a new entry to the list of objects */
static SYMBOL_SET *add_ss_object(const char *file)
{
    struct stat statbuf;
    int i;
    static char const procname[] = "add_ss_object";
    /* Avoid reading any object twice. */
    for (i = 0; i < ss_objects; ++i) {
	if (strcmp((ss_object[i])->source, file) == 0)
	    return(ss_object[i]);
    }
    ++ss_objects;
    ss_object = realloc(ss_object, ss_objects*sizeof(*ss_object));
    if (!ss_object)
	malloc_error("realloc ss_object");
    if (!(ss_object[ss_objects-1] = malloc(sizeof(*ss_object[0]))))
	malloc_error("alloc ss_object[n]");
    ss_init(ss_object[ss_objects-1], file);
    if (stat(file, &statbuf)) {
	ERROR("cannot stat(%s)", file);
	perror(prefix);
    }
    else {
	ss_object[ss_objects-1]->mtime = statbuf.st_mtime;
    }
    return(ss_object[ss_objects-1]);
}

/* 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 '(' -path '*/build' -prune ')' -o -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);
    DEBUG(2, "command '%s'", cmd);
    if (!(f = popen_local(cmd, procname)))
	return;
    free(cmd);

    while (fgets_local(&line, &size, f, procname)) {
	DEBUG(2, "%s", line);
	add_ss_object(line);
	++files;
    }

    pclose_local(f, procname);
    if (!files)
	WARNING("no *.o files in %s.  Is %s a valid module directory?",
	    dir, dir);
}

/* Run the object paths, extracting anything that looks like a module name.
 */
static void run_object_paths(const OPTIONS *options)
{
    struct stat statbuf;
    int i;
    const char *file;
    static char const procname[] = "run_object_paths";
    DEBUG(1, "%s", "");

    for (i = 0; i < options->objects; ++i) {
	file = options->object[i];
	DEBUG_S(2, "checking %s - ", file);
	if (!stat(file, &statbuf) && S_ISDIR(statbuf.st_mode)) {
	    DEBUG_E(2, "%s", "directory, expanding");
	    find_objects(file);
	}
	else {
	    DEBUG_E(2, "%s", "not directory");
	    add_ss_object(file);
	}
    }
}

/* 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.  If a ksyms
 * entry contains the name of its object file, use that by preference.
 * If --ignore-insmod-path is set then use the first object with the
 * same mtime and basename.
 */
void expand_objects(const OPTIONS *options)
{
    struct stat statbuf;
    int i, do_the_lot = 0, run_objects = 0;
    SYMBOL_SET *ss;
    const char *file;
    static char const procname[] = "expand_objects";

    for (i = 0; i < ss_ksyms_modules; ++i) {
	ss = ss_ksyms_module[i];
	if (!ss->object) {
	    do_the_lot = 1;
	    continue;
	}
	file = ss->object;
	if (options->ignore_insmod_path) {
	    /* Use the basename and mtime from insmod */
	    const char *basename = strrchr(file, '/'), *p;
	    int j;
	    if (basename)
		++basename;
	    else
		basename = file;
	    DEBUG(1, "looking for basename %s with mtime %ld for %s",
		basename, ss->mtime, ss->source);
	    if (!run_objects) {
		run_object_paths(options);
		run_objects = 1;
	    }
	    for (j = 0; j < ss_objects; ++j) {
		if (ss_object[j]->mtime == ss->mtime) {
		    p = strrchr(ss_object[j]->source, '/');
		    if (p)
			++p;
		    else
			p = ss_object[j]->source;
		    if (strcmp(p, basename) == 0) {
			DEBUG(1, "found %s for %s", ss_object[j]->source, file);
			free((char *)(ss->object));
			ss->object = strdup(ss_object[j]->source);
			if (!ss->object)
			    malloc_error(procname);
			file = ss->object;
			ss->related = add_ss_object(file);
			break;
		    }
		}
	    }
	    if (j == ss_objects) {
		DEBUG(1, "no match on basename %s with mtime %ld", basename, ss->mtime);
		do_the_lot = 1;
		continue;
	    }
	}
	else {
	    /* Use the full pathname from insmod */
	    DEBUG(1, "using %s for %s", file, ss->source);
	    if (stat(ss->object, &statbuf)) {
		ERROR("cannot stat(%s) for %s", file, ss->source);
		perror(prefix);
		do_the_lot = 1;
		continue;
	    }
	    if (statbuf.st_mtime != ss->mtime)
		WARNING("object %s for module %s has changed since load",
		    file, ss->source);
	    ss->related = add_ss_object(file);
	}
    }

    if (!do_the_lot) {
	DEBUG(1, "%s", "all ksyms modules map to specific object files");
	return;
    }
    DEBUG(1, "%s", "missing or mismatched modutils assists, doing it the hard way");
    if (!run_objects) {
	run_object_paths(options);
	run_objects = 1;
    }
}

/* Map a symbol type to a section code. 0 - text, 1 - data, 2 - read only data,
 * 3 - bss, 4 - sbss, 5 - C (cannot relocate), 6 - the rest.
 */
static int section(char type)
{
    switch (type) {
    case 'T':
    case 't':
    case 'W':		/* Resolved weak reference to text */
	return 0;
    case 'D':
    case 'd':
	return 1;
    case 'R':
    case 'r':
	return 2;
    case 'B':
    case 'b':
	return 3;
    case 'S':
    case 's':
	return 4;
    case 'C':
	return 5;
    default:
	return 6;
    }
}

/* 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;
    addr_t adjust[] = {0, 0, 0, 0, 0, 0, 0};
    char type;
    SYMBOL *sk, *so;
    SYMBOL_SET *ssc;
    static char const procname[] = "adjust_object_offsets";

    ssc = ss_copy(ss->related);

    /* For common symbols, calculate the adjustment */
    for (i = 0; i < ss->used; ++i) {
	sk = ss->symbol+i;
	/* modutils assists have type G, the symbol contains the section name */
	if (sk->type == 'G') {
	    if (strstr(sk->name, "_S.text_"))
		type = 'T';
	    else if (strstr(sk->name, "_S.rodata_"))
		type = 'R';
	    else if (strstr(sk->name, "_S.data_"))
		type = 'D';
	    else if (strstr(sk->name, "_S.bss_"))
		type = 'B';
	    else if (strstr(sk->name, "_S.sbss_"))
		type = 'S';
	    else
		type = ' ';
	    /* Assumption: all sections start at relative offset 0 */
	    adjust[section(type)] = sk->address;
	}
	else if ((so = find_symbol_name(ssc, sk->name, NULL))) {
	    if (!adjust[section(so->type)])
		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 */
    }
    /* Copy modutils assist symbols to object symbol table */
    for (i = 0; i < ss->used; ++i) {
	sk = ss->symbol+i;
	if (sk->type == 'G' || sk->type == 'g')
	    add_symbol_n(ssc, sk->address, sk->type, 1, sk->name);
    }
    ss->related = ssc;  /* map using adjusted copy */
    DEBUG(2, "%s text %llx rodata %llx data %llx bss %llx", ss->source, adjust[0], adjust[1], adjust[2], adjust[3]);
    return(ssc);
}