blob: 82e019bf2dc587c6b5444b519d9632f463a26839 [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;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070026/* Only warn about unresolved symbols */
27static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070028/* How a symbol is exported */
Sam Ravnborgc96fca22006-07-01 11:44:23 +020029enum export {
30 export_plain, export_unused, export_gpl,
31 export_unused_gpl, export_gpl_future, export_unknown
32};
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010034void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 va_list arglist;
37
38 fprintf(stderr, "FATAL: ");
39
40 va_start(arglist, fmt);
41 vfprintf(stderr, fmt, arglist);
42 va_end(arglist);
43
44 exit(1);
45}
46
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010047void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 va_list arglist;
50
51 fprintf(stderr, "WARNING: ");
52
53 va_start(arglist, fmt);
54 vfprintf(stderr, fmt, arglist);
55 va_end(arglist);
56}
57
Sam Ravnborg040fcc82006-01-28 22:15:55 +010058static int is_vmlinux(const char *modname)
59{
60 const char *myname;
61
62 if ((myname = strrchr(modname, '/')))
63 myname++;
64 else
65 myname = modname;
66
67 return strcmp(myname, "vmlinux") == 0;
68}
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070void *do_nofail(void *ptr, const char *expr)
71{
72 if (!ptr) {
73 fatal("modpost: Memory allocation failure: %s.\n", expr);
74 }
75 return ptr;
76}
77
78/* A list of all modules we processed */
79
80static struct module *modules;
81
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010082static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct module *mod;
85
86 for (mod = modules; mod; mod = mod->next)
87 if (strcmp(mod->name, modname) == 0)
88 break;
89 return mod;
90}
91
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010092static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct module *mod;
95 char *p, *s;
Sam Ravnborg62070fa2006-03-03 16:46:04 +010096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 mod = NOFAIL(malloc(sizeof(*mod)));
98 memset(mod, 0, sizeof(*mod));
99 p = NOFAIL(strdup(modname));
100
101 /* strip trailing .o */
102 if ((s = strrchr(p, '.')) != NULL)
103 if (strcmp(s, ".o") == 0)
104 *s = '\0';
105
106 /* add to list */
107 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200108 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 mod->next = modules;
110 modules = mod;
111
112 return mod;
113}
114
115/* A hash of all exported symbols,
116 * struct symbol is also used for lists of unresolved symbols */
117
118#define SYMBOL_HASH_SIZE 1024
119
120struct symbol {
121 struct symbol *next;
122 struct module *module;
123 unsigned int crc;
124 int crc_valid;
125 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100126 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
127 unsigned int kernel:1; /* 1 if symbol is from kernel
128 * (only for external modules) **/
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100129 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Ram Paibd5cbce2006-06-08 22:12:53 -0700130 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 char name[0];
132};
133
134static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
135
136/* This is based on the hash agorithm from gdbm, via tdb */
137static inline unsigned int tdb_hash(const char *name)
138{
139 unsigned value; /* Used to compute the hash value. */
140 unsigned i; /* Used to cycle through random values. */
141
142 /* Set the initial value from the key size. */
143 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
144 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
145
146 return (1103515243 * value + 12345);
147}
148
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100149/**
150 * Allocate a new symbols for use in the hash of exported symbols or
151 * the list of unresolved symbols per module
152 **/
153static struct symbol *alloc_symbol(const char *name, unsigned int weak,
154 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
157
158 memset(s, 0, sizeof(*s));
159 strcpy(s->name, name);
160 s->weak = weak;
161 s->next = next;
162 return s;
163}
164
165/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700166static struct symbol *new_symbol(const char *name, struct module *module,
167 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 unsigned int hash;
170 struct symbol *new;
171
172 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
173 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
174 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700175 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100176 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100179static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 struct symbol *s;
182
183 /* For our purposes, .foo matches foo. PPC64 needs this. */
184 if (name[0] == '.')
185 name++;
186
187 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) {
188 if (strcmp(s->name, name) == 0)
189 return s;
190 }
191 return NULL;
192}
193
Ram Paibd5cbce2006-06-08 22:12:53 -0700194static struct {
195 const char *str;
196 enum export export;
197} export_list[] = {
198 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200199 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700200 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200201 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700202 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
203 { .str = "(unknown)", .export = export_unknown },
204};
205
206
207static const char *export_str(enum export ex)
208{
209 return export_list[ex].str;
210}
211
212static enum export export_no(const char * s)
213{
214 int i;
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200215 if (!s)
216 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700217 for (i = 0; export_list[i].export != export_unknown; i++) {
218 if (strcmp(export_list[i].str, s) == 0)
219 return export_list[i].export;
220 }
221 return export_unknown;
222}
223
224static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
225{
226 if (sec == elf->export_sec)
227 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200228 else if (sec == elf->export_unused_sec)
229 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700230 else if (sec == elf->export_gpl_sec)
231 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200232 else if (sec == elf->export_unused_gpl_sec)
233 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700234 else if (sec == elf->export_gpl_future_sec)
235 return export_gpl_future;
236 else
237 return export_unknown;
238}
239
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100240/**
241 * Add an exported symbol - it may have already been added without a
242 * CRC, in this case just update the CRC
243 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700244static struct symbol *sym_add_exported(const char *name, struct module *mod,
245 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct symbol *s = find_symbol(name);
248
249 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700250 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100251 } else {
252 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100253 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100254 "was in %s%s\n", mod->name, name,
255 s->module->name,
256 is_vmlinux(s->module->name) ?"":".ko");
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100259 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100260 s->vmlinux = is_vmlinux(mod->name);
261 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700262 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100263 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100266static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700267 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100268{
269 struct symbol *s = find_symbol(name);
270
271 if (!s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700272 s = new_symbol(name, mod, export);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100273 s->crc = crc;
274 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100277void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct stat st;
280 void *map;
281 int fd;
282
283 fd = open(filename, O_RDONLY);
284 if (fd < 0 || fstat(fd, &st) != 0)
285 return NULL;
286
287 *size = st.st_size;
288 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
289 close(fd);
290
291 if (map == MAP_FAILED)
292 return NULL;
293 return map;
294}
295
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100296/**
297 * Return a copy of the next line in a mmap'ed file.
298 * spaces in the beginning of the line is trimmed away.
299 * Return a pointer to a static buffer.
300 **/
301char* get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 static char line[4096];
304 int skip = 1;
305 size_t len = 0;
306 signed char *p = (signed char *)file + *pos;
307 char *s = line;
308
309 for (; *pos < size ; (*pos)++)
310 {
311 if (skip && isspace(*p)) {
312 p++;
313 continue;
314 }
315 skip = 0;
316 if (*p != '\n' && (*pos < size)) {
317 len++;
318 *s++ = *p++;
319 if (len > 4095)
320 break; /* Too long, stop */
321 } else {
322 /* End of string */
323 *s = '\0';
324 return line;
325 }
326 }
327 /* End of buffer */
328 return NULL;
329}
330
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100331void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 munmap(file, size);
334}
335
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100336static void parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 unsigned int i;
339 Elf_Ehdr *hdr = info->hdr;
340 Elf_Shdr *sechdrs;
341 Elf_Sym *sym;
342
343 hdr = grab_file(filename, &info->size);
344 if (!hdr) {
345 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200346 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 info->hdr = hdr;
349 if (info->size < sizeof(*hdr))
350 goto truncated;
351
352 /* Fix endianness in ELF header */
353 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
354 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
355 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
356 hdr->e_machine = TO_NATIVE(hdr->e_machine);
357 sechdrs = (void *)hdr + hdr->e_shoff;
358 info->sechdrs = sechdrs;
359
360 /* Fix endianness in section headers */
361 for (i = 0; i < hdr->e_shnum; i++) {
362 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
363 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
364 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
365 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
366 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
367 }
368 /* Find symbol table. */
369 for (i = 1; i < hdr->e_shnum; i++) {
370 const char *secstrings
371 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
Ram Paibd5cbce2006-06-08 22:12:53 -0700372 const char *secname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (sechdrs[i].sh_offset > info->size)
375 goto truncated;
Ram Paibd5cbce2006-06-08 22:12:53 -0700376 secname = secstrings + sechdrs[i].sh_name;
377 if (strcmp(secname, ".modinfo") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
379 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700380 } else if (strcmp(secname, "__ksymtab") == 0)
381 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200382 else if (strcmp(secname, "__ksymtab_unused") == 0)
383 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700384 else if (strcmp(secname, "__ksymtab_gpl") == 0)
385 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200386 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
387 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700388 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
389 info->export_gpl_future_sec = i;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (sechdrs[i].sh_type != SHT_SYMTAB)
392 continue;
393
394 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100395 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100397 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 sechdrs[sechdrs[i].sh_link].sh_offset;
399 }
400 if (!info->symtab_start) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100401 fatal("%s has no symtab?\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 /* Fix endianness in symbols */
404 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
405 sym->st_shndx = TO_NATIVE(sym->st_shndx);
406 sym->st_name = TO_NATIVE(sym->st_name);
407 sym->st_value = TO_NATIVE(sym->st_value);
408 sym->st_size = TO_NATIVE(sym->st_size);
409 }
410 return;
411
412 truncated:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100413 fatal("%s is truncated.\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100416static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 release_file(info->hdr, info->size);
419}
420
Luke Yangf7b05e62006-03-02 18:35:31 +0800421#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
422#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100424static void handle_modversions(struct module *mod, struct elf_info *info,
425 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700428 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 switch (sym->st_shndx) {
431 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100432 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
434 case SHN_ABS:
435 /* CRC'd symbol */
436 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
437 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700438 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
439 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 break;
442 case SHN_UNDEF:
443 /* undefined symbol */
444 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
445 ELF_ST_BIND(sym->st_info) != STB_WEAK)
446 break;
447 /* ignore global offset table */
448 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
449 break;
450 /* ignore __this_module, it will be resolved shortly */
451 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
452 break;
Ben Colline8d529012005-08-19 13:44:57 -0700453/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
454#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
455/* add compatibility with older glibc */
456#ifndef STT_SPARC_REGISTER
457#define STT_SPARC_REGISTER STT_REGISTER
458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (info->hdr->e_machine == EM_SPARC ||
460 info->hdr->e_machine == EM_SPARCV9) {
461 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700462 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100464 if (symname[0] == '.') {
465 char *munged = strdup(symname);
466 munged[0] = '_';
467 munged[1] = toupper(munged[1]);
468 symname = munged;
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
474 strlen(MODULE_SYMBOL_PREFIX)) == 0)
475 mod->unres = alloc_symbol(symname +
476 strlen(MODULE_SYMBOL_PREFIX),
477 ELF_ST_BIND(sym->st_info) == STB_WEAK,
478 mod->unres);
479 break;
480 default:
481 /* All exported symbols */
482 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700483 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
484 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
487 mod->has_init = 1;
488 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
489 mod->has_cleanup = 1;
490 break;
491 }
492}
493
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100494/**
495 * Parse tag=value strings from .modinfo section
496 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497static char *next_string(char *string, unsigned long *secsize)
498{
499 /* Skip non-zero chars */
500 while (string[0]) {
501 string++;
502 if ((*secsize)-- <= 1)
503 return NULL;
504 }
505
506 /* Skip any zero padding. */
507 while (!string[0]) {
508 string++;
509 if ((*secsize)-- <= 1)
510 return NULL;
511 }
512 return string;
513}
514
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200515static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
516 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 char *p;
519 unsigned int taglen = strlen(tag);
520 unsigned long size = modinfo_len;
521
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200522 if (info) {
523 size -= info - (char *)modinfo;
524 modinfo = next_string(info, &size);
525 }
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 for (p = modinfo; p; p = next_string(p, &size)) {
528 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
529 return p + taglen + 1;
530 }
531 return NULL;
532}
533
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200534static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
535 const char *tag)
536
537{
538 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
539}
540
Sam Ravnborg93684d32006-02-19 11:53:35 +0100541/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100542 * Test if string s ends in string sub
543 * return 0 if match
544 **/
545static int strrcmp(const char *s, const char *sub)
546{
547 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100548
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100549 if (!s || !sub)
550 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100551
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100552 slen = strlen(s);
553 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100554
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100555 if ((slen == 0) || (sublen == 0))
556 return 1;
557
558 if (sublen > slen)
559 return 1;
560
561 return memcmp(s + slen - sublen, sub, sublen);
562}
563
564/**
565 * Whitelist to allow certain references to pass with no warning.
566 * Pattern 1:
567 * If a module parameter is declared __initdata and permissions=0
568 * then this is legal despite the warning generated.
569 * We cannot see value of permissions here, so just ignore
570 * this pattern.
571 * The pattern is identified by:
572 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100573 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100574 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100575 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100576 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700577 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100578 * add, remove, probe functions etc.
579 * These functions may often be marked __init and we do not want to
580 * warn here.
581 * the pattern is identified by:
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200582 * tosec = .init.text | .exit.text | .init.data
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100583 * fromsec = .data
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700584 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100585 **/
Magnus Damm9e157a52006-08-08 17:32:11 +0900586static int secref_whitelist(const char *modname, const char *tosec,
587 const char *fromsec, const char *atsym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100588{
589 int f1 = 1, f2 = 1;
590 const char **s;
591 const char *pat2sym[] = {
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700592 "driver",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200593 "_template", /* scsi uses *_template a lot */
594 "_sht", /* scsi also used *_sht to some extent */
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100595 "_ops",
596 "_probe",
597 "_probe_one",
Vivek Goyal118c0ac2007-01-11 01:52:44 +0100598 "_console",
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100599 NULL
600 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100601
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100602 /* Check for pattern 1 */
603 if (strcmp(tosec, ".init.data") != 0)
604 f1 = 0;
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100605 if (strncmp(fromsec, ".data", strlen(".data")) != 0)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100606 f1 = 0;
607 if (strncmp(atsym, "__param", strlen("__param")) != 0)
608 f1 = 0;
609
610 if (f1)
611 return f1;
612
613 /* Check for pattern 2 */
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100614 if ((strcmp(tosec, ".init.text") != 0) &&
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200615 (strcmp(tosec, ".exit.text") != 0) &&
616 (strcmp(tosec, ".init.data") != 0))
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100617 f2 = 0;
618 if (strcmp(fromsec, ".data") != 0)
619 f2 = 0;
620
621 for (s = pat2sym; *s; s++)
622 if (strrcmp(atsym, *s) == 0)
623 f1 = 1;
Magnus Damm9e157a52006-08-08 17:32:11 +0900624 if (f1 && f2)
625 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100626
Magnus Damm9e157a52006-08-08 17:32:11 +0900627 /* Whitelist all references from .pci_fixup section if vmlinux */
628 if (is_vmlinux(modname)) {
629 if ((strcmp(fromsec, ".pci_fixup") == 0) &&
630 (strcmp(tosec, ".init.text") == 0))
631 return 1;
632 }
Sam Ravnborg93659af2006-08-09 08:23:55 +0200633 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100634}
635
636/**
Sam Ravnborg93684d32006-02-19 11:53:35 +0100637 * Find symbol based on relocation record info.
638 * In some cases the symbol supplied is a valid symbol so
639 * return refsym. If st_name != 0 we assume this is a valid symbol.
640 * In other cases the symbol needs to be looked up in the symbol table
641 * based on section and address.
642 * **/
643static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
644 Elf_Sym *relsym)
645{
646 Elf_Sym *sym;
647
648 if (relsym->st_name != 0)
649 return relsym;
650 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
651 if (sym->st_shndx != relsym->st_shndx)
652 continue;
653 if (sym->st_value == addr)
654 return sym;
655 }
656 return NULL;
657}
658
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100659/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100660 * Find symbols before or equal addr and after addr - in the section sec.
661 * If we find two symbols with equal offset prefer one with a valid name.
662 * The ELF format may have a better way to detect what type of symbol
663 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100664 **/
665static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
666 const char *sec,
667 Elf_Sym **before, Elf_Sym **after)
668{
669 Elf_Sym *sym;
670 Elf_Ehdr *hdr = elf->hdr;
671 Elf_Addr beforediff = ~0;
672 Elf_Addr afterdiff = ~0;
673 const char *secstrings = (void *)hdr +
674 elf->sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100675
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100676 *before = NULL;
677 *after = NULL;
678
679 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
680 const char *symsec;
681
682 if (sym->st_shndx >= SHN_LORESERVE)
683 continue;
684 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
685 if (strcmp(symsec, sec) != 0)
686 continue;
687 if (sym->st_value <= addr) {
688 if ((addr - sym->st_value) < beforediff) {
689 beforediff = addr - sym->st_value;
690 *before = sym;
691 }
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100692 else if ((addr - sym->st_value) == beforediff) {
693 /* equal offset, valid name? */
694 const char *name = elf->strtab + sym->st_name;
695 if (name && strlen(name))
696 *before = sym;
697 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100698 }
699 else
700 {
701 if ((sym->st_value - addr) < afterdiff) {
702 afterdiff = sym->st_value - addr;
703 *after = sym;
704 }
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100705 else if ((sym->st_value - addr) == afterdiff) {
706 /* equal offset, valid name? */
707 const char *name = elf->strtab + sym->st_name;
708 if (name && strlen(name))
709 *after = sym;
710 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100711 }
712 }
713}
714
715/**
716 * Print a warning about a section mismatch.
717 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100718 * Check whitelist before warning - it may be a false positive.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100719 **/
720static void warn_sec_mismatch(const char *modname, const char *fromsec,
721 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
722{
Sam Ravnborg93684d32006-02-19 11:53:35 +0100723 const char *refsymname = "";
724 Elf_Sym *before, *after;
725 Elf_Sym *refsym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100726 Elf_Ehdr *hdr = elf->hdr;
727 Elf_Shdr *sechdrs = elf->sechdrs;
728 const char *secstrings = (void *)hdr +
729 sechdrs[hdr->e_shstrndx].sh_offset;
730 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100731
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100732 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
733
Sam Ravnborg93684d32006-02-19 11:53:35 +0100734 refsym = find_elf_symbol(elf, r.r_addend, sym);
735 if (refsym && strlen(elf->strtab + refsym->st_name))
736 refsymname = elf->strtab + refsym->st_name;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100737
738 /* check whitelist - we may ignore it */
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100739 if (before &&
Magnus Damm9e157a52006-08-08 17:32:11 +0900740 secref_whitelist(modname, secname, fromsec,
741 elf->strtab + before->st_name))
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100742 return;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100743
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100744 if (before && after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100745 warn("%s - Section mismatch: reference to %s:%s from %s "
746 "between '%s' (at offset 0x%llx) and '%s'\n",
747 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100748 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100749 (long long)r.r_offset,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100750 elf->strtab + after->st_name);
751 } else if (before) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100752 warn("%s - Section mismatch: reference to %s:%s from %s "
753 "after '%s' (at offset 0x%llx)\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100754 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100755 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100756 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100757 } else if (after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100758 warn("%s - Section mismatch: reference to %s:%s from %s "
759 "before '%s' (at offset -0x%llx)\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100760 modname, secname, refsymname, fromsec,
Eric Sesterhenneaaae382006-04-11 00:54:16 +0200761 elf->strtab + after->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100762 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100763 } else {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100764 warn("%s - Section mismatch: reference to %s:%s from %s "
765 "(offset 0x%llx)\n",
766 modname, secname, fromsec, refsymname,
767 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100768 }
769}
770
771/**
772 * A module includes a number of sections that are discarded
773 * either when loaded or when used as built-in.
774 * For loaded modules all functions marked __init and all data
775 * marked __initdata will be discarded when the module has been intialized.
776 * Likewise for modules used built-in the sections marked __exit
777 * are discarded because __exit marked function are supposed to be called
778 * only when a moduel is unloaded which never happes for built-in modules.
779 * The check_sec_ref() function traverses all relocation records
780 * to find all references to a section that reference a section that will
781 * be discarded and warns about it.
782 **/
783static void check_sec_ref(struct module *mod, const char *modname,
784 struct elf_info *elf,
785 int section(const char*),
786 int section_ref_ok(const char *))
787{
788 int i;
789 Elf_Sym *sym;
790 Elf_Ehdr *hdr = elf->hdr;
791 Elf_Shdr *sechdrs = elf->sechdrs;
792 const char *secstrings = (void *)hdr +
793 sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100794
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100795 /* Walk through all sections */
796 for (i = 0; i < hdr->e_shnum; i++) {
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700797 const char *name = secstrings + sechdrs[i].sh_name;
798 const char *secname;
799 Elf_Rela r;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700800 unsigned int r_sym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100801 /* We want to process only relocation sections and not .init */
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700802 if (sechdrs[i].sh_type == SHT_RELA) {
803 Elf_Rela *rela;
804 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
805 Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
806 name += strlen(".rela");
807 if (section_ref_ok(name))
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100808 continue;
809
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700810 for (rela = start; rela < stop; rela++) {
811 r.r_offset = TO_NATIVE(rela->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700812#if KERNEL_ELFCLASS == ELFCLASS64
813 if (hdr->e_machine == EM_MIPS) {
814 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
815 r_sym = TO_NATIVE(r_sym);
816 } else {
817 r.r_info = TO_NATIVE(rela->r_info);
818 r_sym = ELF_R_SYM(r.r_info);
819 }
820#else
821 r.r_info = TO_NATIVE(rela->r_info);
822 r_sym = ELF_R_SYM(r.r_info);
823#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700824 r.r_addend = TO_NATIVE(rela->r_addend);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700825 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700826 /* Skip special sections */
827 if (sym->st_shndx >= SHN_LORESERVE)
828 continue;
829
830 secname = secstrings +
831 sechdrs[sym->st_shndx].sh_name;
832 if (section(secname))
833 warn_sec_mismatch(modname, name,
834 elf, sym, r);
835 }
836 } else if (sechdrs[i].sh_type == SHT_REL) {
837 Elf_Rel *rel;
838 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
839 Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
840 name += strlen(".rel");
841 if (section_ref_ok(name))
842 continue;
843
844 for (rel = start; rel < stop; rel++) {
845 r.r_offset = TO_NATIVE(rel->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700846#if KERNEL_ELFCLASS == ELFCLASS64
847 if (hdr->e_machine == EM_MIPS) {
848 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
849 r_sym = TO_NATIVE(r_sym);
850 } else {
851 r.r_info = TO_NATIVE(rel->r_info);
852 r_sym = ELF_R_SYM(r.r_info);
853 }
854#else
855 r.r_info = TO_NATIVE(rel->r_info);
856 r_sym = ELF_R_SYM(r.r_info);
857#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700858 r.r_addend = 0;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700859 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700860 /* Skip special sections */
861 if (sym->st_shndx >= SHN_LORESERVE)
862 continue;
863
864 secname = secstrings +
865 sechdrs[sym->st_shndx].sh_name;
866 if (section(secname))
867 warn_sec_mismatch(modname, name,
868 elf, sym, r);
869 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100870 }
871 }
872}
873
874/**
875 * Functions used only during module init is marked __init and is stored in
876 * a .init.text section. Likewise data is marked __initdata and stored in
877 * a .init.data section.
878 * If this section is one of these sections return 1
879 * See include/linux/init.h for the details
880 **/
881static int init_section(const char *name)
882{
883 if (strcmp(name, ".init") == 0)
884 return 1;
885 if (strncmp(name, ".init.", strlen(".init.")) == 0)
886 return 1;
887 return 0;
888}
889
890/**
891 * Identify sections from which references to a .init section is OK.
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100892 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100893 * Unfortunately references to read only data that referenced .init
894 * sections had to be excluded. Almost all of these are false
895 * positives, they are created by gcc. The downside of excluding rodata
896 * is that there really are some user references from rodata to
897 * init code, e.g. drivers/video/vgacon.c:
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100898 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100899 * const struct consw vga_con = {
900 * con_startup: vgacon_startup,
901 *
902 * where vgacon_startup is __init. If you want to wade through the false
903 * positives, take out the check for rodata.
904 **/
905static int init_section_ref_ok(const char *name)
906{
907 const char **s;
908 /* Absolute section names */
909 const char *namelist1[] = {
910 ".init",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100911 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
912 ".toc1", /* used by ppc64 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100913 ".stab",
914 ".rodata",
Rusty Russell139ec7c2006-12-07 02:14:08 +0100915 ".parainstructions",
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100916 ".text.lock",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100917 "__bug_table", /* used by powerpc for BUG() */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100918 ".pci_fixup_header",
919 ".pci_fixup_final",
920 ".pdr",
921 "__param",
Al Viro468d9492006-06-23 23:22:43 +0100922 "__ex_table",
923 ".fixup",
Randy Dunlap35899c52006-06-07 16:23:26 -0700924 ".smp_locks",
Sam Ravnborg909252d2006-06-08 20:37:30 +0200925 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
Benjamin Herrenschmidt21c4ff82006-10-20 11:47:19 +1000926 "__ftr_fixup", /* powerpc cpu feature fixup */
927 "__fw_ftr_fixup", /* powerpc firmware feature fixup */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100928 NULL
929 };
930 /* Start of section names */
931 const char *namelist2[] = {
932 ".init.",
933 ".altinstructions",
934 ".eh_frame",
935 ".debug",
Rusty Russell139ec7c2006-12-07 02:14:08 +0100936 ".parainstructions",
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100937 NULL
938 };
Sam Ravnborg6e101332006-02-22 21:24:50 +0100939 /* part of section name */
940 const char *namelist3 [] = {
941 ".unwind", /* sample: IA_64.unwind.init.text */
942 NULL
943 };
944
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100945 for (s = namelist1; *s; s++)
946 if (strcmp(*s, name) == 0)
947 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100948 for (s = namelist2; *s; s++)
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100949 if (strncmp(*s, name, strlen(*s)) == 0)
950 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100951 for (s = namelist3; *s; s++)
Sam Ravnborge835a392006-03-05 11:34:15 +0100952 if (strstr(name, *s) != NULL)
Sam Ravnborg6e101332006-02-22 21:24:50 +0100953 return 1;
Al Viro468d9492006-06-23 23:22:43 +0100954 if (strrcmp(name, ".init") == 0)
955 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100956 return 0;
957}
958
959/*
960 * Functions used only during module exit is marked __exit and is stored in
961 * a .exit.text section. Likewise data is marked __exitdata and stored in
962 * a .exit.data section.
963 * If this section is one of these sections return 1
964 * See include/linux/init.h for the details
965 **/
966static int exit_section(const char *name)
967{
968 if (strcmp(name, ".exit.text") == 0)
969 return 1;
970 if (strcmp(name, ".exit.data") == 0)
971 return 1;
972 return 0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100973
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100974}
975
976/*
977 * Identify sections from which references to a .exit section is OK.
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100978 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100979 * [OPD] Keith Ownes <kaos@sgi.com> commented:
980 * For our future {in}sanity, add a comment that this is the ppc .opd
981 * section, not the ia64 .opd section.
982 * ia64 .opd should not point to discarded sections.
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200983 * [.rodata] like for .init.text we ignore .rodata references -same reason
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100984 **/
985static int exit_section_ref_ok(const char *name)
986{
987 const char **s;
988 /* Absolute section names */
989 const char *namelist1[] = {
990 ".exit.text",
991 ".exit.data",
992 ".init.text",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200993 ".rodata",
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100994 ".opd", /* See comment [OPD] */
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100995 ".toc1", /* used by ppc64 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100996 ".altinstructions",
997 ".pdr",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100998 "__bug_table", /* used by powerpc for BUG() */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100999 ".exitcall.exit",
1000 ".eh_frame",
Randy Dunlapacd19492006-12-13 00:33:57 -08001001 ".parainstructions",
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001002 ".stab",
Al Viro468d9492006-06-23 23:22:43 +01001003 "__ex_table",
1004 ".fixup",
Randy Dunlap35899c52006-06-07 16:23:26 -07001005 ".smp_locks",
Sam Ravnborg909252d2006-06-08 20:37:30 +02001006 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001007 NULL
1008 };
1009 /* Start of section names */
1010 const char *namelist2[] = {
1011 ".debug",
1012 NULL
1013 };
Sam Ravnborg6e101332006-02-22 21:24:50 +01001014 /* part of section name */
1015 const char *namelist3 [] = {
1016 ".unwind", /* Sample: IA_64.unwind.exit.text */
1017 NULL
1018 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001019
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001020 for (s = namelist1; *s; s++)
1021 if (strcmp(*s, name) == 0)
1022 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001023 for (s = namelist2; *s; s++)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001024 if (strncmp(*s, name, strlen(*s)) == 0)
1025 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001026 for (s = namelist3; *s; s++)
Sam Ravnborge835a392006-03-05 11:34:15 +01001027 if (strstr(name, *s) != NULL)
Sam Ravnborg6e101332006-02-22 21:24:50 +01001028 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001029 return 0;
1030}
1031
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001032static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 const char *symname;
1035 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001036 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct module *mod;
1038 struct elf_info info = { };
1039 Elf_Sym *sym;
1040
1041 parse_elf(&info, modname);
1042
1043 mod = new_module(modname);
1044
1045 /* When there's no vmlinux, don't print warnings about
1046 * unresolved symbols (since there'll be too many ;) */
1047 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 mod->skip = 1;
1050 }
1051
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001052 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1053 while (license) {
1054 if (license_is_gpl_compatible(license))
1055 mod->gpl_compatible = 1;
1056 else {
1057 mod->gpl_compatible = 0;
1058 break;
1059 }
1060 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1061 "license", license);
1062 }
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1065 symname = info.strtab + sym->st_name;
1066
1067 handle_modversions(mod, &info, sym, symname);
1068 handle_moddevtable(mod, &info, sym, symname);
1069 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001070 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1071 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1074 if (version)
1075 maybe_frob_rcs_version(modname, version, info.modinfo,
1076 version - (char *)info.hdr);
1077 if (version || (all_versions && !is_vmlinux(modname)))
1078 get_src_version(modname, mod->srcversion,
1079 sizeof(mod->srcversion)-1);
1080
1081 parse_elf_finish(&info);
1082
1083 /* Our trick to get versioning for struct_module - it's
1084 * never passed as an argument to an exported function, so
1085 * the automatic versioning doesn't pick it up, but it's really
1086 * important anyhow */
1087 if (modversions)
1088 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1089}
1090
1091#define SZ 500
1092
1093/* We first write the generated file into memory using the
1094 * following helper, then compare to the file on disk and
1095 * only update the later if anything changed */
1096
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001097void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1098 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 char tmp[SZ];
1101 int len;
1102 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 va_start(ap, fmt);
1105 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001106 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 va_end(ap);
1108}
1109
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001110void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001113 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 buf->p = realloc(buf->p, buf->size);
1115 }
1116 strncpy(buf->p + buf->pos, s, len);
1117 buf->pos += len;
1118}
1119
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001120static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1121{
1122 const char *e = is_vmlinux(m) ?"":".ko";
1123
1124 switch (exp) {
1125 case export_gpl:
1126 fatal("modpost: GPL-incompatible module %s%s "
1127 "uses GPL-only symbol '%s'\n", m, e, s);
1128 break;
1129 case export_unused_gpl:
1130 fatal("modpost: GPL-incompatible module %s%s "
1131 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1132 break;
1133 case export_gpl_future:
1134 warn("modpost: GPL-incompatible module %s%s "
1135 "uses future GPL-only symbol '%s'\n", m, e, s);
1136 break;
1137 case export_plain:
1138 case export_unused:
1139 case export_unknown:
1140 /* ignore */
1141 break;
1142 }
1143}
1144
1145static void check_for_unused(enum export exp, const char* m, const char* s)
1146{
1147 const char *e = is_vmlinux(m) ?"":".ko";
1148
1149 switch (exp) {
1150 case export_unused:
1151 case export_unused_gpl:
1152 warn("modpost: module %s%s "
1153 "uses symbol '%s' marked UNUSED\n", m, e, s);
1154 break;
1155 default:
1156 /* ignore */
1157 break;
1158 }
1159}
1160
1161static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001162{
1163 struct symbol *s, *exp;
1164
1165 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001166 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001167 exp = find_symbol(s->name);
1168 if (!exp || exp->module == mod)
1169 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001170 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001171 if (basename)
1172 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001173 else
1174 basename = mod->name;
1175 if (!mod->gpl_compatible)
1176 check_for_gpl_usage(exp->export, basename, exp->name);
1177 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001178 }
1179}
1180
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001181/**
1182 * Header for the generated file
1183 **/
1184static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 buf_printf(b, "#include <linux/module.h>\n");
1187 buf_printf(b, "#include <linux/vermagic.h>\n");
1188 buf_printf(b, "#include <linux/compiler.h>\n");
1189 buf_printf(b, "\n");
1190 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1191 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 buf_printf(b, "struct module __this_module\n");
1193 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001194 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (mod->has_init)
1196 buf_printf(b, " .init = init_module,\n");
1197 if (mod->has_cleanup)
1198 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1199 " .exit = cleanup_module,\n"
1200 "#endif\n");
1201 buf_printf(b, "};\n");
1202}
1203
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001204/**
1205 * Record CRCs for unresolved symbols
1206 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001207static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001210 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 for (s = mod->unres; s; s = s->next) {
1213 exp = find_symbol(s->name);
1214 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001215 if (have_vmlinux && !s->weak) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001216 warn("\"%s\" [%s.ko] undefined!\n",
1217 s->name, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001218 err = warn_unresolved ? 0 : 1;
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 continue;
1221 }
1222 s->module = exp->module;
1223 s->crc_valid = exp->crc_valid;
1224 s->crc = exp->crc;
1225 }
1226
1227 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001228 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 buf_printf(b, "\n");
1231 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1232 buf_printf(b, "__attribute_used__\n");
1233 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1234
1235 for (s = mod->unres; s; s = s->next) {
1236 if (!s->module) {
1237 continue;
1238 }
1239 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001240 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 s->name, mod->name);
1242 continue;
1243 }
1244 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1245 }
1246
1247 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001248
1249 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001252static void add_depends(struct buffer *b, struct module *mod,
1253 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 struct symbol *s;
1256 struct module *m;
1257 int first = 1;
1258
1259 for (m = modules; m; m = m->next) {
1260 m->seen = is_vmlinux(m->name);
1261 }
1262
1263 buf_printf(b, "\n");
1264 buf_printf(b, "static const char __module_depends[]\n");
1265 buf_printf(b, "__attribute_used__\n");
1266 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1267 buf_printf(b, "\"depends=");
1268 for (s = mod->unres; s; s = s->next) {
1269 if (!s->module)
1270 continue;
1271
1272 if (s->module->seen)
1273 continue;
1274
1275 s->module->seen = 1;
1276 buf_printf(b, "%s%s", first ? "" : ",",
1277 strrchr(s->module->name, '/') + 1);
1278 first = 0;
1279 }
1280 buf_printf(b, "\";\n");
1281}
1282
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001283static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 if (mod->srcversion[0]) {
1286 buf_printf(b, "\n");
1287 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1288 mod->srcversion);
1289 }
1290}
1291
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001292static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 char *tmp;
1295 FILE *file;
1296 struct stat st;
1297
1298 file = fopen(fname, "r");
1299 if (!file)
1300 goto write;
1301
1302 if (fstat(fileno(file), &st) < 0)
1303 goto close_write;
1304
1305 if (st.st_size != b->pos)
1306 goto close_write;
1307
1308 tmp = NOFAIL(malloc(b->pos));
1309 if (fread(tmp, 1, b->pos, file) != b->pos)
1310 goto free_write;
1311
1312 if (memcmp(tmp, b->p, b->pos) != 0)
1313 goto free_write;
1314
1315 free(tmp);
1316 fclose(file);
1317 return;
1318
1319 free_write:
1320 free(tmp);
1321 close_write:
1322 fclose(file);
1323 write:
1324 file = fopen(fname, "w");
1325 if (!file) {
1326 perror(fname);
1327 exit(1);
1328 }
1329 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1330 perror(fname);
1331 exit(1);
1332 }
1333 fclose(file);
1334}
1335
Ram Paibd5cbce2006-06-08 22:12:53 -07001336/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001337 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001338 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001339static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 unsigned long size, pos = 0;
1342 void *file = grab_file(fname, &size);
1343 char *line;
1344
1345 if (!file)
1346 /* No symbol versions, silently ignore */
1347 return;
1348
1349 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001350 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 unsigned int crc;
1352 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001353 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 if (!(symname = strchr(line, '\t')))
1356 goto fail;
1357 *symname++ = '\0';
1358 if (!(modname = strchr(symname, '\t')))
1359 goto fail;
1360 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001361 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001362 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001363 if (export && ((end = strchr(export, '\t')) != NULL))
1364 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 crc = strtoul(line, &d, 16);
1366 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1367 goto fail;
1368
1369 if (!(mod = find_module(modname))) {
1370 if (is_vmlinux(modname)) {
1371 have_vmlinux = 1;
1372 }
1373 mod = new_module(NOFAIL(strdup(modname)));
1374 mod->skip = 1;
1375 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001376 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001377 s->kernel = kernel;
1378 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001379 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381 return;
1382fail:
1383 fatal("parse error in symbol dump file\n");
1384}
1385
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001386/* For normal builds always dump all symbols.
1387 * For external modules only dump symbols
1388 * that are not read from kernel Module.symvers.
1389 **/
1390static int dump_sym(struct symbol *sym)
1391{
1392 if (!external_module)
1393 return 1;
1394 if (sym->vmlinux || sym->kernel)
1395 return 0;
1396 return 1;
1397}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001398
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001399static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 struct buffer buf = { };
1402 struct symbol *symbol;
1403 int n;
1404
1405 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1406 symbol = symbolhash[n];
1407 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001408 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001409 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001410 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001411 symbol->module->name,
1412 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 symbol = symbol->next;
1414 }
1415 }
1416 write_if_changed(&buf, fname);
1417}
1418
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001419int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 struct module *mod;
1422 struct buffer buf = { };
1423 char fname[SZ];
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001424 char *kernel_read = NULL, *module_read = NULL;
1425 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001427 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001429 while ((opt = getopt(argc, argv, "i:I:mo:aw")) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 switch(opt) {
1431 case 'i':
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001432 kernel_read = optarg;
1433 break;
1434 case 'I':
1435 module_read = optarg;
1436 external_module = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 break;
1438 case 'm':
1439 modversions = 1;
1440 break;
1441 case 'o':
1442 dump_write = optarg;
1443 break;
1444 case 'a':
1445 all_versions = 1;
1446 break;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001447 case 'w':
1448 warn_unresolved = 1;
1449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 default:
1451 exit(1);
1452 }
1453 }
1454
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001455 if (kernel_read)
1456 read_dump(kernel_read, 1);
1457 if (module_read)
1458 read_dump(module_read, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 while (optind < argc) {
1461 read_symbols(argv[optind++]);
1462 }
1463
1464 for (mod = modules; mod; mod = mod->next) {
1465 if (mod->skip)
1466 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001467 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001468 }
1469
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001470 err = 0;
1471
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001472 for (mod = modules; mod; mod = mod->next) {
1473 if (mod->skip)
1474 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 buf.pos = 0;
1477
1478 add_header(&buf, mod);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001479 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 add_depends(&buf, mod, modules);
1481 add_moddevtable(&buf, mod);
1482 add_srcversion(&buf, mod);
1483
1484 sprintf(fname, "%s.mod.c", mod->name);
1485 write_if_changed(&buf, fname);
1486 }
1487
1488 if (dump_write)
1489 write_dump(dump_write);
1490
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}