blob: be0827f734c28715d63819ca81173e25e51f0711 [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 Ravnborg85bd2fd2007-02-26 15:33:52 +0100336static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100339 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 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;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100349 if (info->size < sizeof(*hdr)) {
350 /* file too small, assume this is an empty .o file */
351 return 0;
352 }
353 /* Is this a valid ELF file? */
354 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
355 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
356 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
357 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
358 /* Not an ELF file - silently ignore it */
359 return 0;
360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* Fix endianness in ELF header */
362 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
363 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
364 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
365 hdr->e_machine = TO_NATIVE(hdr->e_machine);
366 sechdrs = (void *)hdr + hdr->e_shoff;
367 info->sechdrs = sechdrs;
368
369 /* Fix endianness in section headers */
370 for (i = 0; i < hdr->e_shnum; i++) {
371 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
372 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
373 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
374 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
375 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
376 }
377 /* Find symbol table. */
378 for (i = 1; i < hdr->e_shnum; i++) {
379 const char *secstrings
380 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
Ram Paibd5cbce2006-06-08 22:12:53 -0700381 const char *secname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100383 if (sechdrs[i].sh_offset > info->size) {
384 fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename, (unsigned int)sechdrs[i].sh_offset, sizeof(*hdr));
385 return 0;
386 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700387 secname = secstrings + sechdrs[i].sh_name;
388 if (strcmp(secname, ".modinfo") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
390 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700391 } else if (strcmp(secname, "__ksymtab") == 0)
392 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200393 else if (strcmp(secname, "__ksymtab_unused") == 0)
394 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700395 else if (strcmp(secname, "__ksymtab_gpl") == 0)
396 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200397 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
398 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700399 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
400 info->export_gpl_future_sec = i;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (sechdrs[i].sh_type != SHT_SYMTAB)
403 continue;
404
405 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100406 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100408 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 sechdrs[sechdrs[i].sh_link].sh_offset;
410 }
411 if (!info->symtab_start) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100412 fatal("%s has no symtab?\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 /* Fix endianness in symbols */
415 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
416 sym->st_shndx = TO_NATIVE(sym->st_shndx);
417 sym->st_name = TO_NATIVE(sym->st_name);
418 sym->st_value = TO_NATIVE(sym->st_value);
419 sym->st_size = TO_NATIVE(sym->st_size);
420 }
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100421 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100424static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 release_file(info->hdr, info->size);
427}
428
Luke Yangf7b05e62006-03-02 18:35:31 +0800429#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
430#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100432static void handle_modversions(struct module *mod, struct elf_info *info,
433 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700436 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 switch (sym->st_shndx) {
439 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100440 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 break;
442 case SHN_ABS:
443 /* CRC'd symbol */
444 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
445 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700446 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
447 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 break;
450 case SHN_UNDEF:
451 /* undefined symbol */
452 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
453 ELF_ST_BIND(sym->st_info) != STB_WEAK)
454 break;
455 /* ignore global offset table */
456 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
457 break;
458 /* ignore __this_module, it will be resolved shortly */
459 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
460 break;
Ben Colline8d529012005-08-19 13:44:57 -0700461/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
462#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
463/* add compatibility with older glibc */
464#ifndef STT_SPARC_REGISTER
465#define STT_SPARC_REGISTER STT_REGISTER
466#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (info->hdr->e_machine == EM_SPARC ||
468 info->hdr->e_machine == EM_SPARCV9) {
469 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700470 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100472 if (symname[0] == '.') {
473 char *munged = strdup(symname);
474 munged[0] = '_';
475 munged[1] = toupper(munged[1]);
476 symname = munged;
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
482 strlen(MODULE_SYMBOL_PREFIX)) == 0)
483 mod->unres = alloc_symbol(symname +
484 strlen(MODULE_SYMBOL_PREFIX),
485 ELF_ST_BIND(sym->st_info) == STB_WEAK,
486 mod->unres);
487 break;
488 default:
489 /* All exported symbols */
490 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700491 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
492 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
495 mod->has_init = 1;
496 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
497 mod->has_cleanup = 1;
498 break;
499 }
500}
501
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100502/**
503 * Parse tag=value strings from .modinfo section
504 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static char *next_string(char *string, unsigned long *secsize)
506{
507 /* Skip non-zero chars */
508 while (string[0]) {
509 string++;
510 if ((*secsize)-- <= 1)
511 return NULL;
512 }
513
514 /* Skip any zero padding. */
515 while (!string[0]) {
516 string++;
517 if ((*secsize)-- <= 1)
518 return NULL;
519 }
520 return string;
521}
522
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200523static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
524 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 char *p;
527 unsigned int taglen = strlen(tag);
528 unsigned long size = modinfo_len;
529
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200530 if (info) {
531 size -= info - (char *)modinfo;
532 modinfo = next_string(info, &size);
533 }
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 for (p = modinfo; p; p = next_string(p, &size)) {
536 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
537 return p + taglen + 1;
538 }
539 return NULL;
540}
541
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200542static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
543 const char *tag)
544
545{
546 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
547}
548
Sam Ravnborg93684d32006-02-19 11:53:35 +0100549/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100550 * Test if string s ends in string sub
551 * return 0 if match
552 **/
553static int strrcmp(const char *s, const char *sub)
554{
555 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100556
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100557 if (!s || !sub)
558 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100559
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100560 slen = strlen(s);
561 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100562
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100563 if ((slen == 0) || (sublen == 0))
564 return 1;
565
566 if (sublen > slen)
567 return 1;
568
569 return memcmp(s + slen - sublen, sub, sublen);
570}
571
572/**
573 * Whitelist to allow certain references to pass with no warning.
574 * Pattern 1:
575 * If a module parameter is declared __initdata and permissions=0
576 * then this is legal despite the warning generated.
577 * We cannot see value of permissions here, so just ignore
578 * this pattern.
579 * The pattern is identified by:
580 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100581 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100582 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100583 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100584 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700585 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100586 * add, remove, probe functions etc.
587 * These functions may often be marked __init and we do not want to
588 * warn here.
589 * the pattern is identified by:
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200590 * tosec = .init.text | .exit.text | .init.data
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100591 * fromsec = .data
Sam Ravnborgaae5f662007-02-26 16:45:41 +0100592 * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one, *_console
Vivek Goyalee6a8542007-01-11 01:52:44 +0100593 *
594 * Pattern 3:
595 * Some symbols belong to init section but still it is ok to reference
596 * these from non-init sections as these symbols don't have any memory
597 * allocated for them and symbol address and value are same. So even
598 * if init section is freed, its ok to reference those symbols.
599 * For ex. symbols marking the init section boundaries.
600 * This pattern is identified by
601 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborgaae5f662007-02-26 16:45:41 +0100602 * Pattern 4:
603 * During the early init phase we have references from .init.text to
604 * .text we have an intended section mismatch - do not warn about it.
605 * See kernel_init() in init/main.c
606 * tosec = .init.text
607 * fromsec = .text
608 * atsym = kernel_init
609 * Some symbols belong to init section but still it is ok to reference
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100610 **/
Magnus Damm9e157a52006-08-08 17:32:11 +0900611static int secref_whitelist(const char *modname, const char *tosec,
Vivek Goyalee6a8542007-01-11 01:52:44 +0100612 const char *fromsec, const char *atsym,
613 const char *refsymname)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100614{
615 int f1 = 1, f2 = 1;
616 const char **s;
617 const char *pat2sym[] = {
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700618 "driver",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200619 "_template", /* scsi uses *_template a lot */
620 "_sht", /* scsi also used *_sht to some extent */
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100621 "_ops",
622 "_probe",
623 "_probe_one",
Vivek Goyal118c0ac2007-01-11 01:52:44 +0100624 "_console",
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100625 NULL
626 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100627
Vivek Goyalee6a8542007-01-11 01:52:44 +0100628 const char *pat3refsym[] = {
629 "__init_begin",
630 "_sinittext",
631 "_einittext",
632 NULL
633 };
634
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100635 /* Check for pattern 1 */
636 if (strcmp(tosec, ".init.data") != 0)
637 f1 = 0;
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100638 if (strncmp(fromsec, ".data", strlen(".data")) != 0)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100639 f1 = 0;
640 if (strncmp(atsym, "__param", strlen("__param")) != 0)
641 f1 = 0;
642
643 if (f1)
644 return f1;
645
646 /* Check for pattern 2 */
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100647 if ((strcmp(tosec, ".init.text") != 0) &&
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200648 (strcmp(tosec, ".exit.text") != 0) &&
649 (strcmp(tosec, ".init.data") != 0))
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100650 f2 = 0;
651 if (strcmp(fromsec, ".data") != 0)
652 f2 = 0;
653
654 for (s = pat2sym; *s; s++)
655 if (strrcmp(atsym, *s) == 0)
656 f1 = 1;
Magnus Damm9e157a52006-08-08 17:32:11 +0900657 if (f1 && f2)
658 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100659
Vivek Goyalf8657e12007-02-13 13:26:22 +0100660 /* Whitelist all references from .pci_fixup section if vmlinux
661 * Whitelist all refereces from .text.head to .init.data if vmlinux
662 * Whitelist all refereces from .text.head to .init.text if vmlinux
663 */
Magnus Damm9e157a52006-08-08 17:32:11 +0900664 if (is_vmlinux(modname)) {
665 if ((strcmp(fromsec, ".pci_fixup") == 0) &&
666 (strcmp(tosec, ".init.text") == 0))
667 return 1;
Vivek Goyalee6a8542007-01-11 01:52:44 +0100668
Vivek Goyalf8657e12007-02-13 13:26:22 +0100669 if ((strcmp(fromsec, ".text.head") == 0) &&
670 ((strcmp(tosec, ".init.data") == 0) ||
671 (strcmp(tosec, ".init.text") == 0)))
672 return 1;
673
Vivek Goyalee6a8542007-01-11 01:52:44 +0100674 /* Check for pattern 3 */
675 for (s = pat3refsym; *s; s++)
676 if (strcmp(refsymname, *s) == 0)
677 return 1;
Magnus Damm9e157a52006-08-08 17:32:11 +0900678 }
Sam Ravnborgaae5f662007-02-26 16:45:41 +0100679 /* Check for pattern 4 */
680 if ((strcmp(tosec, ".init.text") == 0) &&
681 (strcmp(fromsec, ".text") == 0) &&
682 (strcmp(refsymname, "kernel_init") == 0))
683 return 1;
Sam Ravnborg93659af2006-08-09 08:23:55 +0200684 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100685}
686
687/**
Sam Ravnborg93684d32006-02-19 11:53:35 +0100688 * Find symbol based on relocation record info.
689 * In some cases the symbol supplied is a valid symbol so
690 * return refsym. If st_name != 0 we assume this is a valid symbol.
691 * In other cases the symbol needs to be looked up in the symbol table
692 * based on section and address.
693 * **/
694static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
695 Elf_Sym *relsym)
696{
697 Elf_Sym *sym;
698
699 if (relsym->st_name != 0)
700 return relsym;
701 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
702 if (sym->st_shndx != relsym->st_shndx)
703 continue;
704 if (sym->st_value == addr)
705 return sym;
706 }
707 return NULL;
708}
709
David Brownellda68d612007-02-20 13:58:16 -0800710static inline int is_arm_mapping_symbol(const char *str)
711{
712 return str[0] == '$' && strchr("atd", str[1])
713 && (str[2] == '\0' || str[2] == '.');
714}
715
716/*
717 * If there's no name there, ignore it; likewise, ignore it if it's
718 * one of the magic symbols emitted used by current ARM tools.
719 *
720 * Otherwise if find_symbols_between() returns those symbols, they'll
721 * fail the whitelist tests and cause lots of false alarms ... fixable
722 * only by merging __exit and __init sections into __text, bloating
723 * the kernel (which is especially evil on embedded platforms).
724 */
725static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
726{
727 const char *name = elf->strtab + sym->st_name;
728
729 if (!name || !strlen(name))
730 return 0;
731 return !is_arm_mapping_symbol(name);
732}
733
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100734/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100735 * Find symbols before or equal addr and after addr - in the section sec.
736 * If we find two symbols with equal offset prefer one with a valid name.
737 * The ELF format may have a better way to detect what type of symbol
738 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100739 **/
740static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
741 const char *sec,
742 Elf_Sym **before, Elf_Sym **after)
743{
744 Elf_Sym *sym;
745 Elf_Ehdr *hdr = elf->hdr;
746 Elf_Addr beforediff = ~0;
747 Elf_Addr afterdiff = ~0;
748 const char *secstrings = (void *)hdr +
749 elf->sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100750
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100751 *before = NULL;
752 *after = NULL;
753
754 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
755 const char *symsec;
756
757 if (sym->st_shndx >= SHN_LORESERVE)
758 continue;
759 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
760 if (strcmp(symsec, sec) != 0)
761 continue;
David Brownellda68d612007-02-20 13:58:16 -0800762 if (!is_valid_name(elf, sym))
763 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100764 if (sym->st_value <= addr) {
765 if ((addr - sym->st_value) < beforediff) {
766 beforediff = addr - sym->st_value;
767 *before = sym;
768 }
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100769 else if ((addr - sym->st_value) == beforediff) {
David Brownellda68d612007-02-20 13:58:16 -0800770 *before = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100771 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100772 }
773 else
774 {
775 if ((sym->st_value - addr) < afterdiff) {
776 afterdiff = sym->st_value - addr;
777 *after = sym;
778 }
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100779 else if ((sym->st_value - addr) == afterdiff) {
David Brownellda68d612007-02-20 13:58:16 -0800780 *after = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100781 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100782 }
783 }
784}
785
786/**
787 * Print a warning about a section mismatch.
788 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100789 * Check whitelist before warning - it may be a false positive.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100790 **/
791static void warn_sec_mismatch(const char *modname, const char *fromsec,
792 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
793{
Sam Ravnborg93684d32006-02-19 11:53:35 +0100794 const char *refsymname = "";
795 Elf_Sym *before, *after;
796 Elf_Sym *refsym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100797 Elf_Ehdr *hdr = elf->hdr;
798 Elf_Shdr *sechdrs = elf->sechdrs;
799 const char *secstrings = (void *)hdr +
800 sechdrs[hdr->e_shstrndx].sh_offset;
801 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100802
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100803 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
804
Sam Ravnborg93684d32006-02-19 11:53:35 +0100805 refsym = find_elf_symbol(elf, r.r_addend, sym);
806 if (refsym && strlen(elf->strtab + refsym->st_name))
807 refsymname = elf->strtab + refsym->st_name;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100808
809 /* check whitelist - we may ignore it */
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100810 if (before &&
Magnus Damm9e157a52006-08-08 17:32:11 +0900811 secref_whitelist(modname, secname, fromsec,
Vivek Goyalee6a8542007-01-11 01:52:44 +0100812 elf->strtab + before->st_name, refsymname))
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100813 return;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100814
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100815 if (before && after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100816 warn("%s - Section mismatch: reference to %s:%s from %s "
817 "between '%s' (at offset 0x%llx) and '%s'\n",
818 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100819 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100820 (long long)r.r_offset,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100821 elf->strtab + after->st_name);
822 } else if (before) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100823 warn("%s - Section mismatch: reference to %s:%s from %s "
824 "after '%s' (at offset 0x%llx)\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100825 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100826 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100827 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100828 } else if (after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100829 warn("%s - Section mismatch: reference to %s:%s from %s "
830 "before '%s' (at offset -0x%llx)\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100831 modname, secname, refsymname, fromsec,
Eric Sesterhenneaaae382006-04-11 00:54:16 +0200832 elf->strtab + after->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100833 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100834 } else {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100835 warn("%s - Section mismatch: reference to %s:%s from %s "
836 "(offset 0x%llx)\n",
837 modname, secname, fromsec, refsymname,
838 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100839 }
840}
841
842/**
843 * A module includes a number of sections that are discarded
844 * either when loaded or when used as built-in.
845 * For loaded modules all functions marked __init and all data
846 * marked __initdata will be discarded when the module has been intialized.
847 * Likewise for modules used built-in the sections marked __exit
848 * are discarded because __exit marked function are supposed to be called
849 * only when a moduel is unloaded which never happes for built-in modules.
850 * The check_sec_ref() function traverses all relocation records
851 * to find all references to a section that reference a section that will
852 * be discarded and warns about it.
853 **/
854static void check_sec_ref(struct module *mod, const char *modname,
855 struct elf_info *elf,
856 int section(const char*),
857 int section_ref_ok(const char *))
858{
859 int i;
860 Elf_Sym *sym;
861 Elf_Ehdr *hdr = elf->hdr;
862 Elf_Shdr *sechdrs = elf->sechdrs;
863 const char *secstrings = (void *)hdr +
864 sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100865
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100866 /* Walk through all sections */
867 for (i = 0; i < hdr->e_shnum; i++) {
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700868 const char *name = secstrings + sechdrs[i].sh_name;
869 const char *secname;
870 Elf_Rela r;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700871 unsigned int r_sym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100872 /* We want to process only relocation sections and not .init */
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700873 if (sechdrs[i].sh_type == SHT_RELA) {
874 Elf_Rela *rela;
875 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
876 Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
877 name += strlen(".rela");
878 if (section_ref_ok(name))
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100879 continue;
880
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700881 for (rela = start; rela < stop; rela++) {
882 r.r_offset = TO_NATIVE(rela->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700883#if KERNEL_ELFCLASS == ELFCLASS64
884 if (hdr->e_machine == EM_MIPS) {
885 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
886 r_sym = TO_NATIVE(r_sym);
887 } else {
888 r.r_info = TO_NATIVE(rela->r_info);
889 r_sym = ELF_R_SYM(r.r_info);
890 }
891#else
892 r.r_info = TO_NATIVE(rela->r_info);
893 r_sym = ELF_R_SYM(r.r_info);
894#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700895 r.r_addend = TO_NATIVE(rela->r_addend);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700896 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700897 /* Skip special sections */
898 if (sym->st_shndx >= SHN_LORESERVE)
899 continue;
900
901 secname = secstrings +
902 sechdrs[sym->st_shndx].sh_name;
903 if (section(secname))
904 warn_sec_mismatch(modname, name,
905 elf, sym, r);
906 }
907 } else if (sechdrs[i].sh_type == SHT_REL) {
908 Elf_Rel *rel;
909 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
910 Elf_Rel *stop = (void*)start + sechdrs[i].sh_size;
911 name += strlen(".rel");
912 if (section_ref_ok(name))
913 continue;
914
915 for (rel = start; rel < stop; rel++) {
916 r.r_offset = TO_NATIVE(rel->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700917#if KERNEL_ELFCLASS == ELFCLASS64
918 if (hdr->e_machine == EM_MIPS) {
919 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
920 r_sym = TO_NATIVE(r_sym);
921 } else {
922 r.r_info = TO_NATIVE(rel->r_info);
923 r_sym = ELF_R_SYM(r.r_info);
924 }
925#else
926 r.r_info = TO_NATIVE(rel->r_info);
927 r_sym = ELF_R_SYM(r.r_info);
928#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700929 r.r_addend = 0;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -0700930 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -0700931 /* Skip special sections */
932 if (sym->st_shndx >= SHN_LORESERVE)
933 continue;
934
935 secname = secstrings +
936 sechdrs[sym->st_shndx].sh_name;
937 if (section(secname))
938 warn_sec_mismatch(modname, name,
939 elf, sym, r);
940 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100941 }
942 }
943}
944
945/**
946 * Functions used only during module init is marked __init and is stored in
947 * a .init.text section. Likewise data is marked __initdata and stored in
948 * a .init.data section.
949 * If this section is one of these sections return 1
950 * See include/linux/init.h for the details
951 **/
952static int init_section(const char *name)
953{
954 if (strcmp(name, ".init") == 0)
955 return 1;
956 if (strncmp(name, ".init.", strlen(".init.")) == 0)
957 return 1;
958 return 0;
959}
960
961/**
962 * Identify sections from which references to a .init section is OK.
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100963 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100964 * Unfortunately references to read only data that referenced .init
965 * sections had to be excluded. Almost all of these are false
966 * positives, they are created by gcc. The downside of excluding rodata
967 * is that there really are some user references from rodata to
968 * init code, e.g. drivers/video/vgacon.c:
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100969 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100970 * const struct consw vga_con = {
971 * con_startup: vgacon_startup,
972 *
973 * where vgacon_startup is __init. If you want to wade through the false
974 * positives, take out the check for rodata.
975 **/
976static int init_section_ref_ok(const char *name)
977{
978 const char **s;
979 /* Absolute section names */
980 const char *namelist1[] = {
981 ".init",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100982 ".opd", /* see comment [OPD] at exit_section_ref_ok() */
983 ".toc1", /* used by ppc64 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100984 ".stab",
Matthew Wilcox742433b2007-02-05 16:34:00 -0800985 ".data.rel.ro", /* used by parisc64 */
Rusty Russell139ec7c2006-12-07 02:14:08 +0100986 ".parainstructions",
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100987 ".text.lock",
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100988 "__bug_table", /* used by powerpc for BUG() */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100989 ".pci_fixup_header",
990 ".pci_fixup_final",
991 ".pdr",
992 "__param",
Al Viro468d9492006-06-23 23:22:43 +0100993 "__ex_table",
994 ".fixup",
Randy Dunlap35899c52006-06-07 16:23:26 -0700995 ".smp_locks",
Sam Ravnborg909252d2006-06-08 20:37:30 +0200996 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
Benjamin Herrenschmidt21c4ff82006-10-20 11:47:19 +1000997 "__ftr_fixup", /* powerpc cpu feature fixup */
998 "__fw_ftr_fixup", /* powerpc firmware feature fixup */
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100999 NULL
1000 };
1001 /* Start of section names */
1002 const char *namelist2[] = {
1003 ".init.",
1004 ".altinstructions",
1005 ".eh_frame",
1006 ".debug",
Rusty Russell139ec7c2006-12-07 02:14:08 +01001007 ".parainstructions",
Matthew Wilcox742433b2007-02-05 16:34:00 -08001008 ".rodata",
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001009 NULL
1010 };
Sam Ravnborg6e101332006-02-22 21:24:50 +01001011 /* part of section name */
1012 const char *namelist3 [] = {
1013 ".unwind", /* sample: IA_64.unwind.init.text */
1014 NULL
1015 };
1016
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001017 for (s = namelist1; *s; s++)
1018 if (strcmp(*s, name) == 0)
1019 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001020 for (s = namelist2; *s; s++)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001021 if (strncmp(*s, name, strlen(*s)) == 0)
1022 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001023 for (s = namelist3; *s; s++)
Sam Ravnborge835a392006-03-05 11:34:15 +01001024 if (strstr(name, *s) != NULL)
Sam Ravnborg6e101332006-02-22 21:24:50 +01001025 return 1;
Al Viro468d9492006-06-23 23:22:43 +01001026 if (strrcmp(name, ".init") == 0)
1027 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001028 return 0;
1029}
1030
1031/*
1032 * Functions used only during module exit is marked __exit and is stored in
1033 * a .exit.text section. Likewise data is marked __exitdata and stored in
1034 * a .exit.data section.
1035 * If this section is one of these sections return 1
1036 * See include/linux/init.h for the details
1037 **/
1038static int exit_section(const char *name)
1039{
1040 if (strcmp(name, ".exit.text") == 0)
1041 return 1;
1042 if (strcmp(name, ".exit.data") == 0)
1043 return 1;
1044 return 0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001045
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001046}
1047
1048/*
1049 * Identify sections from which references to a .exit section is OK.
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001050 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001051 * [OPD] Keith Ownes <kaos@sgi.com> commented:
1052 * For our future {in}sanity, add a comment that this is the ppc .opd
1053 * section, not the ia64 .opd section.
1054 * ia64 .opd should not point to discarded sections.
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +02001055 * [.rodata] like for .init.text we ignore .rodata references -same reason
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001056 **/
1057static int exit_section_ref_ok(const char *name)
1058{
1059 const char **s;
1060 /* Absolute section names */
1061 const char *namelist1[] = {
1062 ".exit.text",
1063 ".exit.data",
1064 ".init.text",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +02001065 ".rodata",
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001066 ".opd", /* See comment [OPD] */
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001067 ".toc1", /* used by ppc64 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001068 ".altinstructions",
1069 ".pdr",
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001070 "__bug_table", /* used by powerpc for BUG() */
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001071 ".exitcall.exit",
1072 ".eh_frame",
Randy Dunlapacd19492006-12-13 00:33:57 -08001073 ".parainstructions",
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001074 ".stab",
Al Viro468d9492006-06-23 23:22:43 +01001075 "__ex_table",
1076 ".fixup",
Randy Dunlap35899c52006-06-07 16:23:26 -07001077 ".smp_locks",
Sam Ravnborg909252d2006-06-08 20:37:30 +02001078 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001079 NULL
1080 };
1081 /* Start of section names */
1082 const char *namelist2[] = {
1083 ".debug",
1084 NULL
1085 };
Sam Ravnborg6e101332006-02-22 21:24:50 +01001086 /* part of section name */
1087 const char *namelist3 [] = {
1088 ".unwind", /* Sample: IA_64.unwind.exit.text */
1089 NULL
1090 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001091
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001092 for (s = namelist1; *s; s++)
1093 if (strcmp(*s, name) == 0)
1094 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001095 for (s = namelist2; *s; s++)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001096 if (strncmp(*s, name, strlen(*s)) == 0)
1097 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001098 for (s = namelist3; *s; s++)
Sam Ravnborge835a392006-03-05 11:34:15 +01001099 if (strstr(name, *s) != NULL)
Sam Ravnborg6e101332006-02-22 21:24:50 +01001100 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001101 return 0;
1102}
1103
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001104static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 const char *symname;
1107 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001108 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct module *mod;
1110 struct elf_info info = { };
1111 Elf_Sym *sym;
1112
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001113 if (!parse_elf(&info, modname))
1114 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 mod = new_module(modname);
1117
1118 /* When there's no vmlinux, don't print warnings about
1119 * unresolved symbols (since there'll be too many ;) */
1120 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 mod->skip = 1;
1123 }
1124
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001125 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1126 while (license) {
1127 if (license_is_gpl_compatible(license))
1128 mod->gpl_compatible = 1;
1129 else {
1130 mod->gpl_compatible = 0;
1131 break;
1132 }
1133 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1134 "license", license);
1135 }
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1138 symname = info.strtab + sym->st_name;
1139
1140 handle_modversions(mod, &info, sym, symname);
1141 handle_moddevtable(mod, &info, sym, symname);
1142 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001143 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1144 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1147 if (version)
1148 maybe_frob_rcs_version(modname, version, info.modinfo,
1149 version - (char *)info.hdr);
1150 if (version || (all_versions && !is_vmlinux(modname)))
1151 get_src_version(modname, mod->srcversion,
1152 sizeof(mod->srcversion)-1);
1153
1154 parse_elf_finish(&info);
1155
1156 /* Our trick to get versioning for struct_module - it's
1157 * never passed as an argument to an exported function, so
1158 * the automatic versioning doesn't pick it up, but it's really
1159 * important anyhow */
1160 if (modversions)
1161 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1162}
1163
1164#define SZ 500
1165
1166/* We first write the generated file into memory using the
1167 * following helper, then compare to the file on disk and
1168 * only update the later if anything changed */
1169
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001170void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1171 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 char tmp[SZ];
1174 int len;
1175 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 va_start(ap, fmt);
1178 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001179 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 va_end(ap);
1181}
1182
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001183void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001186 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 buf->p = realloc(buf->p, buf->size);
1188 }
1189 strncpy(buf->p + buf->pos, s, len);
1190 buf->pos += len;
1191}
1192
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001193static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1194{
1195 const char *e = is_vmlinux(m) ?"":".ko";
1196
1197 switch (exp) {
1198 case export_gpl:
1199 fatal("modpost: GPL-incompatible module %s%s "
1200 "uses GPL-only symbol '%s'\n", m, e, s);
1201 break;
1202 case export_unused_gpl:
1203 fatal("modpost: GPL-incompatible module %s%s "
1204 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1205 break;
1206 case export_gpl_future:
1207 warn("modpost: GPL-incompatible module %s%s "
1208 "uses future GPL-only symbol '%s'\n", m, e, s);
1209 break;
1210 case export_plain:
1211 case export_unused:
1212 case export_unknown:
1213 /* ignore */
1214 break;
1215 }
1216}
1217
1218static void check_for_unused(enum export exp, const char* m, const char* s)
1219{
1220 const char *e = is_vmlinux(m) ?"":".ko";
1221
1222 switch (exp) {
1223 case export_unused:
1224 case export_unused_gpl:
1225 warn("modpost: module %s%s "
1226 "uses symbol '%s' marked UNUSED\n", m, e, s);
1227 break;
1228 default:
1229 /* ignore */
1230 break;
1231 }
1232}
1233
1234static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001235{
1236 struct symbol *s, *exp;
1237
1238 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001239 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001240 exp = find_symbol(s->name);
1241 if (!exp || exp->module == mod)
1242 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001243 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001244 if (basename)
1245 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001246 else
1247 basename = mod->name;
1248 if (!mod->gpl_compatible)
1249 check_for_gpl_usage(exp->export, basename, exp->name);
1250 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001251 }
1252}
1253
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001254/**
1255 * Header for the generated file
1256 **/
1257static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
1259 buf_printf(b, "#include <linux/module.h>\n");
1260 buf_printf(b, "#include <linux/vermagic.h>\n");
1261 buf_printf(b, "#include <linux/compiler.h>\n");
1262 buf_printf(b, "\n");
1263 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1264 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 buf_printf(b, "struct module __this_module\n");
1266 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001267 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (mod->has_init)
1269 buf_printf(b, " .init = init_module,\n");
1270 if (mod->has_cleanup)
1271 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1272 " .exit = cleanup_module,\n"
1273 "#endif\n");
1274 buf_printf(b, "};\n");
1275}
1276
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001277/**
1278 * Record CRCs for unresolved symbols
1279 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001280static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001283 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 for (s = mod->unres; s; s = s->next) {
1286 exp = find_symbol(s->name);
1287 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001288 if (have_vmlinux && !s->weak) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001289 warn("\"%s\" [%s.ko] undefined!\n",
1290 s->name, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001291 err = warn_unresolved ? 0 : 1;
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 continue;
1294 }
1295 s->module = exp->module;
1296 s->crc_valid = exp->crc_valid;
1297 s->crc = exp->crc;
1298 }
1299
1300 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001301 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 buf_printf(b, "\n");
1304 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1305 buf_printf(b, "__attribute_used__\n");
1306 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1307
1308 for (s = mod->unres; s; s = s->next) {
1309 if (!s->module) {
1310 continue;
1311 }
1312 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001313 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 s->name, mod->name);
1315 continue;
1316 }
1317 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1318 }
1319
1320 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001321
1322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001325static void add_depends(struct buffer *b, struct module *mod,
1326 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 struct symbol *s;
1329 struct module *m;
1330 int first = 1;
1331
1332 for (m = modules; m; m = m->next) {
1333 m->seen = is_vmlinux(m->name);
1334 }
1335
1336 buf_printf(b, "\n");
1337 buf_printf(b, "static const char __module_depends[]\n");
1338 buf_printf(b, "__attribute_used__\n");
1339 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1340 buf_printf(b, "\"depends=");
1341 for (s = mod->unres; s; s = s->next) {
1342 if (!s->module)
1343 continue;
1344
1345 if (s->module->seen)
1346 continue;
1347
1348 s->module->seen = 1;
1349 buf_printf(b, "%s%s", first ? "" : ",",
1350 strrchr(s->module->name, '/') + 1);
1351 first = 0;
1352 }
1353 buf_printf(b, "\";\n");
1354}
1355
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001356static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 if (mod->srcversion[0]) {
1359 buf_printf(b, "\n");
1360 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1361 mod->srcversion);
1362 }
1363}
1364
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001365static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 char *tmp;
1368 FILE *file;
1369 struct stat st;
1370
1371 file = fopen(fname, "r");
1372 if (!file)
1373 goto write;
1374
1375 if (fstat(fileno(file), &st) < 0)
1376 goto close_write;
1377
1378 if (st.st_size != b->pos)
1379 goto close_write;
1380
1381 tmp = NOFAIL(malloc(b->pos));
1382 if (fread(tmp, 1, b->pos, file) != b->pos)
1383 goto free_write;
1384
1385 if (memcmp(tmp, b->p, b->pos) != 0)
1386 goto free_write;
1387
1388 free(tmp);
1389 fclose(file);
1390 return;
1391
1392 free_write:
1393 free(tmp);
1394 close_write:
1395 fclose(file);
1396 write:
1397 file = fopen(fname, "w");
1398 if (!file) {
1399 perror(fname);
1400 exit(1);
1401 }
1402 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1403 perror(fname);
1404 exit(1);
1405 }
1406 fclose(file);
1407}
1408
Ram Paibd5cbce2006-06-08 22:12:53 -07001409/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001410 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001411 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001412static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 unsigned long size, pos = 0;
1415 void *file = grab_file(fname, &size);
1416 char *line;
1417
1418 if (!file)
1419 /* No symbol versions, silently ignore */
1420 return;
1421
1422 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001423 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 unsigned int crc;
1425 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001426 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 if (!(symname = strchr(line, '\t')))
1429 goto fail;
1430 *symname++ = '\0';
1431 if (!(modname = strchr(symname, '\t')))
1432 goto fail;
1433 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001434 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001435 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001436 if (export && ((end = strchr(export, '\t')) != NULL))
1437 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 crc = strtoul(line, &d, 16);
1439 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1440 goto fail;
1441
1442 if (!(mod = find_module(modname))) {
1443 if (is_vmlinux(modname)) {
1444 have_vmlinux = 1;
1445 }
1446 mod = new_module(NOFAIL(strdup(modname)));
1447 mod->skip = 1;
1448 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001449 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001450 s->kernel = kernel;
1451 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001452 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 return;
1455fail:
1456 fatal("parse error in symbol dump file\n");
1457}
1458
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001459/* For normal builds always dump all symbols.
1460 * For external modules only dump symbols
1461 * that are not read from kernel Module.symvers.
1462 **/
1463static int dump_sym(struct symbol *sym)
1464{
1465 if (!external_module)
1466 return 1;
1467 if (sym->vmlinux || sym->kernel)
1468 return 0;
1469 return 1;
1470}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001471
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001472static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 struct buffer buf = { };
1475 struct symbol *symbol;
1476 int n;
1477
1478 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1479 symbol = symbolhash[n];
1480 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001481 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001482 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001483 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001484 symbol->module->name,
1485 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 symbol = symbol->next;
1487 }
1488 }
1489 write_if_changed(&buf, fname);
1490}
1491
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001492int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 struct module *mod;
1495 struct buffer buf = { };
1496 char fname[SZ];
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001497 char *kernel_read = NULL, *module_read = NULL;
1498 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001500 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001502 while ((opt = getopt(argc, argv, "i:I:mo:aw")) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 switch(opt) {
1504 case 'i':
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001505 kernel_read = optarg;
1506 break;
1507 case 'I':
1508 module_read = optarg;
1509 external_module = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 break;
1511 case 'm':
1512 modversions = 1;
1513 break;
1514 case 'o':
1515 dump_write = optarg;
1516 break;
1517 case 'a':
1518 all_versions = 1;
1519 break;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001520 case 'w':
1521 warn_unresolved = 1;
1522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 default:
1524 exit(1);
1525 }
1526 }
1527
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001528 if (kernel_read)
1529 read_dump(kernel_read, 1);
1530 if (module_read)
1531 read_dump(module_read, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 while (optind < argc) {
1534 read_symbols(argv[optind++]);
1535 }
1536
1537 for (mod = modules; mod; mod = mod->next) {
1538 if (mod->skip)
1539 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001540 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001541 }
1542
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001543 err = 0;
1544
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001545 for (mod = modules; mod; mod = mod->next) {
1546 if (mod->skip)
1547 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 buf.pos = 0;
1550
1551 add_header(&buf, mod);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001552 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 add_depends(&buf, mod, modules);
1554 add_moddevtable(&buf, mod);
1555 add_srcversion(&buf, mod);
1556
1557 sprintf(fname, "%s.mod.c", mod->name);
1558 write_if_changed(&buf, fname);
1559 }
1560
1561 if (dump_write)
1562 write_dump(dump_write);
1563
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001564 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}