blob: f3e0e4db833062535dc06905d6b276ba5c6d63d2 [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
5 * Copyright 2006-2008 Sam Ravnborg
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#define _GNU_SOURCE
15#include <stdio.h>
16#include <ctype.h>
17#include <string.h>
18#include "modpost.h"
19#include "../../include/generated/autoconf.h"
20#include "../../include/linux/license.h"
21
22#ifdef CONFIG_SYMBOL_PREFIX
23#define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
24#else
25#define MODULE_SYMBOL_PREFIX ""
26#endif
27
28
29int modversions = 0;
30int have_vmlinux = 0;
31static int all_versions = 0;
32static int external_module = 0;
33static int vmlinux_section_warnings = 1;
34static int section_error_on_mismatch;
35static int warn_unresolved = 0;
36static int sec_mismatch_count = 0;
37static int sec_mismatch_verbose = 1;
38
39enum export {
40 export_plain, export_unused, export_gpl,
41 export_unused_gpl, export_gpl_future, export_unknown
42};
43
44#define PRINTF __attribute__ ((format (printf, 1, 2)))
45
46PRINTF void fatal(const char *fmt, ...)
47{
48 va_list arglist;
49
50 fprintf(stderr, "FATAL: ");
51
52 va_start(arglist, fmt);
53 vfprintf(stderr, fmt, arglist);
54 va_end(arglist);
55
56 exit(1);
57}
58
59PRINTF void warn(const char *fmt, ...)
60{
61 va_list arglist;
62
63 fprintf(stderr, "WARNING: ");
64
65 va_start(arglist, fmt);
66 vfprintf(stderr, fmt, arglist);
67 va_end(arglist);
68}
69
70PRINTF void merror(const char *fmt, ...)
71{
72 va_list arglist;
73
74 fprintf(stderr, "ERROR: ");
75
76 va_start(arglist, fmt);
77 vfprintf(stderr, fmt, arglist);
78 va_end(arglist);
79}
80
81static int is_vmlinux(const char *modname)
82{
83 const char *myname;
84
85 myname = strrchr(modname, '/');
86 if (myname)
87 myname++;
88 else
89 myname = modname;
90
91 return (strcmp(myname, "vmlinux") == 0) ||
92 (strcmp(myname, "vmlinux.o") == 0);
93}
94
95void *do_nofail(void *ptr, const char *expr)
96{
97 if (!ptr)
98 fatal("modpost: Memory allocation failure: %s.\n", expr);
99
100 return ptr;
101}
102
103static struct module *modules;
104
105static struct module *find_module(char *modname)
106{
107 struct module *mod;
108
109 for (mod = modules; mod; mod = mod->next)
110 if (strcmp(mod->name, modname) == 0)
111 break;
112 return mod;
113}
114
115static struct module *new_module(char *modname)
116{
117 struct module *mod;
118 char *p, *s;
119
120 mod = NOFAIL(malloc(sizeof(*mod)));
121 memset(mod, 0, sizeof(*mod));
122 p = NOFAIL(strdup(modname));
123
124
125 s = strrchr(p, '.');
126 if (s != NULL)
127 if (strcmp(s, ".o") == 0) {
128 *s = '\0';
129 mod->is_dot_o = 1;
130 }
131
132
133 mod->name = p;
134 mod->gpl_compatible = -1;
135 mod->next = modules;
136 modules = mod;
137
138 return mod;
139}
140
141
142#define SYMBOL_HASH_SIZE 1024
143
144struct symbol {
145 struct symbol *next;
146 struct module *module;
147 unsigned int crc;
148 int crc_valid;
149 unsigned int weak:1;
150 unsigned int vmlinux:1;
151 unsigned int kernel:1;
152 unsigned int preloaded:1;
153 enum export export;
154 char name[0];
155};
156
157static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
158
159static inline unsigned int tdb_hash(const char *name)
160{
161 unsigned value;
162 unsigned i;
163
164
165 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
166 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
167
168 return (1103515243 * value + 12345);
169}
170
171static struct symbol *alloc_symbol(const char *name, unsigned int weak,
172 struct symbol *next)
173{
174 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
175
176 memset(s, 0, sizeof(*s));
177 strcpy(s->name, name);
178 s->weak = weak;
179 s->next = next;
180 return s;
181}
182
183static struct symbol *new_symbol(const char *name, struct module *module,
184 enum export export)
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;
192 new->export = export;
193 return new;
194}
195
196static struct symbol *find_symbol(const char *name)
197{
198 struct symbol *s;
199
200
201 if (name[0] == '.')
202 name++;
203
204 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
205 if (strcmp(s->name, name) == 0)
206 return s;
207 }
208 return NULL;
209}
210
211static struct {
212 const char *str;
213 enum export export;
214} export_list[] = {
215 { .str = "EXPORT_SYMBOL", .export = export_plain },
216 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
217 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
218 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
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
229static enum export export_no(const char *s)
230{
231 int i;
232
233 if (!s)
234 return export_unknown;
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 const char *sec_name(struct elf_info *elf, int secindex);
243
244#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
245
246static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
247{
248 const char *secname = sec_name(elf, sec);
249
250 if (strstarts(secname, "___ksymtab+"))
251 return export_plain;
252 else if (strstarts(secname, "___ksymtab_unused+"))
253 return export_unused;
254 else if (strstarts(secname, "___ksymtab_gpl+"))
255 return export_gpl;
256 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
257 return export_unused_gpl;
258 else if (strstarts(secname, "___ksymtab_gpl_future+"))
259 return export_gpl_future;
260 else
261 return export_unknown;
262}
263
264static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
265{
266 if (sec == elf->export_sec)
267 return export_plain;
268 else if (sec == elf->export_unused_sec)
269 return export_unused;
270 else if (sec == elf->export_gpl_sec)
271 return export_gpl;
272 else if (sec == elf->export_unused_gpl_sec)
273 return export_unused_gpl;
274 else if (sec == elf->export_gpl_future_sec)
275 return export_gpl_future;
276 else
277 return export_unknown;
278}
279
280static struct symbol *sym_add_exported(const char *name, struct module *mod,
281 enum export export)
282{
283 struct symbol *s = find_symbol(name);
284
285 if (!s) {
286 s = new_symbol(name, mod, export);
287 } else {
288 if (!s->preloaded) {
289 warn("%s: '%s' exported twice. Previous export "
290 "was in %s%s\n", mod->name, name,
291 s->module->name,
292 is_vmlinux(s->module->name) ?"":".ko");
293 } else {
294
295 s->module = mod;
296 }
297 }
298 s->preloaded = 0;
299 s->vmlinux = is_vmlinux(mod->name);
300 s->kernel = 0;
301 s->export = export;
302 return s;
303}
304
305static void sym_update_crc(const char *name, struct module *mod,
306 unsigned int crc, enum export export)
307{
308 struct symbol *s = find_symbol(name);
309
310 if (!s)
311 s = new_symbol(name, mod, export);
312 s->crc = crc;
313 s->crc_valid = 1;
314}
315
316void *grab_file(const char *filename, unsigned long *size)
317{
318 struct stat st;
319 void *map;
320 int fd;
321
322 fd = open(filename, O_RDONLY);
323 if (fd < 0 || fstat(fd, &st) != 0)
324 return NULL;
325
326 *size = st.st_size;
327 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
328 close(fd);
329
330 if (map == MAP_FAILED)
331 return NULL;
332 return map;
333}
334
335char *get_next_line(unsigned long *pos, void *file, unsigned long size)
336{
337 static char line[4096];
338 int skip = 1;
339 size_t len = 0;
340 signed char *p = (signed char *)file + *pos;
341 char *s = line;
342
343 for (; *pos < size ; (*pos)++) {
344 if (skip && isspace(*p)) {
345 p++;
346 continue;
347 }
348 skip = 0;
349 if (*p != '\n' && (*pos < size)) {
350 len++;
351 *s++ = *p++;
352 if (len > 4095)
353 break;
354 } else {
355
356 *s = '\0';
357 return line;
358 }
359 }
360
361 return NULL;
362}
363
364void release_file(void *file, unsigned long size)
365{
366 munmap(file, size);
367}
368
369static int parse_elf(struct elf_info *info, const char *filename)
370{
371 unsigned int i;
372 Elf_Ehdr *hdr;
373 Elf_Shdr *sechdrs;
374 Elf_Sym *sym;
375 const char *secstrings;
376 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
377
378 hdr = grab_file(filename, &info->size);
379 if (!hdr) {
380 perror(filename);
381 exit(1);
382 }
383 info->hdr = hdr;
384 if (info->size < sizeof(*hdr)) {
385
386 return 0;
387 }
388
389 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
390 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
391 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
392 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
393
394 return 0;
395 }
396
397 hdr->e_type = TO_NATIVE(hdr->e_type);
398 hdr->e_machine = TO_NATIVE(hdr->e_machine);
399 hdr->e_version = TO_NATIVE(hdr->e_version);
400 hdr->e_entry = TO_NATIVE(hdr->e_entry);
401 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
402 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
403 hdr->e_flags = TO_NATIVE(hdr->e_flags);
404 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
405 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
406 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
407 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
408 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
409 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
410 sechdrs = (void *)hdr + hdr->e_shoff;
411 info->sechdrs = sechdrs;
412
413
414 if (hdr->e_shoff > info->size) {
415 fatal("section header offset=%lu in file '%s' is bigger than "
416 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
417 filename, info->size);
418 return 0;
419 }
420
421 if (hdr->e_shnum == SHN_UNDEF) {
422 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
423 }
424 else {
425 info->num_sections = hdr->e_shnum;
426 }
427 if (hdr->e_shstrndx == SHN_XINDEX) {
428 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
429 }
430 else {
431 info->secindex_strings = hdr->e_shstrndx;
432 }
433
434
435 for (i = 0; i < info->num_sections; i++) {
436 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
437 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
438 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
439 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
440 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
441 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
442 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
443 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
444 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
445 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
446 }
447
448 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
449 for (i = 1; i < info->num_sections; i++) {
450 const char *secname;
451 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
452
453 if (!nobits && sechdrs[i].sh_offset > info->size) {
454 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
455 "sizeof(*hrd)=%zu\n", filename,
456 (unsigned long)sechdrs[i].sh_offset,
457 sizeof(*hdr));
458 return 0;
459 }
460 secname = secstrings + sechdrs[i].sh_name;
461 if (strcmp(secname, ".modinfo") == 0) {
462 if (nobits)
463 fatal("%s has NOBITS .modinfo\n", filename);
464 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
465 info->modinfo_len = sechdrs[i].sh_size;
466 } else if (strcmp(secname, "__ksymtab") == 0)
467 info->export_sec = i;
468 else if (strcmp(secname, "__ksymtab_unused") == 0)
469 info->export_unused_sec = i;
470 else if (strcmp(secname, "__ksymtab_gpl") == 0)
471 info->export_gpl_sec = i;
472 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
473 info->export_unused_gpl_sec = i;
474 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
475 info->export_gpl_future_sec = i;
476
477 if (sechdrs[i].sh_type == SHT_SYMTAB) {
478 unsigned int sh_link_idx;
479 symtab_idx = i;
480 info->symtab_start = (void *)hdr +
481 sechdrs[i].sh_offset;
482 info->symtab_stop = (void *)hdr +
483 sechdrs[i].sh_offset + sechdrs[i].sh_size;
484 sh_link_idx = sechdrs[i].sh_link;
485 info->strtab = (void *)hdr +
486 sechdrs[sh_link_idx].sh_offset;
487 }
488
489
490 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
491 symtab_shndx_idx = i;
492 info->symtab_shndx_start = (void *)hdr +
493 sechdrs[i].sh_offset;
494 info->symtab_shndx_stop = (void *)hdr +
495 sechdrs[i].sh_offset + sechdrs[i].sh_size;
496 }
497 }
498 if (!info->symtab_start)
499 fatal("%s has no symtab?\n", filename);
500
501
502 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
503 sym->st_shndx = TO_NATIVE(sym->st_shndx);
504 sym->st_name = TO_NATIVE(sym->st_name);
505 sym->st_value = TO_NATIVE(sym->st_value);
506 sym->st_size = TO_NATIVE(sym->st_size);
507 }
508
509 if (symtab_shndx_idx != ~0U) {
510 Elf32_Word *p;
511 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
512 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
513 filename, sechdrs[symtab_shndx_idx].sh_link,
514 symtab_idx);
515
516 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
517 p++)
518 *p = TO_NATIVE(*p);
519 }
520
521 return 1;
522}
523
524static void parse_elf_finish(struct elf_info *info)
525{
526 release_file(info->hdr, info->size);
527}
528
529static int ignore_undef_symbol(struct elf_info *info, const char *symname)
530{
531
532 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
533 return 1;
534
535 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
536 return 1;
537 if (info->hdr->e_machine == EM_PPC)
538
539 if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
540 strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
541 strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
542 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
543 return 1;
544 if (info->hdr->e_machine == EM_PPC64)
545
546 if (strncmp(symname, "_restgpr0_", sizeof("_restgpr0_") - 1) == 0 ||
547 strncmp(symname, "_savegpr0_", sizeof("_savegpr0_") - 1) == 0)
548 return 1;
549
550 return 0;
551}
552
553#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
554#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
555
556static void handle_modversions(struct module *mod, struct elf_info *info,
557 Elf_Sym *sym, const char *symname)
558{
559 unsigned int crc;
560 enum export export;
561
562 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
563 strncmp(symname, "__ksymtab", 9) == 0)
564 export = export_from_secname(info, get_secindex(info, sym));
565 else
566 export = export_from_sec(info, get_secindex(info, sym));
567
568 switch (sym->st_shndx) {
569 case SHN_COMMON:
570 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
571 break;
572 case SHN_ABS:
573
574 if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
575 crc = (unsigned int) sym->st_value;
576 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
577 export);
578 }
579 break;
580 case SHN_UNDEF:
581
582 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
583 ELF_ST_BIND(sym->st_info) != STB_WEAK)
584 break;
585 if (ignore_undef_symbol(info, symname))
586 break;
587#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
588#ifndef STT_SPARC_REGISTER
589#define STT_SPARC_REGISTER STT_REGISTER
590#endif
591 if (info->hdr->e_machine == EM_SPARC ||
592 info->hdr->e_machine == EM_SPARCV9) {
593
594 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
595 break;
596 if (symname[0] == '.') {
597 char *munged = strdup(symname);
598 munged[0] = '_';
599 munged[1] = toupper(munged[1]);
600 symname = munged;
601 }
602 }
603#endif
604
605 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
606 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
607 mod->unres =
608 alloc_symbol(symname +
609 strlen(MODULE_SYMBOL_PREFIX),
610 ELF_ST_BIND(sym->st_info) == STB_WEAK,
611 mod->unres);
612 }
613 break;
614 default:
615
616 if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
617 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
618 export);
619 }
620 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
621 mod->has_init = 1;
622 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
623 mod->has_cleanup = 1;
624 break;
625 }
626}
627
628static char *next_string(char *string, unsigned long *secsize)
629{
630
631 while (string[0]) {
632 string++;
633 if ((*secsize)-- <= 1)
634 return NULL;
635 }
636
637
638 while (!string[0]) {
639 string++;
640 if ((*secsize)-- <= 1)
641 return NULL;
642 }
643 return string;
644}
645
646static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
647 const char *tag, char *info)
648{
649 char *p;
650 unsigned int taglen = strlen(tag);
651 unsigned long size = modinfo_len;
652
653 if (info) {
654 size -= info - (char *)modinfo;
655 modinfo = next_string(info, &size);
656 }
657
658 for (p = modinfo; p; p = next_string(p, &size)) {
659 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
660 return p + taglen + 1;
661 }
662 return NULL;
663}
664
665static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
666 const char *tag)
667
668{
669 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
670}
671
672static int strrcmp(const char *s, const char *sub)
673{
674 int slen, sublen;
675
676 if (!s || !sub)
677 return 1;
678
679 slen = strlen(s);
680 sublen = strlen(sub);
681
682 if ((slen == 0) || (sublen == 0))
683 return 1;
684
685 if (sublen > slen)
686 return 1;
687
688 return memcmp(s + slen - sublen, sub, sublen);
689}
690
691static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
692{
693 if (sym)
694 return elf->strtab + sym->st_name;
695 else
696 return "(unknown)";
697}
698
699static const char *sec_name(struct elf_info *elf, int secindex)
700{
701 Elf_Shdr *sechdrs = elf->sechdrs;
702 return (void *)elf->hdr +
703 elf->sechdrs[elf->secindex_strings].sh_offset +
704 sechdrs[secindex].sh_name;
705}
706
707static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
708{
709 return (void *)elf->hdr +
710 elf->sechdrs[elf->secindex_strings].sh_offset +
711 sechdr->sh_name;
712}
713
714static int number_prefix(const char *sym)
715{
716 if (*sym++ == '\0')
717 return 1;
718 if (*sym != '.')
719 return 0;
720 do {
721 char c = *sym++;
722 if (c < '0' || c > '9')
723 return 0;
724 } while (*sym);
725 return 1;
726}
727
728static int match(const char *sym, const char * const pat[])
729{
730 const char *p;
731 while (*pat) {
732 p = *pat++;
733 const char *endp = p + strlen(p) - 1;
734
735
736 if (*p == '*') {
737 if (strrcmp(sym, p + 1) == 0)
738 return 1;
739 }
740
741 else if (*endp == '*') {
742 if (strncmp(sym, p, strlen(p) - 1) == 0)
743 return 1;
744 }
745
746 else if (*endp == '$') {
747 if (strncmp(sym, p, strlen(p) - 1) == 0) {
748 if (number_prefix(sym + strlen(p) - 1))
749 return 1;
750 }
751 }
752
753 else {
754 if (strcmp(p, sym) == 0)
755 return 1;
756 }
757 }
758
759 return 0;
760}
761
762static const char *section_white_list[] =
763{
764 ".comment*",
765 ".debug*",
766 ".zdebug*",
767 ".GCC-command-line",
768 ".mdebug*",
769 ".pdr",
770 ".stab*",
771 ".note*",
772 ".got*",
773 ".toc*",
774 NULL
775};
776
777static void check_section(const char *modname, struct elf_info *elf,
778 Elf_Shdr *sechdr)
779{
780 const char *sec = sech_name(elf, sechdr);
781
782 if (sechdr->sh_type == SHT_PROGBITS &&
783 !(sechdr->sh_flags & SHF_ALLOC) &&
784 !match(sec, section_white_list)) {
785 warn("%s (%s): unexpected non-allocatable section.\n"
786 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
787 "Note that for example <linux/init.h> contains\n"
788 "section definitions for use in .S files.\n\n",
789 modname, sec);
790 }
791}
792
793
794
795#define ALL_INIT_DATA_SECTIONS \
796 ".init.setup$", ".init.rodata$", \
797 ".devinit.rodata$", ".cpuinit.rodata$", ".meminit.rodata$", \
798 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
799#define ALL_EXIT_DATA_SECTIONS \
800 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
801
802#define ALL_INIT_TEXT_SECTIONS \
803 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
804#define ALL_EXIT_TEXT_SECTIONS \
805 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
806
807#define ALL_XXXINIT_SECTIONS DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, \
808 MEM_INIT_SECTIONS
809#define ALL_XXXEXIT_SECTIONS DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, \
810 MEM_EXIT_SECTIONS
811
812#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
813#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
814
815#define DATA_SECTIONS ".data$", ".data.rel$"
816#define TEXT_SECTIONS ".text$"
817
818#define INIT_SECTIONS ".init.*"
819#define DEV_INIT_SECTIONS ".devinit.*"
820#define CPU_INIT_SECTIONS ".cpuinit.*"
821#define MEM_INIT_SECTIONS ".meminit.*"
822
823#define EXIT_SECTIONS ".exit.*"
824#define DEV_EXIT_SECTIONS ".devexit.*"
825#define CPU_EXIT_SECTIONS ".cpuexit.*"
826#define MEM_EXIT_SECTIONS ".memexit.*"
827
828static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
829
830static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
831
832static const char *init_exit_sections[] =
833 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
834
835static const char *data_sections[] = { DATA_SECTIONS, NULL };
836
837
838#define DEFAULT_SYMBOL_WHITE_LIST \
839 "*driver", \
840 "*_template", \
841 "*_timer", \
842 "*_sht", \
843 "*_ops", \
844 "*_probe", \
845 "*_probe_one", \
846 "*_console"
847
848static const char *head_sections[] = { ".head.text*", NULL };
849static const char *linker_symbols[] =
850 { "__init_begin", "_sinittext", "_einittext", NULL };
851
852enum mismatch {
853 TEXT_TO_ANY_INIT,
854 DATA_TO_ANY_INIT,
855 TEXT_TO_ANY_EXIT,
856 DATA_TO_ANY_EXIT,
857 XXXINIT_TO_SOME_INIT,
858 XXXEXIT_TO_SOME_EXIT,
859 ANY_INIT_TO_ANY_EXIT,
860 ANY_EXIT_TO_ANY_INIT,
861 EXPORT_TO_INIT_EXIT,
862};
863
864struct sectioncheck {
865 const char *fromsec[20];
866 const char *tosec[20];
867 enum mismatch mismatch;
868 const char *symbol_white_list[20];
869};
870
871const struct sectioncheck sectioncheck[] = {
872{
873 .fromsec = { TEXT_SECTIONS, NULL },
874 .tosec = { ALL_INIT_SECTIONS, NULL },
875 .mismatch = TEXT_TO_ANY_INIT,
876 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
877},
878{
879 .fromsec = { DATA_SECTIONS, NULL },
880 .tosec = { ALL_XXXINIT_SECTIONS, NULL },
881 .mismatch = DATA_TO_ANY_INIT,
882 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
883},
884{
885 .fromsec = { DATA_SECTIONS, NULL },
886 .tosec = { INIT_SECTIONS, NULL },
887 .mismatch = DATA_TO_ANY_INIT,
888 .symbol_white_list = {
889 "*_template", "*_timer", "*_sht", "*_ops",
890 "*_probe", "*_probe_one", "*_console", NULL
891 },
892},
893{
894 .fromsec = { TEXT_SECTIONS, NULL },
895 .tosec = { ALL_EXIT_SECTIONS, NULL },
896 .mismatch = TEXT_TO_ANY_EXIT,
897 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
898},
899{
900 .fromsec = { DATA_SECTIONS, NULL },
901 .tosec = { ALL_EXIT_SECTIONS, NULL },
902 .mismatch = DATA_TO_ANY_EXIT,
903 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
904},
905{
906 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
907 .tosec = { INIT_SECTIONS, NULL },
908 .mismatch = XXXINIT_TO_SOME_INIT,
909 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
910},
911{
912 .fromsec = { MEM_INIT_SECTIONS, NULL },
913 .tosec = { CPU_INIT_SECTIONS, NULL },
914 .mismatch = XXXINIT_TO_SOME_INIT,
915 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
916},
917{
918 .fromsec = { CPU_INIT_SECTIONS, NULL },
919 .tosec = { MEM_INIT_SECTIONS, NULL },
920 .mismatch = XXXINIT_TO_SOME_INIT,
921 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
922},
923{
924 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
925 .tosec = { EXIT_SECTIONS, NULL },
926 .mismatch = XXXEXIT_TO_SOME_EXIT,
927 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
928},
929{
930 .fromsec = { MEM_EXIT_SECTIONS, NULL },
931 .tosec = { CPU_EXIT_SECTIONS, NULL },
932 .mismatch = XXXEXIT_TO_SOME_EXIT,
933 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
934},
935{
936 .fromsec = { CPU_EXIT_SECTIONS, NULL },
937 .tosec = { MEM_EXIT_SECTIONS, NULL },
938 .mismatch = XXXEXIT_TO_SOME_EXIT,
939 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
940},
941{
942 .fromsec = { ALL_INIT_SECTIONS, NULL },
943 .tosec = { ALL_EXIT_SECTIONS, NULL },
944 .mismatch = ANY_INIT_TO_ANY_EXIT,
945 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
946},
947{
948 .fromsec = { ALL_EXIT_SECTIONS, NULL },
949 .tosec = { ALL_INIT_SECTIONS, NULL },
950 .mismatch = ANY_EXIT_TO_ANY_INIT,
951 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
952},
953{
954 .fromsec = { "__ksymtab*", NULL },
955 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
956 .mismatch = EXPORT_TO_INIT_EXIT,
957 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
958}
959};
960
961static const struct sectioncheck *section_mismatch(
962 const char *fromsec, const char *tosec)
963{
964 int i;
965 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
966 const struct sectioncheck *check = &sectioncheck[0];
967
968 for (i = 0; i < elems; i++) {
969 if (match(fromsec, check->fromsec) &&
970 match(tosec, check->tosec))
971 return check;
972 check++;
973 }
974 return NULL;
975}
976
977static int secref_whitelist(const struct sectioncheck *mismatch,
978 const char *fromsec, const char *fromsym,
979 const char *tosec, const char *tosym)
980{
981
982 if (match(tosec, init_data_sections) &&
983 match(fromsec, data_sections) &&
984 (strncmp(fromsym, "__param", strlen("__param")) == 0))
985 return 0;
986
987
988 if (strcmp(tosec, ".init.text") == 0 &&
989 match(fromsec, data_sections) &&
990 (strncmp(fromsym, "__param_ops_", strlen("__param_ops_")) == 0))
991 return 0;
992
993
994 if (match(tosec, init_exit_sections) &&
995 match(fromsec, data_sections) &&
996 match(fromsym, mismatch->symbol_white_list))
997 return 0;
998
999
1000 if (match(fromsec, head_sections) &&
1001 match(tosec, init_sections))
1002 return 0;
1003
1004
1005 if (match(tosym, linker_symbols))
1006 return 0;
1007
1008 return 1;
1009}
1010
1011static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
1012 Elf_Sym *relsym)
1013{
1014 Elf_Sym *sym;
1015 Elf_Sym *near = NULL;
1016 Elf64_Sword distance = 20;
1017 Elf64_Sword d;
1018 unsigned int relsym_secindex;
1019
1020 if (relsym->st_name != 0)
1021 return relsym;
1022
1023 relsym_secindex = get_secindex(elf, relsym);
1024 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1025 if (get_secindex(elf, sym) != relsym_secindex)
1026 continue;
1027 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1028 continue;
1029 if (sym->st_value == addr)
1030 return sym;
1031
1032 d = sym->st_value - addr;
1033 if (d < 0)
1034 d = addr - sym->st_value;
1035 if (d < distance) {
1036 distance = d;
1037 near = sym;
1038 }
1039 }
1040
1041 if (distance < 20)
1042 return near;
1043 else
1044 return NULL;
1045}
1046
1047static inline int is_arm_mapping_symbol(const char *str)
1048{
1049 return str[0] == '$' && strchr("atd", str[1])
1050 && (str[2] == '\0' || str[2] == '.');
1051}
1052
1053static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1054{
1055 const char *name = elf->strtab + sym->st_name;
1056
1057 if (!name || !strlen(name))
1058 return 0;
1059 return !is_arm_mapping_symbol(name);
1060}
1061
1062static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1063 const char *sec)
1064{
1065 Elf_Sym *sym;
1066 Elf_Sym *near = NULL;
1067 Elf_Addr distance = ~0;
1068
1069 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1070 const char *symsec;
1071
1072 if (is_shndx_special(sym->st_shndx))
1073 continue;
1074 symsec = sec_name(elf, get_secindex(elf, sym));
1075 if (strcmp(symsec, sec) != 0)
1076 continue;
1077 if (!is_valid_name(elf, sym))
1078 continue;
1079 if (sym->st_value <= addr) {
1080 if ((addr - sym->st_value) < distance) {
1081 distance = addr - sym->st_value;
1082 near = sym;
1083 } else if ((addr - sym->st_value) == distance) {
1084 near = sym;
1085 }
1086 }
1087 }
1088 return near;
1089}
1090
1091static char *sec2annotation(const char *s)
1092{
1093 if (match(s, init_exit_sections)) {
1094 char *p = malloc(20);
1095 char *r = p;
1096
1097 *p++ = '_';
1098 *p++ = '_';
1099 if (*s == '.')
1100 s++;
1101 while (*s && *s != '.')
1102 *p++ = *s++;
1103 *p = '\0';
1104 if (*s == '.')
1105 s++;
1106 if (strstr(s, "rodata") != NULL)
1107 strcat(p, "const ");
1108 else if (strstr(s, "data") != NULL)
1109 strcat(p, "data ");
1110 else
1111 strcat(p, " ");
1112 return r;
1113 } else {
1114 return strdup("");
1115 }
1116}
1117
1118static int is_function(Elf_Sym *sym)
1119{
1120 if (sym)
1121 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1122 else
1123 return -1;
1124}
1125
1126static void print_section_list(const char * const list[20])
1127{
1128 const char *const *s = list;
1129
1130 while (*s) {
1131 fprintf(stderr, "%s", *s);
1132 s++;
1133 if (*s)
1134 fprintf(stderr, ", ");
1135 }
1136 fprintf(stderr, "\n");
1137}
1138
1139static void report_sec_mismatch(const char *modname,
1140 const struct sectioncheck *mismatch,
1141 const char *fromsec,
1142 unsigned long long fromaddr,
1143 const char *fromsym,
1144 int from_is_func,
1145 const char *tosec, const char *tosym,
1146 int to_is_func)
1147{
1148 const char *from, *from_p;
1149 const char *to, *to_p;
1150 char *prl_from;
1151 char *prl_to;
1152
1153 switch (from_is_func) {
1154 case 0: from = "variable"; from_p = ""; break;
1155 case 1: from = "function"; from_p = "()"; break;
1156 default: from = "(unknown reference)"; from_p = ""; break;
1157 }
1158 switch (to_is_func) {
1159 case 0: to = "variable"; to_p = ""; break;
1160 case 1: to = "function"; to_p = "()"; break;
1161 default: to = "(unknown reference)"; to_p = ""; break;
1162 }
1163
1164 sec_mismatch_count++;
1165 if (!sec_mismatch_verbose)
1166 return;
1167
1168 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1169 "to the %s %s:%s%s\n",
1170 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1171 tosym, to_p);
1172
1173 switch (mismatch->mismatch) {
1174 case TEXT_TO_ANY_INIT:
1175 prl_from = sec2annotation(fromsec);
1176 prl_to = sec2annotation(tosec);
1177 fprintf(stderr,
1178 "The function %s%s() references\n"
1179 "the %s %s%s%s.\n"
1180 "This is often because %s lacks a %s\n"
1181 "annotation or the annotation of %s is wrong.\n",
1182 prl_from, fromsym,
1183 to, prl_to, tosym, to_p,
1184 fromsym, prl_to, tosym);
1185 free(prl_from);
1186 free(prl_to);
1187 break;
1188 case DATA_TO_ANY_INIT: {
1189 prl_to = sec2annotation(tosec);
1190 fprintf(stderr,
1191 "The variable %s references\n"
1192 "the %s %s%s%s\n"
1193 "If the reference is valid then annotate the\n"
1194 "variable with __init* or __refdata (see linux/init.h) "
1195 "or name the variable:\n",
1196 fromsym, to, prl_to, tosym, to_p);
1197 print_section_list(mismatch->symbol_white_list);
1198 free(prl_to);
1199 break;
1200 }
1201 case TEXT_TO_ANY_EXIT:
1202 prl_to = sec2annotation(tosec);
1203 fprintf(stderr,
1204 "The function %s() references a %s in an exit section.\n"
1205 "Often the %s %s%s has valid usage outside the exit section\n"
1206 "and the fix is to remove the %sannotation of %s.\n",
1207 fromsym, to, to, tosym, to_p, prl_to, tosym);
1208 free(prl_to);
1209 break;
1210 case DATA_TO_ANY_EXIT: {
1211 prl_to = sec2annotation(tosec);
1212 fprintf(stderr,
1213 "The variable %s references\n"
1214 "the %s %s%s%s\n"
1215 "If the reference is valid then annotate the\n"
1216 "variable with __exit* (see linux/init.h) or "
1217 "name the variable:\n",
1218 fromsym, to, prl_to, tosym, to_p);
1219 print_section_list(mismatch->symbol_white_list);
1220 free(prl_to);
1221 break;
1222 }
1223 case XXXINIT_TO_SOME_INIT:
1224 case XXXEXIT_TO_SOME_EXIT:
1225 prl_from = sec2annotation(fromsec);
1226 prl_to = sec2annotation(tosec);
1227 fprintf(stderr,
1228 "The %s %s%s%s references\n"
1229 "a %s %s%s%s.\n"
1230 "If %s is only used by %s then\n"
1231 "annotate %s with a matching annotation.\n",
1232 from, prl_from, fromsym, from_p,
1233 to, prl_to, tosym, to_p,
1234 tosym, fromsym, tosym);
1235 free(prl_from);
1236 free(prl_to);
1237 break;
1238 case ANY_INIT_TO_ANY_EXIT:
1239 prl_from = sec2annotation(fromsec);
1240 prl_to = sec2annotation(tosec);
1241 fprintf(stderr,
1242 "The %s %s%s%s references\n"
1243 "a %s %s%s%s.\n"
1244 "This is often seen when error handling "
1245 "in the init function\n"
1246 "uses functionality in the exit path.\n"
1247 "The fix is often to remove the %sannotation of\n"
1248 "%s%s so it may be used outside an exit section.\n",
1249 from, prl_from, fromsym, from_p,
1250 to, prl_to, tosym, to_p,
1251 prl_to, tosym, to_p);
1252 free(prl_from);
1253 free(prl_to);
1254 break;
1255 case ANY_EXIT_TO_ANY_INIT:
1256 prl_from = sec2annotation(fromsec);
1257 prl_to = sec2annotation(tosec);
1258 fprintf(stderr,
1259 "The %s %s%s%s references\n"
1260 "a %s %s%s%s.\n"
1261 "This is often seen when error handling "
1262 "in the exit function\n"
1263 "uses functionality in the init path.\n"
1264 "The fix is often to remove the %sannotation of\n"
1265 "%s%s so it may be used outside an init section.\n",
1266 from, prl_from, fromsym, from_p,
1267 to, prl_to, tosym, to_p,
1268 prl_to, tosym, to_p);
1269 free(prl_from);
1270 free(prl_to);
1271 break;
1272 case EXPORT_TO_INIT_EXIT:
1273 prl_to = sec2annotation(tosec);
1274 fprintf(stderr,
1275 "The symbol %s is exported and annotated %s\n"
1276 "Fix this by removing the %sannotation of %s "
1277 "or drop the export.\n",
1278 tosym, prl_to, prl_to, tosym);
1279 free(prl_to);
1280 break;
1281 }
1282 fprintf(stderr, "\n");
1283}
1284
1285static void check_section_mismatch(const char *modname, struct elf_info *elf,
1286 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1287{
1288 const char *tosec;
1289 const struct sectioncheck *mismatch;
1290
1291 tosec = sec_name(elf, get_secindex(elf, sym));
1292 mismatch = section_mismatch(fromsec, tosec);
1293 if (mismatch) {
1294 Elf_Sym *to;
1295 Elf_Sym *from;
1296 const char *tosym;
1297 const char *fromsym;
1298
1299 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1300 fromsym = sym_name(elf, from);
1301 to = find_elf_symbol(elf, r->r_addend, sym);
1302 tosym = sym_name(elf, to);
1303
1304
1305 if (secref_whitelist(mismatch,
1306 fromsec, fromsym, tosec, tosym)) {
1307 report_sec_mismatch(modname, mismatch,
1308 fromsec, r->r_offset, fromsym,
1309 is_function(from), tosec, tosym,
1310 is_function(to));
1311 }
1312 }
1313}
1314
1315static unsigned int *reloc_location(struct elf_info *elf,
1316 Elf_Shdr *sechdr, Elf_Rela *r)
1317{
1318 Elf_Shdr *sechdrs = elf->sechdrs;
1319 int section = sechdr->sh_info;
1320
1321 return (void *)elf->hdr + sechdrs[section].sh_offset +
1322 r->r_offset;
1323}
1324
1325static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1326{
1327 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1328 unsigned int *location = reloc_location(elf, sechdr, r);
1329
1330 switch (r_typ) {
1331 case R_386_32:
1332 r->r_addend = TO_NATIVE(*location);
1333 break;
1334 case R_386_PC32:
1335 r->r_addend = TO_NATIVE(*location) + 4;
1336
1337 if (elf->hdr->e_type == ET_EXEC)
1338 r->r_addend += r->r_offset;
1339 break;
1340 }
1341 return 0;
1342}
1343
1344#ifndef R_ARM_CALL
1345#define R_ARM_CALL 28
1346#endif
1347#ifndef R_ARM_JUMP24
1348#define R_ARM_JUMP24 29
1349#endif
1350
1351static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1352{
1353 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1354
1355 switch (r_typ) {
1356 case R_ARM_ABS32:
1357
1358 r->r_addend = (int)(long)
1359 (elf->symtab_start + ELF_R_SYM(r->r_info));
1360 break;
1361 case R_ARM_PC24:
1362 case R_ARM_CALL:
1363 case R_ARM_JUMP24:
1364
1365 r->r_addend = (int)(long)(elf->hdr +
1366 sechdr->sh_offset +
1367 (r->r_offset - sechdr->sh_addr));
1368 break;
1369 default:
1370 return 1;
1371 }
1372 return 0;
1373}
1374
1375static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
1376{
1377 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1378 unsigned int *location = reloc_location(elf, sechdr, r);
1379 unsigned int inst;
1380
1381 if (r_typ == R_MIPS_HI16)
1382 return 1;
1383 inst = TO_NATIVE(*location);
1384 switch (r_typ) {
1385 case R_MIPS_LO16:
1386 r->r_addend = inst & 0xffff;
1387 break;
1388 case R_MIPS_26:
1389 r->r_addend = (inst & 0x03ffffff) << 2;
1390 break;
1391 case R_MIPS_32:
1392 r->r_addend = inst;
1393 break;
1394 }
1395 return 0;
1396}
1397
1398static void section_rela(const char *modname, struct elf_info *elf,
1399 Elf_Shdr *sechdr)
1400{
1401 Elf_Sym *sym;
1402 Elf_Rela *rela;
1403 Elf_Rela r;
1404 unsigned int r_sym;
1405 const char *fromsec;
1406
1407 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
1408 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1409
1410 fromsec = sech_name(elf, sechdr);
1411 fromsec += strlen(".rela");
1412
1413 if (match(fromsec, section_white_list))
1414 return;
1415
1416 for (rela = start; rela < stop; rela++) {
1417 r.r_offset = TO_NATIVE(rela->r_offset);
1418#if KERNEL_ELFCLASS == ELFCLASS64
1419 if (elf->hdr->e_machine == EM_MIPS) {
1420 unsigned int r_typ;
1421 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1422 r_sym = TO_NATIVE(r_sym);
1423 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1424 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1425 } else {
1426 r.r_info = TO_NATIVE(rela->r_info);
1427 r_sym = ELF_R_SYM(r.r_info);
1428 }
1429#else
1430 r.r_info = TO_NATIVE(rela->r_info);
1431 r_sym = ELF_R_SYM(r.r_info);
1432#endif
1433 r.r_addend = TO_NATIVE(rela->r_addend);
1434 sym = elf->symtab_start + r_sym;
1435
1436 if (is_shndx_special(sym->st_shndx))
1437 continue;
1438 check_section_mismatch(modname, elf, &r, sym, fromsec);
1439 }
1440}
1441
1442static void section_rel(const char *modname, struct elf_info *elf,
1443 Elf_Shdr *sechdr)
1444{
1445 Elf_Sym *sym;
1446 Elf_Rel *rel;
1447 Elf_Rela r;
1448 unsigned int r_sym;
1449 const char *fromsec;
1450
1451 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
1452 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1453
1454 fromsec = sech_name(elf, sechdr);
1455 fromsec += strlen(".rel");
1456
1457 if (match(fromsec, section_white_list))
1458 return;
1459
1460 for (rel = start; rel < stop; rel++) {
1461 r.r_offset = TO_NATIVE(rel->r_offset);
1462#if KERNEL_ELFCLASS == ELFCLASS64
1463 if (elf->hdr->e_machine == EM_MIPS) {
1464 unsigned int r_typ;
1465 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1466 r_sym = TO_NATIVE(r_sym);
1467 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1468 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1469 } else {
1470 r.r_info = TO_NATIVE(rel->r_info);
1471 r_sym = ELF_R_SYM(r.r_info);
1472 }
1473#else
1474 r.r_info = TO_NATIVE(rel->r_info);
1475 r_sym = ELF_R_SYM(r.r_info);
1476#endif
1477 r.r_addend = 0;
1478 switch (elf->hdr->e_machine) {
1479 case EM_386:
1480 if (addend_386_rel(elf, sechdr, &r))
1481 continue;
1482 break;
1483 case EM_ARM:
1484 if (addend_arm_rel(elf, sechdr, &r))
1485 continue;
1486 break;
1487 case EM_MIPS:
1488 if (addend_mips_rel(elf, sechdr, &r))
1489 continue;
1490 break;
1491 }
1492 sym = elf->symtab_start + r_sym;
1493
1494 if (is_shndx_special(sym->st_shndx))
1495 continue;
1496 check_section_mismatch(modname, elf, &r, sym, fromsec);
1497 }
1498}
1499
1500static void check_sec_ref(struct module *mod, const char *modname,
1501 struct elf_info *elf)
1502{
1503 int i;
1504 Elf_Shdr *sechdrs = elf->sechdrs;
1505
1506
1507 for (i = 0; i < elf->num_sections; i++) {
1508 check_section(modname, elf, &elf->sechdrs[i]);
1509
1510 if (sechdrs[i].sh_type == SHT_RELA)
1511 section_rela(modname, elf, &elf->sechdrs[i]);
1512 else if (sechdrs[i].sh_type == SHT_REL)
1513 section_rel(modname, elf, &elf->sechdrs[i]);
1514 }
1515}
1516
1517static void read_symbols(char *modname)
1518{
1519 const char *symname;
1520 char *version;
1521 char *license;
1522 struct module *mod;
1523 struct elf_info info = { };
1524 Elf_Sym *sym;
1525
1526 if (!parse_elf(&info, modname))
1527 return;
1528
1529 mod = new_module(modname);
1530
1531 if (is_vmlinux(modname)) {
1532 have_vmlinux = 1;
1533 mod->skip = 1;
1534 }
1535
1536 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1537 if (info.modinfo && !license && !is_vmlinux(modname))
1538 warn("modpost: missing MODULE_LICENSE() in %s\n"
1539 "see include/linux/module.h for "
1540 "more information\n", modname);
1541 while (license) {
1542 if (license_is_gpl_compatible(license))
1543 mod->gpl_compatible = 1;
1544 else {
1545 mod->gpl_compatible = 0;
1546 break;
1547 }
1548 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1549 "license", license);
1550 }
1551
1552 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1553 symname = info.strtab + sym->st_name;
1554
1555 handle_modversions(mod, &info, sym, symname);
1556 handle_moddevtable(mod, &info, sym, symname);
1557 }
1558 if (!is_vmlinux(modname) ||
1559 (is_vmlinux(modname) && vmlinux_section_warnings))
1560 check_sec_ref(mod, modname, &info);
1561
1562 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1563 if (version)
1564 maybe_frob_rcs_version(modname, version, info.modinfo,
1565 version - (char *)info.hdr);
1566 if (version || (all_versions && !is_vmlinux(modname)))
1567 get_src_version(modname, mod->srcversion,
1568 sizeof(mod->srcversion)-1);
1569
1570 parse_elf_finish(&info);
1571
1572 if (modversions)
1573 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
1574}
1575
1576#define SZ 500
1577
1578
1579void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1580 const char *fmt, ...)
1581{
1582 char tmp[SZ];
1583 int len;
1584 va_list ap;
1585
1586 va_start(ap, fmt);
1587 len = vsnprintf(tmp, SZ, fmt, ap);
1588 buf_write(buf, tmp, len);
1589 va_end(ap);
1590}
1591
1592void buf_write(struct buffer *buf, const char *s, int len)
1593{
1594 if (buf->size - buf->pos < len) {
1595 buf->size += len + SZ;
1596 buf->p = realloc(buf->p, buf->size);
1597 }
1598 strncpy(buf->p + buf->pos, s, len);
1599 buf->pos += len;
1600}
1601
1602static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1603{
1604 const char *e = is_vmlinux(m) ?"":".ko";
1605
1606 switch (exp) {
1607 case export_gpl:
1608 fatal("modpost: GPL-incompatible module %s%s "
1609 "uses GPL-only symbol '%s'\n", m, e, s);
1610 break;
1611 case export_unused_gpl:
1612 fatal("modpost: GPL-incompatible module %s%s "
1613 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1614 break;
1615 case export_gpl_future:
1616 warn("modpost: GPL-incompatible module %s%s "
1617 "uses future GPL-only symbol '%s'\n", m, e, s);
1618 break;
1619 case export_plain:
1620 case export_unused:
1621 case export_unknown:
1622
1623 break;
1624 }
1625}
1626
1627static void check_for_unused(enum export exp, const char *m, const char *s)
1628{
1629 const char *e = is_vmlinux(m) ?"":".ko";
1630
1631 switch (exp) {
1632 case export_unused:
1633 case export_unused_gpl:
1634 warn("modpost: module %s%s "
1635 "uses symbol '%s' marked UNUSED\n", m, e, s);
1636 break;
1637 default:
1638
1639 break;
1640 }
1641}
1642
1643static void check_exports(struct module *mod)
1644{
1645 struct symbol *s, *exp;
1646
1647 for (s = mod->unres; s; s = s->next) {
1648 const char *basename;
1649 exp = find_symbol(s->name);
1650 if (!exp || exp->module == mod)
1651 continue;
1652 basename = strrchr(mod->name, '/');
1653 if (basename)
1654 basename++;
1655 else
1656 basename = mod->name;
1657 if (!mod->gpl_compatible)
1658 check_for_gpl_usage(exp->export, basename, exp->name);
1659 check_for_unused(exp->export, basename, exp->name);
1660 }
1661}
1662
1663static void add_header(struct buffer *b, struct module *mod)
1664{
1665 buf_printf(b, "#include <linux/module.h>\n");
1666 buf_printf(b, "#include <linux/vermagic.h>\n");
1667 buf_printf(b, "#include <linux/compiler.h>\n");
1668 buf_printf(b, "\n");
1669 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1670 buf_printf(b, "\n");
1671 buf_printf(b, "struct module __this_module\n");
1672 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
1673 buf_printf(b, " .name = KBUILD_MODNAME,\n");
1674 if (mod->has_init)
1675 buf_printf(b, " .init = init_module,\n");
1676 if (mod->has_cleanup)
1677 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1678 " .exit = cleanup_module,\n"
1679 "#endif\n");
1680 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
1681 buf_printf(b, "};\n");
1682}
1683
1684static void add_intree_flag(struct buffer *b, int is_intree)
1685{
1686 if (is_intree)
1687 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
1688}
1689
1690static void add_staging_flag(struct buffer *b, const char *name)
1691{
1692 static const char *staging_dir = "drivers/staging";
1693
1694 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
1695 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1696}
1697
1698static int add_versions(struct buffer *b, struct module *mod)
1699{
1700 struct symbol *s, *exp;
1701 int err = 0;
1702
1703 for (s = mod->unres; s; s = s->next) {
1704 exp = find_symbol(s->name);
1705 if (!exp || exp->module == mod) {
1706 if (have_vmlinux && !s->weak) {
1707 if (warn_unresolved) {
1708 warn("\"%s\" [%s.ko] undefined!\n",
1709 s->name, mod->name);
1710 } else {
1711 merror("\"%s\" [%s.ko] undefined!\n",
1712 s->name, mod->name);
1713 err = 1;
1714 }
1715 }
1716 continue;
1717 }
1718 s->module = exp->module;
1719 s->crc_valid = exp->crc_valid;
1720 s->crc = exp->crc;
1721 }
1722
1723 if (!modversions)
1724 return err;
1725
1726 buf_printf(b, "\n");
1727 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1728 buf_printf(b, "__used\n");
1729 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1730
1731 for (s = mod->unres; s; s = s->next) {
1732 if (!s->module)
1733 continue;
1734 if (!s->crc_valid) {
1735 warn("\"%s\" [%s.ko] has no CRC!\n",
1736 s->name, mod->name);
1737 continue;
1738 }
1739 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1740 }
1741
1742 buf_printf(b, "};\n");
1743
1744 return err;
1745}
1746
1747static void add_depends(struct buffer *b, struct module *mod,
1748 struct module *modules)
1749{
1750 struct symbol *s;
1751 struct module *m;
1752 int first = 1;
1753
1754 for (m = modules; m; m = m->next)
1755 m->seen = is_vmlinux(m->name);
1756
1757 buf_printf(b, "\n");
1758 buf_printf(b, "static const char __module_depends[]\n");
1759 buf_printf(b, "__used\n");
1760 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1761 buf_printf(b, "\"depends=");
1762 for (s = mod->unres; s; s = s->next) {
1763 const char *p;
1764 if (!s->module)
1765 continue;
1766
1767 if (s->module->seen)
1768 continue;
1769
1770 s->module->seen = 1;
1771 p = strrchr(s->module->name, '/');
1772 if (p)
1773 p++;
1774 else
1775 p = s->module->name;
1776 buf_printf(b, "%s%s", first ? "" : ",", p);
1777 first = 0;
1778 }
1779 buf_printf(b, "\";\n");
1780}
1781
1782static void add_srcversion(struct buffer *b, struct module *mod)
1783{
1784 if (mod->srcversion[0]) {
1785 buf_printf(b, "\n");
1786 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1787 mod->srcversion);
1788 }
1789}
1790
1791static void write_if_changed(struct buffer *b, const char *fname)
1792{
1793 char *tmp;
1794 FILE *file;
1795 struct stat st;
1796
1797 file = fopen(fname, "r");
1798 if (!file)
1799 goto write;
1800
1801 if (fstat(fileno(file), &st) < 0)
1802 goto close_write;
1803
1804 if (st.st_size != b->pos)
1805 goto close_write;
1806
1807 tmp = NOFAIL(malloc(b->pos));
1808 if (fread(tmp, 1, b->pos, file) != b->pos)
1809 goto free_write;
1810
1811 if (memcmp(tmp, b->p, b->pos) != 0)
1812 goto free_write;
1813
1814 free(tmp);
1815 fclose(file);
1816 return;
1817
1818 free_write:
1819 free(tmp);
1820 close_write:
1821 fclose(file);
1822 write:
1823 file = fopen(fname, "w");
1824 if (!file) {
1825 perror(fname);
1826 exit(1);
1827 }
1828 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1829 perror(fname);
1830 exit(1);
1831 }
1832 fclose(file);
1833}
1834
1835static void read_dump(const char *fname, unsigned int kernel)
1836{
1837 unsigned long size, pos = 0;
1838 void *file = grab_file(fname, &size);
1839 char *line;
1840
1841 if (!file)
1842
1843 return;
1844
1845 while ((line = get_next_line(&pos, file, size))) {
1846 char *symname, *modname, *d, *export, *end;
1847 unsigned int crc;
1848 struct module *mod;
1849 struct symbol *s;
1850
1851 if (!(symname = strchr(line, '\t')))
1852 goto fail;
1853 *symname++ = '\0';
1854 if (!(modname = strchr(symname, '\t')))
1855 goto fail;
1856 *modname++ = '\0';
1857 if ((export = strchr(modname, '\t')) != NULL)
1858 *export++ = '\0';
1859 if (export && ((end = strchr(export, '\t')) != NULL))
1860 *end = '\0';
1861 crc = strtoul(line, &d, 16);
1862 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1863 goto fail;
1864 mod = find_module(modname);
1865 if (!mod) {
1866 if (is_vmlinux(modname))
1867 have_vmlinux = 1;
1868 mod = new_module(modname);
1869 mod->skip = 1;
1870 }
1871 s = sym_add_exported(symname, mod, export_no(export));
1872 s->kernel = kernel;
1873 s->preloaded = 1;
1874 sym_update_crc(symname, mod, crc, export_no(export));
1875 }
1876 return;
1877fail:
1878 fatal("parse error in symbol dump file\n");
1879}
1880
1881static int dump_sym(struct symbol *sym)
1882{
1883 if (!external_module)
1884 return 1;
1885 if (sym->vmlinux || sym->kernel)
1886 return 0;
1887 return 1;
1888}
1889
1890static void write_dump(const char *fname)
1891{
1892 struct buffer buf = { };
1893 struct symbol *symbol;
1894 int n;
1895
1896 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1897 symbol = symbolhash[n];
1898 while (symbol) {
1899 if (dump_sym(symbol))
1900 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
1901 symbol->crc, symbol->name,
1902 symbol->module->name,
1903 export_str(symbol->export));
1904 symbol = symbol->next;
1905 }
1906 }
1907 write_if_changed(&buf, fname);
1908}
1909
1910struct ext_sym_list {
1911 struct ext_sym_list *next;
1912 const char *file;
1913};
1914
1915int main(int argc, char **argv)
1916{
1917 struct module *mod;
1918 struct buffer buf = { };
1919 char *kernel_read = NULL, *module_read = NULL;
1920 char *dump_write = NULL;
1921 int opt;
1922 int err;
1923 struct ext_sym_list *extsym_iter;
1924 struct ext_sym_list *extsym_start = NULL;
1925
1926 while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:E")) != -1) {
1927 switch (opt) {
1928 case 'i':
1929 kernel_read = optarg;
1930 break;
1931 case 'I':
1932 module_read = optarg;
1933 external_module = 1;
1934 break;
1935 case 'c':
1936 cross_build = 1;
1937 break;
1938 case 'e':
1939 external_module = 1;
1940 extsym_iter =
1941 NOFAIL(malloc(sizeof(*extsym_iter)));
1942 extsym_iter->next = extsym_start;
1943 extsym_iter->file = optarg;
1944 extsym_start = extsym_iter;
1945 break;
1946 case 'm':
1947 modversions = 1;
1948 break;
1949 case 'o':
1950 dump_write = optarg;
1951 break;
1952 case 'a':
1953 all_versions = 1;
1954 break;
1955 case 's':
1956 vmlinux_section_warnings = 0;
1957 break;
1958 case 'S':
1959 sec_mismatch_verbose = 0;
1960 break;
1961 case 'w':
1962 warn_unresolved = 1;
1963 break;
1964 case 'E':
1965 section_error_on_mismatch = 1;
1966 break;
1967 default:
1968 exit(1);
1969 }
1970 }
1971
1972 if (kernel_read)
1973 read_dump(kernel_read, 1);
1974 if (module_read)
1975 read_dump(module_read, 0);
1976 while (extsym_start) {
1977 read_dump(extsym_start->file, 0);
1978 extsym_iter = extsym_start->next;
1979 free(extsym_start);
1980 extsym_start = extsym_iter;
1981 }
1982
1983 while (optind < argc)
1984 read_symbols(argv[optind++]);
1985
1986 for (mod = modules; mod; mod = mod->next) {
1987 if (mod->skip)
1988 continue;
1989 check_exports(mod);
1990 }
1991
1992 err = 0;
1993
1994 for (mod = modules; mod; mod = mod->next) {
1995 char fname[strlen(mod->name) + 10];
1996
1997 if (mod->skip)
1998 continue;
1999
2000 buf.pos = 0;
2001
2002 add_header(&buf, mod);
2003 add_intree_flag(&buf, !external_module);
2004 add_staging_flag(&buf, mod->name);
2005 err |= add_versions(&buf, mod);
2006 add_depends(&buf, mod, modules);
2007 add_moddevtable(&buf, mod);
2008 add_srcversion(&buf, mod);
2009
2010 sprintf(fname, "%s.mod.c", mod->name);
2011 write_if_changed(&buf, fname);
2012 }
2013
2014 if (dump_write)
2015 write_dump(dump_write);
2016
2017 if (sec_mismatch_count && !sec_mismatch_verbose) {
2018 merror(
2019 "modpost: Found %d section mismatch(es).\n"
2020 "To see full details build your kernel with:\n"
2021 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2022 sec_mismatch_count);
2023
2024 }
2025
2026 if (sec_mismatch_count && section_error_on_mismatch) {
2027 err |= 1;
2028 printf(
2029 "To build the kernel despite the mismatches, "
2030 "build with:\n'make CONFIG_NO_ERROR_ON_MISMATCH=y'\n"
2031 "(NOTE: This is not recommended)\n");
2032 }
2033
2034 return err;
2035}