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