kbuild, modpost: fix "unexpected non-allocatable" warning with SUSE gcc
[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
b2e3e658
MD
14#define _GNU_SOURCE
15#include <stdio.h>
1da177e4
LT
16#include <ctype.h>
17#include "modpost.h"
b817f6fe 18#include "../../include/linux/license.h"
1da177e4
LT
19
20/* Are we using CONFIG_MODVERSIONS? */
21int modversions = 0;
22/* Warn about undefined symbols? (do so if we have vmlinux) */
23int have_vmlinux = 0;
24/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
25static int all_versions = 0;
040fcc81
SR
26/* If we are modposting external module set to 1 */
27static int external_module = 0;
8d8d8289
SR
28/* Warn about section mismatch in vmlinux if set to 1 */
29static int vmlinux_section_warnings = 1;
c53ddacd
KK
30/* Only warn about unresolved symbols */
31static int warn_unresolved = 0;
bd5cbced 32/* How a symbol is exported */
588ccd73
SR
33static int sec_mismatch_count = 0;
34static int sec_mismatch_verbose = 1;
35
c96fca21
SR
36enum export {
37 export_plain, export_unused, export_gpl,
38 export_unused_gpl, export_gpl_future, export_unknown
39};
1da177e4 40
6d9a89ea
AK
41#define PRINTF __attribute__ ((format (printf, 1, 2)))
42
43PRINTF void fatal(const char *fmt, ...)
1da177e4
LT
44{
45 va_list arglist;
46
47 fprintf(stderr, "FATAL: ");
48
49 va_start(arglist, fmt);
50 vfprintf(stderr, fmt, arglist);
51 va_end(arglist);
52
53 exit(1);
54}
55
6d9a89ea 56PRINTF void warn(const char *fmt, ...)
1da177e4
LT
57{
58 va_list arglist;
59
60 fprintf(stderr, "WARNING: ");
61
62 va_start(arglist, fmt);
63 vfprintf(stderr, fmt, arglist);
64 va_end(arglist);
65}
66
6d9a89ea 67PRINTF void merror(const char *fmt, ...)
2a116659
MW
68{
69 va_list arglist;
70
71 fprintf(stderr, "ERROR: ");
72
73 va_start(arglist, fmt);
74 vfprintf(stderr, fmt, arglist);
75 va_end(arglist);
76}
77
040fcc81
SR
78static int is_vmlinux(const char *modname)
79{
80 const char *myname;
81
df578e7d
SR
82 myname = strrchr(modname, '/');
83 if (myname)
040fcc81
SR
84 myname++;
85 else
86 myname = modname;
87
741f98fe
SR
88 return (strcmp(myname, "vmlinux") == 0) ||
89 (strcmp(myname, "vmlinux.o") == 0);
040fcc81
SR
90}
91
1da177e4
LT
92void *do_nofail(void *ptr, const char *expr)
93{
df578e7d 94 if (!ptr)
1da177e4 95 fatal("modpost: Memory allocation failure: %s.\n", expr);
df578e7d 96
1da177e4
LT
97 return ptr;
98}
99
100/* A list of all modules we processed */
1da177e4
LT
101static struct module *modules;
102
5c3ead8c 103static struct module *find_module(char *modname)
1da177e4
LT
104{
105 struct module *mod;
106
107 for (mod = modules; mod; mod = mod->next)
108 if (strcmp(mod->name, modname) == 0)
109 break;
110 return mod;
111}
112
5c3ead8c 113static struct module *new_module(char *modname)
1da177e4
LT
114{
115 struct module *mod;
116 char *p, *s;
62070fa4 117
1da177e4
LT
118 mod = NOFAIL(malloc(sizeof(*mod)));
119 memset(mod, 0, sizeof(*mod));
120 p = NOFAIL(strdup(modname));
121
122 /* strip trailing .o */
df578e7d
SR
123 s = strrchr(p, '.');
124 if (s != NULL)
1da177e4
LT
125 if (strcmp(s, ".o") == 0)
126 *s = '\0';
127
128 /* add to list */
129 mod->name = p;
b817f6fe 130 mod->gpl_compatible = -1;
1da177e4
LT
131 mod->next = modules;
132 modules = mod;
133
134 return mod;
135}
136
137/* A hash of all exported symbols,
138 * struct symbol is also used for lists of unresolved symbols */
139
140#define SYMBOL_HASH_SIZE 1024
141
142struct symbol {
143 struct symbol *next;
144 struct module *module;
145 unsigned int crc;
146 int crc_valid;
147 unsigned int weak:1;
040fcc81
SR
148 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
149 unsigned int kernel:1; /* 1 if symbol is from kernel
150 * (only for external modules) **/
8e70c458 151 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
bd5cbced 152 enum export export; /* Type of export */
1da177e4
LT
153 char name[0];
154};
155
156static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
157
158/* This is based on the hash agorithm from gdbm, via tdb */
159static inline unsigned int tdb_hash(const char *name)
160{
161 unsigned value; /* Used to compute the hash value. */
162 unsigned i; /* Used to cycle through random values. */
163
164 /* Set the initial value from the key size. */
df578e7d 165 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
1da177e4
LT
166 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
167
168 return (1103515243 * value + 12345);
169}
170
5c3ead8c
SR
171/**
172 * Allocate a new symbols for use in the hash of exported symbols or
173 * the list of unresolved symbols per module
174 **/
175static struct symbol *alloc_symbol(const char *name, unsigned int weak,
176 struct symbol *next)
1da177e4
LT
177{
178 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
179
180 memset(s, 0, sizeof(*s));
181 strcpy(s->name, name);
182 s->weak = weak;
183 s->next = next;
184 return s;
185}
186
187/* For the hash of exported symbols */
bd5cbced
RP
188static struct symbol *new_symbol(const char *name, struct module *module,
189 enum export export)
1da177e4
LT
190{
191 unsigned int hash;
192 struct symbol *new;
193
194 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
195 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
196 new->module = module;
bd5cbced 197 new->export = export;
040fcc81 198 return new;
1da177e4
LT
199}
200
5c3ead8c 201static struct symbol *find_symbol(const char *name)
1da177e4
LT
202{
203 struct symbol *s;
204
205 /* For our purposes, .foo matches foo. PPC64 needs this. */
206 if (name[0] == '.')
207 name++;
208
df578e7d 209 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
1da177e4
LT
210 if (strcmp(s->name, name) == 0)
211 return s;
212 }
213 return NULL;
214}
215
bd5cbced
RP
216static struct {
217 const char *str;
218 enum export export;
219} export_list[] = {
220 { .str = "EXPORT_SYMBOL", .export = export_plain },
c96fca21 221 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
bd5cbced 222 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
c96fca21 223 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
bd5cbced
RP
224 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
225 { .str = "(unknown)", .export = export_unknown },
226};
227
228
229static const char *export_str(enum export ex)
230{
231 return export_list[ex].str;
232}
233
df578e7d 234static enum export export_no(const char *s)
bd5cbced
RP
235{
236 int i;
df578e7d 237
534b89a9
SR
238 if (!s)
239 return export_unknown;
bd5cbced
RP
240 for (i = 0; export_list[i].export != export_unknown; i++) {
241 if (strcmp(export_list[i].str, s) == 0)
242 return export_list[i].export;
243 }
244 return export_unknown;
245}
246
247static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
248{
249 if (sec == elf->export_sec)
250 return export_plain;
c96fca21
SR
251 else if (sec == elf->export_unused_sec)
252 return export_unused;
bd5cbced
RP
253 else if (sec == elf->export_gpl_sec)
254 return export_gpl;
c96fca21
SR
255 else if (sec == elf->export_unused_gpl_sec)
256 return export_unused_gpl;
bd5cbced
RP
257 else if (sec == elf->export_gpl_future_sec)
258 return export_gpl_future;
259 else
260 return export_unknown;
261}
262
5c3ead8c
SR
263/**
264 * Add an exported symbol - it may have already been added without a
265 * CRC, in this case just update the CRC
266 **/
bd5cbced
RP
267static struct symbol *sym_add_exported(const char *name, struct module *mod,
268 enum export export)
1da177e4
LT
269{
270 struct symbol *s = find_symbol(name);
271
272 if (!s) {
bd5cbced 273 s = new_symbol(name, mod, export);
8e70c458
SR
274 } else {
275 if (!s->preloaded) {
7b75b13c 276 warn("%s: '%s' exported twice. Previous export "
8e70c458
SR
277 "was in %s%s\n", mod->name, name,
278 s->module->name,
279 is_vmlinux(s->module->name) ?"":".ko");
4b21960f
TP
280 } else {
281 /* In case Modules.symvers was out of date */
282 s->module = mod;
8e70c458 283 }
1da177e4 284 }
8e70c458 285 s->preloaded = 0;
040fcc81
SR
286 s->vmlinux = is_vmlinux(mod->name);
287 s->kernel = 0;
bd5cbced 288 s->export = export;
040fcc81
SR
289 return s;
290}
291
292static void sym_update_crc(const char *name, struct module *mod,
bd5cbced 293 unsigned int crc, enum export export)
040fcc81
SR
294{
295 struct symbol *s = find_symbol(name);
296
297 if (!s)
bd5cbced 298 s = new_symbol(name, mod, export);
040fcc81
SR
299 s->crc = crc;
300 s->crc_valid = 1;
1da177e4
LT
301}
302
5c3ead8c 303void *grab_file(const char *filename, unsigned long *size)
1da177e4
LT
304{
305 struct stat st;
306 void *map;
307 int fd;
308
309 fd = open(filename, O_RDONLY);
310 if (fd < 0 || fstat(fd, &st) != 0)
311 return NULL;
312
313 *size = st.st_size;
314 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
315 close(fd);
316
317 if (map == MAP_FAILED)
318 return NULL;
319 return map;
320}
321
5c3ead8c
SR
322/**
323 * Return a copy of the next line in a mmap'ed file.
324 * spaces in the beginning of the line is trimmed away.
325 * Return a pointer to a static buffer.
326 **/
df578e7d 327char *get_next_line(unsigned long *pos, void *file, unsigned long size)
1da177e4
LT
328{
329 static char line[4096];
330 int skip = 1;
331 size_t len = 0;
332 signed char *p = (signed char *)file + *pos;
333 char *s = line;
334
df578e7d 335 for (; *pos < size ; (*pos)++) {
1da177e4
LT
336 if (skip && isspace(*p)) {
337 p++;
338 continue;
339 }
340 skip = 0;
341 if (*p != '\n' && (*pos < size)) {
342 len++;
343 *s++ = *p++;
344 if (len > 4095)
345 break; /* Too long, stop */
346 } else {
347 /* End of string */
348 *s = '\0';
349 return line;
350 }
351 }
352 /* End of buffer */
353 return NULL;
354}
355
5c3ead8c 356void release_file(void *file, unsigned long size)
1da177e4
LT
357{
358 munmap(file, size);
359}
360
85bd2fdd 361static int parse_elf(struct elf_info *info, const char *filename)
1da177e4
LT
362{
363 unsigned int i;
85bd2fdd 364 Elf_Ehdr *hdr;
1da177e4
LT
365 Elf_Shdr *sechdrs;
366 Elf_Sym *sym;
367
368 hdr = grab_file(filename, &info->size);
369 if (!hdr) {
370 perror(filename);
6803dc0e 371 exit(1);
1da177e4
LT
372 }
373 info->hdr = hdr;
85bd2fdd
SR
374 if (info->size < sizeof(*hdr)) {
375 /* file too small, assume this is an empty .o file */
376 return 0;
377 }
378 /* Is this a valid ELF file? */
379 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
380 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
381 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
382 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
383 /* Not an ELF file - silently ignore it */
384 return 0;
385 }
1da177e4 386 /* Fix endianness in ELF header */
7d875a02
AK
387 hdr->e_type = TO_NATIVE(hdr->e_type);
388 hdr->e_machine = TO_NATIVE(hdr->e_machine);
389 hdr->e_version = TO_NATIVE(hdr->e_version);
390 hdr->e_entry = TO_NATIVE(hdr->e_entry);
391 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
392 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
393 hdr->e_flags = TO_NATIVE(hdr->e_flags);
394 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
395 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
396 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
397 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
398 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
399 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
1da177e4
LT
400 sechdrs = (void *)hdr + hdr->e_shoff;
401 info->sechdrs = sechdrs;
402
a83710e5
PS
403 /* Check if file offset is correct */
404 if (hdr->e_shoff > info->size) {
df578e7d
SR
405 fatal("section header offset=%lu in file '%s' is bigger than "
406 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
407 filename, info->size);
a83710e5
PS
408 return 0;
409 }
410
1da177e4
LT
411 /* Fix endianness in section headers */
412 for (i = 0; i < hdr->e_shnum; i++) {
7d875a02
AK
413 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
414 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
415 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
416 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
417 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
418 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
419 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
420 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
421 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
422 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
1da177e4
LT
423 }
424 /* Find symbol table. */
425 for (i = 1; i < hdr->e_shnum; i++) {
426 const char *secstrings
427 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
bd5cbced 428 const char *secname;
56fc82c5 429 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
1da177e4 430
56fc82c5 431 if (!nobits && sechdrs[i].sh_offset > info->size) {
df578e7d
SR
432 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
433 "sizeof(*hrd)=%zu\n", filename,
434 (unsigned long)sechdrs[i].sh_offset,
435 sizeof(*hdr));
85bd2fdd
SR
436 return 0;
437 }
bd5cbced
RP
438 secname = secstrings + sechdrs[i].sh_name;
439 if (strcmp(secname, ".modinfo") == 0) {
56fc82c5
TH
440 if (nobits)
441 fatal("%s has NOBITS .modinfo\n", filename);
1da177e4
LT
442 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
443 info->modinfo_len = sechdrs[i].sh_size;
bd5cbced
RP
444 } else if (strcmp(secname, "__ksymtab") == 0)
445 info->export_sec = i;
c96fca21
SR
446 else if (strcmp(secname, "__ksymtab_unused") == 0)
447 info->export_unused_sec = i;
bd5cbced
RP
448 else if (strcmp(secname, "__ksymtab_gpl") == 0)
449 info->export_gpl_sec = i;
c96fca21
SR
450 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
451 info->export_unused_gpl_sec = i;
bd5cbced
RP
452 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
453 info->export_gpl_future_sec = i;
b2e3e658
MD
454 else if (strcmp(secname, "__markers_strings") == 0)
455 info->markers_strings_sec = i;
bd5cbced 456
1da177e4
LT
457 if (sechdrs[i].sh_type != SHT_SYMTAB)
458 continue;
459
460 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
62070fa4 461 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
1da177e4 462 + sechdrs[i].sh_size;
62070fa4 463 info->strtab = (void *)hdr +
1da177e4
LT
464 sechdrs[sechdrs[i].sh_link].sh_offset;
465 }
df578e7d 466 if (!info->symtab_start)
cb80514d 467 fatal("%s has no symtab?\n", filename);
df578e7d 468
1da177e4
LT
469 /* Fix endianness in symbols */
470 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
471 sym->st_shndx = TO_NATIVE(sym->st_shndx);
472 sym->st_name = TO_NATIVE(sym->st_name);
473 sym->st_value = TO_NATIVE(sym->st_value);
474 sym->st_size = TO_NATIVE(sym->st_size);
475 }
85bd2fdd 476 return 1;
1da177e4
LT
477}
478
5c3ead8c 479static void parse_elf_finish(struct elf_info *info)
1da177e4
LT
480{
481 release_file(info->hdr, info->size);
482}
483
4d7365d6
SR
484static int ignore_undef_symbol(struct elf_info *info, const char *symname)
485{
486 /* ignore __this_module, it will be resolved shortly */
487 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
488 return 1;
489 /* ignore global offset table */
490 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
491 return 1;
492 if (info->hdr->e_machine == EM_PPC)
493 /* Special register function linked on all modules during final link of .ko */
494 if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
495 strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
496 strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
497 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
498 return 1;
499 /* Do not ignore this symbol */
500 return 0;
501}
502
f7b05e64
LY
503#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
504#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
1da177e4 505
5c3ead8c
SR
506static void handle_modversions(struct module *mod, struct elf_info *info,
507 Elf_Sym *sym, const char *symname)
1da177e4
LT
508{
509 unsigned int crc;
bd5cbced 510 enum export export = export_from_sec(info, sym->st_shndx);
1da177e4
LT
511
512 switch (sym->st_shndx) {
513 case SHN_COMMON:
cb80514d 514 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
1da177e4
LT
515 break;
516 case SHN_ABS:
517 /* CRC'd symbol */
518 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
519 crc = (unsigned int) sym->st_value;
bd5cbced
RP
520 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
521 export);
1da177e4
LT
522 }
523 break;
524 case SHN_UNDEF:
525 /* undefined symbol */
526 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
527 ELF_ST_BIND(sym->st_info) != STB_WEAK)
528 break;
4d7365d6 529 if (ignore_undef_symbol(info, symname))
1da177e4 530 break;
8d529014
BC
531/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
532#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
533/* add compatibility with older glibc */
534#ifndef STT_SPARC_REGISTER
535#define STT_SPARC_REGISTER STT_REGISTER
536#endif
1da177e4
LT
537 if (info->hdr->e_machine == EM_SPARC ||
538 info->hdr->e_machine == EM_SPARCV9) {
539 /* Ignore register directives. */
8d529014 540 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
1da177e4 541 break;
62070fa4
SR
542 if (symname[0] == '.') {
543 char *munged = strdup(symname);
544 munged[0] = '_';
545 munged[1] = toupper(munged[1]);
546 symname = munged;
547 }
1da177e4
LT
548 }
549#endif
62070fa4 550
1da177e4 551 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
df578e7d
SR
552 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
553 mod->unres =
554 alloc_symbol(symname +
555 strlen(MODULE_SYMBOL_PREFIX),
556 ELF_ST_BIND(sym->st_info) == STB_WEAK,
557 mod->unres);
558 }
1da177e4
LT
559 break;
560 default:
561 /* All exported symbols */
562 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
bd5cbced
RP
563 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
564 export);
1da177e4
LT
565 }
566 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
567 mod->has_init = 1;
568 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
569 mod->has_cleanup = 1;
570 break;
571 }
572}
573
5c3ead8c
SR
574/**
575 * Parse tag=value strings from .modinfo section
576 **/
1da177e4
LT
577static char *next_string(char *string, unsigned long *secsize)
578{
579 /* Skip non-zero chars */
580 while (string[0]) {
581 string++;
582 if ((*secsize)-- <= 1)
583 return NULL;
584 }
585
586 /* Skip any zero padding. */
587 while (!string[0]) {
588 string++;
589 if ((*secsize)-- <= 1)
590 return NULL;
591 }
592 return string;
593}
594
b817f6fe
SR
595static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
596 const char *tag, char *info)
1da177e4
LT
597{
598 char *p;
599 unsigned int taglen = strlen(tag);
600 unsigned long size = modinfo_len;
601
b817f6fe
SR
602 if (info) {
603 size -= info - (char *)modinfo;
604 modinfo = next_string(info, &size);
605 }
606
1da177e4
LT
607 for (p = modinfo; p; p = next_string(p, &size)) {
608 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
609 return p + taglen + 1;
610 }
611 return NULL;
612}
613
b817f6fe
SR
614static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
615 const char *tag)
616
617{
618 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
619}
620
4c8fbca5
SR
621/**
622 * Test if string s ends in string sub
623 * return 0 if match
624 **/
625static int strrcmp(const char *s, const char *sub)
626{
df578e7d 627 int slen, sublen;
62070fa4 628
4c8fbca5
SR
629 if (!s || !sub)
630 return 1;
62070fa4 631
4c8fbca5 632 slen = strlen(s);
df578e7d 633 sublen = strlen(sub);
62070fa4 634
4c8fbca5
SR
635 if ((slen == 0) || (sublen == 0))
636 return 1;
637
df578e7d
SR
638 if (sublen > slen)
639 return 1;
4c8fbca5 640
df578e7d 641 return memcmp(s + slen - sublen, sub, sublen);
4c8fbca5
SR
642}
643
ff13f926
SR
644static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
645{
58fb0d4f
SR
646 if (sym)
647 return elf->strtab + sym->st_name;
648 else
f666751a 649 return "(unknown)";
ff13f926
SR
650}
651
652static const char *sec_name(struct elf_info *elf, int shndx)
653{
654 Elf_Shdr *sechdrs = elf->sechdrs;
655 return (void *)elf->hdr +
656 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
657 sechdrs[shndx].sh_name;
658}
659
660static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
661{
662 return (void *)elf->hdr +
663 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
664 sechdr->sh_name;
665}
666
10668220
SR
667/* if sym is empty or point to a string
668 * like ".[0-9]+" then return 1.
669 * This is the optional prefix added by ld to some sections
670 */
671static int number_prefix(const char *sym)
672{
673 if (*sym++ == '\0')
674 return 1;
675 if (*sym != '.')
676 return 0;
677 do {
678 char c = *sym++;
679 if (c < '0' || c > '9')
680 return 0;
681 } while (*sym);
682 return 1;
683}
684
685/* The pattern is an array of simple patterns.
686 * "foo" will match an exact string equal to "foo"
6c5bd235 687 * "*foo" will match a string that ends with "foo"
10668220
SR
688 * "foo*" will match a string that begins with "foo"
689 * "foo$" will match a string equal to "foo" or "foo.1"
690 * where the '1' can be any number including several digits.
691 * The $ syntax is for sections where ld append a dot number
692 * to make section name unique.
693 */
694int match(const char *sym, const char * const pat[])
695{
696 const char *p;
697 while (*pat) {
698 p = *pat++;
699 const char *endp = p + strlen(p) - 1;
700
6c5bd235
SR
701 /* "*foo" */
702 if (*p == '*') {
703 if (strrcmp(sym, p + 1) == 0)
704 return 1;
705 }
10668220 706 /* "foo*" */
6c5bd235 707 else if (*endp == '*') {
10668220
SR
708 if (strncmp(sym, p, strlen(p) - 1) == 0)
709 return 1;
710 }
711 /* "foo$" */
712 else if (*endp == '$') {
713 if (strncmp(sym, p, strlen(p) - 1) == 0) {
714 if (number_prefix(sym + strlen(p) - 1))
715 return 1;
716 }
717 }
718 /* no wildcards */
719 else {
720 if (strcmp(p, sym) == 0)
721 return 1;
722 }
723 }
724 /* no match */
725 return 0;
726}
727
10668220
SR
728/* sections that we do not want to do full section mismatch check on */
729static const char *section_white_list[] =
028ecebd 730 { ".comment*", ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
10668220 731
e241a630 732/*
b614a697 733 * This is used to find sections missing the SHF_ALLOC flag.
e241a630 734 * The cause of this is often a section specified in assembler
b614a697 735 * without "ax" / "aw".
e241a630 736 */
b614a697
AK
737static void check_section(const char *modname, struct elf_info *elf,
738 Elf_Shdr *sechdr)
e241a630 739{
b614a697
AK
740 const char *sec = sech_name(elf, sechdr);
741
742 if (sechdr->sh_type == SHT_PROGBITS &&
743 !(sechdr->sh_flags & SHF_ALLOC) &&
744 !match(sec, section_white_list)) {
745 warn("%s (%s): unexpected non-allocatable section.\n"
746 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
747 "Note that for example <linux/init.h> contains\n"
748 "section definitions for use in .S files.\n\n",
749 modname, sec);
e241a630 750 }
e241a630
SR
751}
752
753
754
eb8f6890
SR
755#define ALL_INIT_DATA_SECTIONS \
756 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
757#define ALL_EXIT_DATA_SECTIONS \
758 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
10668220 759
eb8f6890
SR
760#define ALL_INIT_TEXT_SECTIONS \
761 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
762#define ALL_EXIT_TEXT_SECTIONS \
763 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
10668220 764
eb8f6890
SR
765#define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
766#define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
10668220 767
6c5bd235 768#define DATA_SECTIONS ".data$", ".data.rel$"
10668220
SR
769#define TEXT_SECTIONS ".text$"
770
eb8f6890
SR
771#define INIT_SECTIONS ".init.data$", ".init.text$"
772#define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
773#define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
774#define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
775
776#define EXIT_SECTIONS ".exit.data$", ".exit.text$"
777#define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
778#define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
779#define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
780
6c5bd235 781/* init data sections */
eb8f6890 782static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
6c5bd235
SR
783
784/* all init sections */
eb8f6890 785static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
6c5bd235
SR
786
787/* All init and exit sections (code + data) */
788static const char *init_exit_sections[] =
eb8f6890 789 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
6c5bd235
SR
790
791/* data section */
792static const char *data_sections[] = { DATA_SECTIONS, NULL };
793
6c5bd235
SR
794
795/* symbols in .data that may refer to init/exit sections */
796static const char *symbol_white_list[] =
797{
798 "*driver",
799 "*_template", /* scsi uses *_template a lot */
800 "*_timer", /* arm uses ops structures named _timer a lot */
801 "*_sht", /* scsi also used *_sht to some extent */
802 "*_ops",
803 "*_probe",
804 "*_probe_one",
805 "*_console",
806 NULL
807};
808
809static const char *head_sections[] = { ".head.text*", NULL };
810static const char *linker_symbols[] =
811 { "__init_begin", "_sinittext", "_einittext", NULL };
812
588ccd73
SR
813enum mismatch {
814 NO_MISMATCH,
815 TEXT_TO_INIT,
816 DATA_TO_INIT,
817 TEXT_TO_EXIT,
818 DATA_TO_EXIT,
819 XXXINIT_TO_INIT,
820 XXXEXIT_TO_EXIT,
821 INIT_TO_EXIT,
822 EXIT_TO_INIT,
823 EXPORT_TO_INIT_EXIT,
824};
825
10668220
SR
826struct sectioncheck {
827 const char *fromsec[20];
828 const char *tosec[20];
588ccd73 829 enum mismatch mismatch;
10668220
SR
830};
831
832const struct sectioncheck sectioncheck[] = {
833/* Do not reference init/exit code/data from
834 * normal code and data
835 */
836{
588ccd73
SR
837 .fromsec = { TEXT_SECTIONS, NULL },
838 .tosec = { ALL_INIT_SECTIONS, NULL },
839 .mismatch = TEXT_TO_INIT,
840},
841{
842 .fromsec = { DATA_SECTIONS, NULL },
843 .tosec = { ALL_INIT_SECTIONS, NULL },
844 .mismatch = DATA_TO_INIT,
845},
846{
847 .fromsec = { TEXT_SECTIONS, NULL },
848 .tosec = { ALL_EXIT_SECTIONS, NULL },
849 .mismatch = TEXT_TO_EXIT,
850},
851{
852 .fromsec = { DATA_SECTIONS, NULL },
853 .tosec = { ALL_EXIT_SECTIONS, NULL },
854 .mismatch = DATA_TO_EXIT,
eb8f6890
SR
855},
856/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
857{
858 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
588ccd73
SR
859 .tosec = { INIT_SECTIONS, NULL },
860 .mismatch = XXXINIT_TO_INIT,
eb8f6890
SR
861},
862/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
863{
864 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
588ccd73
SR
865 .tosec = { EXIT_SECTIONS, NULL },
866 .mismatch = XXXEXIT_TO_EXIT,
10668220
SR
867},
868/* Do not use exit code/data from init code */
869{
eb8f6890
SR
870 .fromsec = { ALL_INIT_SECTIONS, NULL },
871 .tosec = { ALL_EXIT_SECTIONS, NULL },
588ccd73 872 .mismatch = INIT_TO_EXIT,
10668220
SR
873},
874/* Do not use init code/data from exit code */
875{
eb8f6890 876 .fromsec = { ALL_EXIT_SECTIONS, NULL },
588ccd73
SR
877 .tosec = { ALL_INIT_SECTIONS, NULL },
878 .mismatch = EXIT_TO_INIT,
10668220
SR
879},
880/* Do not export init/exit functions or data */
881{
882 .fromsec = { "__ksymtab*", NULL },
fa95eb1f 883 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
588ccd73 884 .mismatch = EXPORT_TO_INIT_EXIT
10668220
SR
885}
886};
887
888static int section_mismatch(const char *fromsec, const char *tosec)
889{
890 int i;
891 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
892 const struct sectioncheck *check = &sectioncheck[0];
893
894 for (i = 0; i < elems; i++) {
895 if (match(fromsec, check->fromsec) &&
896 match(tosec, check->tosec))
588ccd73 897 return check->mismatch;
10668220
SR
898 check++;
899 }
588ccd73 900 return NO_MISMATCH;
10668220
SR
901}
902
4c8fbca5
SR
903/**
904 * Whitelist to allow certain references to pass with no warning.
0e0d314e 905 *
4c8fbca5
SR
906 * Pattern 1:
907 * If a module parameter is declared __initdata and permissions=0
908 * then this is legal despite the warning generated.
909 * We cannot see value of permissions here, so just ignore
910 * this pattern.
911 * The pattern is identified by:
912 * tosec = .init.data
9209aed0 913 * fromsec = .data*
4c8fbca5 914 * atsym =__param*
62070fa4 915 *
4c8fbca5 916 * Pattern 2:
72ee59b5 917 * Many drivers utilise a *driver container with references to
4c8fbca5
SR
918 * add, remove, probe functions etc.
919 * These functions may often be marked __init and we do not want to
920 * warn here.
921 * the pattern is identified by:
83cda2bb
SR
922 * tosec = init or exit section
923 * fromsec = data section
df578e7d
SR
924 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
925 * *probe_one, *_console, *_timer
ee6a8545
VG
926 *
927 * Pattern 3:
c993971f 928 * Whitelist all references from .head.text to any init section
9bf8cb9b 929 *
1d8af559 930 * Pattern 4:
ee6a8545
VG
931 * Some symbols belong to init section but still it is ok to reference
932 * these from non-init sections as these symbols don't have any memory
933 * allocated for them and symbol address and value are same. So even
934 * if init section is freed, its ok to reference those symbols.
935 * For ex. symbols marking the init section boundaries.
936 * This pattern is identified by
937 * refsymname = __init_begin, _sinittext, _einittext
9bf8cb9b 938 *
4c8fbca5 939 **/
58fb0d4f
SR
940static int secref_whitelist(const char *fromsec, const char *fromsym,
941 const char *tosec, const char *tosym)
4c8fbca5 942{
4c8fbca5 943 /* Check for pattern 1 */
6c5bd235
SR
944 if (match(tosec, init_data_sections) &&
945 match(fromsec, data_sections) &&
58fb0d4f
SR
946 (strncmp(fromsym, "__param", strlen("__param")) == 0))
947 return 0;
4c8fbca5
SR
948
949 /* Check for pattern 2 */
6c5bd235
SR
950 if (match(tosec, init_exit_sections) &&
951 match(fromsec, data_sections) &&
58fb0d4f
SR
952 match(fromsym, symbol_white_list))
953 return 0;
4c8fbca5 954
9bf8cb9b 955 /* Check for pattern 3 */
6c5bd235
SR
956 if (match(fromsec, head_sections) &&
957 match(tosec, init_sections))
58fb0d4f 958 return 0;
9bf8cb9b 959
1d8af559 960 /* Check for pattern 4 */
58fb0d4f
SR
961 if (match(tosym, linker_symbols))
962 return 0;
9bf8cb9b 963
58fb0d4f 964 return 1;
4c8fbca5
SR
965}
966
93684d3b
SR
967/**
968 * Find symbol based on relocation record info.
969 * In some cases the symbol supplied is a valid symbol so
970 * return refsym. If st_name != 0 we assume this is a valid symbol.
971 * In other cases the symbol needs to be looked up in the symbol table
972 * based on section and address.
973 * **/
9ad21c3f 974static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
93684d3b
SR
975 Elf_Sym *relsym)
976{
977 Elf_Sym *sym;
9ad21c3f
SR
978 Elf_Sym *near = NULL;
979 Elf64_Sword distance = 20;
980 Elf64_Sword d;
93684d3b
SR
981
982 if (relsym->st_name != 0)
983 return relsym;
984 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
985 if (sym->st_shndx != relsym->st_shndx)
986 continue;
ae4ac123
AN
987 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
988 continue;
93684d3b
SR
989 if (sym->st_value == addr)
990 return sym;
9ad21c3f
SR
991 /* Find a symbol nearby - addr are maybe negative */
992 d = sym->st_value - addr;
993 if (d < 0)
994 d = addr - sym->st_value;
995 if (d < distance) {
996 distance = d;
997 near = sym;
998 }
93684d3b 999 }
9ad21c3f
SR
1000 /* We need a close match */
1001 if (distance < 20)
1002 return near;
1003 else
1004 return NULL;
93684d3b
SR
1005}
1006
da68d61f
DB
1007static inline int is_arm_mapping_symbol(const char *str)
1008{
1009 return str[0] == '$' && strchr("atd", str[1])
1010 && (str[2] == '\0' || str[2] == '.');
1011}
1012
1013/*
1014 * If there's no name there, ignore it; likewise, ignore it if it's
1015 * one of the magic symbols emitted used by current ARM tools.
1016 *
1017 * Otherwise if find_symbols_between() returns those symbols, they'll
1018 * fail the whitelist tests and cause lots of false alarms ... fixable
1019 * only by merging __exit and __init sections into __text, bloating
1020 * the kernel (which is especially evil on embedded platforms).
1021 */
1022static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1023{
1024 const char *name = elf->strtab + sym->st_name;
1025
1026 if (!name || !strlen(name))
1027 return 0;
1028 return !is_arm_mapping_symbol(name);
1029}
1030
b39927cf 1031/*
43c74d17
SR
1032 * Find symbols before or equal addr and after addr - in the section sec.
1033 * If we find two symbols with equal offset prefer one with a valid name.
1034 * The ELF format may have a better way to detect what type of symbol
1035 * it is, but this works for now.
b39927cf 1036 **/
157c23c8
SR
1037static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1038 const char *sec)
b39927cf
SR
1039{
1040 Elf_Sym *sym;
157c23c8 1041 Elf_Sym *near = NULL;
157c23c8 1042 Elf_Addr distance = ~0;
62070fa4 1043
b39927cf
SR
1044 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1045 const char *symsec;
1046
1047 if (sym->st_shndx >= SHN_LORESERVE)
1048 continue;
ff13f926 1049 symsec = sec_name(elf, sym->st_shndx);
b39927cf
SR
1050 if (strcmp(symsec, sec) != 0)
1051 continue;
da68d61f
DB
1052 if (!is_valid_name(elf, sym))
1053 continue;
b39927cf 1054 if (sym->st_value <= addr) {
157c23c8
SR
1055 if ((addr - sym->st_value) < distance) {
1056 distance = addr - sym->st_value;
1057 near = sym;
1058 } else if ((addr - sym->st_value) == distance) {
1059 near = sym;
43c74d17 1060 }
b39927cf
SR
1061 }
1062 }
157c23c8 1063 return near;
b39927cf
SR
1064}
1065
588ccd73
SR
1066/*
1067 * Convert a section name to the function/data attribute
1068 * .init.text => __init
1069 * .cpuinit.data => __cpudata
1070 * .memexitconst => __memconst
1071 * etc.
1072*/
1073static char *sec2annotation(const char *s)
1074{
1075 if (match(s, init_exit_sections)) {
1076 char *p = malloc(20);
1077 char *r = p;
1078
1079 *p++ = '_';
1080 *p++ = '_';
1081 if (*s == '.')
1082 s++;
1083 while (*s && *s != '.')
1084 *p++ = *s++;
1085 *p = '\0';
1086 if (*s == '.')
1087 s++;
1088 if (strstr(s, "rodata") != NULL)
1089 strcat(p, "const ");
1090 else if (strstr(s, "data") != NULL)
1091 strcat(p, "data ");
1092 else
1093 strcat(p, " ");
1094 return r; /* we leak her but we do not care */
1095 } else {
1096 return "";
1097 }
1098}
1099
1100static int is_function(Elf_Sym *sym)
1101{
1102 if (sym)
1103 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1104 else
f666751a 1105 return -1;
588ccd73
SR
1106}
1107
58fb0d4f 1108/*
b39927cf
SR
1109 * Print a warning about a section mismatch.
1110 * Try to find symbols near it so user can find it.
4c8fbca5 1111 * Check whitelist before warning - it may be a false positive.
58fb0d4f 1112 */
588ccd73 1113static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
58fb0d4f
SR
1114 const char *fromsec,
1115 unsigned long long fromaddr,
1116 const char *fromsym,
588ccd73
SR
1117 int from_is_func,
1118 const char *tosec, const char *tosym,
1119 int to_is_func)
1120{
1121 const char *from, *from_p;
1122 const char *to, *to_p;
f666751a
SR
1123
1124 switch (from_is_func) {
1125 case 0: from = "variable"; from_p = ""; break;
1126 case 1: from = "function"; from_p = "()"; break;
1127 default: from = "(unknown reference)"; from_p = ""; break;
1128 }
1129 switch (to_is_func) {
1130 case 0: to = "variable"; to_p = ""; break;
1131 case 1: to = "function"; to_p = "()"; break;
1132 default: to = "(unknown reference)"; to_p = ""; break;
1133 }
588ccd73 1134
e5f95c8b
SR
1135 sec_mismatch_count++;
1136 if (!sec_mismatch_verbose)
1137 return;
1138
7c0ac495
GU
1139 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1140 "to the %s %s:%s%s\n",
1141 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1142 tosym, to_p);
588ccd73 1143
588ccd73
SR
1144 switch (mismatch) {
1145 case TEXT_TO_INIT:
1146 fprintf(stderr,
f666751a 1147 "The function %s%s() references\n"
588ccd73
SR
1148 "the %s %s%s%s.\n"
1149 "This is often because %s lacks a %s\n"
1150 "annotation or the annotation of %s is wrong.\n",
1151 sec2annotation(fromsec), fromsym,
1152 to, sec2annotation(tosec), tosym, to_p,
1153 fromsym, sec2annotation(tosec), tosym);
1154 break;
1155 case DATA_TO_INIT: {
1156 const char **s = symbol_white_list;
1157 fprintf(stderr,
1158 "The variable %s references\n"
1159 "the %s %s%s%s\n"
1160 "If the reference is valid then annotate the\n"
1161 "variable with __init* (see linux/init.h) "
1162 "or name the variable:\n",
1163 fromsym, to, sec2annotation(tosec), tosym, to_p);
1164 while (*s)
1165 fprintf(stderr, "%s, ", *s++);
1166 fprintf(stderr, "\n");
1167 break;
58fb0d4f 1168 }
588ccd73
SR
1169 case TEXT_TO_EXIT:
1170 fprintf(stderr,
1171 "The function %s() references a %s in an exit section.\n"
1172 "Often the %s %s%s has valid usage outside the exit section\n"
1173 "and the fix is to remove the %sannotation of %s.\n",
1174 fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
1175 break;
1176 case DATA_TO_EXIT: {
1177 const char **s = symbol_white_list;
1178 fprintf(stderr,
1179 "The variable %s references\n"
1180 "the %s %s%s%s\n"
1181 "If the reference is valid then annotate the\n"
1182 "variable with __exit* (see linux/init.h) or "
1183 "name the variable:\n",
1184 fromsym, to, sec2annotation(tosec), tosym, to_p);
1185 while (*s)
1186 fprintf(stderr, "%s, ", *s++);
1187 fprintf(stderr, "\n");
1188 break;
1189 }
1190 case XXXINIT_TO_INIT:
1191 case XXXEXIT_TO_EXIT:
1192 fprintf(stderr,
1193 "The %s %s%s%s references\n"
1194 "a %s %s%s%s.\n"
1195 "If %s is only used by %s then\n"
1196 "annotate %s with a matching annotation.\n",
1197 from, sec2annotation(fromsec), fromsym, from_p,
1198 to, sec2annotation(tosec), tosym, to_p,
b1d2675a 1199 tosym, fromsym, tosym);
588ccd73
SR
1200 break;
1201 case INIT_TO_EXIT:
1202 fprintf(stderr,
1203 "The %s %s%s%s references\n"
1204 "a %s %s%s%s.\n"
1205 "This is often seen when error handling "
1206 "in the init function\n"
1207 "uses functionality in the exit path.\n"
1208 "The fix is often to remove the %sannotation of\n"
1209 "%s%s so it may be used outside an exit section.\n",
1210 from, sec2annotation(fromsec), fromsym, from_p,
1211 to, sec2annotation(tosec), tosym, to_p,
1212 sec2annotation(tosec), tosym, to_p);
1213 break;
1214 case EXIT_TO_INIT:
1215 fprintf(stderr,
1216 "The %s %s%s%s references\n"
1217 "a %s %s%s%s.\n"
1218 "This is often seen when error handling "
1219 "in the exit function\n"
1220 "uses functionality in the init path.\n"
1221 "The fix is often to remove the %sannotation of\n"
1222 "%s%s so it may be used outside an init section.\n",
1223 from, sec2annotation(fromsec), fromsym, from_p,
1224 to, sec2annotation(tosec), tosym, to_p,
1225 sec2annotation(tosec), tosym, to_p);
1226 break;
1227 case EXPORT_TO_INIT_EXIT:
1228 fprintf(stderr,
1229 "The symbol %s is exported and annotated %s\n"
1230 "Fix this by removing the %sannotation of %s "
1231 "or drop the export.\n",
1232 tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
1233 case NO_MISMATCH:
1234 /* To get warnings on missing members */
1235 break;
1236 }
1237 fprintf(stderr, "\n");
58fb0d4f
SR
1238}
1239
1240static void check_section_mismatch(const char *modname, struct elf_info *elf,
1241 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1242{
1243 const char *tosec;
588ccd73 1244 enum mismatch mismatch;
58fb0d4f
SR
1245
1246 tosec = sec_name(elf, sym->st_shndx);
588ccd73
SR
1247 mismatch = section_mismatch(fromsec, tosec);
1248 if (mismatch != NO_MISMATCH) {
1249 Elf_Sym *to;
1250 Elf_Sym *from;
58fb0d4f 1251 const char *tosym;
588ccd73 1252 const char *fromsym;
58fb0d4f 1253
588ccd73
SR
1254 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1255 fromsym = sym_name(elf, from);
1256 to = find_elf_symbol(elf, r->r_addend, sym);
1257 tosym = sym_name(elf, to);
58fb0d4f
SR
1258
1259 /* check whitelist - we may ignore it */
1260 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
588ccd73
SR
1261 report_sec_mismatch(modname, mismatch,
1262 fromsec, r->r_offset, fromsym,
1263 is_function(from), tosec, tosym,
1264 is_function(to));
58fb0d4f 1265 }
b39927cf
SR
1266 }
1267}
1268
ae4ac123 1269static unsigned int *reloc_location(struct elf_info *elf,
5b24c071 1270 Elf_Shdr *sechdr, Elf_Rela *r)
ae4ac123
AN
1271{
1272 Elf_Shdr *sechdrs = elf->sechdrs;
5b24c071 1273 int section = sechdr->sh_info;
ae4ac123
AN
1274
1275 return (void *)elf->hdr + sechdrs[section].sh_offset +
1276 (r->r_offset - sechdrs[section].sh_addr);
1277}
1278
5b24c071 1279static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
ae4ac123
AN
1280{
1281 unsigned int r_typ = ELF_R_TYPE(r->r_info);
5b24c071 1282 unsigned int *location = reloc_location(elf, sechdr, r);
ae4ac123
AN
1283
1284 switch (r_typ) {
1285 case R_386_32:
1286 r->r_addend = TO_NATIVE(*location);
1287 break;
1288 case R_386_PC32:
1289 r->r_addend = TO_NATIVE(*location) + 4;
1290 /* For CONFIG_RELOCATABLE=y */
1291 if (elf->hdr->e_type == ET_EXEC)
1292 r->r_addend += r->r_offset;
1293 break;
1294 }
1295 return 0;
1296}
1297
5b24c071 1298static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
56a974fa
SR
1299{
1300 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1301
1302 switch (r_typ) {
1303 case R_ARM_ABS32:
1304 /* From ARM ABI: (S + A) | T */
df578e7d
SR
1305 r->r_addend = (int)(long)
1306 (elf->symtab_start + ELF_R_SYM(r->r_info));
56a974fa
SR
1307 break;
1308 case R_ARM_PC24:
1309 /* From ARM ABI: ((S + A) | T) - P */
df578e7d 1310 r->r_addend = (int)(long)(elf->hdr +
5b24c071
SR
1311 sechdr->sh_offset +
1312 (r->r_offset - sechdr->sh_addr));
56a974fa
SR
1313 break;
1314 default:
1315 return 1;
1316 }
1317 return 0;
1318}
1319
5b24c071 1320static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
ae4ac123
AN
1321{
1322 unsigned int r_typ = ELF_R_TYPE(r->r_info);
5b24c071 1323 unsigned int *location = reloc_location(elf, sechdr, r);
ae4ac123
AN
1324 unsigned int inst;
1325
1326 if (r_typ == R_MIPS_HI16)
1327 return 1; /* skip this */
1328 inst = TO_NATIVE(*location);
1329 switch (r_typ) {
1330 case R_MIPS_LO16:
1331 r->r_addend = inst & 0xffff;
1332 break;
1333 case R_MIPS_26:
1334 r->r_addend = (inst & 0x03ffffff) << 2;
1335 break;
1336 case R_MIPS_32:
1337 r->r_addend = inst;
1338 break;
1339 }
1340 return 0;
1341}
1342
5b24c071 1343static void section_rela(const char *modname, struct elf_info *elf,
10668220 1344 Elf_Shdr *sechdr)
5b24c071
SR
1345{
1346 Elf_Sym *sym;
1347 Elf_Rela *rela;
1348 Elf_Rela r;
1349 unsigned int r_sym;
1350 const char *fromsec;
5b24c071 1351
ff13f926 1352 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
5b24c071
SR
1353 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1354
ff13f926 1355 fromsec = sech_name(elf, sechdr);
5b24c071
SR
1356 fromsec += strlen(".rela");
1357 /* if from section (name) is know good then skip it */
b614a697 1358 if (match(fromsec, section_white_list))
5b24c071 1359 return;
e241a630 1360
5b24c071
SR
1361 for (rela = start; rela < stop; rela++) {
1362 r.r_offset = TO_NATIVE(rela->r_offset);
1363#if KERNEL_ELFCLASS == ELFCLASS64
ff13f926 1364 if (elf->hdr->e_machine == EM_MIPS) {
5b24c071
SR
1365 unsigned int r_typ;
1366 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1367 r_sym = TO_NATIVE(r_sym);
1368 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1369 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1370 } else {
1371 r.r_info = TO_NATIVE(rela->r_info);
1372 r_sym = ELF_R_SYM(r.r_info);
1373 }
1374#else
1375 r.r_info = TO_NATIVE(rela->r_info);
1376 r_sym = ELF_R_SYM(r.r_info);
1377#endif
1378 r.r_addend = TO_NATIVE(rela->r_addend);
1379 sym = elf->symtab_start + r_sym;
1380 /* Skip special sections */
1381 if (sym->st_shndx >= SHN_LORESERVE)
1382 continue;
58fb0d4f 1383 check_section_mismatch(modname, elf, &r, sym, fromsec);
5b24c071
SR
1384 }
1385}
1386
1387static void section_rel(const char *modname, struct elf_info *elf,
10668220 1388 Elf_Shdr *sechdr)
5b24c071
SR
1389{
1390 Elf_Sym *sym;
1391 Elf_Rel *rel;
1392 Elf_Rela r;
1393 unsigned int r_sym;
1394 const char *fromsec;
5b24c071 1395
ff13f926 1396 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
5b24c071
SR
1397 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1398
ff13f926 1399 fromsec = sech_name(elf, sechdr);
5b24c071
SR
1400 fromsec += strlen(".rel");
1401 /* if from section (name) is know good then skip it */
b614a697 1402 if (match(fromsec, section_white_list))
5b24c071
SR
1403 return;
1404
1405 for (rel = start; rel < stop; rel++) {
1406 r.r_offset = TO_NATIVE(rel->r_offset);
1407#if KERNEL_ELFCLASS == ELFCLASS64
ff13f926 1408 if (elf->hdr->e_machine == EM_MIPS) {
5b24c071
SR
1409 unsigned int r_typ;
1410 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1411 r_sym = TO_NATIVE(r_sym);
1412 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1413 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1414 } else {
1415 r.r_info = TO_NATIVE(rel->r_info);
1416 r_sym = ELF_R_SYM(r.r_info);
1417 }
1418#else
1419 r.r_info = TO_NATIVE(rel->r_info);
1420 r_sym = ELF_R_SYM(r.r_info);
1421#endif
1422 r.r_addend = 0;
ff13f926 1423 switch (elf->hdr->e_machine) {
5b24c071
SR
1424 case EM_386:
1425 if (addend_386_rel(elf, sechdr, &r))
1426 continue;
1427 break;
1428 case EM_ARM:
1429 if (addend_arm_rel(elf, sechdr, &r))
1430 continue;
1431 break;
1432 case EM_MIPS:
1433 if (addend_mips_rel(elf, sechdr, &r))
1434 continue;
1435 break;
1436 }
1437 sym = elf->symtab_start + r_sym;
1438 /* Skip special sections */
1439 if (sym->st_shndx >= SHN_LORESERVE)
1440 continue;
58fb0d4f 1441 check_section_mismatch(modname, elf, &r, sym, fromsec);
5b24c071
SR
1442 }
1443}
1444
b39927cf
SR
1445/**
1446 * A module includes a number of sections that are discarded
1447 * either when loaded or when used as built-in.
1448 * For loaded modules all functions marked __init and all data
1449 * marked __initdata will be discarded when the module has been intialized.
1450 * Likewise for modules used built-in the sections marked __exit
1451 * are discarded because __exit marked function are supposed to be called
32be1d22 1452 * only when a module is unloaded which never happens for built-in modules.
b39927cf
SR
1453 * The check_sec_ref() function traverses all relocation records
1454 * to find all references to a section that reference a section that will
1455 * be discarded and warns about it.
1456 **/
1457static void check_sec_ref(struct module *mod, const char *modname,
10668220 1458 struct elf_info *elf)
b39927cf
SR
1459{
1460 int i;
b39927cf 1461 Elf_Shdr *sechdrs = elf->sechdrs;
62070fa4 1462
b39927cf 1463 /* Walk through all sections */
ff13f926 1464 for (i = 0; i < elf->hdr->e_shnum; i++) {
b614a697 1465 check_section(modname, elf, &elf->sechdrs[i]);
b39927cf 1466 /* We want to process only relocation sections and not .init */
5b24c071 1467 if (sechdrs[i].sh_type == SHT_RELA)
10668220 1468 section_rela(modname, elf, &elf->sechdrs[i]);
5b24c071 1469 else if (sechdrs[i].sh_type == SHT_REL)
10668220 1470 section_rel(modname, elf, &elf->sechdrs[i]);
b39927cf
SR
1471 }
1472}
1473
b2e3e658
MD
1474static void get_markers(struct elf_info *info, struct module *mod)
1475{
1476 const Elf_Shdr *sh = &info->sechdrs[info->markers_strings_sec];
1477 const char *strings = (const char *) info->hdr + sh->sh_offset;
1478 const Elf_Sym *sym, *first_sym, *last_sym;
1479 size_t n;
1480
1481 if (!info->markers_strings_sec)
1482 return;
1483
1484 /*
1485 * First count the strings. We look for all the symbols defined
1486 * in the __markers_strings section named __mstrtab_*. For
1487 * these local names, the compiler puts a random .NNN suffix on,
1488 * so the names don't correspond exactly.
1489 */
1490 first_sym = last_sym = NULL;
1491 n = 0;
1492 for (sym = info->symtab_start; sym < info->symtab_stop; sym++)
1493 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1494 sym->st_shndx == info->markers_strings_sec &&
1495 !strncmp(info->strtab + sym->st_name,
1496 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1497 if (first_sym == NULL)
1498 first_sym = sym;
1499 last_sym = sym;
1500 ++n;
1501 }
1502
1503 if (n == 0)
1504 return;
1505
1506 /*
1507 * Now collect each name and format into a line for the output.
1508 * Lines look like:
1509 * marker_name vmlinux marker %s format %d
1510 * The format string after the second \t can use whitespace.
1511 */
1512 mod->markers = NOFAIL(malloc(sizeof mod->markers[0] * n));
1513 mod->nmarkers = n;
1514
1515 n = 0;
1516 for (sym = first_sym; sym <= last_sym; sym++)
1517 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1518 sym->st_shndx == info->markers_strings_sec &&
1519 !strncmp(info->strtab + sym->st_name,
1520 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1521 const char *name = strings + sym->st_value;
1522 const char *fmt = strchr(name, '\0') + 1;
1523 char *line = NULL;
1524 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1525 NOFAIL(line);
1526 mod->markers[n++] = line;
1527 }
1528}
1529
5c3ead8c 1530static void read_symbols(char *modname)
1da177e4
LT
1531{
1532 const char *symname;
1533 char *version;
b817f6fe 1534 char *license;
1da177e4
LT
1535 struct module *mod;
1536 struct elf_info info = { };
1537 Elf_Sym *sym;
1538
85bd2fdd
SR
1539 if (!parse_elf(&info, modname))
1540 return;
1da177e4
LT
1541
1542 mod = new_module(modname);
1543
1544 /* When there's no vmlinux, don't print warnings about
1545 * unresolved symbols (since there'll be too many ;) */
1546 if (is_vmlinux(modname)) {
1da177e4 1547 have_vmlinux = 1;
1da177e4
LT
1548 mod->skip = 1;
1549 }
1550
b817f6fe 1551 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
2fa36568
SR
1552 if (info.modinfo && !license && !is_vmlinux(modname))
1553 warn("modpost: missing MODULE_LICENSE() in %s\n"
1554 "see include/linux/module.h for "
1555 "more information\n", modname);
b817f6fe
SR
1556 while (license) {
1557 if (license_is_gpl_compatible(license))
1558 mod->gpl_compatible = 1;
1559 else {
1560 mod->gpl_compatible = 0;
1561 break;
1562 }
1563 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1564 "license", license);
1565 }
1566
1da177e4
LT
1567 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1568 symname = info.strtab + sym->st_name;
1569
1570 handle_modversions(mod, &info, sym, symname);
1571 handle_moddevtable(mod, &info, sym, symname);
1572 }
d1f25e66 1573 if (!is_vmlinux(modname) ||
10668220
SR
1574 (is_vmlinux(modname) && vmlinux_section_warnings))
1575 check_sec_ref(mod, modname, &info);
1da177e4
LT
1576
1577 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1578 if (version)
1579 maybe_frob_rcs_version(modname, version, info.modinfo,
1580 version - (char *)info.hdr);
1581 if (version || (all_versions && !is_vmlinux(modname)))
1582 get_src_version(modname, mod->srcversion,
1583 sizeof(mod->srcversion)-1);
1584
b2e3e658
MD
1585 get_markers(&info, mod);
1586
1da177e4
LT
1587 parse_elf_finish(&info);
1588
8c8ef42a 1589 /* Our trick to get versioning for module struct etc. - it's
1da177e4
LT
1590 * never passed as an argument to an exported function, so
1591 * the automatic versioning doesn't pick it up, but it's really
1592 * important anyhow */
1593 if (modversions)
8c8ef42a 1594 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
1da177e4
LT
1595}
1596
1597#define SZ 500
1598
1599/* We first write the generated file into memory using the
1600 * following helper, then compare to the file on disk and
1601 * only update the later if anything changed */
1602
5c3ead8c
SR
1603void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1604 const char *fmt, ...)
1da177e4
LT
1605{
1606 char tmp[SZ];
1607 int len;
1608 va_list ap;
62070fa4 1609
1da177e4
LT
1610 va_start(ap, fmt);
1611 len = vsnprintf(tmp, SZ, fmt, ap);
7670f023 1612 buf_write(buf, tmp, len);
1da177e4
LT
1613 va_end(ap);
1614}
1615
5c3ead8c 1616void buf_write(struct buffer *buf, const char *s, int len)
1da177e4
LT
1617{
1618 if (buf->size - buf->pos < len) {
7670f023 1619 buf->size += len + SZ;
1da177e4
LT
1620 buf->p = realloc(buf->p, buf->size);
1621 }
1622 strncpy(buf->p + buf->pos, s, len);
1623 buf->pos += len;
1624}
1625
c96fca21
SR
1626static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1627{
1628 const char *e = is_vmlinux(m) ?"":".ko";
1629
1630 switch (exp) {
1631 case export_gpl:
1632 fatal("modpost: GPL-incompatible module %s%s "
1633 "uses GPL-only symbol '%s'\n", m, e, s);
1634 break;
1635 case export_unused_gpl:
1636 fatal("modpost: GPL-incompatible module %s%s "
1637 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1638 break;
1639 case export_gpl_future:
1640 warn("modpost: GPL-incompatible module %s%s "
1641 "uses future GPL-only symbol '%s'\n", m, e, s);
1642 break;
1643 case export_plain:
1644 case export_unused:
1645 case export_unknown:
1646 /* ignore */
1647 break;
1648 }
1649}
1650
df578e7d 1651static void check_for_unused(enum export exp, const char *m, const char *s)
c96fca21
SR
1652{
1653 const char *e = is_vmlinux(m) ?"":".ko";
1654
1655 switch (exp) {
1656 case export_unused:
1657 case export_unused_gpl:
1658 warn("modpost: module %s%s "
1659 "uses symbol '%s' marked UNUSED\n", m, e, s);
1660 break;
1661 default:
1662 /* ignore */
1663 break;
1664 }
1665}
1666
1667static void check_exports(struct module *mod)
b817f6fe
SR
1668{
1669 struct symbol *s, *exp;
1670
1671 for (s = mod->unres; s; s = s->next) {
6449bd62 1672 const char *basename;
b817f6fe
SR
1673 exp = find_symbol(s->name);
1674 if (!exp || exp->module == mod)
1675 continue;
6449bd62 1676 basename = strrchr(mod->name, '/');
b817f6fe
SR
1677 if (basename)
1678 basename++;
c96fca21
SR
1679 else
1680 basename = mod->name;
1681 if (!mod->gpl_compatible)
1682 check_for_gpl_usage(exp->export, basename, exp->name);
1683 check_for_unused(exp->export, basename, exp->name);
df578e7d 1684 }
b817f6fe
SR
1685}
1686
5c3ead8c
SR
1687/**
1688 * Header for the generated file
1689 **/
1690static void add_header(struct buffer *b, struct module *mod)
1da177e4
LT
1691{
1692 buf_printf(b, "#include <linux/module.h>\n");
1693 buf_printf(b, "#include <linux/vermagic.h>\n");
1694 buf_printf(b, "#include <linux/compiler.h>\n");
1695 buf_printf(b, "\n");
1696 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1697 buf_printf(b, "\n");
1da177e4
LT
1698 buf_printf(b, "struct module __this_module\n");
1699 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
f83b5e32 1700 buf_printf(b, " .name = KBUILD_MODNAME,\n");
1da177e4
LT
1701 if (mod->has_init)
1702 buf_printf(b, " .init = init_module,\n");
1703 if (mod->has_cleanup)
1704 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1705 " .exit = cleanup_module,\n"
1706 "#endif\n");
e61a1c1c 1707 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
1da177e4
LT
1708 buf_printf(b, "};\n");
1709}
1710
a9860bf0
GKH
1711void add_staging_flag(struct buffer *b, const char *name)
1712{
1713 static const char *staging_dir = "drivers/staging";
1714
1715 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
1716 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1717}
1718
5c3ead8c
SR
1719/**
1720 * Record CRCs for unresolved symbols
1721 **/
c53ddacd 1722static int add_versions(struct buffer *b, struct module *mod)
1da177e4
LT
1723{
1724 struct symbol *s, *exp;
c53ddacd 1725 int err = 0;
1da177e4
LT
1726
1727 for (s = mod->unres; s; s = s->next) {
1728 exp = find_symbol(s->name);
1729 if (!exp || exp->module == mod) {
c53ddacd 1730 if (have_vmlinux && !s->weak) {
2a116659
MW
1731 if (warn_unresolved) {
1732 warn("\"%s\" [%s.ko] undefined!\n",
1733 s->name, mod->name);
1734 } else {
1735 merror("\"%s\" [%s.ko] undefined!\n",
1736 s->name, mod->name);
1737 err = 1;
1738 }
c53ddacd 1739 }
1da177e4
LT
1740 continue;
1741 }
1742 s->module = exp->module;
1743 s->crc_valid = exp->crc_valid;
1744 s->crc = exp->crc;
1745 }
1746
1747 if (!modversions)
c53ddacd 1748 return err;
1da177e4
LT
1749
1750 buf_printf(b, "\n");
1751 buf_printf(b, "static const struct modversion_info ____versions[]\n");
3ff6eecc 1752 buf_printf(b, "__used\n");
1da177e4
LT
1753 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1754
1755 for (s = mod->unres; s; s = s->next) {
df578e7d 1756 if (!s->module)
1da177e4 1757 continue;
1da177e4 1758 if (!s->crc_valid) {
cb80514d 1759 warn("\"%s\" [%s.ko] has no CRC!\n",
1da177e4
LT
1760 s->name, mod->name);
1761 continue;
1762 }
1763 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1764 }
1765
1766 buf_printf(b, "};\n");
c53ddacd
KK
1767
1768 return err;
1da177e4
LT
1769}
1770
5c3ead8c
SR
1771static void add_depends(struct buffer *b, struct module *mod,
1772 struct module *modules)
1da177e4
LT
1773{
1774 struct symbol *s;
1775 struct module *m;
1776 int first = 1;
1777
df578e7d 1778 for (m = modules; m; m = m->next)
1da177e4 1779 m->seen = is_vmlinux(m->name);
1da177e4
LT
1780
1781 buf_printf(b, "\n");
1782 buf_printf(b, "static const char __module_depends[]\n");
3ff6eecc 1783 buf_printf(b, "__used\n");
1da177e4
LT
1784 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1785 buf_printf(b, "\"depends=");
1786 for (s = mod->unres; s; s = s->next) {
a61b2dfd 1787 const char *p;
1da177e4
LT
1788 if (!s->module)
1789 continue;
1790
1791 if (s->module->seen)
1792 continue;
1793
1794 s->module->seen = 1;
df578e7d
SR
1795 p = strrchr(s->module->name, '/');
1796 if (p)
a61b2dfd
SR
1797 p++;
1798 else
1799 p = s->module->name;
1800 buf_printf(b, "%s%s", first ? "" : ",", p);
1da177e4
LT
1801 first = 0;
1802 }
1803 buf_printf(b, "\";\n");
1804}
1805
5c3ead8c 1806static void add_srcversion(struct buffer *b, struct module *mod)
1da177e4
LT
1807{
1808 if (mod->srcversion[0]) {
1809 buf_printf(b, "\n");
1810 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1811 mod->srcversion);
1812 }
1813}
1814
5c3ead8c 1815static void write_if_changed(struct buffer *b, const char *fname)
1da177e4
LT
1816{
1817 char *tmp;
1818 FILE *file;
1819 struct stat st;
1820
1821 file = fopen(fname, "r");
1822 if (!file)
1823 goto write;
1824
1825 if (fstat(fileno(file), &st) < 0)
1826 goto close_write;
1827
1828 if (st.st_size != b->pos)
1829 goto close_write;
1830
1831 tmp = NOFAIL(malloc(b->pos));
1832 if (fread(tmp, 1, b->pos, file) != b->pos)
1833 goto free_write;
1834
1835 if (memcmp(tmp, b->p, b->pos) != 0)
1836 goto free_write;
1837
1838 free(tmp);
1839 fclose(file);
1840 return;
1841
1842 free_write:
1843 free(tmp);
1844 close_write:
1845 fclose(file);
1846 write:
1847 file = fopen(fname, "w");
1848 if (!file) {
1849 perror(fname);
1850 exit(1);
1851 }
1852 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1853 perror(fname);
1854 exit(1);
1855 }
1856 fclose(file);
1857}
1858
bd5cbced 1859/* parse Module.symvers file. line format:
534b89a9 1860 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
bd5cbced 1861 **/
040fcc81 1862static void read_dump(const char *fname, unsigned int kernel)
1da177e4
LT
1863{
1864 unsigned long size, pos = 0;
1865 void *file = grab_file(fname, &size);
1866 char *line;
1867
df578e7d 1868 if (!file)
1da177e4
LT
1869 /* No symbol versions, silently ignore */
1870 return;
1871
1872 while ((line = get_next_line(&pos, file, size))) {
534b89a9 1873 char *symname, *modname, *d, *export, *end;
1da177e4
LT
1874 unsigned int crc;
1875 struct module *mod;
040fcc81 1876 struct symbol *s;
1da177e4
LT
1877
1878 if (!(symname = strchr(line, '\t')))
1879 goto fail;
1880 *symname++ = '\0';
1881 if (!(modname = strchr(symname, '\t')))
1882 goto fail;
1883 *modname++ = '\0';
9ac545b0 1884 if ((export = strchr(modname, '\t')) != NULL)
bd5cbced 1885 *export++ = '\0';
534b89a9
SR
1886 if (export && ((end = strchr(export, '\t')) != NULL))
1887 *end = '\0';
1da177e4
LT
1888 crc = strtoul(line, &d, 16);
1889 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1890 goto fail;
df578e7d
SR
1891 mod = find_module(modname);
1892 if (!mod) {
1893 if (is_vmlinux(modname))
1da177e4 1894 have_vmlinux = 1;
0fa3a88c 1895 mod = new_module(modname);
1da177e4
LT
1896 mod->skip = 1;
1897 }
bd5cbced 1898 s = sym_add_exported(symname, mod, export_no(export));
8e70c458
SR
1899 s->kernel = kernel;
1900 s->preloaded = 1;
bd5cbced 1901 sym_update_crc(symname, mod, crc, export_no(export));
1da177e4
LT
1902 }
1903 return;
1904fail:
1905 fatal("parse error in symbol dump file\n");
1906}
1907
040fcc81
SR
1908/* For normal builds always dump all symbols.
1909 * For external modules only dump symbols
1910 * that are not read from kernel Module.symvers.
1911 **/
1912static int dump_sym(struct symbol *sym)
1913{
1914 if (!external_module)
1915 return 1;
1916 if (sym->vmlinux || sym->kernel)
1917 return 0;
1918 return 1;
1919}
62070fa4 1920
5c3ead8c 1921static void write_dump(const char *fname)
1da177e4
LT
1922{
1923 struct buffer buf = { };
1924 struct symbol *symbol;
1925 int n;
1926
1927 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1928 symbol = symbolhash[n];
1929 while (symbol) {
040fcc81 1930 if (dump_sym(symbol))
bd5cbced 1931 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
62070fa4 1932 symbol->crc, symbol->name,
bd5cbced
RP
1933 symbol->module->name,
1934 export_str(symbol->export));
1da177e4
LT
1935 symbol = symbol->next;
1936 }
1937 }
1938 write_if_changed(&buf, fname);
1939}
1940
b2e3e658
MD
1941static void add_marker(struct module *mod, const char *name, const char *fmt)
1942{
1943 char *line = NULL;
1944 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1945 NOFAIL(line);
1946
1947 mod->markers = NOFAIL(realloc(mod->markers, ((mod->nmarkers + 1) *
1948 sizeof mod->markers[0])));
1949 mod->markers[mod->nmarkers++] = line;
1950}
1951
1952static void read_markers(const char *fname)
1953{
1954 unsigned long size, pos = 0;
1955 void *file = grab_file(fname, &size);
1956 char *line;
1957
1958 if (!file) /* No old markers, silently ignore */
1959 return;
1960
1961 while ((line = get_next_line(&pos, file, size))) {
1962 char *marker, *modname, *fmt;
1963 struct module *mod;
1964
1965 marker = line;
1966 modname = strchr(marker, '\t');
1967 if (!modname)
1968 goto fail;
1969 *modname++ = '\0';
1970 fmt = strchr(modname, '\t');
1971 if (!fmt)
1972 goto fail;
1973 *fmt++ = '\0';
1974 if (*marker == '\0' || *modname == '\0')
1975 goto fail;
1976
1977 mod = find_module(modname);
1978 if (!mod) {
0fa3a88c 1979 mod = new_module(modname);
b2e3e658
MD
1980 mod->skip = 1;
1981 }
87f3b6b6
MD
1982 if (is_vmlinux(modname)) {
1983 have_vmlinux = 1;
1984 mod->skip = 0;
1985 }
b2e3e658 1986
d35cb360
MD
1987 if (!mod->skip)
1988 add_marker(mod, marker, fmt);
b2e3e658 1989 }
99e3a1eb 1990 release_file(file, size);
b2e3e658
MD
1991 return;
1992fail:
1993 fatal("parse error in markers list file\n");
1994}
1995
1996static int compare_strings(const void *a, const void *b)
1997{
1998 return strcmp(*(const char **) a, *(const char **) b);
1999}
2000
2001static void write_markers(const char *fname)
2002{
2003 struct buffer buf = { };
2004 struct module *mod;
2005 size_t i;
2006
2007 for (mod = modules; mod; mod = mod->next)
2008 if ((!external_module || !mod->skip) && mod->markers != NULL) {
2009 /*
2010 * Sort the strings so we can skip duplicates when
2011 * we write them out.
2012 */
2013 qsort(mod->markers, mod->nmarkers,
2014 sizeof mod->markers[0], &compare_strings);
2015 for (i = 0; i < mod->nmarkers; ++i) {
2016 char *line = mod->markers[i];
2017 buf_write(&buf, line, strlen(line));
2018 while (i + 1 < mod->nmarkers &&
2019 !strcmp(mod->markers[i],
2020 mod->markers[i + 1]))
2021 free(mod->markers[i++]);
2022 free(mod->markers[i]);
2023 }
2024 free(mod->markers);
2025 mod->markers = NULL;
2026 }
2027
2028 write_if_changed(&buf, fname);
2029}
2030
2d04b5ae
RH
2031struct ext_sym_list {
2032 struct ext_sym_list *next;
2033 const char *file;
2034};
2035
5c3ead8c 2036int main(int argc, char **argv)
1da177e4
LT
2037{
2038 struct module *mod;
2039 struct buffer buf = { };
040fcc81
SR
2040 char *kernel_read = NULL, *module_read = NULL;
2041 char *dump_write = NULL;
b2e3e658
MD
2042 char *markers_read = NULL;
2043 char *markers_write = NULL;
1da177e4 2044 int opt;
c53ddacd 2045 int err;
2d04b5ae
RH
2046 struct ext_sym_list *extsym_iter;
2047 struct ext_sym_list *extsym_start = NULL;
1da177e4 2048
2d04b5ae 2049 while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
df578e7d
SR
2050 switch (opt) {
2051 case 'i':
2052 kernel_read = optarg;
2053 break;
2054 case 'I':
2055 module_read = optarg;
2056 external_module = 1;
2057 break;
4ce6efed
SR
2058 case 'c':
2059 cross_build = 1;
2060 break;
2d04b5ae
RH
2061 case 'e':
2062 external_module = 1;
2063 extsym_iter =
2064 NOFAIL(malloc(sizeof(*extsym_iter)));
2065 extsym_iter->next = extsym_start;
2066 extsym_iter->file = optarg;
2067 extsym_start = extsym_iter;
2068 break;
df578e7d
SR
2069 case 'm':
2070 modversions = 1;
2071 break;
2072 case 'o':
2073 dump_write = optarg;
2074 break;
2075 case 'a':
2076 all_versions = 1;
2077 break;
2078 case 's':
2079 vmlinux_section_warnings = 0;
2080 break;
588ccd73
SR
2081 case 'S':
2082 sec_mismatch_verbose = 0;
2083 break;
df578e7d
SR
2084 case 'w':
2085 warn_unresolved = 1;
2086 break;
b2e3e658
MD
2087 case 'M':
2088 markers_write = optarg;
2089 break;
2090 case 'K':
2091 markers_read = optarg;
2092 break;
df578e7d
SR
2093 default:
2094 exit(1);
1da177e4
LT
2095 }
2096 }
2097
040fcc81
SR
2098 if (kernel_read)
2099 read_dump(kernel_read, 1);
2100 if (module_read)
2101 read_dump(module_read, 0);
2d04b5ae
RH
2102 while (extsym_start) {
2103 read_dump(extsym_start->file, 0);
2104 extsym_iter = extsym_start->next;
2105 free(extsym_start);
2106 extsym_start = extsym_iter;
2107 }
1da177e4 2108
df578e7d 2109 while (optind < argc)
1da177e4 2110 read_symbols(argv[optind++]);
1da177e4 2111
b817f6fe
SR
2112 for (mod = modules; mod; mod = mod->next) {
2113 if (mod->skip)
2114 continue;
c96fca21 2115 check_exports(mod);
b817f6fe
SR
2116 }
2117
c53ddacd
KK
2118 err = 0;
2119
1da177e4 2120 for (mod = modules; mod; mod = mod->next) {
666ab414
AK
2121 char fname[strlen(mod->name) + 10];
2122
1da177e4
LT
2123 if (mod->skip)
2124 continue;
2125
2126 buf.pos = 0;
2127
2128 add_header(&buf, mod);
a9860bf0 2129 add_staging_flag(&buf, mod->name);
c53ddacd 2130 err |= add_versions(&buf, mod);
1da177e4
LT
2131 add_depends(&buf, mod, modules);
2132 add_moddevtable(&buf, mod);
2133 add_srcversion(&buf, mod);
2134
2135 sprintf(fname, "%s.mod.c", mod->name);
2136 write_if_changed(&buf, fname);
2137 }
2138
2139 if (dump_write)
2140 write_dump(dump_write);
588ccd73 2141 if (sec_mismatch_count && !sec_mismatch_verbose)
7c0ac495
GU
2142 warn("modpost: Found %d section mismatch(es).\n"
2143 "To see full details build your kernel with:\n"
2144 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2145 sec_mismatch_count);
1da177e4 2146
b2e3e658
MD
2147 if (markers_read)
2148 read_markers(markers_read);
2149
2150 if (markers_write)
2151 write_markers(markers_write);
2152
c53ddacd 2153 return err;
1da177e4 2154}