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