blob: 65411665e13c45697fccb76885eea9fd8bc9f721 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborg382168f2006-02-26 20:11:17 +01005 * Copyright 2006 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
14#include <ctype.h>
15#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020016#include "../../include/linux/license.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/* Are we using CONFIG_MODVERSIONS? */
19int modversions = 0;
20/* Warn about undefined symbols? (do so if we have vmlinux) */
21int have_vmlinux = 0;
22/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
23static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010024/* If we are modposting external module set to 1 */
25static int external_module = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070026/* How a symbol is exported */
27enum export {export_plain, export_gpl, export_gpl_future, export_unknown};
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010029void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 va_list arglist;
32
33 fprintf(stderr, "FATAL: ");
34
35 va_start(arglist, fmt);
36 vfprintf(stderr, fmt, arglist);
37 va_end(arglist);
38
39 exit(1);
40}
41
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010042void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 va_list arglist;
45
46 fprintf(stderr, "WARNING: ");
47
48 va_start(arglist, fmt);
49 vfprintf(stderr, fmt, arglist);
50 va_end(arglist);
51}
52
Sam Ravnborg040fcc82006-01-28 22:15:55 +010053static int is_vmlinux(const char *modname)
54{
55 const char *myname;
56
57 if ((myname = strrchr(modname, '/')))
58 myname++;
59 else
60 myname = modname;
61
62 return strcmp(myname, "vmlinux") == 0;
63}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065void *do_nofail(void *ptr, const char *expr)
66{
67 if (!ptr) {
68 fatal("modpost: Memory allocation failure: %s.\n", expr);
69 }
70 return ptr;
71}
72
73/* A list of all modules we processed */
74
75static struct module *modules;
76
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010077static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 struct module *mod;
80
81 for (mod = modules; mod; mod = mod->next)
82 if (strcmp(mod->name, modname) == 0)
83 break;
84 return mod;
85}
86
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010087static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct module *mod;
90 char *p, *s;
Sam Ravnborg62070fa2006-03-03 16:46:04 +010091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 mod = NOFAIL(malloc(sizeof(*mod)));
93 memset(mod, 0, sizeof(*mod));
94 p = NOFAIL(strdup(modname));
95
96 /* strip trailing .o */
97 if ((s = strrchr(p, '.')) != NULL)
98 if (strcmp(s, ".o") == 0)
99 *s = '\0';
100
101 /* add to list */
102 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200103 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 mod->next = modules;
105 modules = mod;
106
107 return mod;
108}
109
110/* A hash of all exported symbols,
111 * struct symbol is also used for lists of unresolved symbols */
112
113#define SYMBOL_HASH_SIZE 1024
114
115struct symbol {
116 struct symbol *next;
117 struct module *module;
118 unsigned int crc;
119 int crc_valid;
120 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100121 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
122 unsigned int kernel:1; /* 1 if symbol is from kernel
123 * (only for external modules) **/
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100124 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Ram Paibd5cbce2006-06-08 22:12:53 -0700125 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 char name[0];
127};
128
129static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
130
131/* This is based on the hash agorithm from gdbm, via tdb */
132static inline unsigned int tdb_hash(const char *name)
133{
134 unsigned value; /* Used to compute the hash value. */
135 unsigned i; /* Used to cycle through random values. */
136
137 /* Set the initial value from the key size. */
138 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
139 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
140
141 return (1103515243 * value + 12345);
142}
143
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100144/**
145 * Allocate a new symbols for use in the hash of exported symbols or
146 * the list of unresolved symbols per module
147 **/
148static struct symbol *alloc_symbol(const char *name, unsigned int weak,
149 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
152
153 memset(s, 0, sizeof(*s));
154 strcpy(s->name, name);
155 s->weak = weak;
156 s->next = next;
157 return s;
158}
159
160/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700161static struct symbol *new_symbol(const char *name, struct module *module,
162 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 unsigned int hash;
165 struct symbol *new;
166
167 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
168 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
169 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700170 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100171 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100174static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct symbol *s;
177
178 /* For our purposes, .foo matches foo. PPC64 needs this. */
179 if (name[0] == '.')
180 name++;
181
182 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
183 if (strcmp(s->name, name) == 0)
184 return s;
185 }
186 return NULL;
187}
188
Ram Paibd5cbce2006-06-08 22:12:53 -0700189static struct {
190 const char *str;
191 enum export export;
192} export_list[] = {
193 { .str = "EXPORT_SYMBOL", .export = export_plain },
194 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
195 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
196 { .str = "(unknown)", .export = export_unknown },
197};
198
199
200static const char *export_str(enum export ex)
201{
202 return export_list[ex].str;
203}
204
205static enum export export_no(const char * s)
206{
207 int i;
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200208 if (!s)
209 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700210 for (i = 0; export_list[i].export != export_unknown; i++) {
211 if (strcmp(export_list[i].str, s) == 0)
212 return export_list[i].export;
213 }
214 return export_unknown;
215}
216
217static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
218{
219 if (sec == elf->export_sec)
220 return export_plain;
221 else if (sec == elf->export_gpl_sec)
222 return export_gpl;
223 else if (sec == elf->export_gpl_future_sec)
224 return export_gpl_future;
225 else
226 return export_unknown;
227}
228
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100229/**
230 * Add an exported symbol - it may have already been added without a
231 * CRC, in this case just update the CRC
232 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700233static struct symbol *sym_add_exported(const char *name, struct module *mod,
234 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 struct symbol *s = find_symbol(name);
237
238 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700239 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100240 } else {
241 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100242 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100243 "was in %s%s\n", mod->name, name,
244 s->module->name,
245 is_vmlinux(s->module->name) ?"":".ko");
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100248 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100249 s->vmlinux = is_vmlinux(mod->name);
250 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700251 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100252 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100255static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700256 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100257{
258 struct symbol *s = find_symbol(name);
259
260 if (!s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700261 s = new_symbol(name, mod, export);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100262 s->crc = crc;
263 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100266void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct stat st;
269 void *map;
270 int fd;
271
272 fd = open(filename, O_RDONLY);
273 if (fd < 0 || fstat(fd, &st) != 0)
274 return NULL;
275
276 *size = st.st_size;
277 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
278 close(fd);
279
280 if (map == MAP_FAILED)
281 return NULL;
282 return map;
283}
284
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100285/**
286 * Return a copy of the next line in a mmap'ed file.
287 * spaces in the beginning of the line is trimmed away.
288 * Return a pointer to a static buffer.
289 **/
290char* get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 static char line[4096];
293 int skip = 1;
294 size_t len = 0;
295 signed char *p = (signed char *)file + *pos;
296 char *s = line;
297
298 for (; *pos < size ; (*pos)++)
299 {
300 if (skip && isspace(*p)) {
301 p++;
302 continue;
303 }
304 skip = 0;
305 if (*p != '\n' && (*pos < size)) {
306 len++;
307 *s++ = *p++;
308 if (len > 4095)
309 break; /* Too long, stop */
310 } else {
311 /* End of string */
312 *s = '\0';
313 return line;
314 }
315 }
316 /* End of buffer */
317 return NULL;
318}
319
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100320void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 munmap(file, size);
323}
324
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100325static void parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned int i;
328 Elf_Ehdr *hdr = info->hdr;
329 Elf_Shdr *sechdrs;
330 Elf_Sym *sym;
331
332 hdr = grab_file(filename, &info->size);
333 if (!hdr) {
334 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200335 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 info->hdr = hdr;
338 if (info->size < sizeof(*hdr))
339 goto truncated;
340
341 /* Fix endianness in ELF header */
342 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
343 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
344 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
345 hdr->e_machine = TO_NATIVE(hdr->e_machine);
346 sechdrs = (void *)hdr + hdr->e_shoff;
347 info->sechdrs = sechdrs;
348
349 /* Fix endianness in section headers */
350 for (i = 0; i < hdr->e_shnum; i++) {
351 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
352 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
353 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
354 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
355 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
356 }
357 /* Find symbol table. */
358 for (i = 1; i < hdr->e_shnum; i++) {
359 const char *secstrings
360 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
Ram Paibd5cbce2006-06-08 22:12:53 -0700361 const char *secname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (sechdrs[i].sh_offset > info->size)
364 goto truncated;
Ram Paibd5cbce2006-06-08 22:12:53 -0700365 secname = secstrings + sechdrs[i].sh_name;
366 if (strcmp(secname, ".modinfo") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
368 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700369 } else if (strcmp(secname, "__ksymtab") == 0)
370 info->export_sec = i;
371 else if (strcmp(secname, "__ksymtab_gpl") == 0)
372 info->export_gpl_sec = i;
373 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
374 info->export_gpl_future_sec = i;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (sechdrs[i].sh_type != SHT_SYMTAB)
377 continue;
378
379 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100380 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100382 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 sechdrs[sechdrs[i].sh_link].sh_offset;
384 }
385 if (!info->symtab_start) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100386 fatal("%s has no symtab?\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 /* Fix endianness in symbols */
389 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
390 sym->st_shndx = TO_NATIVE(sym->st_shndx);
391 sym->st_name = TO_NATIVE(sym->st_name);
392 sym->st_value = TO_NATIVE(sym->st_value);
393 sym->st_size = TO_NATIVE(sym->st_size);
394 }
395 return;
396
397 truncated:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100398 fatal("%s is truncated.\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100401static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 release_file(info->hdr, info->size);
404}
405
Luke Yangf7b05e62006-03-02 18:35:31 +0800406#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
407#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100409static void handle_modversions(struct module *mod, struct elf_info *info,
410 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700413 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 switch (sym->st_shndx) {
416 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100417 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 break;
419 case SHN_ABS:
420 /* CRC'd symbol */
421 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
422 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700423 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
424 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 break;
427 case SHN_UNDEF:
428 /* undefined symbol */
429 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
430 ELF_ST_BIND(sym->st_info) != STB_WEAK)
431 break;
432 /* ignore global offset table */
433 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
434 break;
435 /* ignore __this_module, it will be resolved shortly */
436 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
437 break;
Ben Colline8d529012005-08-19 13:44:57 -0700438/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
439#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
440/* add compatibility with older glibc */
441#ifndef STT_SPARC_REGISTER
442#define STT_SPARC_REGISTER STT_REGISTER
443#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (info->hdr->e_machine == EM_SPARC ||
445 info->hdr->e_machine == EM_SPARCV9) {
446 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700447 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100449 if (symname[0] == '.') {
450 char *munged = strdup(symname);
451 munged[0] = '_';
452 munged[1] = toupper(munged[1]);
453 symname = munged;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
459 strlen(MODULE_SYMBOL_PREFIX)) == 0)
460 mod->unres = alloc_symbol(symname +
461 strlen(MODULE_SYMBOL_PREFIX),
462 ELF_ST_BIND(sym->st_info) == STB_WEAK,
463 mod->unres);
464 break;
465 default:
466 /* All exported symbols */
467 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700468 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
469 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
472 mod->has_init = 1;
473 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
474 mod->has_cleanup = 1;
475 break;
476 }
477}
478
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100479/**
480 * Parse tag=value strings from .modinfo section
481 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static char *next_string(char *string, unsigned long *secsize)
483{
484 /* Skip non-zero chars */
485 while (string[0]) {
486 string++;
487 if ((*secsize)-- <= 1)
488 return NULL;
489 }
490
491 /* Skip any zero padding. */
492 while (!string[0]) {
493 string++;
494 if ((*secsize)-- <= 1)
495 return NULL;
496 }
497 return string;
498}
499
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200500static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
501 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 char *p;
504 unsigned int taglen = strlen(tag);
505 unsigned long size = modinfo_len;
506
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200507 if (info) {
508 size -= info - (char *)modinfo;
509 modinfo = next_string(info, &size);
510 }
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 for (p = modinfo; p; p = next_string(p, &size)) {
513 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
514 return p + taglen + 1;
515 }
516 return NULL;
517}
518
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200519static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
520 const char *tag)
521
522{
523 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
524}
525
Sam Ravnborg93684d32006-02-19 11:53:35 +0100526/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100527 * Test if string s ends in string sub
528 * return 0 if match
529 **/
530static int strrcmp(const char *s, const char *sub)
531{
532 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100533
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100534 if (!s || !sub)
535 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100536
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100537 slen = strlen(s);
538 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100539
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100540 if ((slen == 0) || (sublen == 0))
541 return 1;
542
543 if (sublen > slen)
544 return 1;
545
546 return memcmp(s + slen - sublen, sub, sublen);
547}
548
549/**
550 * Whitelist to allow certain references to pass with no warning.
551 * Pattern 1:
552 * If a module parameter is declared __initdata and permissions=0
553 * then this is legal despite the warning generated.
554 * We cannot see value of permissions here, so just ignore
555 * this pattern.
556 * The pattern is identified by:
557 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100558 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100559 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100560 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100561 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700562 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100563 * add, remove, probe functions etc.
564 * These functions may often be marked __init and we do not want to
565 * warn here.
566 * the pattern is identified by:
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200567 * tosec = .init.text | .exit.text | .init.data
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100568 * fromsec = .data
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700569 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100570 **/
571static int secref_whitelist(const char *tosec, const char *fromsec,
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200572 const char *atsym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100573{
574 int f1 = 1, f2 = 1;
575 const char **s;
576 const char *pat2sym[] = {
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700577 "driver",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200578 "_template", /* scsi uses *_template a lot */
579 "_sht", /* scsi also used *_sht to some extent */
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100580 "_ops",
581 "_probe",
582 "_probe_one",
583 NULL
584 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100585
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100586 /* Check for pattern 1 */
587 if (strcmp(tosec, ".init.data") != 0)
588 f1 = 0;
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100589 if (strncmp(fromsec, ".data", strlen(".data")) != 0)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100590 f1 = 0;
591 if (strncmp(atsym, "__param", strlen("__param")) != 0)
592 f1 = 0;
593
594 if (f1)
595 return f1;
596
597 /* Check for pattern 2 */
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100598 if ((strcmp(tosec, ".init.text") != 0) &&
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200599 (strcmp(tosec, ".exit.text") != 0) &&
600 (strcmp(tosec, ".init.data") != 0))
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100601 f2 = 0;
602 if (strcmp(fromsec, ".data") != 0)
603 f2 = 0;
604
605 for (s = pat2sym; *s; s++)
606 if (strrcmp(atsym, *s) == 0)
607 f1 = 1;
608
609 return f1 && f2;
610}
611
612/**
Sam Ravnborg93684d32006-02-19 11:53:35 +0100613 * Find symbol based on relocation record info.
614 * In some cases the symbol supplied is a valid symbol so
615 * return refsym. If st_name != 0 we assume this is a valid symbol.
616 * In other cases the symbol needs to be looked up in the symbol table
617 * based on section and address.
618 * **/
619static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
620 Elf_Sym *relsym)
621{
622 Elf_Sym *sym;
623
624 if (relsym->st_name != 0)
625 return relsym;
626 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
627 if (sym->st_shndx != relsym->st_shndx)
628 continue;
629 if (sym->st_value == addr)
630 return sym;
631 }
632 return NULL;
633}
634
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100635/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100636 * Find symbols before or equal addr and after addr - in the section sec.
637 * If we find two symbols with equal offset prefer one with a valid name.
638 * The ELF format may have a better way to detect what type of symbol
639 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100640 **/
641static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
642 const char *sec,
643 Elf_Sym **before, Elf_Sym **after)
644{
645 Elf_Sym *sym;
646 Elf_Ehdr *hdr = elf->hdr;
647 Elf_Addr beforediff = ~0;
648 Elf_Addr afterdiff = ~0;
649 const char *secstrings = (void *)hdr +
650 elf->sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100651
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100652 *before = NULL;
653 *after = NULL;
654
655 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
656 const char *symsec;
657
658 if (sym->st_shndx >= SHN_LORESERVE)
659 continue;
660 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
661 if (strcmp(symsec, sec) != 0)
662 continue;
663 if (sym->st_value <= addr) {
664 if ((addr - sym->st_value) < beforediff) {
665 beforediff = addr - sym->st_value;
666 *before = sym;
667 }
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100668 else if ((addr - sym->st_value) == beforediff) {
669 /* equal offset, valid name? */
670 const char *name = elf->strtab + sym->st_name;
671 if (name && strlen(name))
672 *before = sym;
673 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100674 }
675 else
676 {
677 if ((sym->st_value - addr) < afterdiff) {
678 afterdiff = sym->st_value - addr;
679 *after = sym;
680 }
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100681 else if ((sym->st_value - addr) == afterdiff) {
682 /* equal offset, valid name? */
683 const char *name = elf->strtab + sym->st_name;
684 if (name && strlen(name))
685 *after = sym;
686 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100687 }
688 }
689}
690
691/**
692 * Print a warning about a section mismatch.
693 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100694 * Check whitelist before warning - it may be a false positive.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100695 **/
696static void warn_sec_mismatch(const char *modname, const char *fromsec,
697 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
698{
Sam Ravnborg93684d32006-02-19 11:53:35 +0100699 const char *refsymname = "";
700 Elf_Sym *before, *after;
701 Elf_Sym *refsym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100702 Elf_Ehdr *hdr = elf->hdr;
703 Elf_Shdr *sechdrs = elf->sechdrs;
704 const char *secstrings = (void *)hdr +
705 sechdrs[hdr->e_shstrndx].sh_offset;
706 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100707
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100708 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
709
Sam Ravnborg93684d32006-02-19 11:53:35 +0100710 refsym = find_elf_symbol(elf, r.r_addend, sym);
711 if (refsym && strlen(elf->strtab + refsym->st_name))
712 refsymname = elf->strtab + refsym->st_name;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100713
714 /* check whitelist - we may ignore it */
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100715 if (before &&
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100716 secref_whitelist(secname, fromsec, elf->strtab + before->st_name))
717 return;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100718
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100719 if (before && after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100720 warn("%s - Section mismatch: reference to %s:%s from %s "
721 "between '%s' (at offset 0x%llx) and '%s'\n",
722 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100723 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100724 (long long)r.r_offset,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100725 elf->strtab + after->st_name);
726 } else if (before) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100727 warn("%s - Section mismatch: reference to %s:%s from %s "
728 "after '%s' (at offset 0x%llx)\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100729 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100730 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100731 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100732 } else if (after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100733 warn("%s - Section mismatch: reference to %s:%s from %s "
734 "before '%s' (at offset -0x%llx)\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100735 modname, secname, refsymname, fromsec,
Eric Sesterhenneaaae382006-04-11 00:54:16 +0200736 elf->strtab + after->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100737 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100738 } else {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100739 warn("%s - Section mismatch: reference to %s:%s from %s "
740 "(offset 0x%llx)\n",
741 modname, secname, fromsec, refsymname,
742 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100743 }
744}
745
746/**
747 * A module includes a number of sections that are discarded
748 * either when loaded or when used as built-in.
749 * For loaded modules all functions marked __init and all data
750 * marked __initdata will be discarded when the module has been intialized.
751 * Likewise for modules used built-in the sections marked __exit
752 * are discarded because __exit marked function are supposed to be called
753 * only when a moduel is unloaded which never happes for built-in modules.
754 * The check_sec_ref() function traverses all relocation records
755 * to find all references to a section that reference a section that will
756 * be discarded and warns about it.
757 **/
758static void check_sec_ref(struct module *mod, const char *modname,
759 struct elf_info *elf,
760 int section(const char*),
761 int section_ref_ok(const char *))
762{
763 int i;
764 Elf_Sym *sym;
765 Elf_Ehdr *hdr = elf->hdr;
766 Elf_Shdr *sechdrs = elf->sechdrs;
767 const char *secstrings = (void *)hdr +
768 sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100769
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100770 /* Walk through all sections */
771 for (i = 0; i < hdr->e_shnum; i++) {
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700772 const char *name = secstrings + sechdrs[i].sh_name;
773 const char *secname;
774 Elf_Rela r;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700775 unsigned int r_sym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100776 /* We want to process only relocation sections and not .init */
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700777 if (sechdrs[i].sh_type == SHT_RELA) {
778 Elf_Rela *rela;
779 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
780 Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
781 name += strlen(".rela");
782 if (section_ref_ok(name))
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100783 continue;
784
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700785 for (rela = start; rela < stop; rela++) {
786 r.r_offset = TO_NATIVE(rela->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700787#if KERNEL_ELFCLASS == ELFCLASS64
788 if (hdr->e_machine == EM_MIPS) {
789 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
790 r_sym = TO_NATIVE(r_sym);
791 } else {
792 r.r_info = TO_NATIVE(rela->r_info);
793 r_sym = ELF_R_SYM(r.r_info);
794 }
795#else
796 r.r_info = TO_NATIVE(rela->r_info);
797 r_sym = ELF_R_SYM(r.r_info);
798#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700799 r.r_addend = TO_NATIVE(rela->r_addend);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700800 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700801 /* Skip special sections */
802 if (sym->st_shndx >= SHN_LORESERVE)
803 continue;
804
805 secname = secstrings +
806 sechdrs[sym->st_shndx].sh_name;
807 if (section(secname))
808 warn_sec_mismatch(modname, name,
809 elf, sym, r);
810 }
811 } else if (sechdrs[i].sh_type == SHT_REL) {
812 Elf_Rel *rel;
813 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
814 Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
815 name += strlen(".rel");
816 if (section_ref_ok(name))
817 continue;
818
819 for (rel = start; rel < stop; rel++) {
820 r.r_offset = TO_NATIVE(rel->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700821#if KERNEL_ELFCLASS == ELFCLASS64
822 if (hdr->e_machine == EM_MIPS) {
823 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
824 r_sym = TO_NATIVE(r_sym);
825 } else {
826 r.r_info = TO_NATIVE(rel->r_info);
827 r_sym = ELF_R_SYM(r.r_info);
828 }
829#else
830 r.r_info = TO_NATIVE(rel->r_info);
831 r_sym = ELF_R_SYM(r.r_info);
832#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700833 r.r_addend = 0;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700834 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700835 /* Skip special sections */
836 if (sym->st_shndx >= SHN_LORESERVE)
837 continue;
838
839 secname = secstrings +
840 sechdrs[sym->st_shndx].sh_name;
841 if (section(secname))
842 warn_sec_mismatch(modname, name,
843 elf, sym, r);
844 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100845 }
846 }
847}
848
849/**
850 * Functions used only during module init is marked __init and is stored in
851 * a .init.text section. Likewise data is marked __initdata and stored in
852 * a .init.data section.
853 * If this section is one of these sections return 1
854 * See include/linux/init.h for the details
855 **/
856static int init_section(const char *name)
857{
858 if (strcmp(name, ".init") == 0)
859 return 1;
860 if (strncmp(name, ".init.", strlen(".init.")) == 0)
861 return 1;
862 return 0;
863}
864
865/**
866 * Identify sections from which references to a .init section is OK.
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100867 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100868 * Unfortunately references to read only data that referenced .init
869 * sections had to be excluded. Almost all of these are false
870 * positives, they are created by gcc. The downside of excluding rodata
871 * is that there really are some user references from rodata to
872 * init code, e.g. drivers/video/vgacon.c:
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100873 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100874 * const struct consw vga_con = {
875 * con_startup: vgacon_startup,
876 *
877 * where vgacon_startup is __init. If you want to wade through the false
878 * positives, take out the check for rodata.
879 **/
880static int init_section_ref_ok(const char *name)
881{
882 const char **s;
883 /* Absolute section names */
884 const char *namelist1[] = {
885 ".init",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100886 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
887 ".toc1", /* used by ppc64 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100888 ".stab",
889 ".rodata",
890 ".text.lock",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100891 "__bug_table", /* used by powerpc for BUG() */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100892 ".pci_fixup_header",
893 ".pci_fixup_final",
894 ".pdr",
895 "__param",
Al Viro468d9492006-06-23 23:22:43 +0100896 "__ex_table",
897 ".fixup",
Randy Dunlap35899c52006-06-07 16:23:26 -0700898 ".smp_locks",
Sam Ravnborg909252d2006-06-08 20:37:30 +0200899 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100900 NULL
901 };
902 /* Start of section names */
903 const char *namelist2[] = {
904 ".init.",
905 ".altinstructions",
906 ".eh_frame",
907 ".debug",
908 NULL
909 };
Sam Ravnborg6e101332006-02-22 21:24:50 +0100910 /* part of section name */
911 const char *namelist3 [] = {
912 ".unwind", /* sample: IA_64.unwind.init.text */
913 NULL
914 };
915
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100916 for (s = namelist1; *s; s++)
917 if (strcmp(*s, name) == 0)
918 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100919 for (s = namelist2; *s; s++)
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100920 if (strncmp(*s, name, strlen(*s)) == 0)
921 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100922 for (s = namelist3; *s; s++)
Sam Ravnborge835a392006-03-05 11:34:15 +0100923 if (strstr(name, *s) != NULL)
Sam Ravnborg6e101332006-02-22 21:24:50 +0100924 return 1;
Al Viro468d9492006-06-23 23:22:43 +0100925 if (strrcmp(name, ".init") == 0)
926 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100927 return 0;
928}
929
930/*
931 * Functions used only during module exit is marked __exit and is stored in
932 * a .exit.text section. Likewise data is marked __exitdata and stored in
933 * a .exit.data section.
934 * If this section is one of these sections return 1
935 * See include/linux/init.h for the details
936 **/
937static int exit_section(const char *name)
938{
939 if (strcmp(name, ".exit.text") == 0)
940 return 1;
941 if (strcmp(name, ".exit.data") == 0)
942 return 1;
943 return 0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100944
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100945}
946
947/*
948 * Identify sections from which references to a .exit section is OK.
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100949 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100950 * [OPD] Keith Ownes <kaos@sgi.com> commented:
951 * For our future {in}sanity, add a comment that this is the ppc .opd
952 * section, not the ia64 .opd section.
953 * ia64 .opd should not point to discarded sections.
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200954 * [.rodata] like for .init.text we ignore .rodata references -same reason
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100955 **/
956static int exit_section_ref_ok(const char *name)
957{
958 const char **s;
959 /* Absolute section names */
960 const char *namelist1[] = {
961 ".exit.text",
962 ".exit.data",
963 ".init.text",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200964 ".rodata",
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100965 ".opd", /* See comment [OPD] */
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100966 ".toc1", /* used by ppc64 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100967 ".altinstructions",
968 ".pdr",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100969 "__bug_table", /* used by powerpc for BUG() */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100970 ".exitcall.exit",
971 ".eh_frame",
972 ".stab",
Al Viro468d9492006-06-23 23:22:43 +0100973 "__ex_table",
974 ".fixup",
Randy Dunlap35899c52006-06-07 16:23:26 -0700975 ".smp_locks",
Sam Ravnborg909252d2006-06-08 20:37:30 +0200976 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100977 NULL
978 };
979 /* Start of section names */
980 const char *namelist2[] = {
981 ".debug",
982 NULL
983 };
Sam Ravnborg6e101332006-02-22 21:24:50 +0100984 /* part of section name */
985 const char *namelist3 [] = {
986 ".unwind", /* Sample: IA_64.unwind.exit.text */
987 NULL
988 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100989
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100990 for (s = namelist1; *s; s++)
991 if (strcmp(*s, name) == 0)
992 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100993 for (s = namelist2; *s; s++)
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100994 if (strncmp(*s, name, strlen(*s)) == 0)
995 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100996 for (s = namelist3; *s; s++)
Sam Ravnborge835a392006-03-05 11:34:15 +0100997 if (strstr(name, *s) != NULL)
Sam Ravnborg6e101332006-02-22 21:24:50 +0100998 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100999 return 0;
1000}
1001
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001002static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 const char *symname;
1005 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001006 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct module *mod;
1008 struct elf_info info = { };
1009 Elf_Sym *sym;
1010
1011 parse_elf(&info, modname);
1012
1013 mod = new_module(modname);
1014
1015 /* When there's no vmlinux, don't print warnings about
1016 * unresolved symbols (since there'll be too many ;) */
1017 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 mod->skip = 1;
1020 }
1021
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001022 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1023 while (license) {
1024 if (license_is_gpl_compatible(license))
1025 mod->gpl_compatible = 1;
1026 else {
1027 mod->gpl_compatible = 0;
1028 break;
1029 }
1030 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1031 "license", license);
1032 }
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1035 symname = info.strtab + sym->st_name;
1036
1037 handle_modversions(mod, &info, sym, symname);
1038 handle_moddevtable(mod, &info, sym, symname);
1039 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001040 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1041 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1044 if (version)
1045 maybe_frob_rcs_version(modname, version, info.modinfo,
1046 version - (char *)info.hdr);
1047 if (version || (all_versions && !is_vmlinux(modname)))
1048 get_src_version(modname, mod->srcversion,
1049 sizeof(mod->srcversion)-1);
1050
1051 parse_elf_finish(&info);
1052
1053 /* Our trick to get versioning for struct_module - it's
1054 * never passed as an argument to an exported function, so
1055 * the automatic versioning doesn't pick it up, but it's really
1056 * important anyhow */
1057 if (modversions)
1058 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1059}
1060
1061#define SZ 500
1062
1063/* We first write the generated file into memory using the
1064 * following helper, then compare to the file on disk and
1065 * only update the later if anything changed */
1066
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001067void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1068 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 char tmp[SZ];
1071 int len;
1072 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 va_start(ap, fmt);
1075 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001076 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 va_end(ap);
1078}
1079
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001080void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001083 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 buf->p = realloc(buf->p, buf->size);
1085 }
1086 strncpy(buf->p + buf->pos, s, len);
1087 buf->pos += len;
1088}
1089
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001090void check_license(struct module *mod)
1091{
1092 struct symbol *s, *exp;
1093
1094 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001095 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001096 if (mod->gpl_compatible == 1) {
1097 /* GPL-compatible modules may use all symbols */
1098 continue;
1099 }
1100 exp = find_symbol(s->name);
1101 if (!exp || exp->module == mod)
1102 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001103 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001104 if (basename)
1105 basename++;
1106 switch (exp->export) {
1107 case export_gpl:
1108 fatal("modpost: GPL-incompatible module %s "
1109 "uses GPL-only symbol '%s'\n",
1110 basename ? basename : mod->name,
1111 exp->name);
1112 break;
1113 case export_gpl_future:
1114 warn("modpost: GPL-incompatible module %s "
1115 "uses future GPL-only symbol '%s'\n",
1116 basename ? basename : mod->name,
1117 exp->name);
1118 break;
1119 case export_plain: /* ignore */ break;
1120 case export_unknown: /* ignore */ break;
1121 }
1122 }
1123}
1124
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001125/**
1126 * Header for the generated file
1127 **/
1128static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 buf_printf(b, "#include <linux/module.h>\n");
1131 buf_printf(b, "#include <linux/vermagic.h>\n");
1132 buf_printf(b, "#include <linux/compiler.h>\n");
1133 buf_printf(b, "\n");
1134 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1135 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 buf_printf(b, "struct module __this_module\n");
1137 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001138 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (mod->has_init)
1140 buf_printf(b, " .init = init_module,\n");
1141 if (mod->has_cleanup)
1142 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1143 " .exit = cleanup_module,\n"
1144 "#endif\n");
1145 buf_printf(b, "};\n");
1146}
1147
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001148/**
1149 * Record CRCs for unresolved symbols
1150 **/
1151static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct symbol *s, *exp;
1154
1155 for (s = mod->unres; s; s = s->next) {
1156 exp = find_symbol(s->name);
1157 if (!exp || exp->module == mod) {
1158 if (have_vmlinux && !s->weak)
Sam Ravnborgcb805142006-01-28 16:57:26 +01001159 warn("\"%s\" [%s.ko] undefined!\n",
1160 s->name, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 continue;
1162 }
1163 s->module = exp->module;
1164 s->crc_valid = exp->crc_valid;
1165 s->crc = exp->crc;
1166 }
1167
1168 if (!modversions)
1169 return;
1170
1171 buf_printf(b, "\n");
1172 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1173 buf_printf(b, "__attribute_used__\n");
1174 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1175
1176 for (s = mod->unres; s; s = s->next) {
1177 if (!s->module) {
1178 continue;
1179 }
1180 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001181 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 s->name, mod->name);
1183 continue;
1184 }
1185 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1186 }
1187
1188 buf_printf(b, "};\n");
1189}
1190
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001191static void add_depends(struct buffer *b, struct module *mod,
1192 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 struct symbol *s;
1195 struct module *m;
1196 int first = 1;
1197
1198 for (m = modules; m; m = m->next) {
1199 m->seen = is_vmlinux(m->name);
1200 }
1201
1202 buf_printf(b, "\n");
1203 buf_printf(b, "static const char __module_depends[]\n");
1204 buf_printf(b, "__attribute_used__\n");
1205 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1206 buf_printf(b, "\"depends=");
1207 for (s = mod->unres; s; s = s->next) {
1208 if (!s->module)
1209 continue;
1210
1211 if (s->module->seen)
1212 continue;
1213
1214 s->module->seen = 1;
1215 buf_printf(b, "%s%s", first ? "" : ",",
1216 strrchr(s->module->name, '/') + 1);
1217 first = 0;
1218 }
1219 buf_printf(b, "\";\n");
1220}
1221
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001222static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 if (mod->srcversion[0]) {
1225 buf_printf(b, "\n");
1226 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1227 mod->srcversion);
1228 }
1229}
1230
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001231static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 char *tmp;
1234 FILE *file;
1235 struct stat st;
1236
1237 file = fopen(fname, "r");
1238 if (!file)
1239 goto write;
1240
1241 if (fstat(fileno(file), &st) < 0)
1242 goto close_write;
1243
1244 if (st.st_size != b->pos)
1245 goto close_write;
1246
1247 tmp = NOFAIL(malloc(b->pos));
1248 if (fread(tmp, 1, b->pos, file) != b->pos)
1249 goto free_write;
1250
1251 if (memcmp(tmp, b->p, b->pos) != 0)
1252 goto free_write;
1253
1254 free(tmp);
1255 fclose(file);
1256 return;
1257
1258 free_write:
1259 free(tmp);
1260 close_write:
1261 fclose(file);
1262 write:
1263 file = fopen(fname, "w");
1264 if (!file) {
1265 perror(fname);
1266 exit(1);
1267 }
1268 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1269 perror(fname);
1270 exit(1);
1271 }
1272 fclose(file);
1273}
1274
Ram Paibd5cbce2006-06-08 22:12:53 -07001275/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001276 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001277 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001278static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 unsigned long size, pos = 0;
1281 void *file = grab_file(fname, &size);
1282 char *line;
1283
1284 if (!file)
1285 /* No symbol versions, silently ignore */
1286 return;
1287
1288 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001289 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 unsigned int crc;
1291 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001292 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if (!(symname = strchr(line, '\t')))
1295 goto fail;
1296 *symname++ = '\0';
1297 if (!(modname = strchr(symname, '\t')))
1298 goto fail;
1299 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001300 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001301 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001302 if (export && ((end = strchr(export, '\t')) != NULL))
1303 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 crc = strtoul(line, &d, 16);
1305 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1306 goto fail;
1307
1308 if (!(mod = find_module(modname))) {
1309 if (is_vmlinux(modname)) {
1310 have_vmlinux = 1;
1311 }
1312 mod = new_module(NOFAIL(strdup(modname)));
1313 mod->skip = 1;
1314 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001315 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001316 s->kernel = kernel;
1317 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001318 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 return;
1321fail:
1322 fatal("parse error in symbol dump file\n");
1323}
1324
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001325/* For normal builds always dump all symbols.
1326 * For external modules only dump symbols
1327 * that are not read from kernel Module.symvers.
1328 **/
1329static int dump_sym(struct symbol *sym)
1330{
1331 if (!external_module)
1332 return 1;
1333 if (sym->vmlinux || sym->kernel)
1334 return 0;
1335 return 1;
1336}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001337
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001338static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct buffer buf = { };
1341 struct symbol *symbol;
1342 int n;
1343
1344 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1345 symbol = symbolhash[n];
1346 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001347 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001348 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001349 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001350 symbol->module->name,
1351 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 symbol = symbol->next;
1353 }
1354 }
1355 write_if_changed(&buf, fname);
1356}
1357
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001358int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 struct module *mod;
1361 struct buffer buf = { };
1362 char fname[SZ];
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001363 char *kernel_read = NULL, *module_read = NULL;
1364 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 int opt;
1366
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001367 while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 switch(opt) {
1369 case 'i':
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001370 kernel_read = optarg;
1371 break;
1372 case 'I':
1373 module_read = optarg;
1374 external_module = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 break;
1376 case 'm':
1377 modversions = 1;
1378 break;
1379 case 'o':
1380 dump_write = optarg;
1381 break;
1382 case 'a':
1383 all_versions = 1;
1384 break;
1385 default:
1386 exit(1);
1387 }
1388 }
1389
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001390 if (kernel_read)
1391 read_dump(kernel_read, 1);
1392 if (module_read)
1393 read_dump(module_read, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 while (optind < argc) {
1396 read_symbols(argv[optind++]);
1397 }
1398
1399 for (mod = modules; mod; mod = mod->next) {
1400 if (mod->skip)
1401 continue;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001402 check_license(mod);
1403 }
1404
1405 for (mod = modules; mod; mod = mod->next) {
1406 if (mod->skip)
1407 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 buf.pos = 0;
1410
1411 add_header(&buf, mod);
1412 add_versions(&buf, mod);
1413 add_depends(&buf, mod, modules);
1414 add_moddevtable(&buf, mod);
1415 add_srcversion(&buf, mod);
1416
1417 sprintf(fname, "%s.mod.c", mod->name);
1418 write_if_changed(&buf, fname);
1419 }
1420
1421 if (dump_write)
1422 write_dump(dump_write);
1423
1424 return 0;
1425}