Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Postprocess module symbol versions |
2 | * | |
3 | * Copyright 2003 Kai Germaschewski | |
4 | * Copyright 2002-2004 Rusty Russell, IBM Corporation | |
382168f4 | 5 | * Copyright 2006 Sam Ravnborg |
1da177e4 LT |
6 | * Based in part on module-init-tools/depmod.c,file2alias |
7 | * | |
8 | * This software may be used and distributed according to the terms | |
9 | * of the GNU General Public License, incorporated herein by reference. | |
10 | * | |
11 | * Usage: modpost vmlinux module1.o module2.o ... | |
12 | */ | |
13 | ||
14 | #include <ctype.h> | |
15 | #include "modpost.h" | |
b817f6fe | 16 | #include "../../include/linux/license.h" |
1da177e4 LT |
17 | |
18 | /* Are we using CONFIG_MODVERSIONS? */ | |
19 | int modversions = 0; | |
20 | /* Warn about undefined symbols? (do so if we have vmlinux) */ | |
21 | int have_vmlinux = 0; | |
22 | /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ | |
23 | static int all_versions = 0; | |
040fcc81 SR |
24 | /* If we are modposting external module set to 1 */ |
25 | static int external_module = 0; | |
c53ddacd KK |
26 | /* Only warn about unresolved symbols */ |
27 | static int warn_unresolved = 0; | |
bd5cbced | 28 | /* How a symbol is exported */ |
c96fca21 SR |
29 | enum export { |
30 | export_plain, export_unused, export_gpl, | |
31 | export_unused_gpl, export_gpl_future, export_unknown | |
32 | }; | |
1da177e4 | 33 | |
5c3ead8c | 34 | void fatal(const char *fmt, ...) |
1da177e4 LT |
35 | { |
36 | va_list arglist; | |
37 | ||
38 | fprintf(stderr, "FATAL: "); | |
39 | ||
40 | va_start(arglist, fmt); | |
41 | vfprintf(stderr, fmt, arglist); | |
42 | va_end(arglist); | |
43 | ||
44 | exit(1); | |
45 | } | |
46 | ||
5c3ead8c | 47 | void warn(const char *fmt, ...) |
1da177e4 LT |
48 | { |
49 | va_list arglist; | |
50 | ||
51 | fprintf(stderr, "WARNING: "); | |
52 | ||
53 | va_start(arglist, fmt); | |
54 | vfprintf(stderr, fmt, arglist); | |
55 | va_end(arglist); | |
56 | } | |
57 | ||
040fcc81 SR |
58 | static int is_vmlinux(const char *modname) |
59 | { | |
60 | const char *myname; | |
61 | ||
62 | if ((myname = strrchr(modname, '/'))) | |
63 | myname++; | |
64 | else | |
65 | myname = modname; | |
66 | ||
67 | return strcmp(myname, "vmlinux") == 0; | |
68 | } | |
69 | ||
1da177e4 LT |
70 | void *do_nofail(void *ptr, const char *expr) |
71 | { | |
72 | if (!ptr) { | |
73 | fatal("modpost: Memory allocation failure: %s.\n", expr); | |
74 | } | |
75 | return ptr; | |
76 | } | |
77 | ||
78 | /* A list of all modules we processed */ | |
79 | ||
80 | static struct module *modules; | |
81 | ||
5c3ead8c | 82 | static struct module *find_module(char *modname) |
1da177e4 LT |
83 | { |
84 | struct module *mod; | |
85 | ||
86 | for (mod = modules; mod; mod = mod->next) | |
87 | if (strcmp(mod->name, modname) == 0) | |
88 | break; | |
89 | return mod; | |
90 | } | |
91 | ||
5c3ead8c | 92 | static struct module *new_module(char *modname) |
1da177e4 LT |
93 | { |
94 | struct module *mod; | |
95 | char *p, *s; | |
62070fa4 | 96 | |
1da177e4 LT |
97 | mod = NOFAIL(malloc(sizeof(*mod))); |
98 | memset(mod, 0, sizeof(*mod)); | |
99 | p = NOFAIL(strdup(modname)); | |
100 | ||
101 | /* strip trailing .o */ | |
102 | if ((s = strrchr(p, '.')) != NULL) | |
103 | if (strcmp(s, ".o") == 0) | |
104 | *s = '\0'; | |
105 | ||
106 | /* add to list */ | |
107 | mod->name = p; | |
b817f6fe | 108 | mod->gpl_compatible = -1; |
1da177e4 LT |
109 | mod->next = modules; |
110 | modules = mod; | |
111 | ||
112 | return mod; | |
113 | } | |
114 | ||
115 | /* A hash of all exported symbols, | |
116 | * struct symbol is also used for lists of unresolved symbols */ | |
117 | ||
118 | #define SYMBOL_HASH_SIZE 1024 | |
119 | ||
120 | struct symbol { | |
121 | struct symbol *next; | |
122 | struct module *module; | |
123 | unsigned int crc; | |
124 | int crc_valid; | |
125 | unsigned int weak:1; | |
040fcc81 SR |
126 | unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ |
127 | unsigned int kernel:1; /* 1 if symbol is from kernel | |
128 | * (only for external modules) **/ | |
8e70c458 | 129 | unsigned int preloaded:1; /* 1 if symbol from Module.symvers */ |
bd5cbced | 130 | enum export export; /* Type of export */ |
1da177e4 LT |
131 | char name[0]; |
132 | }; | |
133 | ||
134 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; | |
135 | ||
136 | /* This is based on the hash agorithm from gdbm, via tdb */ | |
137 | static inline unsigned int tdb_hash(const char *name) | |
138 | { | |
139 | unsigned value; /* Used to compute the hash value. */ | |
140 | unsigned i; /* Used to cycle through random values. */ | |
141 | ||
142 | /* Set the initial value from the key size. */ | |
143 | for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++) | |
144 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); | |
145 | ||
146 | return (1103515243 * value + 12345); | |
147 | } | |
148 | ||
5c3ead8c SR |
149 | /** |
150 | * Allocate a new symbols for use in the hash of exported symbols or | |
151 | * the list of unresolved symbols per module | |
152 | **/ | |
153 | static struct symbol *alloc_symbol(const char *name, unsigned int weak, | |
154 | struct symbol *next) | |
1da177e4 LT |
155 | { |
156 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); | |
157 | ||
158 | memset(s, 0, sizeof(*s)); | |
159 | strcpy(s->name, name); | |
160 | s->weak = weak; | |
161 | s->next = next; | |
162 | return s; | |
163 | } | |
164 | ||
165 | /* For the hash of exported symbols */ | |
bd5cbced RP |
166 | static struct symbol *new_symbol(const char *name, struct module *module, |
167 | enum export export) | |
1da177e4 LT |
168 | { |
169 | unsigned int hash; | |
170 | struct symbol *new; | |
171 | ||
172 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; | |
173 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); | |
174 | new->module = module; | |
bd5cbced | 175 | new->export = export; |
040fcc81 | 176 | return new; |
1da177e4 LT |
177 | } |
178 | ||
5c3ead8c | 179 | static struct symbol *find_symbol(const char *name) |
1da177e4 LT |
180 | { |
181 | struct symbol *s; | |
182 | ||
183 | /* For our purposes, .foo matches foo. PPC64 needs this. */ | |
184 | if (name[0] == '.') | |
185 | name++; | |
186 | ||
187 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) { | |
188 | if (strcmp(s->name, name) == 0) | |
189 | return s; | |
190 | } | |
191 | return NULL; | |
192 | } | |
193 | ||
bd5cbced RP |
194 | static struct { |
195 | const char *str; | |
196 | enum export export; | |
197 | } export_list[] = { | |
198 | { .str = "EXPORT_SYMBOL", .export = export_plain }, | |
c96fca21 | 199 | { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused }, |
bd5cbced | 200 | { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl }, |
c96fca21 | 201 | { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl }, |
bd5cbced RP |
202 | { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future }, |
203 | { .str = "(unknown)", .export = export_unknown }, | |
204 | }; | |
205 | ||
206 | ||
207 | static const char *export_str(enum export ex) | |
208 | { | |
209 | return export_list[ex].str; | |
210 | } | |
211 | ||
212 | static enum export export_no(const char * s) | |
213 | { | |
214 | int i; | |
534b89a9 SR |
215 | if (!s) |
216 | return export_unknown; | |
bd5cbced RP |
217 | for (i = 0; export_list[i].export != export_unknown; i++) { |
218 | if (strcmp(export_list[i].str, s) == 0) | |
219 | return export_list[i].export; | |
220 | } | |
221 | return export_unknown; | |
222 | } | |
223 | ||
224 | static enum export export_from_sec(struct elf_info *elf, Elf_Section sec) | |
225 | { | |
226 | if (sec == elf->export_sec) | |
227 | return export_plain; | |
c96fca21 SR |
228 | else if (sec == elf->export_unused_sec) |
229 | return export_unused; | |
bd5cbced RP |
230 | else if (sec == elf->export_gpl_sec) |
231 | return export_gpl; | |
c96fca21 SR |
232 | else if (sec == elf->export_unused_gpl_sec) |
233 | return export_unused_gpl; | |
bd5cbced RP |
234 | else if (sec == elf->export_gpl_future_sec) |
235 | return export_gpl_future; | |
236 | else | |
237 | return export_unknown; | |
238 | } | |
239 | ||
5c3ead8c SR |
240 | /** |
241 | * Add an exported symbol - it may have already been added without a | |
242 | * CRC, in this case just update the CRC | |
243 | **/ | |
bd5cbced RP |
244 | static struct symbol *sym_add_exported(const char *name, struct module *mod, |
245 | enum export export) | |
1da177e4 LT |
246 | { |
247 | struct symbol *s = find_symbol(name); | |
248 | ||
249 | if (!s) { | |
bd5cbced | 250 | s = new_symbol(name, mod, export); |
8e70c458 SR |
251 | } else { |
252 | if (!s->preloaded) { | |
7b75b13c | 253 | warn("%s: '%s' exported twice. Previous export " |
8e70c458 SR |
254 | "was in %s%s\n", mod->name, name, |
255 | s->module->name, | |
256 | is_vmlinux(s->module->name) ?"":".ko"); | |
257 | } | |
1da177e4 | 258 | } |
8e70c458 | 259 | s->preloaded = 0; |
040fcc81 SR |
260 | s->vmlinux = is_vmlinux(mod->name); |
261 | s->kernel = 0; | |
bd5cbced | 262 | s->export = export; |
040fcc81 SR |
263 | return s; |
264 | } | |
265 | ||
266 | static void sym_update_crc(const char *name, struct module *mod, | |
bd5cbced | 267 | unsigned int crc, enum export export) |
040fcc81 SR |
268 | { |
269 | struct symbol *s = find_symbol(name); | |
270 | ||
271 | if (!s) | |
bd5cbced | 272 | s = new_symbol(name, mod, export); |
040fcc81 SR |
273 | s->crc = crc; |
274 | s->crc_valid = 1; | |
1da177e4 LT |
275 | } |
276 | ||
5c3ead8c | 277 | void *grab_file(const char *filename, unsigned long *size) |
1da177e4 LT |
278 | { |
279 | struct stat st; | |
280 | void *map; | |
281 | int fd; | |
282 | ||
283 | fd = open(filename, O_RDONLY); | |
284 | if (fd < 0 || fstat(fd, &st) != 0) | |
285 | return NULL; | |
286 | ||
287 | *size = st.st_size; | |
288 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); | |
289 | close(fd); | |
290 | ||
291 | if (map == MAP_FAILED) | |
292 | return NULL; | |
293 | return map; | |
294 | } | |
295 | ||
5c3ead8c SR |
296 | /** |
297 | * Return a copy of the next line in a mmap'ed file. | |
298 | * spaces in the beginning of the line is trimmed away. | |
299 | * Return a pointer to a static buffer. | |
300 | **/ | |
301 | char* get_next_line(unsigned long *pos, void *file, unsigned long size) | |
1da177e4 LT |
302 | { |
303 | static char line[4096]; | |
304 | int skip = 1; | |
305 | size_t len = 0; | |
306 | signed char *p = (signed char *)file + *pos; | |
307 | char *s = line; | |
308 | ||
309 | for (; *pos < size ; (*pos)++) | |
310 | { | |
311 | if (skip && isspace(*p)) { | |
312 | p++; | |
313 | continue; | |
314 | } | |
315 | skip = 0; | |
316 | if (*p != '\n' && (*pos < size)) { | |
317 | len++; | |
318 | *s++ = *p++; | |
319 | if (len > 4095) | |
320 | break; /* Too long, stop */ | |
321 | } else { | |
322 | /* End of string */ | |
323 | *s = '\0'; | |
324 | return line; | |
325 | } | |
326 | } | |
327 | /* End of buffer */ | |
328 | return NULL; | |
329 | } | |
330 | ||
5c3ead8c | 331 | void release_file(void *file, unsigned long size) |
1da177e4 LT |
332 | { |
333 | munmap(file, size); | |
334 | } | |
335 | ||
5c3ead8c | 336 | static void parse_elf(struct elf_info *info, const char *filename) |
1da177e4 LT |
337 | { |
338 | unsigned int i; | |
339 | Elf_Ehdr *hdr = info->hdr; | |
340 | Elf_Shdr *sechdrs; | |
341 | Elf_Sym *sym; | |
342 | ||
343 | hdr = grab_file(filename, &info->size); | |
344 | if (!hdr) { | |
345 | perror(filename); | |
6803dc0e | 346 | exit(1); |
1da177e4 LT |
347 | } |
348 | info->hdr = hdr; | |
349 | if (info->size < sizeof(*hdr)) | |
350 | goto truncated; | |
351 | ||
352 | /* Fix endianness in ELF header */ | |
353 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); | |
354 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); | |
355 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); | |
356 | hdr->e_machine = TO_NATIVE(hdr->e_machine); | |
357 | sechdrs = (void *)hdr + hdr->e_shoff; | |
358 | info->sechdrs = sechdrs; | |
359 | ||
360 | /* Fix endianness in section headers */ | |
361 | for (i = 0; i < hdr->e_shnum; i++) { | |
362 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); | |
363 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); | |
364 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); | |
365 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); | |
366 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); | |
367 | } | |
368 | /* Find symbol table. */ | |
369 | for (i = 1; i < hdr->e_shnum; i++) { | |
370 | const char *secstrings | |
371 | = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; | |
bd5cbced | 372 | const char *secname; |
1da177e4 LT |
373 | |
374 | if (sechdrs[i].sh_offset > info->size) | |
375 | goto truncated; | |
bd5cbced RP |
376 | secname = secstrings + sechdrs[i].sh_name; |
377 | if (strcmp(secname, ".modinfo") == 0) { | |
1da177e4 LT |
378 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
379 | info->modinfo_len = sechdrs[i].sh_size; | |
bd5cbced RP |
380 | } else if (strcmp(secname, "__ksymtab") == 0) |
381 | info->export_sec = i; | |
c96fca21 SR |
382 | else if (strcmp(secname, "__ksymtab_unused") == 0) |
383 | info->export_unused_sec = i; | |
bd5cbced RP |
384 | else if (strcmp(secname, "__ksymtab_gpl") == 0) |
385 | info->export_gpl_sec = i; | |
c96fca21 SR |
386 | else if (strcmp(secname, "__ksymtab_unused_gpl") == 0) |
387 | info->export_unused_gpl_sec = i; | |
bd5cbced RP |
388 | else if (strcmp(secname, "__ksymtab_gpl_future") == 0) |
389 | info->export_gpl_future_sec = i; | |
390 | ||
1da177e4 LT |
391 | if (sechdrs[i].sh_type != SHT_SYMTAB) |
392 | continue; | |
393 | ||
394 | info->symtab_start = (void *)hdr + sechdrs[i].sh_offset; | |
62070fa4 | 395 | info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset |
1da177e4 | 396 | + sechdrs[i].sh_size; |
62070fa4 | 397 | info->strtab = (void *)hdr + |
1da177e4 LT |
398 | sechdrs[sechdrs[i].sh_link].sh_offset; |
399 | } | |
400 | if (!info->symtab_start) { | |
cb80514d | 401 | fatal("%s has no symtab?\n", filename); |
1da177e4 LT |
402 | } |
403 | /* Fix endianness in symbols */ | |
404 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { | |
405 | sym->st_shndx = TO_NATIVE(sym->st_shndx); | |
406 | sym->st_name = TO_NATIVE(sym->st_name); | |
407 | sym->st_value = TO_NATIVE(sym->st_value); | |
408 | sym->st_size = TO_NATIVE(sym->st_size); | |
409 | } | |
410 | return; | |
411 | ||
412 | truncated: | |
cb80514d | 413 | fatal("%s is truncated.\n", filename); |
1da177e4 LT |
414 | } |
415 | ||
5c3ead8c | 416 | static void parse_elf_finish(struct elf_info *info) |
1da177e4 LT |
417 | { |
418 | release_file(info->hdr, info->size); | |
419 | } | |
420 | ||
f7b05e64 LY |
421 | #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_" |
422 | #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_" | |
1da177e4 | 423 | |
5c3ead8c SR |
424 | static void handle_modversions(struct module *mod, struct elf_info *info, |
425 | Elf_Sym *sym, const char *symname) | |
1da177e4 LT |
426 | { |
427 | unsigned int crc; | |
bd5cbced | 428 | enum export export = export_from_sec(info, sym->st_shndx); |
1da177e4 LT |
429 | |
430 | switch (sym->st_shndx) { | |
431 | case SHN_COMMON: | |
cb80514d | 432 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); |
1da177e4 LT |
433 | break; |
434 | case SHN_ABS: | |
435 | /* CRC'd symbol */ | |
436 | if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) { | |
437 | crc = (unsigned int) sym->st_value; | |
bd5cbced RP |
438 | sym_update_crc(symname + strlen(CRC_PFX), mod, crc, |
439 | export); | |
1da177e4 LT |
440 | } |
441 | break; | |
442 | case SHN_UNDEF: | |
443 | /* undefined symbol */ | |
444 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && | |
445 | ELF_ST_BIND(sym->st_info) != STB_WEAK) | |
446 | break; | |
447 | /* ignore global offset table */ | |
448 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) | |
449 | break; | |
450 | /* ignore __this_module, it will be resolved shortly */ | |
451 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0) | |
452 | break; | |
8d529014 BC |
453 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
454 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) | |
455 | /* add compatibility with older glibc */ | |
456 | #ifndef STT_SPARC_REGISTER | |
457 | #define STT_SPARC_REGISTER STT_REGISTER | |
458 | #endif | |
1da177e4 LT |
459 | if (info->hdr->e_machine == EM_SPARC || |
460 | info->hdr->e_machine == EM_SPARCV9) { | |
461 | /* Ignore register directives. */ | |
8d529014 | 462 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
1da177e4 | 463 | break; |
62070fa4 SR |
464 | if (symname[0] == '.') { |
465 | char *munged = strdup(symname); | |
466 | munged[0] = '_'; | |
467 | munged[1] = toupper(munged[1]); | |
468 | symname = munged; | |
469 | } | |
1da177e4 LT |
470 | } |
471 | #endif | |
62070fa4 | 472 | |
1da177e4 LT |
473 | if (memcmp(symname, MODULE_SYMBOL_PREFIX, |
474 | strlen(MODULE_SYMBOL_PREFIX)) == 0) | |
475 | mod->unres = alloc_symbol(symname + | |
476 | strlen(MODULE_SYMBOL_PREFIX), | |
477 | ELF_ST_BIND(sym->st_info) == STB_WEAK, | |
478 | mod->unres); | |
479 | break; | |
480 | default: | |
481 | /* All exported symbols */ | |
482 | if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) { | |
bd5cbced RP |
483 | sym_add_exported(symname + strlen(KSYMTAB_PFX), mod, |
484 | export); | |
1da177e4 LT |
485 | } |
486 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0) | |
487 | mod->has_init = 1; | |
488 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0) | |
489 | mod->has_cleanup = 1; | |
490 | break; | |
491 | } | |
492 | } | |
493 | ||
5c3ead8c SR |
494 | /** |
495 | * Parse tag=value strings from .modinfo section | |
496 | **/ | |
1da177e4 LT |
497 | static char *next_string(char *string, unsigned long *secsize) |
498 | { | |
499 | /* Skip non-zero chars */ | |
500 | while (string[0]) { | |
501 | string++; | |
502 | if ((*secsize)-- <= 1) | |
503 | return NULL; | |
504 | } | |
505 | ||
506 | /* Skip any zero padding. */ | |
507 | while (!string[0]) { | |
508 | string++; | |
509 | if ((*secsize)-- <= 1) | |
510 | return NULL; | |
511 | } | |
512 | return string; | |
513 | } | |
514 | ||
b817f6fe SR |
515 | static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len, |
516 | const char *tag, char *info) | |
1da177e4 LT |
517 | { |
518 | char *p; | |
519 | unsigned int taglen = strlen(tag); | |
520 | unsigned long size = modinfo_len; | |
521 | ||
b817f6fe SR |
522 | if (info) { |
523 | size -= info - (char *)modinfo; | |
524 | modinfo = next_string(info, &size); | |
525 | } | |
526 | ||
1da177e4 LT |
527 | for (p = modinfo; p; p = next_string(p, &size)) { |
528 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') | |
529 | return p + taglen + 1; | |
530 | } | |
531 | return NULL; | |
532 | } | |
533 | ||
b817f6fe SR |
534 | static char *get_modinfo(void *modinfo, unsigned long modinfo_len, |
535 | const char *tag) | |
536 | ||
537 | { | |
538 | return get_next_modinfo(modinfo, modinfo_len, tag, NULL); | |
539 | } | |
540 | ||
4c8fbca5 SR |
541 | /** |
542 | * Test if string s ends in string sub | |
543 | * return 0 if match | |
544 | **/ | |
545 | static int strrcmp(const char *s, const char *sub) | |
546 | { | |
547 | int slen, sublen; | |
62070fa4 | 548 | |
4c8fbca5 SR |
549 | if (!s || !sub) |
550 | return 1; | |
62070fa4 | 551 | |
4c8fbca5 SR |
552 | slen = strlen(s); |
553 | sublen = strlen(sub); | |
62070fa4 | 554 | |
4c8fbca5 SR |
555 | if ((slen == 0) || (sublen == 0)) |
556 | return 1; | |
557 | ||
558 | if (sublen > slen) | |
559 | return 1; | |
560 | ||
561 | return memcmp(s + slen - sublen, sub, sublen); | |
562 | } | |
563 | ||
564 | /** | |
565 | * Whitelist to allow certain references to pass with no warning. | |
566 | * Pattern 1: | |
567 | * If a module parameter is declared __initdata and permissions=0 | |
568 | * then this is legal despite the warning generated. | |
569 | * We cannot see value of permissions here, so just ignore | |
570 | * this pattern. | |
571 | * The pattern is identified by: | |
572 | * tosec = .init.data | |
9209aed0 | 573 | * fromsec = .data* |
4c8fbca5 | 574 | * atsym =__param* |
62070fa4 | 575 | * |
4c8fbca5 | 576 | * Pattern 2: |
72ee59b5 | 577 | * Many drivers utilise a *driver container with references to |
4c8fbca5 SR |
578 | * add, remove, probe functions etc. |
579 | * These functions may often be marked __init and we do not want to | |
580 | * warn here. | |
581 | * the pattern is identified by: | |
5ecdd0f6 | 582 | * tosec = .init.text | .exit.text | .init.data |
4c8fbca5 | 583 | * fromsec = .data |
72ee59b5 | 584 | * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one |
ee6a8545 VG |
585 | * |
586 | * Pattern 3: | |
587 | * Some symbols belong to init section but still it is ok to reference | |
588 | * these from non-init sections as these symbols don't have any memory | |
589 | * allocated for them and symbol address and value are same. So even | |
590 | * if init section is freed, its ok to reference those symbols. | |
591 | * For ex. symbols marking the init section boundaries. | |
592 | * This pattern is identified by | |
593 | * refsymname = __init_begin, _sinittext, _einittext | |
4c8fbca5 | 594 | **/ |
9e157a5a | 595 | static int secref_whitelist(const char *modname, const char *tosec, |
ee6a8545 VG |
596 | const char *fromsec, const char *atsym, |
597 | const char *refsymname) | |
4c8fbca5 SR |
598 | { |
599 | int f1 = 1, f2 = 1; | |
600 | const char **s; | |
601 | const char *pat2sym[] = { | |
72ee59b5 | 602 | "driver", |
5ecdd0f6 SR |
603 | "_template", /* scsi uses *_template a lot */ |
604 | "_sht", /* scsi also used *_sht to some extent */ | |
4c8fbca5 SR |
605 | "_ops", |
606 | "_probe", | |
607 | "_probe_one", | |
118c0ace | 608 | "_console", |
4c8fbca5 SR |
609 | NULL |
610 | }; | |
62070fa4 | 611 | |
ee6a8545 VG |
612 | const char *pat3refsym[] = { |
613 | "__init_begin", | |
614 | "_sinittext", | |
615 | "_einittext", | |
616 | NULL | |
617 | }; | |
618 | ||
4c8fbca5 SR |
619 | /* Check for pattern 1 */ |
620 | if (strcmp(tosec, ".init.data") != 0) | |
621 | f1 = 0; | |
9209aed0 | 622 | if (strncmp(fromsec, ".data", strlen(".data")) != 0) |
4c8fbca5 SR |
623 | f1 = 0; |
624 | if (strncmp(atsym, "__param", strlen("__param")) != 0) | |
625 | f1 = 0; | |
626 | ||
627 | if (f1) | |
628 | return f1; | |
629 | ||
630 | /* Check for pattern 2 */ | |
62070fa4 | 631 | if ((strcmp(tosec, ".init.text") != 0) && |
5ecdd0f6 SR |
632 | (strcmp(tosec, ".exit.text") != 0) && |
633 | (strcmp(tosec, ".init.data") != 0)) | |
4c8fbca5 SR |
634 | f2 = 0; |
635 | if (strcmp(fromsec, ".data") != 0) | |
636 | f2 = 0; | |
637 | ||
638 | for (s = pat2sym; *s; s++) | |
639 | if (strrcmp(atsym, *s) == 0) | |
640 | f1 = 1; | |
9e157a5a MD |
641 | if (f1 && f2) |
642 | return 1; | |
4c8fbca5 | 643 | |
f8657e1b VG |
644 | /* Whitelist all references from .pci_fixup section if vmlinux |
645 | * Whitelist all refereces from .text.head to .init.data if vmlinux | |
646 | * Whitelist all refereces from .text.head to .init.text if vmlinux | |
647 | */ | |
9e157a5a MD |
648 | if (is_vmlinux(modname)) { |
649 | if ((strcmp(fromsec, ".pci_fixup") == 0) && | |
650 | (strcmp(tosec, ".init.text") == 0)) | |
651 | return 1; | |
ee6a8545 | 652 | |
f8657e1b VG |
653 | if ((strcmp(fromsec, ".text.head") == 0) && |
654 | ((strcmp(tosec, ".init.data") == 0) || | |
655 | (strcmp(tosec, ".init.text") == 0))) | |
656 | return 1; | |
657 | ||
ee6a8545 VG |
658 | /* Check for pattern 3 */ |
659 | for (s = pat3refsym; *s; s++) | |
660 | if (strcmp(refsymname, *s) == 0) | |
661 | return 1; | |
9e157a5a | 662 | } |
93659af1 | 663 | return 0; |
4c8fbca5 SR |
664 | } |
665 | ||
93684d3b SR |
666 | /** |
667 | * Find symbol based on relocation record info. | |
668 | * In some cases the symbol supplied is a valid symbol so | |
669 | * return refsym. If st_name != 0 we assume this is a valid symbol. | |
670 | * In other cases the symbol needs to be looked up in the symbol table | |
671 | * based on section and address. | |
672 | * **/ | |
673 | static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr, | |
674 | Elf_Sym *relsym) | |
675 | { | |
676 | Elf_Sym *sym; | |
677 | ||
678 | if (relsym->st_name != 0) | |
679 | return relsym; | |
680 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { | |
681 | if (sym->st_shndx != relsym->st_shndx) | |
682 | continue; | |
683 | if (sym->st_value == addr) | |
684 | return sym; | |
685 | } | |
686 | return NULL; | |
687 | } | |
688 | ||
b39927cf | 689 | /* |
43c74d17 SR |
690 | * Find symbols before or equal addr and after addr - in the section sec. |
691 | * If we find two symbols with equal offset prefer one with a valid name. | |
692 | * The ELF format may have a better way to detect what type of symbol | |
693 | * it is, but this works for now. | |
b39927cf SR |
694 | **/ |
695 | static void find_symbols_between(struct elf_info *elf, Elf_Addr addr, | |
696 | const char *sec, | |
697 | Elf_Sym **before, Elf_Sym **after) | |
698 | { | |
699 | Elf_Sym *sym; | |
700 | Elf_Ehdr *hdr = elf->hdr; | |
701 | Elf_Addr beforediff = ~0; | |
702 | Elf_Addr afterdiff = ~0; | |
703 | const char *secstrings = (void *)hdr + | |
704 | elf->sechdrs[hdr->e_shstrndx].sh_offset; | |
62070fa4 | 705 | |
b39927cf SR |
706 | *before = NULL; |
707 | *after = NULL; | |
708 | ||
709 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { | |
710 | const char *symsec; | |
711 | ||
712 | if (sym->st_shndx >= SHN_LORESERVE) | |
713 | continue; | |
714 | symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name; | |
715 | if (strcmp(symsec, sec) != 0) | |
716 | continue; | |
717 | if (sym->st_value <= addr) { | |
718 | if ((addr - sym->st_value) < beforediff) { | |
719 | beforediff = addr - sym->st_value; | |
720 | *before = sym; | |
721 | } | |
43c74d17 SR |
722 | else if ((addr - sym->st_value) == beforediff) { |
723 | /* equal offset, valid name? */ | |
724 | const char *name = elf->strtab + sym->st_name; | |
725 | if (name && strlen(name)) | |
726 | *before = sym; | |
727 | } | |
b39927cf SR |
728 | } |
729 | else | |
730 | { | |
731 | if ((sym->st_value - addr) < afterdiff) { | |
732 | afterdiff = sym->st_value - addr; | |
733 | *after = sym; | |
734 | } | |
43c74d17 SR |
735 | else if ((sym->st_value - addr) == afterdiff) { |
736 | /* equal offset, valid name? */ | |
737 | const char *name = elf->strtab + sym->st_name; | |
738 | if (name && strlen(name)) | |
739 | *after = sym; | |
740 | } | |
b39927cf SR |
741 | } |
742 | } | |
743 | } | |
744 | ||
745 | /** | |
746 | * Print a warning about a section mismatch. | |
747 | * Try to find symbols near it so user can find it. | |
4c8fbca5 | 748 | * Check whitelist before warning - it may be a false positive. |
b39927cf SR |
749 | **/ |
750 | static void warn_sec_mismatch(const char *modname, const char *fromsec, | |
751 | struct elf_info *elf, Elf_Sym *sym, Elf_Rela r) | |
752 | { | |
93684d3b SR |
753 | const char *refsymname = ""; |
754 | Elf_Sym *before, *after; | |
755 | Elf_Sym *refsym; | |
b39927cf SR |
756 | Elf_Ehdr *hdr = elf->hdr; |
757 | Elf_Shdr *sechdrs = elf->sechdrs; | |
758 | const char *secstrings = (void *)hdr + | |
759 | sechdrs[hdr->e_shstrndx].sh_offset; | |
760 | const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name; | |
62070fa4 | 761 | |
b39927cf SR |
762 | find_symbols_between(elf, r.r_offset, fromsec, &before, &after); |
763 | ||
93684d3b SR |
764 | refsym = find_elf_symbol(elf, r.r_addend, sym); |
765 | if (refsym && strlen(elf->strtab + refsym->st_name)) | |
766 | refsymname = elf->strtab + refsym->st_name; | |
4c8fbca5 SR |
767 | |
768 | /* check whitelist - we may ignore it */ | |
62070fa4 | 769 | if (before && |
9e157a5a | 770 | secref_whitelist(modname, secname, fromsec, |
ee6a8545 | 771 | elf->strtab + before->st_name, refsymname)) |
4c8fbca5 | 772 | return; |
62070fa4 | 773 | |
b39927cf | 774 | if (before && after) { |
93684d3b SR |
775 | warn("%s - Section mismatch: reference to %s:%s from %s " |
776 | "between '%s' (at offset 0x%llx) and '%s'\n", | |
777 | modname, secname, refsymname, fromsec, | |
b39927cf | 778 | elf->strtab + before->st_name, |
93684d3b | 779 | (long long)r.r_offset, |
b39927cf SR |
780 | elf->strtab + after->st_name); |
781 | } else if (before) { | |
93684d3b SR |
782 | warn("%s - Section mismatch: reference to %s:%s from %s " |
783 | "after '%s' (at offset 0x%llx)\n", | |
62070fa4 | 784 | modname, secname, refsymname, fromsec, |
b39927cf | 785 | elf->strtab + before->st_name, |
93684d3b | 786 | (long long)r.r_offset); |
b39927cf | 787 | } else if (after) { |
93684d3b SR |
788 | warn("%s - Section mismatch: reference to %s:%s from %s " |
789 | "before '%s' (at offset -0x%llx)\n", | |
62070fa4 | 790 | modname, secname, refsymname, fromsec, |
eaaae38c | 791 | elf->strtab + after->st_name, |
93684d3b | 792 | (long long)r.r_offset); |
b39927cf | 793 | } else { |
93684d3b SR |
794 | warn("%s - Section mismatch: reference to %s:%s from %s " |
795 | "(offset 0x%llx)\n", | |
796 | modname, secname, fromsec, refsymname, | |
797 | (long long)r.r_offset); | |
b39927cf SR |
798 | } |
799 | } | |
800 | ||
801 | /** | |
802 | * A module includes a number of sections that are discarded | |
803 | * either when loaded or when used as built-in. | |
804 | * For loaded modules all functions marked __init and all data | |
805 | * marked __initdata will be discarded when the module has been intialized. | |
806 | * Likewise for modules used built-in the sections marked __exit | |
807 | * are discarded because __exit marked function are supposed to be called | |
808 | * only when a moduel is unloaded which never happes for built-in modules. | |
809 | * The check_sec_ref() function traverses all relocation records | |
810 | * to find all references to a section that reference a section that will | |
811 | * be discarded and warns about it. | |
812 | **/ | |
813 | static void check_sec_ref(struct module *mod, const char *modname, | |
814 | struct elf_info *elf, | |
815 | int section(const char*), | |
816 | int section_ref_ok(const char *)) | |
817 | { | |
818 | int i; | |
819 | Elf_Sym *sym; | |
820 | Elf_Ehdr *hdr = elf->hdr; | |
821 | Elf_Shdr *sechdrs = elf->sechdrs; | |
822 | const char *secstrings = (void *)hdr + | |
823 | sechdrs[hdr->e_shstrndx].sh_offset; | |
62070fa4 | 824 | |
b39927cf SR |
825 | /* Walk through all sections */ |
826 | for (i = 0; i < hdr->e_shnum; i++) { | |
2c1a51f3 AN |
827 | const char *name = secstrings + sechdrs[i].sh_name; |
828 | const char *secname; | |
829 | Elf_Rela r; | |
eae07ac6 | 830 | unsigned int r_sym; |
b39927cf | 831 | /* We want to process only relocation sections and not .init */ |
2c1a51f3 AN |
832 | if (sechdrs[i].sh_type == SHT_RELA) { |
833 | Elf_Rela *rela; | |
834 | Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset; | |
835 | Elf_Rela *stop = (void*)start + sechdrs[i].sh_size; | |
836 | name += strlen(".rela"); | |
837 | if (section_ref_ok(name)) | |
838 | continue; | |
b39927cf | 839 | |
2c1a51f3 AN |
840 | for (rela = start; rela < stop; rela++) { |
841 | r.r_offset = TO_NATIVE(rela->r_offset); | |
eae07ac6 AN |
842 | #if KERNEL_ELFCLASS == ELFCLASS64 |
843 | if (hdr->e_machine == EM_MIPS) { | |
844 | r_sym = ELF64_MIPS_R_SYM(rela->r_info); | |
845 | r_sym = TO_NATIVE(r_sym); | |
846 | } else { | |
847 | r.r_info = TO_NATIVE(rela->r_info); | |
848 | r_sym = ELF_R_SYM(r.r_info); | |
849 | } | |
850 | #else | |
851 | r.r_info = TO_NATIVE(rela->r_info); | |
852 | r_sym = ELF_R_SYM(r.r_info); | |
853 | #endif | |
2c1a51f3 | 854 | r.r_addend = TO_NATIVE(rela->r_addend); |
eae07ac6 | 855 | sym = elf->symtab_start + r_sym; |
2c1a51f3 AN |
856 | /* Skip special sections */ |
857 | if (sym->st_shndx >= SHN_LORESERVE) | |
858 | continue; | |
859 | ||
860 | secname = secstrings + | |
861 | sechdrs[sym->st_shndx].sh_name; | |
862 | if (section(secname)) | |
863 | warn_sec_mismatch(modname, name, | |
864 | elf, sym, r); | |
865 | } | |
866 | } else if (sechdrs[i].sh_type == SHT_REL) { | |
867 | Elf_Rel *rel; | |
868 | Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset; | |
869 | Elf_Rel *stop = (void*)start + sechdrs[i].sh_size; | |
870 | name += strlen(".rel"); | |
871 | if (section_ref_ok(name)) | |
b39927cf SR |
872 | continue; |
873 | ||
2c1a51f3 AN |
874 | for (rel = start; rel < stop; rel++) { |
875 | r.r_offset = TO_NATIVE(rel->r_offset); | |
eae07ac6 AN |
876 | #if KERNEL_ELFCLASS == ELFCLASS64 |
877 | if (hdr->e_machine == EM_MIPS) { | |
878 | r_sym = ELF64_MIPS_R_SYM(rel->r_info); | |
879 | r_sym = TO_NATIVE(r_sym); | |
880 | } else { | |
881 | r.r_info = TO_NATIVE(rel->r_info); | |
882 | r_sym = ELF_R_SYM(r.r_info); | |
883 | } | |
884 | #else | |
885 | r.r_info = TO_NATIVE(rel->r_info); | |
886 | r_sym = ELF_R_SYM(r.r_info); | |
887 | #endif | |
2c1a51f3 | 888 | r.r_addend = 0; |
eae07ac6 | 889 | sym = elf->symtab_start + r_sym; |
2c1a51f3 AN |
890 | /* Skip special sections */ |
891 | if (sym->st_shndx >= SHN_LORESERVE) | |
892 | continue; | |
893 | ||
894 | secname = secstrings + | |
895 | sechdrs[sym->st_shndx].sh_name; | |
896 | if (section(secname)) | |
897 | warn_sec_mismatch(modname, name, | |
898 | elf, sym, r); | |
899 | } | |
b39927cf SR |
900 | } |
901 | } | |
902 | } | |
903 | ||
904 | /** | |
905 | * Functions used only during module init is marked __init and is stored in | |
906 | * a .init.text section. Likewise data is marked __initdata and stored in | |
907 | * a .init.data section. | |
908 | * If this section is one of these sections return 1 | |
909 | * See include/linux/init.h for the details | |
910 | **/ | |
911 | static int init_section(const char *name) | |
912 | { | |
913 | if (strcmp(name, ".init") == 0) | |
914 | return 1; | |
915 | if (strncmp(name, ".init.", strlen(".init.")) == 0) | |
916 | return 1; | |
917 | return 0; | |
918 | } | |
919 | ||
920 | /** | |
921 | * Identify sections from which references to a .init section is OK. | |
62070fa4 | 922 | * |
b39927cf SR |
923 | * Unfortunately references to read only data that referenced .init |
924 | * sections had to be excluded. Almost all of these are false | |
925 | * positives, they are created by gcc. The downside of excluding rodata | |
926 | * is that there really are some user references from rodata to | |
927 | * init code, e.g. drivers/video/vgacon.c: | |
62070fa4 | 928 | * |
b39927cf SR |
929 | * const struct consw vga_con = { |
930 | * con_startup: vgacon_startup, | |
931 | * | |
932 | * where vgacon_startup is __init. If you want to wade through the false | |
933 | * positives, take out the check for rodata. | |
934 | **/ | |
935 | static int init_section_ref_ok(const char *name) | |
936 | { | |
937 | const char **s; | |
938 | /* Absolute section names */ | |
939 | const char *namelist1[] = { | |
940 | ".init", | |
9209aed0 SR |
941 | ".opd", /* see comment [OPD] at exit_section_ref_ok() */ |
942 | ".toc1", /* used by ppc64 */ | |
b39927cf SR |
943 | ".stab", |
944 | ".rodata", | |
139ec7c4 | 945 | ".parainstructions", |
b39927cf | 946 | ".text.lock", |
9209aed0 | 947 | "__bug_table", /* used by powerpc for BUG() */ |
b39927cf SR |
948 | ".pci_fixup_header", |
949 | ".pci_fixup_final", | |
950 | ".pdr", | |
951 | "__param", | |
468d9494 AV |
952 | "__ex_table", |
953 | ".fixup", | |
35899c57 | 954 | ".smp_locks", |
909252d2 | 955 | ".plt", /* seen on ARCH=um build on x86_64. Harmless */ |
21c4ff80 BH |
956 | "__ftr_fixup", /* powerpc cpu feature fixup */ |
957 | "__fw_ftr_fixup", /* powerpc firmware feature fixup */ | |
b39927cf SR |
958 | NULL |
959 | }; | |
960 | /* Start of section names */ | |
961 | const char *namelist2[] = { | |
962 | ".init.", | |
963 | ".altinstructions", | |
964 | ".eh_frame", | |
965 | ".debug", | |
139ec7c4 | 966 | ".parainstructions", |
b39927cf SR |
967 | NULL |
968 | }; | |
6e10133f SR |
969 | /* part of section name */ |
970 | const char *namelist3 [] = { | |
971 | ".unwind", /* sample: IA_64.unwind.init.text */ | |
972 | NULL | |
973 | }; | |
974 | ||
b39927cf SR |
975 | for (s = namelist1; *s; s++) |
976 | if (strcmp(*s, name) == 0) | |
977 | return 1; | |
62070fa4 | 978 | for (s = namelist2; *s; s++) |
b39927cf SR |
979 | if (strncmp(*s, name, strlen(*s)) == 0) |
980 | return 1; | |
62070fa4 | 981 | for (s = namelist3; *s; s++) |
e835a39c | 982 | if (strstr(name, *s) != NULL) |
6e10133f | 983 | return 1; |
468d9494 AV |
984 | if (strrcmp(name, ".init") == 0) |
985 | return 1; | |
b39927cf SR |
986 | return 0; |
987 | } | |
988 | ||
989 | /* | |
990 | * Functions used only during module exit is marked __exit and is stored in | |
991 | * a .exit.text section. Likewise data is marked __exitdata and stored in | |
992 | * a .exit.data section. | |
993 | * If this section is one of these sections return 1 | |
994 | * See include/linux/init.h for the details | |
995 | **/ | |
996 | static int exit_section(const char *name) | |
997 | { | |
998 | if (strcmp(name, ".exit.text") == 0) | |
999 | return 1; | |
1000 | if (strcmp(name, ".exit.data") == 0) | |
1001 | return 1; | |
1002 | return 0; | |
62070fa4 | 1003 | |
b39927cf SR |
1004 | } |
1005 | ||
1006 | /* | |
1007 | * Identify sections from which references to a .exit section is OK. | |
62070fa4 | 1008 | * |
b39927cf SR |
1009 | * [OPD] Keith Ownes <kaos@sgi.com> commented: |
1010 | * For our future {in}sanity, add a comment that this is the ppc .opd | |
1011 | * section, not the ia64 .opd section. | |
1012 | * ia64 .opd should not point to discarded sections. | |
5ecdd0f6 | 1013 | * [.rodata] like for .init.text we ignore .rodata references -same reason |
b39927cf SR |
1014 | **/ |
1015 | static int exit_section_ref_ok(const char *name) | |
1016 | { | |
1017 | const char **s; | |
1018 | /* Absolute section names */ | |
1019 | const char *namelist1[] = { | |
1020 | ".exit.text", | |
1021 | ".exit.data", | |
1022 | ".init.text", | |
5ecdd0f6 | 1023 | ".rodata", |
b39927cf | 1024 | ".opd", /* See comment [OPD] */ |
9209aed0 | 1025 | ".toc1", /* used by ppc64 */ |
b39927cf SR |
1026 | ".altinstructions", |
1027 | ".pdr", | |
9209aed0 | 1028 | "__bug_table", /* used by powerpc for BUG() */ |
b39927cf SR |
1029 | ".exitcall.exit", |
1030 | ".eh_frame", | |
acd19499 | 1031 | ".parainstructions", |
b39927cf | 1032 | ".stab", |
468d9494 AV |
1033 | "__ex_table", |
1034 | ".fixup", | |
35899c57 | 1035 | ".smp_locks", |
909252d2 | 1036 | ".plt", /* seen on ARCH=um build on x86_64. Harmless */ |
b39927cf SR |
1037 | NULL |
1038 | }; | |
1039 | /* Start of section names */ | |
1040 | const char *namelist2[] = { | |
1041 | ".debug", | |
1042 | NULL | |
1043 | }; | |
6e10133f SR |
1044 | /* part of section name */ |
1045 | const char *namelist3 [] = { | |
1046 | ".unwind", /* Sample: IA_64.unwind.exit.text */ | |
1047 | NULL | |
1048 | }; | |
62070fa4 | 1049 | |
b39927cf SR |
1050 | for (s = namelist1; *s; s++) |
1051 | if (strcmp(*s, name) == 0) | |
1052 | return 1; | |
62070fa4 | 1053 | for (s = namelist2; *s; s++) |
b39927cf SR |
1054 | if (strncmp(*s, name, strlen(*s)) == 0) |
1055 | return 1; | |
62070fa4 | 1056 | for (s = namelist3; *s; s++) |
e835a39c | 1057 | if (strstr(name, *s) != NULL) |
6e10133f | 1058 | return 1; |
b39927cf SR |
1059 | return 0; |
1060 | } | |
1061 | ||
5c3ead8c | 1062 | static void read_symbols(char *modname) |
1da177e4 LT |
1063 | { |
1064 | const char *symname; | |
1065 | char *version; | |
b817f6fe | 1066 | char *license; |
1da177e4 LT |
1067 | struct module *mod; |
1068 | struct elf_info info = { }; | |
1069 | Elf_Sym *sym; | |
1070 | ||
1071 | parse_elf(&info, modname); | |
1072 | ||
1073 | mod = new_module(modname); | |
1074 | ||
1075 | /* When there's no vmlinux, don't print warnings about | |
1076 | * unresolved symbols (since there'll be too many ;) */ | |
1077 | if (is_vmlinux(modname)) { | |
1da177e4 | 1078 | have_vmlinux = 1; |
1da177e4 LT |
1079 | mod->skip = 1; |
1080 | } | |
1081 | ||
b817f6fe SR |
1082 | license = get_modinfo(info.modinfo, info.modinfo_len, "license"); |
1083 | while (license) { | |
1084 | if (license_is_gpl_compatible(license)) | |
1085 | mod->gpl_compatible = 1; | |
1086 | else { | |
1087 | mod->gpl_compatible = 0; | |
1088 | break; | |
1089 | } | |
1090 | license = get_next_modinfo(info.modinfo, info.modinfo_len, | |
1091 | "license", license); | |
1092 | } | |
1093 | ||
1da177e4 LT |
1094 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
1095 | symname = info.strtab + sym->st_name; | |
1096 | ||
1097 | handle_modversions(mod, &info, sym, symname); | |
1098 | handle_moddevtable(mod, &info, sym, symname); | |
1099 | } | |
b39927cf SR |
1100 | check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok); |
1101 | check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok); | |
1da177e4 LT |
1102 | |
1103 | version = get_modinfo(info.modinfo, info.modinfo_len, "version"); | |
1104 | if (version) | |
1105 | maybe_frob_rcs_version(modname, version, info.modinfo, | |
1106 | version - (char *)info.hdr); | |
1107 | if (version || (all_versions && !is_vmlinux(modname))) | |
1108 | get_src_version(modname, mod->srcversion, | |
1109 | sizeof(mod->srcversion)-1); | |
1110 | ||
1111 | parse_elf_finish(&info); | |
1112 | ||
1113 | /* Our trick to get versioning for struct_module - it's | |
1114 | * never passed as an argument to an exported function, so | |
1115 | * the automatic versioning doesn't pick it up, but it's really | |
1116 | * important anyhow */ | |
1117 | if (modversions) | |
1118 | mod->unres = alloc_symbol("struct_module", 0, mod->unres); | |
1119 | } | |
1120 | ||
1121 | #define SZ 500 | |
1122 | ||
1123 | /* We first write the generated file into memory using the | |
1124 | * following helper, then compare to the file on disk and | |
1125 | * only update the later if anything changed */ | |
1126 | ||
5c3ead8c SR |
1127 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
1128 | const char *fmt, ...) | |
1da177e4 LT |
1129 | { |
1130 | char tmp[SZ]; | |
1131 | int len; | |
1132 | va_list ap; | |
62070fa4 | 1133 | |
1da177e4 LT |
1134 | va_start(ap, fmt); |
1135 | len = vsnprintf(tmp, SZ, fmt, ap); | |
7670f023 | 1136 | buf_write(buf, tmp, len); |
1da177e4 LT |
1137 | va_end(ap); |
1138 | } | |
1139 | ||
5c3ead8c | 1140 | void buf_write(struct buffer *buf, const char *s, int len) |
1da177e4 LT |
1141 | { |
1142 | if (buf->size - buf->pos < len) { | |
7670f023 | 1143 | buf->size += len + SZ; |
1da177e4 LT |
1144 | buf->p = realloc(buf->p, buf->size); |
1145 | } | |
1146 | strncpy(buf->p + buf->pos, s, len); | |
1147 | buf->pos += len; | |
1148 | } | |
1149 | ||
c96fca21 SR |
1150 | static void check_for_gpl_usage(enum export exp, const char *m, const char *s) |
1151 | { | |
1152 | const char *e = is_vmlinux(m) ?"":".ko"; | |
1153 | ||
1154 | switch (exp) { | |
1155 | case export_gpl: | |
1156 | fatal("modpost: GPL-incompatible module %s%s " | |
1157 | "uses GPL-only symbol '%s'\n", m, e, s); | |
1158 | break; | |
1159 | case export_unused_gpl: | |
1160 | fatal("modpost: GPL-incompatible module %s%s " | |
1161 | "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s); | |
1162 | break; | |
1163 | case export_gpl_future: | |
1164 | warn("modpost: GPL-incompatible module %s%s " | |
1165 | "uses future GPL-only symbol '%s'\n", m, e, s); | |
1166 | break; | |
1167 | case export_plain: | |
1168 | case export_unused: | |
1169 | case export_unknown: | |
1170 | /* ignore */ | |
1171 | break; | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | static void check_for_unused(enum export exp, const char* m, const char* s) | |
1176 | { | |
1177 | const char *e = is_vmlinux(m) ?"":".ko"; | |
1178 | ||
1179 | switch (exp) { | |
1180 | case export_unused: | |
1181 | case export_unused_gpl: | |
1182 | warn("modpost: module %s%s " | |
1183 | "uses symbol '%s' marked UNUSED\n", m, e, s); | |
1184 | break; | |
1185 | default: | |
1186 | /* ignore */ | |
1187 | break; | |
1188 | } | |
1189 | } | |
1190 | ||
1191 | static void check_exports(struct module *mod) | |
b817f6fe SR |
1192 | { |
1193 | struct symbol *s, *exp; | |
1194 | ||
1195 | for (s = mod->unres; s; s = s->next) { | |
6449bd62 | 1196 | const char *basename; |
b817f6fe SR |
1197 | exp = find_symbol(s->name); |
1198 | if (!exp || exp->module == mod) | |
1199 | continue; | |
6449bd62 | 1200 | basename = strrchr(mod->name, '/'); |
b817f6fe SR |
1201 | if (basename) |
1202 | basename++; | |
c96fca21 SR |
1203 | else |
1204 | basename = mod->name; | |
1205 | if (!mod->gpl_compatible) | |
1206 | check_for_gpl_usage(exp->export, basename, exp->name); | |
1207 | check_for_unused(exp->export, basename, exp->name); | |
b817f6fe SR |
1208 | } |
1209 | } | |
1210 | ||
5c3ead8c SR |
1211 | /** |
1212 | * Header for the generated file | |
1213 | **/ | |
1214 | static void add_header(struct buffer *b, struct module *mod) | |
1da177e4 LT |
1215 | { |
1216 | buf_printf(b, "#include <linux/module.h>\n"); | |
1217 | buf_printf(b, "#include <linux/vermagic.h>\n"); | |
1218 | buf_printf(b, "#include <linux/compiler.h>\n"); | |
1219 | buf_printf(b, "\n"); | |
1220 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); | |
1221 | buf_printf(b, "\n"); | |
1da177e4 LT |
1222 | buf_printf(b, "struct module __this_module\n"); |
1223 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); | |
f83b5e32 | 1224 | buf_printf(b, " .name = KBUILD_MODNAME,\n"); |
1da177e4 LT |
1225 | if (mod->has_init) |
1226 | buf_printf(b, " .init = init_module,\n"); | |
1227 | if (mod->has_cleanup) | |
1228 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" | |
1229 | " .exit = cleanup_module,\n" | |
1230 | "#endif\n"); | |
1231 | buf_printf(b, "};\n"); | |
1232 | } | |
1233 | ||
5c3ead8c SR |
1234 | /** |
1235 | * Record CRCs for unresolved symbols | |
1236 | **/ | |
c53ddacd | 1237 | static int add_versions(struct buffer *b, struct module *mod) |
1da177e4 LT |
1238 | { |
1239 | struct symbol *s, *exp; | |
c53ddacd | 1240 | int err = 0; |
1da177e4 LT |
1241 | |
1242 | for (s = mod->unres; s; s = s->next) { | |
1243 | exp = find_symbol(s->name); | |
1244 | if (!exp || exp->module == mod) { | |
c53ddacd | 1245 | if (have_vmlinux && !s->weak) { |
cb80514d SR |
1246 | warn("\"%s\" [%s.ko] undefined!\n", |
1247 | s->name, mod->name); | |
c53ddacd KK |
1248 | err = warn_unresolved ? 0 : 1; |
1249 | } | |
1da177e4 LT |
1250 | continue; |
1251 | } | |
1252 | s->module = exp->module; | |
1253 | s->crc_valid = exp->crc_valid; | |
1254 | s->crc = exp->crc; | |
1255 | } | |
1256 | ||
1257 | if (!modversions) | |
c53ddacd | 1258 | return err; |
1da177e4 LT |
1259 | |
1260 | buf_printf(b, "\n"); | |
1261 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); | |
1262 | buf_printf(b, "__attribute_used__\n"); | |
1263 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); | |
1264 | ||
1265 | for (s = mod->unres; s; s = s->next) { | |
1266 | if (!s->module) { | |
1267 | continue; | |
1268 | } | |
1269 | if (!s->crc_valid) { | |
cb80514d | 1270 | warn("\"%s\" [%s.ko] has no CRC!\n", |
1da177e4 LT |
1271 | s->name, mod->name); |
1272 | continue; | |
1273 | } | |
1274 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); | |
1275 | } | |
1276 | ||
1277 | buf_printf(b, "};\n"); | |
c53ddacd KK |
1278 | |
1279 | return err; | |
1da177e4 LT |
1280 | } |
1281 | ||
5c3ead8c SR |
1282 | static void add_depends(struct buffer *b, struct module *mod, |
1283 | struct module *modules) | |
1da177e4 LT |
1284 | { |
1285 | struct symbol *s; | |
1286 | struct module *m; | |
1287 | int first = 1; | |
1288 | ||
1289 | for (m = modules; m; m = m->next) { | |
1290 | m->seen = is_vmlinux(m->name); | |
1291 | } | |
1292 | ||
1293 | buf_printf(b, "\n"); | |
1294 | buf_printf(b, "static const char __module_depends[]\n"); | |
1295 | buf_printf(b, "__attribute_used__\n"); | |
1296 | buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); | |
1297 | buf_printf(b, "\"depends="); | |
1298 | for (s = mod->unres; s; s = s->next) { | |
1299 | if (!s->module) | |
1300 | continue; | |
1301 | ||
1302 | if (s->module->seen) | |
1303 | continue; | |
1304 | ||
1305 | s->module->seen = 1; | |
1306 | buf_printf(b, "%s%s", first ? "" : ",", | |
1307 | strrchr(s->module->name, '/') + 1); | |
1308 | first = 0; | |
1309 | } | |
1310 | buf_printf(b, "\";\n"); | |
1311 | } | |
1312 | ||
5c3ead8c | 1313 | static void add_srcversion(struct buffer *b, struct module *mod) |
1da177e4 LT |
1314 | { |
1315 | if (mod->srcversion[0]) { | |
1316 | buf_printf(b, "\n"); | |
1317 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", | |
1318 | mod->srcversion); | |
1319 | } | |
1320 | } | |
1321 | ||
5c3ead8c | 1322 | static void write_if_changed(struct buffer *b, const char *fname) |
1da177e4 LT |
1323 | { |
1324 | char *tmp; | |
1325 | FILE *file; | |
1326 | struct stat st; | |
1327 | ||
1328 | file = fopen(fname, "r"); | |
1329 | if (!file) | |
1330 | goto write; | |
1331 | ||
1332 | if (fstat(fileno(file), &st) < 0) | |
1333 | goto close_write; | |
1334 | ||
1335 | if (st.st_size != b->pos) | |
1336 | goto close_write; | |
1337 | ||
1338 | tmp = NOFAIL(malloc(b->pos)); | |
1339 | if (fread(tmp, 1, b->pos, file) != b->pos) | |
1340 | goto free_write; | |
1341 | ||
1342 | if (memcmp(tmp, b->p, b->pos) != 0) | |
1343 | goto free_write; | |
1344 | ||
1345 | free(tmp); | |
1346 | fclose(file); | |
1347 | return; | |
1348 | ||
1349 | free_write: | |
1350 | free(tmp); | |
1351 | close_write: | |
1352 | fclose(file); | |
1353 | write: | |
1354 | file = fopen(fname, "w"); | |
1355 | if (!file) { | |
1356 | perror(fname); | |
1357 | exit(1); | |
1358 | } | |
1359 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { | |
1360 | perror(fname); | |
1361 | exit(1); | |
1362 | } | |
1363 | fclose(file); | |
1364 | } | |
1365 | ||
bd5cbced | 1366 | /* parse Module.symvers file. line format: |
534b89a9 | 1367 | * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something] |
bd5cbced | 1368 | **/ |
040fcc81 | 1369 | static void read_dump(const char *fname, unsigned int kernel) |
1da177e4 LT |
1370 | { |
1371 | unsigned long size, pos = 0; | |
1372 | void *file = grab_file(fname, &size); | |
1373 | char *line; | |
1374 | ||
1375 | if (!file) | |
1376 | /* No symbol versions, silently ignore */ | |
1377 | return; | |
1378 | ||
1379 | while ((line = get_next_line(&pos, file, size))) { | |
534b89a9 | 1380 | char *symname, *modname, *d, *export, *end; |
1da177e4 LT |
1381 | unsigned int crc; |
1382 | struct module *mod; | |
040fcc81 | 1383 | struct symbol *s; |
1da177e4 LT |
1384 | |
1385 | if (!(symname = strchr(line, '\t'))) | |
1386 | goto fail; | |
1387 | *symname++ = '\0'; | |
1388 | if (!(modname = strchr(symname, '\t'))) | |
1389 | goto fail; | |
1390 | *modname++ = '\0'; | |
9ac545b0 | 1391 | if ((export = strchr(modname, '\t')) != NULL) |
bd5cbced | 1392 | *export++ = '\0'; |
534b89a9 SR |
1393 | if (export && ((end = strchr(export, '\t')) != NULL)) |
1394 | *end = '\0'; | |
1da177e4 LT |
1395 | crc = strtoul(line, &d, 16); |
1396 | if (*symname == '\0' || *modname == '\0' || *d != '\0') | |
1397 | goto fail; | |
1398 | ||
1399 | if (!(mod = find_module(modname))) { | |
1400 | if (is_vmlinux(modname)) { | |
1401 | have_vmlinux = 1; | |
1402 | } | |
1403 | mod = new_module(NOFAIL(strdup(modname))); | |
1404 | mod->skip = 1; | |
1405 | } | |
bd5cbced | 1406 | s = sym_add_exported(symname, mod, export_no(export)); |
8e70c458 SR |
1407 | s->kernel = kernel; |
1408 | s->preloaded = 1; | |
bd5cbced | 1409 | sym_update_crc(symname, mod, crc, export_no(export)); |
1da177e4 LT |
1410 | } |
1411 | return; | |
1412 | fail: | |
1413 | fatal("parse error in symbol dump file\n"); | |
1414 | } | |
1415 | ||
040fcc81 SR |
1416 | /* For normal builds always dump all symbols. |
1417 | * For external modules only dump symbols | |
1418 | * that are not read from kernel Module.symvers. | |
1419 | **/ | |
1420 | static int dump_sym(struct symbol *sym) | |
1421 | { | |
1422 | if (!external_module) | |
1423 | return 1; | |
1424 | if (sym->vmlinux || sym->kernel) | |
1425 | return 0; | |
1426 | return 1; | |
1427 | } | |
62070fa4 | 1428 | |
5c3ead8c | 1429 | static void write_dump(const char *fname) |
1da177e4 LT |
1430 | { |
1431 | struct buffer buf = { }; | |
1432 | struct symbol *symbol; | |
1433 | int n; | |
1434 | ||
1435 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { | |
1436 | symbol = symbolhash[n]; | |
1437 | while (symbol) { | |
040fcc81 | 1438 | if (dump_sym(symbol)) |
bd5cbced | 1439 | buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", |
62070fa4 | 1440 | symbol->crc, symbol->name, |
bd5cbced RP |
1441 | symbol->module->name, |
1442 | export_str(symbol->export)); | |
1da177e4 LT |
1443 | symbol = symbol->next; |
1444 | } | |
1445 | } | |
1446 | write_if_changed(&buf, fname); | |
1447 | } | |
1448 | ||
5c3ead8c | 1449 | int main(int argc, char **argv) |
1da177e4 LT |
1450 | { |
1451 | struct module *mod; | |
1452 | struct buffer buf = { }; | |
1453 | char fname[SZ]; | |
040fcc81 SR |
1454 | char *kernel_read = NULL, *module_read = NULL; |
1455 | char *dump_write = NULL; | |
1da177e4 | 1456 | int opt; |
c53ddacd | 1457 | int err; |
1da177e4 | 1458 | |
c53ddacd | 1459 | while ((opt = getopt(argc, argv, "i:I:mo:aw")) != -1) { |
1da177e4 LT |
1460 | switch(opt) { |
1461 | case 'i': | |
040fcc81 SR |
1462 | kernel_read = optarg; |
1463 | break; | |
1464 | case 'I': | |
1465 | module_read = optarg; | |
1466 | external_module = 1; | |
1da177e4 LT |
1467 | break; |
1468 | case 'm': | |
1469 | modversions = 1; | |
1470 | break; | |
1471 | case 'o': | |
1472 | dump_write = optarg; | |
1473 | break; | |
1474 | case 'a': | |
1475 | all_versions = 1; | |
1476 | break; | |
c53ddacd KK |
1477 | case 'w': |
1478 | warn_unresolved = 1; | |
1479 | break; | |
1da177e4 LT |
1480 | default: |
1481 | exit(1); | |
1482 | } | |
1483 | } | |
1484 | ||
040fcc81 SR |
1485 | if (kernel_read) |
1486 | read_dump(kernel_read, 1); | |
1487 | if (module_read) | |
1488 | read_dump(module_read, 0); | |
1da177e4 LT |
1489 | |
1490 | while (optind < argc) { | |
1491 | read_symbols(argv[optind++]); | |
1492 | } | |
1493 | ||
b817f6fe SR |
1494 | for (mod = modules; mod; mod = mod->next) { |
1495 | if (mod->skip) | |
1496 | continue; | |
c96fca21 | 1497 | check_exports(mod); |
b817f6fe SR |
1498 | } |
1499 | ||
c53ddacd KK |
1500 | err = 0; |
1501 | ||
1da177e4 LT |
1502 | for (mod = modules; mod; mod = mod->next) { |
1503 | if (mod->skip) | |
1504 | continue; | |
1505 | ||
1506 | buf.pos = 0; | |
1507 | ||
1508 | add_header(&buf, mod); | |
c53ddacd | 1509 | err |= add_versions(&buf, mod); |
1da177e4 LT |
1510 | add_depends(&buf, mod, modules); |
1511 | add_moddevtable(&buf, mod); | |
1512 | add_srcversion(&buf, mod); | |
1513 | ||
1514 | sprintf(fname, "%s.mod.c", mod->name); | |
1515 | write_if_changed(&buf, fname); | |
1516 | } | |
1517 | ||
1518 | if (dump_write) | |
1519 | write_dump(dump_write); | |
1520 | ||
c53ddacd | 1521 | return err; |
1da177e4 | 1522 | } |