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