blob: dfde0e87a765e2e53c55f82fd505f22e76523999 [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 */
Sam Ravnborgc96fca22006-07-01 11:44:23 +020027enum export {
28 export_plain, export_unused, export_gpl,
29 export_unused_gpl, export_gpl_future, export_unknown
30};
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010032void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 va_list arglist;
35
36 fprintf(stderr, "FATAL: ");
37
38 va_start(arglist, fmt);
39 vfprintf(stderr, fmt, arglist);
40 va_end(arglist);
41
42 exit(1);
43}
44
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010045void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 va_list arglist;
48
49 fprintf(stderr, "WARNING: ");
50
51 va_start(arglist, fmt);
52 vfprintf(stderr, fmt, arglist);
53 va_end(arglist);
54}
55
Sam Ravnborg040fcc82006-01-28 22:15:55 +010056static int is_vmlinux(const char *modname)
57{
58 const char *myname;
59
60 if ((myname = strrchr(modname, '/')))
61 myname++;
62 else
63 myname = modname;
64
65 return strcmp(myname, "vmlinux") == 0;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068void *do_nofail(void *ptr, const char *expr)
69{
70 if (!ptr) {
71 fatal("modpost: Memory allocation failure: %s.\n", expr);
72 }
73 return ptr;
74}
75
76/* A list of all modules we processed */
77
78static struct module *modules;
79
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010080static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct module *mod;
83
84 for (mod = modules; mod; mod = mod->next)
85 if (strcmp(mod->name, modname) == 0)
86 break;
87 return mod;
88}
89
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010090static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 struct module *mod;
93 char *p, *s;
Sam Ravnborg62070fa2006-03-03 16:46:04 +010094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 mod = NOFAIL(malloc(sizeof(*mod)));
96 memset(mod, 0, sizeof(*mod));
97 p = NOFAIL(strdup(modname));
98
99 /* strip trailing .o */
100 if ((s = strrchr(p, '.')) != NULL)
101 if (strcmp(s, ".o") == 0)
102 *s = '\0';
103
104 /* add to list */
105 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200106 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 mod->next = modules;
108 modules = mod;
109
110 return mod;
111}
112
113/* A hash of all exported symbols,
114 * struct symbol is also used for lists of unresolved symbols */
115
116#define SYMBOL_HASH_SIZE 1024
117
118struct symbol {
119 struct symbol *next;
120 struct module *module;
121 unsigned int crc;
122 int crc_valid;
123 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100124 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
125 unsigned int kernel:1; /* 1 if symbol is from kernel
126 * (only for external modules) **/
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100127 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Ram Paibd5cbce2006-06-08 22:12:53 -0700128 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 char name[0];
130};
131
132static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
133
134/* This is based on the hash agorithm from gdbm, via tdb */
135static inline unsigned int tdb_hash(const char *name)
136{
137 unsigned value; /* Used to compute the hash value. */
138 unsigned i; /* Used to cycle through random values. */
139
140 /* Set the initial value from the key size. */
141 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
142 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
143
144 return (1103515243 * value + 12345);
145}
146
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100147/**
148 * Allocate a new symbols for use in the hash of exported symbols or
149 * the list of unresolved symbols per module
150 **/
151static struct symbol *alloc_symbol(const char *name, unsigned int weak,
152 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
155
156 memset(s, 0, sizeof(*s));
157 strcpy(s->name, name);
158 s->weak = weak;
159 s->next = next;
160 return s;
161}
162
163/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700164static struct symbol *new_symbol(const char *name, struct module *module,
165 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 unsigned int hash;
168 struct symbol *new;
169
170 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
171 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
172 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700173 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100174 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100177static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct symbol *s;
180
181 /* For our purposes, .foo matches foo. PPC64 needs this. */
182 if (name[0] == '.')
183 name++;
184
185 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
186 if (strcmp(s->name, name) == 0)
187 return s;
188 }
189 return NULL;
190}
191
Ram Paibd5cbce2006-06-08 22:12:53 -0700192static struct {
193 const char *str;
194 enum export export;
195} export_list[] = {
196 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200197 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700198 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200199 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700200 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
201 { .str = "(unknown)", .export = export_unknown },
202};
203
204
205static const char *export_str(enum export ex)
206{
207 return export_list[ex].str;
208}
209
210static enum export export_no(const char * s)
211{
212 int i;
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200213 if (!s)
214 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700215 for (i = 0; export_list[i].export != export_unknown; i++) {
216 if (strcmp(export_list[i].str, s) == 0)
217 return export_list[i].export;
218 }
219 return export_unknown;
220}
221
222static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
223{
224 if (sec == elf->export_sec)
225 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200226 else if (sec == elf->export_unused_sec)
227 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700228 else if (sec == elf->export_gpl_sec)
229 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200230 else if (sec == elf->export_unused_gpl_sec)
231 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700232 else if (sec == elf->export_gpl_future_sec)
233 return export_gpl_future;
234 else
235 return export_unknown;
236}
237
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100238/**
239 * Add an exported symbol - it may have already been added without a
240 * CRC, in this case just update the CRC
241 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700242static struct symbol *sym_add_exported(const char *name, struct module *mod,
243 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct symbol *s = find_symbol(name);
246
247 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700248 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100249 } else {
250 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100251 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100252 "was in %s%s\n", mod->name, name,
253 s->module->name,
254 is_vmlinux(s->module->name) ?"":".ko");
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100257 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100258 s->vmlinux = is_vmlinux(mod->name);
259 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700260 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100261 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100264static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700265 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100266{
267 struct symbol *s = find_symbol(name);
268
269 if (!s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700270 s = new_symbol(name, mod, export);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100271 s->crc = crc;
272 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100275void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct stat st;
278 void *map;
279 int fd;
280
281 fd = open(filename, O_RDONLY);
282 if (fd < 0 || fstat(fd, &st) != 0)
283 return NULL;
284
285 *size = st.st_size;
286 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
287 close(fd);
288
289 if (map == MAP_FAILED)
290 return NULL;
291 return map;
292}
293
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100294/**
295 * Return a copy of the next line in a mmap'ed file.
296 * spaces in the beginning of the line is trimmed away.
297 * Return a pointer to a static buffer.
298 **/
299char* get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 static char line[4096];
302 int skip = 1;
303 size_t len = 0;
304 signed char *p = (signed char *)file + *pos;
305 char *s = line;
306
307 for (; *pos < size ; (*pos)++)
308 {
309 if (skip && isspace(*p)) {
310 p++;
311 continue;
312 }
313 skip = 0;
314 if (*p != '\n' && (*pos < size)) {
315 len++;
316 *s++ = *p++;
317 if (len > 4095)
318 break; /* Too long, stop */
319 } else {
320 /* End of string */
321 *s = '\0';
322 return line;
323 }
324 }
325 /* End of buffer */
326 return NULL;
327}
328
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100329void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 munmap(file, size);
332}
333
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100334static void parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 unsigned int i;
337 Elf_Ehdr *hdr = info->hdr;
338 Elf_Shdr *sechdrs;
339 Elf_Sym *sym;
340
341 hdr = grab_file(filename, &info->size);
342 if (!hdr) {
343 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200344 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 info->hdr = hdr;
347 if (info->size < sizeof(*hdr))
348 goto truncated;
349
350 /* Fix endianness in ELF header */
351 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
352 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
353 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
354 hdr->e_machine = TO_NATIVE(hdr->e_machine);
355 sechdrs = (void *)hdr + hdr->e_shoff;
356 info->sechdrs = sechdrs;
357
358 /* Fix endianness in section headers */
359 for (i = 0; i < hdr->e_shnum; i++) {
360 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
361 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
362 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
363 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
364 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
365 }
366 /* Find symbol table. */
367 for (i = 1; i < hdr->e_shnum; i++) {
368 const char *secstrings
369 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
Ram Paibd5cbce2006-06-08 22:12:53 -0700370 const char *secname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 if (sechdrs[i].sh_offset > info->size)
373 goto truncated;
Ram Paibd5cbce2006-06-08 22:12:53 -0700374 secname = secstrings + sechdrs[i].sh_name;
375 if (strcmp(secname, ".modinfo") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
377 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700378 } else if (strcmp(secname, "__ksymtab") == 0)
379 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200380 else if (strcmp(secname, "__ksymtab_unused") == 0)
381 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700382 else if (strcmp(secname, "__ksymtab_gpl") == 0)
383 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200384 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
385 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700386 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
387 info->export_gpl_future_sec = i;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (sechdrs[i].sh_type != SHT_SYMTAB)
390 continue;
391
392 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100393 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100395 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sechdrs[sechdrs[i].sh_link].sh_offset;
397 }
398 if (!info->symtab_start) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100399 fatal("%s has no symtab?\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 /* Fix endianness in symbols */
402 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
403 sym->st_shndx = TO_NATIVE(sym->st_shndx);
404 sym->st_name = TO_NATIVE(sym->st_name);
405 sym->st_value = TO_NATIVE(sym->st_value);
406 sym->st_size = TO_NATIVE(sym->st_size);
407 }
408 return;
409
410 truncated:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100411 fatal("%s is truncated.\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100414static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 release_file(info->hdr, info->size);
417}
418
Luke Yangf7b05e62006-03-02 18:35:31 +0800419#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
420#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100422static void handle_modversions(struct module *mod, struct elf_info *info,
423 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700426 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 switch (sym->st_shndx) {
429 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100430 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 case SHN_ABS:
433 /* CRC'd symbol */
434 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
435 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700436 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
437 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 break;
440 case SHN_UNDEF:
441 /* undefined symbol */
442 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
443 ELF_ST_BIND(sym->st_info) != STB_WEAK)
444 break;
445 /* ignore global offset table */
446 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
447 break;
448 /* ignore __this_module, it will be resolved shortly */
449 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
450 break;
Ben Colline8d529012005-08-19 13:44:57 -0700451/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
452#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
453/* add compatibility with older glibc */
454#ifndef STT_SPARC_REGISTER
455#define STT_SPARC_REGISTER STT_REGISTER
456#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (info->hdr->e_machine == EM_SPARC ||
458 info->hdr->e_machine == EM_SPARCV9) {
459 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700460 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100462 if (symname[0] == '.') {
463 char *munged = strdup(symname);
464 munged[0] = '_';
465 munged[1] = toupper(munged[1]);
466 symname = munged;
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
472 strlen(MODULE_SYMBOL_PREFIX)) == 0)
473 mod->unres = alloc_symbol(symname +
474 strlen(MODULE_SYMBOL_PREFIX),
475 ELF_ST_BIND(sym->st_info) == STB_WEAK,
476 mod->unres);
477 break;
478 default:
479 /* All exported symbols */
480 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700481 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
482 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
485 mod->has_init = 1;
486 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
487 mod->has_cleanup = 1;
488 break;
489 }
490}
491
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100492/**
493 * Parse tag=value strings from .modinfo section
494 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static char *next_string(char *string, unsigned long *secsize)
496{
497 /* Skip non-zero chars */
498 while (string[0]) {
499 string++;
500 if ((*secsize)-- <= 1)
501 return NULL;
502 }
503
504 /* Skip any zero padding. */
505 while (!string[0]) {
506 string++;
507 if ((*secsize)-- <= 1)
508 return NULL;
509 }
510 return string;
511}
512
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200513static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
514 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 char *p;
517 unsigned int taglen = strlen(tag);
518 unsigned long size = modinfo_len;
519
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200520 if (info) {
521 size -= info - (char *)modinfo;
522 modinfo = next_string(info, &size);
523 }
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 for (p = modinfo; p; p = next_string(p, &size)) {
526 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
527 return p + taglen + 1;
528 }
529 return NULL;
530}
531
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200532static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
533 const char *tag)
534
535{
536 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
537}
538
Sam Ravnborg93684d32006-02-19 11:53:35 +0100539/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100540 * Test if string s ends in string sub
541 * return 0 if match
542 **/
543static int strrcmp(const char *s, const char *sub)
544{
545 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100546
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100547 if (!s || !sub)
548 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100549
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100550 slen = strlen(s);
551 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100552
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100553 if ((slen == 0) || (sublen == 0))
554 return 1;
555
556 if (sublen > slen)
557 return 1;
558
559 return memcmp(s + slen - sublen, sub, sublen);
560}
561
562/**
563 * Whitelist to allow certain references to pass with no warning.
564 * Pattern 1:
565 * If a module parameter is declared __initdata and permissions=0
566 * then this is legal despite the warning generated.
567 * We cannot see value of permissions here, so just ignore
568 * this pattern.
569 * The pattern is identified by:
570 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100571 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100572 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100573 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100574 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700575 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100576 * add, remove, probe functions etc.
577 * These functions may often be marked __init and we do not want to
578 * warn here.
579 * the pattern is identified by:
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200580 * tosec = .init.text | .exit.text | .init.data
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100581 * fromsec = .data
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700582 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100583 **/
584static int secref_whitelist(const char *tosec, const char *fromsec,
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200585 const char *atsym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100586{
587 int f1 = 1, f2 = 1;
588 const char **s;
589 const char *pat2sym[] = {
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700590 "driver",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200591 "_template", /* scsi uses *_template a lot */
592 "_sht", /* scsi also used *_sht to some extent */
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100593 "_ops",
594 "_probe",
595 "_probe_one",
596 NULL
597 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100598
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100599 /* Check for pattern 1 */
600 if (strcmp(tosec, ".init.data") != 0)
601 f1 = 0;
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100602 if (strncmp(fromsec, ".data", strlen(".data")) != 0)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100603 f1 = 0;
604 if (strncmp(atsym, "__param", strlen("__param")) != 0)
605 f1 = 0;
606
607 if (f1)
608 return f1;
609
610 /* Check for pattern 2 */
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100611 if ((strcmp(tosec, ".init.text") != 0) &&
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200612 (strcmp(tosec, ".exit.text") != 0) &&
613 (strcmp(tosec, ".init.data") != 0))
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100614 f2 = 0;
615 if (strcmp(fromsec, ".data") != 0)
616 f2 = 0;
617
618 for (s = pat2sym; *s; s++)
619 if (strrcmp(atsym, *s) == 0)
620 f1 = 1;
621
622 return f1 && f2;
623}
624
625/**
Sam Ravnborg93684d32006-02-19 11:53:35 +0100626 * Find symbol based on relocation record info.
627 * In some cases the symbol supplied is a valid symbol so
628 * return refsym. If st_name != 0 we assume this is a valid symbol.
629 * In other cases the symbol needs to be looked up in the symbol table
630 * based on section and address.
631 * **/
632static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
633 Elf_Sym *relsym)
634{
635 Elf_Sym *sym;
636
637 if (relsym->st_name != 0)
638 return relsym;
639 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
640 if (sym->st_shndx != relsym->st_shndx)
641 continue;
642 if (sym->st_value == addr)
643 return sym;
644 }
645 return NULL;
646}
647
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100648/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100649 * Find symbols before or equal addr and after addr - in the section sec.
650 * If we find two symbols with equal offset prefer one with a valid name.
651 * The ELF format may have a better way to detect what type of symbol
652 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100653 **/
654static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
655 const char *sec,
656 Elf_Sym **before, Elf_Sym **after)
657{
658 Elf_Sym *sym;
659 Elf_Ehdr *hdr = elf->hdr;
660 Elf_Addr beforediff = ~0;
661 Elf_Addr afterdiff = ~0;
662 const char *secstrings = (void *)hdr +
663 elf->sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100664
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100665 *before = NULL;
666 *after = NULL;
667
668 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
669 const char *symsec;
670
671 if (sym->st_shndx >= SHN_LORESERVE)
672 continue;
673 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
674 if (strcmp(symsec, sec) != 0)
675 continue;
676 if (sym->st_value <= addr) {
677 if ((addr - sym->st_value) < beforediff) {
678 beforediff = addr - sym->st_value;
679 *before = sym;
680 }
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100681 else if ((addr - sym->st_value) == beforediff) {
682 /* equal offset, valid name? */
683 const char *name = elf->strtab + sym->st_name;
684 if (name && strlen(name))
685 *before = sym;
686 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100687 }
688 else
689 {
690 if ((sym->st_value - addr) < afterdiff) {
691 afterdiff = sym->st_value - addr;
692 *after = sym;
693 }
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100694 else if ((sym->st_value - addr) == afterdiff) {
695 /* equal offset, valid name? */
696 const char *name = elf->strtab + sym->st_name;
697 if (name && strlen(name))
698 *after = sym;
699 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100700 }
701 }
702}
703
704/**
705 * Print a warning about a section mismatch.
706 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100707 * Check whitelist before warning - it may be a false positive.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100708 **/
709static void warn_sec_mismatch(const char *modname, const char *fromsec,
710 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
711{
Sam Ravnborg93684d32006-02-19 11:53:35 +0100712 const char *refsymname = "";
713 Elf_Sym *before, *after;
714 Elf_Sym *refsym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100715 Elf_Ehdr *hdr = elf->hdr;
716 Elf_Shdr *sechdrs = elf->sechdrs;
717 const char *secstrings = (void *)hdr +
718 sechdrs[hdr->e_shstrndx].sh_offset;
719 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100720
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100721 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
722
Sam Ravnborg93684d32006-02-19 11:53:35 +0100723 refsym = find_elf_symbol(elf, r.r_addend, sym);
724 if (refsym && strlen(elf->strtab + refsym->st_name))
725 refsymname = elf->strtab + refsym->st_name;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100726
727 /* check whitelist - we may ignore it */
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100728 if (before &&
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100729 secref_whitelist(secname, fromsec, elf->strtab + before->st_name))
730 return;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100731
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100732 if (before && after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100733 warn("%s - Section mismatch: reference to %s:%s from %s "
734 "between '%s' (at offset 0x%llx) and '%s'\n",
735 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100736 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100737 (long long)r.r_offset,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100738 elf->strtab + after->st_name);
739 } else if (before) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100740 warn("%s - Section mismatch: reference to %s:%s from %s "
741 "after '%s' (at offset 0x%llx)\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100742 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100743 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100744 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100745 } else if (after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100746 warn("%s - Section mismatch: reference to %s:%s from %s "
747 "before '%s' (at offset -0x%llx)\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100748 modname, secname, refsymname, fromsec,
Eric Sesterhenneaaae382006-04-11 00:54:16 +0200749 elf->strtab + after->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100750 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100751 } else {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100752 warn("%s - Section mismatch: reference to %s:%s from %s "
753 "(offset 0x%llx)\n",
754 modname, secname, fromsec, refsymname,
755 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100756 }
757}
758
759/**
760 * A module includes a number of sections that are discarded
761 * either when loaded or when used as built-in.
762 * For loaded modules all functions marked __init and all data
763 * marked __initdata will be discarded when the module has been intialized.
764 * Likewise for modules used built-in the sections marked __exit
765 * are discarded because __exit marked function are supposed to be called
766 * only when a moduel is unloaded which never happes for built-in modules.
767 * The check_sec_ref() function traverses all relocation records
768 * to find all references to a section that reference a section that will
769 * be discarded and warns about it.
770 **/
771static void check_sec_ref(struct module *mod, const char *modname,
772 struct elf_info *elf,
773 int section(const char*),
774 int section_ref_ok(const char *))
775{
776 int i;
777 Elf_Sym *sym;
778 Elf_Ehdr *hdr = elf->hdr;
779 Elf_Shdr *sechdrs = elf->sechdrs;
780 const char *secstrings = (void *)hdr +
781 sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100782
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100783 /* Walk through all sections */
784 for (i = 0; i < hdr->e_shnum; i++) {
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700785 const char *name = secstrings + sechdrs[i].sh_name;
786 const char *secname;
787 Elf_Rela r;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700788 unsigned int r_sym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100789 /* We want to process only relocation sections and not .init */
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700790 if (sechdrs[i].sh_type == SHT_RELA) {
791 Elf_Rela *rela;
792 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
793 Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
794 name += strlen(".rela");
795 if (section_ref_ok(name))
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100796 continue;
797
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700798 for (rela = start; rela < stop; rela++) {
799 r.r_offset = TO_NATIVE(rela->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700800#if KERNEL_ELFCLASS == ELFCLASS64
801 if (hdr->e_machine == EM_MIPS) {
802 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
803 r_sym = TO_NATIVE(r_sym);
804 } else {
805 r.r_info = TO_NATIVE(rela->r_info);
806 r_sym = ELF_R_SYM(r.r_info);
807 }
808#else
809 r.r_info = TO_NATIVE(rela->r_info);
810 r_sym = ELF_R_SYM(r.r_info);
811#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700812 r.r_addend = TO_NATIVE(rela->r_addend);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700813 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700814 /* Skip special sections */
815 if (sym->st_shndx >= SHN_LORESERVE)
816 continue;
817
818 secname = secstrings +
819 sechdrs[sym->st_shndx].sh_name;
820 if (section(secname))
821 warn_sec_mismatch(modname, name,
822 elf, sym, r);
823 }
824 } else if (sechdrs[i].sh_type == SHT_REL) {
825 Elf_Rel *rel;
826 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
827 Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
828 name += strlen(".rel");
829 if (section_ref_ok(name))
830 continue;
831
832 for (rel = start; rel < stop; rel++) {
833 r.r_offset = TO_NATIVE(rel->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700834#if KERNEL_ELFCLASS == ELFCLASS64
835 if (hdr->e_machine == EM_MIPS) {
836 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
837 r_sym = TO_NATIVE(r_sym);
838 } else {
839 r.r_info = TO_NATIVE(rel->r_info);
840 r_sym = ELF_R_SYM(r.r_info);
841 }
842#else
843 r.r_info = TO_NATIVE(rel->r_info);
844 r_sym = ELF_R_SYM(r.r_info);
845#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700846 r.r_addend = 0;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700847 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700848 /* Skip special sections */
849 if (sym->st_shndx >= SHN_LORESERVE)
850 continue;
851
852 secname = secstrings +
853 sechdrs[sym->st_shndx].sh_name;
854 if (section(secname))
855 warn_sec_mismatch(modname, name,
856 elf, sym, r);
857 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100858 }
859 }
860}
861
862/**
863 * Functions used only during module init is marked __init and is stored in
864 * a .init.text section. Likewise data is marked __initdata and stored in
865 * a .init.data section.
866 * If this section is one of these sections return 1
867 * See include/linux/init.h for the details
868 **/
869static int init_section(const char *name)
870{
871 if (strcmp(name, ".init") == 0)
872 return 1;
873 if (strncmp(name, ".init.", strlen(".init.")) == 0)
874 return 1;
875 return 0;
876}
877
878/**
879 * Identify sections from which references to a .init section is OK.
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100880 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100881 * Unfortunately references to read only data that referenced .init
882 * sections had to be excluded. Almost all of these are false
883 * positives, they are created by gcc. The downside of excluding rodata
884 * is that there really are some user references from rodata to
885 * init code, e.g. drivers/video/vgacon.c:
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100886 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100887 * const struct consw vga_con = {
888 * con_startup: vgacon_startup,
889 *
890 * where vgacon_startup is __init. If you want to wade through the false
891 * positives, take out the check for rodata.
892 **/
893static int init_section_ref_ok(const char *name)
894{
895 const char **s;
896 /* Absolute section names */
897 const char *namelist1[] = {
898 ".init",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100899 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
900 ".toc1", /* used by ppc64 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100901 ".stab",
902 ".rodata",
903 ".text.lock",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100904 "__bug_table", /* used by powerpc for BUG() */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100905 ".pci_fixup_header",
906 ".pci_fixup_final",
907 ".pdr",
908 "__param",
Al Viro468d9492006-06-23 23:22:43 +0100909 "__ex_table",
910 ".fixup",
Randy Dunlap35899c52006-06-07 16:23:26 -0700911 ".smp_locks",
Sam Ravnborg909252d2006-06-08 20:37:30 +0200912 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100913 NULL
914 };
915 /* Start of section names */
916 const char *namelist2[] = {
917 ".init.",
918 ".altinstructions",
919 ".eh_frame",
920 ".debug",
921 NULL
922 };
Sam Ravnborg6e101332006-02-22 21:24:50 +0100923 /* part of section name */
924 const char *namelist3 [] = {
925 ".unwind", /* sample: IA_64.unwind.init.text */
926 NULL
927 };
928
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100929 for (s = namelist1; *s; s++)
930 if (strcmp(*s, name) == 0)
931 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100932 for (s = namelist2; *s; s++)
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100933 if (strncmp(*s, name, strlen(*s)) == 0)
934 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100935 for (s = namelist3; *s; s++)
Sam Ravnborge835a392006-03-05 11:34:15 +0100936 if (strstr(name, *s) != NULL)
Sam Ravnborg6e101332006-02-22 21:24:50 +0100937 return 1;
Al Viro468d9492006-06-23 23:22:43 +0100938 if (strrcmp(name, ".init") == 0)
939 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100940 return 0;
941}
942
943/*
944 * Functions used only during module exit is marked __exit and is stored in
945 * a .exit.text section. Likewise data is marked __exitdata and stored in
946 * a .exit.data section.
947 * If this section is one of these sections return 1
948 * See include/linux/init.h for the details
949 **/
950static int exit_section(const char *name)
951{
952 if (strcmp(name, ".exit.text") == 0)
953 return 1;
954 if (strcmp(name, ".exit.data") == 0)
955 return 1;
956 return 0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100957
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100958}
959
960/*
961 * Identify sections from which references to a .exit section is OK.
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100962 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100963 * [OPD] Keith Ownes <kaos@sgi.com> commented:
964 * For our future {in}sanity, add a comment that this is the ppc .opd
965 * section, not the ia64 .opd section.
966 * ia64 .opd should not point to discarded sections.
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200967 * [.rodata] like for .init.text we ignore .rodata references -same reason
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100968 **/
969static int exit_section_ref_ok(const char *name)
970{
971 const char **s;
972 /* Absolute section names */
973 const char *namelist1[] = {
974 ".exit.text",
975 ".exit.data",
976 ".init.text",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200977 ".rodata",
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100978 ".opd", /* See comment [OPD] */
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100979 ".toc1", /* used by ppc64 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100980 ".altinstructions",
981 ".pdr",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100982 "__bug_table", /* used by powerpc for BUG() */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100983 ".exitcall.exit",
984 ".eh_frame",
985 ".stab",
Al Viro468d9492006-06-23 23:22:43 +0100986 "__ex_table",
987 ".fixup",
Randy Dunlap35899c52006-06-07 16:23:26 -0700988 ".smp_locks",
Sam Ravnborg909252d2006-06-08 20:37:30 +0200989 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100990 NULL
991 };
992 /* Start of section names */
993 const char *namelist2[] = {
994 ".debug",
995 NULL
996 };
Sam Ravnborg6e101332006-02-22 21:24:50 +0100997 /* part of section name */
998 const char *namelist3 [] = {
999 ".unwind", /* Sample: IA_64.unwind.exit.text */
1000 NULL
1001 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001002
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001003 for (s = namelist1; *s; s++)
1004 if (strcmp(*s, name) == 0)
1005 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001006 for (s = namelist2; *s; s++)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001007 if (strncmp(*s, name, strlen(*s)) == 0)
1008 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001009 for (s = namelist3; *s; s++)
Sam Ravnborge835a392006-03-05 11:34:15 +01001010 if (strstr(name, *s) != NULL)
Sam Ravnborg6e101332006-02-22 21:24:50 +01001011 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001012 return 0;
1013}
1014
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001015static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 const char *symname;
1018 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001019 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct module *mod;
1021 struct elf_info info = { };
1022 Elf_Sym *sym;
1023
1024 parse_elf(&info, modname);
1025
1026 mod = new_module(modname);
1027
1028 /* When there's no vmlinux, don't print warnings about
1029 * unresolved symbols (since there'll be too many ;) */
1030 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 mod->skip = 1;
1033 }
1034
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001035 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1036 while (license) {
1037 if (license_is_gpl_compatible(license))
1038 mod->gpl_compatible = 1;
1039 else {
1040 mod->gpl_compatible = 0;
1041 break;
1042 }
1043 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1044 "license", license);
1045 }
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1048 symname = info.strtab + sym->st_name;
1049
1050 handle_modversions(mod, &info, sym, symname);
1051 handle_moddevtable(mod, &info, sym, symname);
1052 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001053 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1054 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1057 if (version)
1058 maybe_frob_rcs_version(modname, version, info.modinfo,
1059 version - (char *)info.hdr);
1060 if (version || (all_versions && !is_vmlinux(modname)))
1061 get_src_version(modname, mod->srcversion,
1062 sizeof(mod->srcversion)-1);
1063
1064 parse_elf_finish(&info);
1065
1066 /* Our trick to get versioning for struct_module - it's
1067 * never passed as an argument to an exported function, so
1068 * the automatic versioning doesn't pick it up, but it's really
1069 * important anyhow */
1070 if (modversions)
1071 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1072}
1073
1074#define SZ 500
1075
1076/* We first write the generated file into memory using the
1077 * following helper, then compare to the file on disk and
1078 * only update the later if anything changed */
1079
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001080void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1081 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 char tmp[SZ];
1084 int len;
1085 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 va_start(ap, fmt);
1088 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001089 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 va_end(ap);
1091}
1092
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001093void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001096 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 buf->p = realloc(buf->p, buf->size);
1098 }
1099 strncpy(buf->p + buf->pos, s, len);
1100 buf->pos += len;
1101}
1102
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001103static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1104{
1105 const char *e = is_vmlinux(m) ?"":".ko";
1106
1107 switch (exp) {
1108 case export_gpl:
1109 fatal("modpost: GPL-incompatible module %s%s "
1110 "uses GPL-only symbol '%s'\n", m, e, s);
1111 break;
1112 case export_unused_gpl:
1113 fatal("modpost: GPL-incompatible module %s%s "
1114 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1115 break;
1116 case export_gpl_future:
1117 warn("modpost: GPL-incompatible module %s%s "
1118 "uses future GPL-only symbol '%s'\n", m, e, s);
1119 break;
1120 case export_plain:
1121 case export_unused:
1122 case export_unknown:
1123 /* ignore */
1124 break;
1125 }
1126}
1127
1128static void check_for_unused(enum export exp, const char* m, const char* s)
1129{
1130 const char *e = is_vmlinux(m) ?"":".ko";
1131
1132 switch (exp) {
1133 case export_unused:
1134 case export_unused_gpl:
1135 warn("modpost: module %s%s "
1136 "uses symbol '%s' marked UNUSED\n", m, e, s);
1137 break;
1138 default:
1139 /* ignore */
1140 break;
1141 }
1142}
1143
1144static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001145{
1146 struct symbol *s, *exp;
1147
1148 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001149 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001150 exp = find_symbol(s->name);
1151 if (!exp || exp->module == mod)
1152 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001153 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001154 if (basename)
1155 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001156 else
1157 basename = mod->name;
1158 if (!mod->gpl_compatible)
1159 check_for_gpl_usage(exp->export, basename, exp->name);
1160 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001161 }
1162}
1163
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001164/**
1165 * Header for the generated file
1166 **/
1167static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 buf_printf(b, "#include <linux/module.h>\n");
1170 buf_printf(b, "#include <linux/vermagic.h>\n");
1171 buf_printf(b, "#include <linux/compiler.h>\n");
1172 buf_printf(b, "\n");
1173 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1174 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 buf_printf(b, "struct module __this_module\n");
1176 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001177 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (mod->has_init)
1179 buf_printf(b, " .init = init_module,\n");
1180 if (mod->has_cleanup)
1181 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1182 " .exit = cleanup_module,\n"
1183 "#endif\n");
1184 buf_printf(b, "};\n");
1185}
1186
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001187/**
1188 * Record CRCs for unresolved symbols
1189 **/
1190static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 struct symbol *s, *exp;
1193
1194 for (s = mod->unres; s; s = s->next) {
1195 exp = find_symbol(s->name);
1196 if (!exp || exp->module == mod) {
1197 if (have_vmlinux && !s->weak)
Sam Ravnborgcb805142006-01-28 16:57:26 +01001198 warn("\"%s\" [%s.ko] undefined!\n",
1199 s->name, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 continue;
1201 }
1202 s->module = exp->module;
1203 s->crc_valid = exp->crc_valid;
1204 s->crc = exp->crc;
1205 }
1206
1207 if (!modversions)
1208 return;
1209
1210 buf_printf(b, "\n");
1211 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1212 buf_printf(b, "__attribute_used__\n");
1213 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1214
1215 for (s = mod->unres; s; s = s->next) {
1216 if (!s->module) {
1217 continue;
1218 }
1219 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001220 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 s->name, mod->name);
1222 continue;
1223 }
1224 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1225 }
1226
1227 buf_printf(b, "};\n");
1228}
1229
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001230static void add_depends(struct buffer *b, struct module *mod,
1231 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct symbol *s;
1234 struct module *m;
1235 int first = 1;
1236
1237 for (m = modules; m; m = m->next) {
1238 m->seen = is_vmlinux(m->name);
1239 }
1240
1241 buf_printf(b, "\n");
1242 buf_printf(b, "static const char __module_depends[]\n");
1243 buf_printf(b, "__attribute_used__\n");
1244 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1245 buf_printf(b, "\"depends=");
1246 for (s = mod->unres; s; s = s->next) {
1247 if (!s->module)
1248 continue;
1249
1250 if (s->module->seen)
1251 continue;
1252
1253 s->module->seen = 1;
1254 buf_printf(b, "%s%s", first ? "" : ",",
1255 strrchr(s->module->name, '/') + 1);
1256 first = 0;
1257 }
1258 buf_printf(b, "\";\n");
1259}
1260
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001261static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 if (mod->srcversion[0]) {
1264 buf_printf(b, "\n");
1265 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1266 mod->srcversion);
1267 }
1268}
1269
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001270static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 char *tmp;
1273 FILE *file;
1274 struct stat st;
1275
1276 file = fopen(fname, "r");
1277 if (!file)
1278 goto write;
1279
1280 if (fstat(fileno(file), &st) < 0)
1281 goto close_write;
1282
1283 if (st.st_size != b->pos)
1284 goto close_write;
1285
1286 tmp = NOFAIL(malloc(b->pos));
1287 if (fread(tmp, 1, b->pos, file) != b->pos)
1288 goto free_write;
1289
1290 if (memcmp(tmp, b->p, b->pos) != 0)
1291 goto free_write;
1292
1293 free(tmp);
1294 fclose(file);
1295 return;
1296
1297 free_write:
1298 free(tmp);
1299 close_write:
1300 fclose(file);
1301 write:
1302 file = fopen(fname, "w");
1303 if (!file) {
1304 perror(fname);
1305 exit(1);
1306 }
1307 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1308 perror(fname);
1309 exit(1);
1310 }
1311 fclose(file);
1312}
1313
Ram Paibd5cbce2006-06-08 22:12:53 -07001314/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001315 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001316 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001317static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 unsigned long size, pos = 0;
1320 void *file = grab_file(fname, &size);
1321 char *line;
1322
1323 if (!file)
1324 /* No symbol versions, silently ignore */
1325 return;
1326
1327 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001328 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 unsigned int crc;
1330 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001331 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (!(symname = strchr(line, '\t')))
1334 goto fail;
1335 *symname++ = '\0';
1336 if (!(modname = strchr(symname, '\t')))
1337 goto fail;
1338 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001339 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001340 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001341 if (export && ((end = strchr(export, '\t')) != NULL))
1342 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 crc = strtoul(line, &d, 16);
1344 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1345 goto fail;
1346
1347 if (!(mod = find_module(modname))) {
1348 if (is_vmlinux(modname)) {
1349 have_vmlinux = 1;
1350 }
1351 mod = new_module(NOFAIL(strdup(modname)));
1352 mod->skip = 1;
1353 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001354 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001355 s->kernel = kernel;
1356 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001357 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 return;
1360fail:
1361 fatal("parse error in symbol dump file\n");
1362}
1363
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001364/* For normal builds always dump all symbols.
1365 * For external modules only dump symbols
1366 * that are not read from kernel Module.symvers.
1367 **/
1368static int dump_sym(struct symbol *sym)
1369{
1370 if (!external_module)
1371 return 1;
1372 if (sym->vmlinux || sym->kernel)
1373 return 0;
1374 return 1;
1375}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001376
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001377static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct buffer buf = { };
1380 struct symbol *symbol;
1381 int n;
1382
1383 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1384 symbol = symbolhash[n];
1385 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001386 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001387 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001388 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001389 symbol->module->name,
1390 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 symbol = symbol->next;
1392 }
1393 }
1394 write_if_changed(&buf, fname);
1395}
1396
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001397int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 struct module *mod;
1400 struct buffer buf = { };
1401 char fname[SZ];
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001402 char *kernel_read = NULL, *module_read = NULL;
1403 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int opt;
1405
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001406 while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 switch(opt) {
1408 case 'i':
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001409 kernel_read = optarg;
1410 break;
1411 case 'I':
1412 module_read = optarg;
1413 external_module = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 break;
1415 case 'm':
1416 modversions = 1;
1417 break;
1418 case 'o':
1419 dump_write = optarg;
1420 break;
1421 case 'a':
1422 all_versions = 1;
1423 break;
1424 default:
1425 exit(1);
1426 }
1427 }
1428
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001429 if (kernel_read)
1430 read_dump(kernel_read, 1);
1431 if (module_read)
1432 read_dump(module_read, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 while (optind < argc) {
1435 read_symbols(argv[optind++]);
1436 }
1437
1438 for (mod = modules; mod; mod = mod->next) {
1439 if (mod->skip)
1440 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001441 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001442 }
1443
1444 for (mod = modules; mod; mod = mod->next) {
1445 if (mod->skip)
1446 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 buf.pos = 0;
1449
1450 add_header(&buf, mod);
1451 add_versions(&buf, mod);
1452 add_depends(&buf, mod, modules);
1453 add_moddevtable(&buf, mod);
1454 add_srcversion(&buf, mod);
1455
1456 sprintf(fname, "%s.mod.c", mod->name);
1457 write_if_changed(&buf, fname);
1458 }
1459
1460 if (dump_write)
1461 write_dump(dump_write);
1462
1463 return 0;
1464}