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