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