File: map.c

package info (click to toggle)
ksymoops 2.3.4-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 248 kB
  • ctags: 209
  • sloc: ansic: 3,621; makefile: 75
file content (270 lines) | stat: -rw-r--r-- 7,999 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
/*
    map.c.

    Read System.map for ksymoops, create merged System.map.

    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>

/* Read the symbols from System.map */
void read_system_map(const char *system_map)
{
    FILE *f;
    char *line = NULL, **string = NULL;
    int i, size = 0;
    static int merged = 0;
    static char const procname[] = "read_system_map";

    if (!system_map)
	return;
    ss_init(&ss_system_map, "System.map");
    DEBUG(1, "%s", system_map);

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

    if (!(f = fopen_local(system_map, "r", procname)))
	return;

    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);
	    /* If the input is a merged system map then it can contain module
	     * data, add these as if they came from ksyms.  The system map is
	     * read last so we can check if there is any other module data,
	     * reading from both a merged map and module data doe snot make
	     * sense.
	     */
	    if (string[5]) {
		if (++merged == 1 && ss_ksyms_modules)
		    ERROR("%s appears to be a merged System.map, it contains [module] names.\n"
			  "\t\tYou also supplied ksyms, this combination makes no sense.\n"
			  "\t\tPlease use -VKLO -m %s.",
			  system_map, system_map);
		add_ksyms(string[1], string[2][0], string[3], string[5]);
	    }
	    else
		add_symbol(&ss_system_map, string[1], *string[2],
		       1, string[3]);
	}
    }

    fclose_local(f, procname);
    re_strings_free(&re_nm, &string);
    free(line);
    if (ss_system_map.used) {
	ss_sort_na(&ss_system_map);
	extract_Version(&ss_system_map);
    }
    else {
	WARNING("no kernel symbols in System.map, is %s a valid System.map "
	        "file?", system_map);
    }

    DEBUG(2, "%s used %d out of %d entries", ss_system_map.source,
        ss_system_map.used, ss_system_map.alloc);
}

/* Compare two maps, all symbols in the first should appear in the second. */
void compare_maps(const SYMBOL_SET *ss1, const SYMBOL_SET *ss2,
	     int precedence)
{
    int i, start = 0;
    SYMBOL *s1, *s2, **sdrop = precedence == 1 ? &s2 : &s1;
    const SYMBOL_SET **ssdrop = precedence == 1 ? &ss2 : &ss1;
    static char const procname[] = "compare_maps";

    if (!(ss1->used && ss2->used))
	return;

    DEBUG(2, "%s vs %s, %s takes precedence", ss1->source, ss2->source,
        precedence == 1 ? ss1->source : ss2->source);

    for (i = 0; i < ss1->used; ++i) {
	s1 = ss1->symbol+i;
	if (!(s1->keep))
	    continue;
	s2 = find_symbol_name(ss2, s1->name, &start);
	if (!s2) {
	    /* Some types only appear in nm output, not in things
	     * like System.map.  Silently ignore them.
	     */
	    if (s1->type == 'a' || s1->type == 't')
		continue;
	    /* modutils assist uses generated symbols */
	    if (s1->type == 'G' || s1->type == 'g')
		continue;
	    WARNING("%s symbol %s not found in %s.  Ignoring %s entry",
		ss1->source, s1->name,
		ss2->source, (*ssdrop)->source);
	    if (*sdrop)
		(*sdrop)->keep = 0;
	}
	else if (s1->address != s2->address) {
	    /* Type C symbols cannot be resolved from nm to ksyms,
	     * silently ignore them.
	     */
	    if (s1->type == 'C' || s2->type == 'C')
		continue;
	    /* Jakub Jelinek says that ___[fbsahi]_.* symbols are relocated at
	     * boot time so silently ignore any mismatches on these symbols.
	     */
	    if (strncmp(s1->name, "___", 3) == 0 &&
	        s1->name[4] == '_' &&
	        (s1->name[3] == 'f' ||
	         s1->name[3] == 'b' ||
	         s1->name[3] == 's' ||
	         s1->name[3] == 'a' ||
	         s1->name[3] == 'h' ||
	         s1->name[3] == 'i'))
		continue;
	    WARNING("mismatch on symbol %s %c, %s says %llx, %s says %llx.  "
		    "Ignoring %s entry",
		s1->name, s1->type, ss1->source, s1->address,
		ss2->source, s2->address, (*ssdrop)->source);
	    if (*sdrop)
		(*sdrop)->keep = 0;
	}
	else
	    ++start;    /* step to next entry in ss2 */
    }
}

/* Append the second symbol set onto the first */
static void append_map(SYMBOL_SET *symbol, const SYMBOL_SET *related, char *module)
{
    int i, symnum;
    SYMBOL *s;
    static char const procname[] = "append_map";

    if (!related || !related->used)
	return;
    DEBUG_S(2, "%s to %s", related->source, symbol->source);
    if (module)
        DEBUG_E(2, " [%s]", module);
    else
        DEBUG_E(2, "");

    for (i = 0; i < related->used; ++i) {
	s = related->symbol+i;
	if (s->keep) {
	    symnum = add_symbol_n(symbol, s->address, s->type, 1, s->name);
	    (symbol->symbol)[symnum].module = module;
        }
    }
}

/* Compare the various sources and build a merged system map */
void merge_maps(const char *save_system_map, const OPTIONS *options)
{
    int i;
    SYMBOL *s;
    FILE *f;
    static char const procname[] = "merge_maps";

    DEBUG(1, "");

    /* Using_Versions only appears in ksyms, copy to other tables */
    if ((s = find_symbol_name(&ss_ksyms_base,
	    "Using_Versions", 0))) {
	if (ss_system_map.used) {
	    add_symbol_n(&ss_system_map, s->address,
		s->type, s->keep, s->name);
	    ss_sort_na(&ss_system_map);
	}
	if (ss_vmlinux.used) {
	    add_symbol_n(&ss_vmlinux, s->address, s->type,
		s->keep, s->name);
	    ss_sort_na(&ss_vmlinux);
	}
    }

    compare_Version();  /* highlight any version problems first */
    compare_ksyms_lsmod();  /* highlight any missing modules next */
    compare_maps(&ss_ksyms_base, &ss_vmlinux, 2);
    compare_maps(&ss_system_map, &ss_vmlinux, 2);
    compare_maps(&ss_vmlinux, &ss_system_map, 1);
    compare_maps(&ss_ksyms_base, &ss_system_map, 2);

    if (ss_objects) {
	map_ksyms_to_modules();
    }

    ss_init(&ss_merged, "merged");
    append_map(&ss_merged, &ss_vmlinux, NULL);
    append_map(&ss_merged, &ss_ksyms_base, NULL);
    append_map(&ss_merged, &ss_system_map, NULL);
    for (i = 0; i < ss_ksyms_modules; ++i)
	append_map(&ss_merged,
	    (ss_ksyms_module[i]->related ?
		ss_ksyms_module[i]->related :
		ss_ksyms_module[i]),
	    ss_ksyms_module[i]->source);
    if (!ss_merged.used)
	WARNING("no symbols in merged map");

    /* drop duplicates, type a (registers), type G (modutils assist) and 
     * gcc2_compiled.
     */
    ss_sort_atn(&ss_merged);
    s = ss_merged.symbol;
    for (i = 0; i < ss_merged.used-1; ++i) {
	if (s->type == 'a' || s->type == 'G' ||
	    (s->type == 't' && !strcmp(s->name, "gcc2_compiled."))) {
	    DEBUG(3, "dropping non duplicate %s %c", s->name, s->type);
	    s->keep = 0;
	}
	else if (strcmp(s->name, (s+1)->name) == 0 &&
	    s->address == (s+1)->address) {
	    if (s->type != ' ')
		(s+1)->keep = 0;
	    else
		s->keep = 0;
	}
	++s;
    }
    ss_sort_atn(&ss_merged);    /* will remove dropped variables */

    if (save_system_map) {
	DEBUG(1, "writing merged map to %s", save_system_map);
	if (!(f = fopen_local(save_system_map, "w", procname)))
	    return;
	s = ss_merged.symbol;
	for (i = 0; i < ss_merged.used; ++i) {
	    if (s->keep) {
		fprintf(f, "%s %c %s",
		    format_address(s->address, options),
		    s->type, s->name);
		    if (s->module)
		        fprintf(f, "\t[%s]", s->module);
		    fprintf(f, "\n");
	    }
	    ++s;
	}
    }

    /* The merged map may contain symbols with an address of 0, e.g.
     * Using_Versions.  These give incorrect results for low addresses in
     * map_address, such addresses map to "Using_Versions+xxx".  Remove
     * any addresses below (arbitrary) 4096 from the merged map.  AFAIK,
     * Linux does not use the first page on any arch.
     */
    for (i = 0; i < ss_merged.used; ++i) {
	if ((ss_merged.symbol+i)->address < 4096) {
	    DEBUG(3, "dropping low address %s %c", s->name, s->type);
	    (ss_merged.symbol+i)->keep = 0;
	}
	else
	    break;
    }
    if (i)
	ss_sort_atn(&ss_merged);    /* remove dropped variables */
}