blob: 3cf1ba8220d24230fb0e683c1e4223e98176a9f0 [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 Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 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;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020026/* Warn about section mismatch in vmlinux if set to 1 */
27static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070028/* Only warn about unresolved symbols */
29static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070030/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010031static int sec_mismatch_count = 0;
32static int sec_mismatch_verbose = 1;
33
Sam Ravnborgc96fca22006-07-01 11:44:23 +020034enum export {
35 export_plain, export_unused, export_gpl,
36 export_unused_gpl, export_gpl_future, export_unknown
37};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Andi Kleen6d9a89e2007-11-22 03:43:08 +010039#define PRINTF __attribute__ ((format (printf, 1, 2)))
40
41PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 va_list arglist;
44
45 fprintf(stderr, "FATAL: ");
46
47 va_start(arglist, fmt);
48 vfprintf(stderr, fmt, arglist);
49 va_end(arglist);
50
51 exit(1);
52}
53
Andi Kleen6d9a89e2007-11-22 03:43:08 +010054PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 va_list arglist;
57
58 fprintf(stderr, "WARNING: ");
59
60 va_start(arglist, fmt);
61 vfprintf(stderr, fmt, arglist);
62 va_end(arglist);
63}
64
Andi Kleen6d9a89e2007-11-22 03:43:08 +010065PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060066{
67 va_list arglist;
68
69 fprintf(stderr, "ERROR: ");
70
71 va_start(arglist, fmt);
72 vfprintf(stderr, fmt, arglist);
73 va_end(arglist);
74}
75
Sam Ravnborg040fcc82006-01-28 22:15:55 +010076static int is_vmlinux(const char *modname)
77{
78 const char *myname;
79
Sam Ravnborgdf578e72008-01-11 19:17:15 +010080 myname = strrchr(modname, '/');
81 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010082 myname++;
83 else
84 myname = modname;
85
Sam Ravnborg741f98f2007-07-17 10:54:06 +020086 return (strcmp(myname, "vmlinux") == 0) ||
87 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +010088}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090void *do_nofail(void *ptr, const char *expr)
91{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010092 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +010094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return ptr;
96}
97
98/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static struct module *modules;
100
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100101static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct module *mod;
104
105 for (mod = modules; mod; mod = mod->next)
106 if (strcmp(mod->name, modname) == 0)
107 break;
108 return mod;
109}
110
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100111static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct module *mod;
114 char *p, *s;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 mod = NOFAIL(malloc(sizeof(*mod)));
117 memset(mod, 0, sizeof(*mod));
118 p = NOFAIL(strdup(modname));
119
120 /* strip trailing .o */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100121 s = strrchr(p, '.');
122 if (s != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (strcmp(s, ".o") == 0)
124 *s = '\0';
125
126 /* add to list */
127 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200128 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 mod->next = modules;
130 modules = mod;
131
132 return mod;
133}
134
135/* A hash of all exported symbols,
136 * struct symbol is also used for lists of unresolved symbols */
137
138#define SYMBOL_HASH_SIZE 1024
139
140struct symbol {
141 struct symbol *next;
142 struct module *module;
143 unsigned int crc;
144 int crc_valid;
145 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100146 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
147 unsigned int kernel:1; /* 1 if symbol is from kernel
148 * (only for external modules) **/
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100149 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Ram Paibd5cbce2006-06-08 22:12:53 -0700150 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 char name[0];
152};
153
154static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
155
156/* This is based on the hash agorithm from gdbm, via tdb */
157static inline unsigned int tdb_hash(const char *name)
158{
159 unsigned value; /* Used to compute the hash value. */
160 unsigned i; /* Used to cycle through random values. */
161
162 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100163 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
165
166 return (1103515243 * value + 12345);
167}
168
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100169/**
170 * Allocate a new symbols for use in the hash of exported symbols or
171 * the list of unresolved symbols per module
172 **/
173static struct symbol *alloc_symbol(const char *name, unsigned int weak,
174 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
177
178 memset(s, 0, sizeof(*s));
179 strcpy(s->name, name);
180 s->weak = weak;
181 s->next = next;
182 return s;
183}
184
185/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700186static struct symbol *new_symbol(const char *name, struct module *module,
187 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 unsigned int hash;
190 struct symbol *new;
191
192 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
193 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
194 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700195 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100196 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100199static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 struct symbol *s;
202
203 /* For our purposes, .foo matches foo. PPC64 needs this. */
204 if (name[0] == '.')
205 name++;
206
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100207 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (strcmp(s->name, name) == 0)
209 return s;
210 }
211 return NULL;
212}
213
Ram Paibd5cbce2006-06-08 22:12:53 -0700214static struct {
215 const char *str;
216 enum export export;
217} export_list[] = {
218 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200219 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700220 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200221 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700222 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
223 { .str = "(unknown)", .export = export_unknown },
224};
225
226
227static const char *export_str(enum export ex)
228{
229 return export_list[ex].str;
230}
231
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100232static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700233{
234 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100235
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200236 if (!s)
237 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700238 for (i = 0; export_list[i].export != export_unknown; i++) {
239 if (strcmp(export_list[i].str, s) == 0)
240 return export_list[i].export;
241 }
242 return export_unknown;
243}
244
245static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
246{
247 if (sec == elf->export_sec)
248 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200249 else if (sec == elf->export_unused_sec)
250 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700251 else if (sec == elf->export_gpl_sec)
252 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200253 else if (sec == elf->export_unused_gpl_sec)
254 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700255 else if (sec == elf->export_gpl_future_sec)
256 return export_gpl_future;
257 else
258 return export_unknown;
259}
260
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100261/**
262 * Add an exported symbol - it may have already been added without a
263 * CRC, in this case just update the CRC
264 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700265static struct symbol *sym_add_exported(const char *name, struct module *mod,
266 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct symbol *s = find_symbol(name);
269
270 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700271 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100272 } else {
273 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100274 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100275 "was in %s%s\n", mod->name, name,
276 s->module->name,
277 is_vmlinux(s->module->name) ?"":".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700278 } else {
279 /* In case Modules.symvers was out of date */
280 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100283 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100284 s->vmlinux = is_vmlinux(mod->name);
285 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700286 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100287 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100290static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700291 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100292{
293 struct symbol *s = find_symbol(name);
294
295 if (!s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700296 s = new_symbol(name, mod, export);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100297 s->crc = crc;
298 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100301void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct stat st;
304 void *map;
305 int fd;
306
307 fd = open(filename, O_RDONLY);
308 if (fd < 0 || fstat(fd, &st) != 0)
309 return NULL;
310
311 *size = st.st_size;
312 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
313 close(fd);
314
315 if (map == MAP_FAILED)
316 return NULL;
317 return map;
318}
319
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100320/**
321 * Return a copy of the next line in a mmap'ed file.
322 * spaces in the beginning of the line is trimmed away.
323 * Return a pointer to a static buffer.
324 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100325char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 static char line[4096];
328 int skip = 1;
329 size_t len = 0;
330 signed char *p = (signed char *)file + *pos;
331 char *s = line;
332
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100333 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (skip && isspace(*p)) {
335 p++;
336 continue;
337 }
338 skip = 0;
339 if (*p != '\n' && (*pos < size)) {
340 len++;
341 *s++ = *p++;
342 if (len > 4095)
343 break; /* Too long, stop */
344 } else {
345 /* End of string */
346 *s = '\0';
347 return line;
348 }
349 }
350 /* End of buffer */
351 return NULL;
352}
353
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100354void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 munmap(file, size);
357}
358
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100359static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100362 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 Elf_Shdr *sechdrs;
364 Elf_Sym *sym;
365
366 hdr = grab_file(filename, &info->size);
367 if (!hdr) {
368 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200369 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100372 if (info->size < sizeof(*hdr)) {
373 /* file too small, assume this is an empty .o file */
374 return 0;
375 }
376 /* Is this a valid ELF file? */
377 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
378 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
379 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
380 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
381 /* Not an ELF file - silently ignore it */
382 return 0;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /* Fix endianness in ELF header */
385 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
386 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
387 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
388 hdr->e_machine = TO_NATIVE(hdr->e_machine);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900389 hdr->e_type = TO_NATIVE(hdr->e_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 sechdrs = (void *)hdr + hdr->e_shoff;
391 info->sechdrs = sechdrs;
392
Petr Stetiara83710e2007-08-27 12:15:07 +0200393 /* Check if file offset is correct */
394 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100395 fatal("section header offset=%lu in file '%s' is bigger than "
396 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
397 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200398 return 0;
399 }
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* Fix endianness in section headers */
402 for (i = 0; i < hdr->e_shnum; i++) {
403 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
404 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
405 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
406 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
407 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900408 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
409 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 /* Find symbol table. */
412 for (i = 1; i < hdr->e_shnum; i++) {
413 const char *secstrings
414 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
Ram Paibd5cbce2006-06-08 22:12:53 -0700415 const char *secname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100417 if (sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100418 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
419 "sizeof(*hrd)=%zu\n", filename,
420 (unsigned long)sechdrs[i].sh_offset,
421 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100422 return 0;
423 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700424 secname = secstrings + sechdrs[i].sh_name;
425 if (strcmp(secname, ".modinfo") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
427 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700428 } else if (strcmp(secname, "__ksymtab") == 0)
429 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200430 else if (strcmp(secname, "__ksymtab_unused") == 0)
431 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700432 else if (strcmp(secname, "__ksymtab_gpl") == 0)
433 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200434 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
435 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700436 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
437 info->export_gpl_future_sec = i;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (sechdrs[i].sh_type != SHT_SYMTAB)
440 continue;
441
442 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100443 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100445 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 sechdrs[sechdrs[i].sh_link].sh_offset;
447 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100448 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100449 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* Fix endianness in symbols */
452 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
453 sym->st_shndx = TO_NATIVE(sym->st_shndx);
454 sym->st_name = TO_NATIVE(sym->st_name);
455 sym->st_value = TO_NATIVE(sym->st_value);
456 sym->st_size = TO_NATIVE(sym->st_size);
457 }
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100458 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100461static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 release_file(info->hdr, info->size);
464}
465
Luke Yangf7b05e62006-03-02 18:35:31 +0800466#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
467#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100469static void handle_modversions(struct module *mod, struct elf_info *info,
470 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700473 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 switch (sym->st_shndx) {
476 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100477 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 case SHN_ABS:
480 /* CRC'd symbol */
481 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
482 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700483 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
484 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 break;
487 case SHN_UNDEF:
488 /* undefined symbol */
489 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
490 ELF_ST_BIND(sym->st_info) != STB_WEAK)
491 break;
492 /* ignore global offset table */
493 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
494 break;
495 /* ignore __this_module, it will be resolved shortly */
496 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
497 break;
Ben Colline8d529012005-08-19 13:44:57 -0700498/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
499#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
500/* add compatibility with older glibc */
501#ifndef STT_SPARC_REGISTER
502#define STT_SPARC_REGISTER STT_REGISTER
503#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (info->hdr->e_machine == EM_SPARC ||
505 info->hdr->e_machine == EM_SPARCV9) {
506 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700507 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100509 if (symname[0] == '.') {
510 char *munged = strdup(symname);
511 munged[0] = '_';
512 munged[1] = toupper(munged[1]);
513 symname = munged;
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100519 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
520 mod->unres =
521 alloc_symbol(symname +
522 strlen(MODULE_SYMBOL_PREFIX),
523 ELF_ST_BIND(sym->st_info) == STB_WEAK,
524 mod->unres);
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
527 default:
528 /* All exported symbols */
529 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700530 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
531 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
534 mod->has_init = 1;
535 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
536 mod->has_cleanup = 1;
537 break;
538 }
539}
540
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100541/**
542 * Parse tag=value strings from .modinfo section
543 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544static char *next_string(char *string, unsigned long *secsize)
545{
546 /* Skip non-zero chars */
547 while (string[0]) {
548 string++;
549 if ((*secsize)-- <= 1)
550 return NULL;
551 }
552
553 /* Skip any zero padding. */
554 while (!string[0]) {
555 string++;
556 if ((*secsize)-- <= 1)
557 return NULL;
558 }
559 return string;
560}
561
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200562static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
563 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 char *p;
566 unsigned int taglen = strlen(tag);
567 unsigned long size = modinfo_len;
568
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200569 if (info) {
570 size -= info - (char *)modinfo;
571 modinfo = next_string(info, &size);
572 }
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 for (p = modinfo; p; p = next_string(p, &size)) {
575 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
576 return p + taglen + 1;
577 }
578 return NULL;
579}
580
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200581static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
582 const char *tag)
583
584{
585 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
586}
587
Sam Ravnborg93684d32006-02-19 11:53:35 +0100588/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100589 * Test if string s ends in string sub
590 * return 0 if match
591 **/
592static int strrcmp(const char *s, const char *sub)
593{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100594 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100595
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100596 if (!s || !sub)
597 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100598
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100599 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100600 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100601
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100602 if ((slen == 0) || (sublen == 0))
603 return 1;
604
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100605 if (sublen > slen)
606 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100607
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100608 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100609}
610
Sam Ravnborgff13f922008-01-23 19:54:27 +0100611static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
612{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100613 if (sym)
614 return elf->strtab + sym->st_name;
615 else
616 return "";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100617}
618
619static const char *sec_name(struct elf_info *elf, int shndx)
620{
621 Elf_Shdr *sechdrs = elf->sechdrs;
622 return (void *)elf->hdr +
623 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
624 sechdrs[shndx].sh_name;
625}
626
627static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
628{
629 return (void *)elf->hdr +
630 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
631 sechdr->sh_name;
632}
633
Sam Ravnborg10668222008-01-13 22:21:31 +0100634/* if sym is empty or point to a string
635 * like ".[0-9]+" then return 1.
636 * This is the optional prefix added by ld to some sections
637 */
638static int number_prefix(const char *sym)
639{
640 if (*sym++ == '\0')
641 return 1;
642 if (*sym != '.')
643 return 0;
644 do {
645 char c = *sym++;
646 if (c < '0' || c > '9')
647 return 0;
648 } while (*sym);
649 return 1;
650}
651
652/* The pattern is an array of simple patterns.
653 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100654 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100655 * "foo*" will match a string that begins with "foo"
656 * "foo$" will match a string equal to "foo" or "foo.1"
657 * where the '1' can be any number including several digits.
658 * The $ syntax is for sections where ld append a dot number
659 * to make section name unique.
660 */
661int match(const char *sym, const char * const pat[])
662{
663 const char *p;
664 while (*pat) {
665 p = *pat++;
666 const char *endp = p + strlen(p) - 1;
667
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100668 /* "*foo" */
669 if (*p == '*') {
670 if (strrcmp(sym, p + 1) == 0)
671 return 1;
672 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100673 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100674 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100675 if (strncmp(sym, p, strlen(p) - 1) == 0)
676 return 1;
677 }
678 /* "foo$" */
679 else if (*endp == '$') {
680 if (strncmp(sym, p, strlen(p) - 1) == 0) {
681 if (number_prefix(sym + strlen(p) - 1))
682 return 1;
683 }
684 }
685 /* no wildcards */
686 else {
687 if (strcmp(p, sym) == 0)
688 return 1;
689 }
690 }
691 /* no match */
692 return 0;
693}
694
Sam Ravnborg10668222008-01-13 22:21:31 +0100695/* sections that we do not want to do full section mismatch check on */
696static const char *section_white_list[] =
697 { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
698
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100699#define ALL_INIT_DATA_SECTIONS \
700 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
701#define ALL_EXIT_DATA_SECTIONS \
702 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100703
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100704#define ALL_INIT_TEXT_SECTIONS \
705 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
706#define ALL_EXIT_TEXT_SECTIONS \
707 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100708
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100709#define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
710#define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100711
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100712#define DATA_SECTIONS ".data$", ".data.rel$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100713#define TEXT_SECTIONS ".text$"
714
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100715#define INIT_SECTIONS ".init.data$", ".init.text$"
716#define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
717#define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
718#define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
719
720#define EXIT_SECTIONS ".exit.data$", ".exit.text$"
721#define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
722#define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
723#define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
724
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100725/* init data sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100726static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100727
728/* all init sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100729static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100730
731/* All init and exit sections (code + data) */
732static const char *init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100733 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100734
735/* data section */
736static const char *data_sections[] = { DATA_SECTIONS, NULL };
737
738/* sections that may refer to an init/exit section with no warning */
739static const char *initref_sections[] =
740{
741 ".text.init.refok*",
742 ".exit.text.refok*",
743 ".data.init.refok*",
744 NULL
745};
746
747
748/* symbols in .data that may refer to init/exit sections */
749static const char *symbol_white_list[] =
750{
751 "*driver",
752 "*_template", /* scsi uses *_template a lot */
753 "*_timer", /* arm uses ops structures named _timer a lot */
754 "*_sht", /* scsi also used *_sht to some extent */
755 "*_ops",
756 "*_probe",
757 "*_probe_one",
758 "*_console",
759 NULL
760};
761
762static const char *head_sections[] = { ".head.text*", NULL };
763static const char *linker_symbols[] =
764 { "__init_begin", "_sinittext", "_einittext", NULL };
765
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100766enum mismatch {
767 NO_MISMATCH,
768 TEXT_TO_INIT,
769 DATA_TO_INIT,
770 TEXT_TO_EXIT,
771 DATA_TO_EXIT,
772 XXXINIT_TO_INIT,
773 XXXEXIT_TO_EXIT,
774 INIT_TO_EXIT,
775 EXIT_TO_INIT,
776 EXPORT_TO_INIT_EXIT,
777};
778
Sam Ravnborg10668222008-01-13 22:21:31 +0100779struct sectioncheck {
780 const char *fromsec[20];
781 const char *tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100782 enum mismatch mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100783};
784
785const struct sectioncheck sectioncheck[] = {
786/* Do not reference init/exit code/data from
787 * normal code and data
788 */
789{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100790 .fromsec = { TEXT_SECTIONS, NULL },
791 .tosec = { ALL_INIT_SECTIONS, NULL },
792 .mismatch = TEXT_TO_INIT,
793},
794{
795 .fromsec = { DATA_SECTIONS, NULL },
796 .tosec = { ALL_INIT_SECTIONS, NULL },
797 .mismatch = DATA_TO_INIT,
798},
799{
800 .fromsec = { TEXT_SECTIONS, NULL },
801 .tosec = { ALL_EXIT_SECTIONS, NULL },
802 .mismatch = TEXT_TO_EXIT,
803},
804{
805 .fromsec = { DATA_SECTIONS, NULL },
806 .tosec = { ALL_EXIT_SECTIONS, NULL },
807 .mismatch = DATA_TO_EXIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100808},
809/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
810{
811 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100812 .tosec = { INIT_SECTIONS, NULL },
813 .mismatch = XXXINIT_TO_INIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100814},
815/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
816{
817 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100818 .tosec = { EXIT_SECTIONS, NULL },
819 .mismatch = XXXEXIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100820},
821/* Do not use exit code/data from init code */
822{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100823 .fromsec = { ALL_INIT_SECTIONS, NULL },
824 .tosec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100825 .mismatch = INIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100826},
827/* Do not use init code/data from exit code */
828{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100829 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100830 .tosec = { ALL_INIT_SECTIONS, NULL },
831 .mismatch = EXIT_TO_INIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100832},
833/* Do not export init/exit functions or data */
834{
835 .fromsec = { "__ksymtab*", NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100836 .tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL },
837 .mismatch = EXPORT_TO_INIT_EXIT
Sam Ravnborg10668222008-01-13 22:21:31 +0100838}
839};
840
841static int section_mismatch(const char *fromsec, const char *tosec)
842{
843 int i;
844 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
845 const struct sectioncheck *check = &sectioncheck[0];
846
847 for (i = 0; i < elems; i++) {
848 if (match(fromsec, check->fromsec) &&
849 match(tosec, check->tosec))
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100850 return check->mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100851 check++;
852 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100853 return NO_MISMATCH;
Sam Ravnborg10668222008-01-13 22:21:31 +0100854}
855
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100856/**
857 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200858 *
859 * Pattern 0:
860 * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
861 * The pattern is identified by:
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200862 * fromsec = .text.init.refok* | .data.init.refok*
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200863 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100864 * Pattern 1:
865 * If a module parameter is declared __initdata and permissions=0
866 * then this is legal despite the warning generated.
867 * We cannot see value of permissions here, so just ignore
868 * this pattern.
869 * The pattern is identified by:
870 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100871 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100872 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100873 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100874 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700875 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100876 * add, remove, probe functions etc.
877 * These functions may often be marked __init and we do not want to
878 * warn here.
879 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +0200880 * tosec = init or exit section
881 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100882 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
883 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +0100884 *
885 * Pattern 3:
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100886 * Whitelist all refereces from .text.head to .init.data
887 * Whitelist all refereces from .text.head to .init.text
888 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200889 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +0100890 * Some symbols belong to init section but still it is ok to reference
891 * these from non-init sections as these symbols don't have any memory
892 * allocated for them and symbol address and value are same. So even
893 * if init section is freed, its ok to reference those symbols.
894 * For ex. symbols marking the init section boundaries.
895 * This pattern is identified by
896 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100897 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100898 **/
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100899static int secref_whitelist(const char *fromsec, const char *fromsym,
900 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100901{
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200902 /* Check for pattern 0 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100903 if (match(fromsec, initref_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100904 return 0;
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200905
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100906 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100907 if (match(tosec, init_data_sections) &&
908 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100909 (strncmp(fromsym, "__param", strlen("__param")) == 0))
910 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100911
912 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100913 if (match(tosec, init_exit_sections) &&
914 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100915 match(fromsym, symbol_white_list))
916 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100917
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100918 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100919 if (match(fromsec, head_sections) &&
920 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100921 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100922
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200923 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100924 if (match(tosym, linker_symbols))
925 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100926
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100927 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100928}
929
930/**
Sam Ravnborg93684d32006-02-19 11:53:35 +0100931 * Find symbol based on relocation record info.
932 * In some cases the symbol supplied is a valid symbol so
933 * return refsym. If st_name != 0 we assume this is a valid symbol.
934 * In other cases the symbol needs to be looked up in the symbol table
935 * based on section and address.
936 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100937static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100938 Elf_Sym *relsym)
939{
940 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100941 Elf_Sym *near = NULL;
942 Elf64_Sword distance = 20;
943 Elf64_Sword d;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100944
945 if (relsym->st_name != 0)
946 return relsym;
947 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
948 if (sym->st_shndx != relsym->st_shndx)
949 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900950 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
951 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100952 if (sym->st_value == addr)
953 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100954 /* Find a symbol nearby - addr are maybe negative */
955 d = sym->st_value - addr;
956 if (d < 0)
957 d = addr - sym->st_value;
958 if (d < distance) {
959 distance = d;
960 near = sym;
961 }
Sam Ravnborg93684d32006-02-19 11:53:35 +0100962 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100963 /* We need a close match */
964 if (distance < 20)
965 return near;
966 else
967 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100968}
969
David Brownellda68d612007-02-20 13:58:16 -0800970static inline int is_arm_mapping_symbol(const char *str)
971{
972 return str[0] == '$' && strchr("atd", str[1])
973 && (str[2] == '\0' || str[2] == '.');
974}
975
976/*
977 * If there's no name there, ignore it; likewise, ignore it if it's
978 * one of the magic symbols emitted used by current ARM tools.
979 *
980 * Otherwise if find_symbols_between() returns those symbols, they'll
981 * fail the whitelist tests and cause lots of false alarms ... fixable
982 * only by merging __exit and __init sections into __text, bloating
983 * the kernel (which is especially evil on embedded platforms).
984 */
985static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
986{
987 const char *name = elf->strtab + sym->st_name;
988
989 if (!name || !strlen(name))
990 return 0;
991 return !is_arm_mapping_symbol(name);
992}
993
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100994/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100995 * Find symbols before or equal addr and after addr - in the section sec.
996 * If we find two symbols with equal offset prefer one with a valid name.
997 * The ELF format may have a better way to detect what type of symbol
998 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100999 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001000static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1001 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001002{
1003 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001004 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001005 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001006
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001007 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1008 const char *symsec;
1009
1010 if (sym->st_shndx >= SHN_LORESERVE)
1011 continue;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001012 symsec = sec_name(elf, sym->st_shndx);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001013 if (strcmp(symsec, sec) != 0)
1014 continue;
David Brownellda68d612007-02-20 13:58:16 -08001015 if (!is_valid_name(elf, sym))
1016 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001017 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001018 if ((addr - sym->st_value) < distance) {
1019 distance = addr - sym->st_value;
1020 near = sym;
1021 } else if ((addr - sym->st_value) == distance) {
1022 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001023 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001024 }
1025 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001026 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001027}
1028
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001029/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001030 * Convert a section name to the function/data attribute
1031 * .init.text => __init
1032 * .cpuinit.data => __cpudata
1033 * .memexitconst => __memconst
1034 * etc.
1035*/
1036static char *sec2annotation(const char *s)
1037{
1038 if (match(s, init_exit_sections)) {
1039 char *p = malloc(20);
1040 char *r = p;
1041
1042 *p++ = '_';
1043 *p++ = '_';
1044 if (*s == '.')
1045 s++;
1046 while (*s && *s != '.')
1047 *p++ = *s++;
1048 *p = '\0';
1049 if (*s == '.')
1050 s++;
1051 if (strstr(s, "rodata") != NULL)
1052 strcat(p, "const ");
1053 else if (strstr(s, "data") != NULL)
1054 strcat(p, "data ");
1055 else
1056 strcat(p, " ");
1057 return r; /* we leak her but we do not care */
1058 } else {
1059 return "";
1060 }
1061}
1062
1063static int is_function(Elf_Sym *sym)
1064{
1065 if (sym)
1066 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1067 else
1068 return 0;
1069}
1070
1071/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001072 * Print a warning about a section mismatch.
1073 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001074 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001075 */
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001076static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001077 const char *fromsec,
1078 unsigned long long fromaddr,
1079 const char *fromsym,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001080 int from_is_func,
1081 const char *tosec, const char *tosym,
1082 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001083{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001084 const char *from, *from_p;
1085 const char *to, *to_p;
1086 from = from_is_func ? "function" : "variable";
1087 from_p = from_is_func ? "()" : "";
1088 to = to_is_func ? "function" : "variable";
1089 to_p = to_is_func ? "()" : "";
1090
1091 fprintf(stderr, "WARNING: %s(%s+0x%llx): Section mismatch in"
1092 " reference from the %s %s%s to the %s %s:%s%s\n",
1093 modname, fromsec, fromaddr, from, fromsym, from_p,
1094 to, tosec, tosym, to_p);
1095
1096 sec_mismatch_count++;
1097 if (!sec_mismatch_verbose)
1098 return;
1099
1100 switch (mismatch) {
1101 case TEXT_TO_INIT:
1102 fprintf(stderr,
1103 "The function %s %s() references\n"
1104 "the %s %s%s%s.\n"
1105 "This is often because %s lacks a %s\n"
1106 "annotation or the annotation of %s is wrong.\n",
1107 sec2annotation(fromsec), fromsym,
1108 to, sec2annotation(tosec), tosym, to_p,
1109 fromsym, sec2annotation(tosec), tosym);
1110 break;
1111 case DATA_TO_INIT: {
1112 const char **s = symbol_white_list;
1113 fprintf(stderr,
1114 "The variable %s references\n"
1115 "the %s %s%s%s\n"
1116 "If the reference is valid then annotate the\n"
1117 "variable with __init* (see linux/init.h) "
1118 "or name the variable:\n",
1119 fromsym, to, sec2annotation(tosec), tosym, to_p);
1120 while (*s)
1121 fprintf(stderr, "%s, ", *s++);
1122 fprintf(stderr, "\n");
1123 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001124 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001125 case TEXT_TO_EXIT:
1126 fprintf(stderr,
1127 "The function %s() references a %s in an exit section.\n"
1128 "Often the %s %s%s has valid usage outside the exit section\n"
1129 "and the fix is to remove the %sannotation of %s.\n",
1130 fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
1131 break;
1132 case DATA_TO_EXIT: {
1133 const char **s = symbol_white_list;
1134 fprintf(stderr,
1135 "The variable %s references\n"
1136 "the %s %s%s%s\n"
1137 "If the reference is valid then annotate the\n"
1138 "variable with __exit* (see linux/init.h) or "
1139 "name the variable:\n",
1140 fromsym, to, sec2annotation(tosec), tosym, to_p);
1141 while (*s)
1142 fprintf(stderr, "%s, ", *s++);
1143 fprintf(stderr, "\n");
1144 break;
1145 }
1146 case XXXINIT_TO_INIT:
1147 case XXXEXIT_TO_EXIT:
1148 fprintf(stderr,
1149 "The %s %s%s%s references\n"
1150 "a %s %s%s%s.\n"
1151 "If %s is only used by %s then\n"
1152 "annotate %s with a matching annotation.\n",
1153 from, sec2annotation(fromsec), fromsym, from_p,
1154 to, sec2annotation(tosec), tosym, to_p,
1155 fromsym, tosym, fromsym);
1156 break;
1157 case INIT_TO_EXIT:
1158 fprintf(stderr,
1159 "The %s %s%s%s references\n"
1160 "a %s %s%s%s.\n"
1161 "This is often seen when error handling "
1162 "in the init function\n"
1163 "uses functionality in the exit path.\n"
1164 "The fix is often to remove the %sannotation of\n"
1165 "%s%s so it may be used outside an exit section.\n",
1166 from, sec2annotation(fromsec), fromsym, from_p,
1167 to, sec2annotation(tosec), tosym, to_p,
1168 sec2annotation(tosec), tosym, to_p);
1169 break;
1170 case EXIT_TO_INIT:
1171 fprintf(stderr,
1172 "The %s %s%s%s references\n"
1173 "a %s %s%s%s.\n"
1174 "This is often seen when error handling "
1175 "in the exit function\n"
1176 "uses functionality in the init path.\n"
1177 "The fix is often to remove the %sannotation of\n"
1178 "%s%s so it may be used outside an init section.\n",
1179 from, sec2annotation(fromsec), fromsym, from_p,
1180 to, sec2annotation(tosec), tosym, to_p,
1181 sec2annotation(tosec), tosym, to_p);
1182 break;
1183 case EXPORT_TO_INIT_EXIT:
1184 fprintf(stderr,
1185 "The symbol %s is exported and annotated %s\n"
1186 "Fix this by removing the %sannotation of %s "
1187 "or drop the export.\n",
1188 tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
1189 case NO_MISMATCH:
1190 /* To get warnings on missing members */
1191 break;
1192 }
1193 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001194}
1195
1196static void check_section_mismatch(const char *modname, struct elf_info *elf,
1197 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1198{
1199 const char *tosec;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001200 enum mismatch mismatch;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001201
1202 tosec = sec_name(elf, sym->st_shndx);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001203 mismatch = section_mismatch(fromsec, tosec);
1204 if (mismatch != NO_MISMATCH) {
1205 Elf_Sym *to;
1206 Elf_Sym *from;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001207 const char *tosym;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001208 const char *fromsym;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001209
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001210 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1211 fromsym = sym_name(elf, from);
1212 to = find_elf_symbol(elf, r->r_addend, sym);
1213 tosym = sym_name(elf, to);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001214
1215 /* check whitelist - we may ignore it */
1216 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001217 report_sec_mismatch(modname, mismatch,
1218 fromsec, r->r_offset, fromsym,
1219 is_function(from), tosec, tosym,
1220 is_function(to));
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001221 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001222 }
1223}
1224
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001225static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001226 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001227{
1228 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001229 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001230
1231 return (void *)elf->hdr + sechdrs[section].sh_offset +
1232 (r->r_offset - sechdrs[section].sh_addr);
1233}
1234
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001235static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001236{
1237 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001238 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001239
1240 switch (r_typ) {
1241 case R_386_32:
1242 r->r_addend = TO_NATIVE(*location);
1243 break;
1244 case R_386_PC32:
1245 r->r_addend = TO_NATIVE(*location) + 4;
1246 /* For CONFIG_RELOCATABLE=y */
1247 if (elf->hdr->e_type == ET_EXEC)
1248 r->r_addend += r->r_offset;
1249 break;
1250 }
1251 return 0;
1252}
1253
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001254static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001255{
1256 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1257
1258 switch (r_typ) {
1259 case R_ARM_ABS32:
1260 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001261 r->r_addend = (int)(long)
1262 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001263 break;
1264 case R_ARM_PC24:
1265 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001266 r->r_addend = (int)(long)(elf->hdr +
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001267 sechdr->sh_offset +
1268 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001269 break;
1270 default:
1271 return 1;
1272 }
1273 return 0;
1274}
1275
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001276static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001277{
1278 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001279 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001280 unsigned int inst;
1281
1282 if (r_typ == R_MIPS_HI16)
1283 return 1; /* skip this */
1284 inst = TO_NATIVE(*location);
1285 switch (r_typ) {
1286 case R_MIPS_LO16:
1287 r->r_addend = inst & 0xffff;
1288 break;
1289 case R_MIPS_26:
1290 r->r_addend = (inst & 0x03ffffff) << 2;
1291 break;
1292 case R_MIPS_32:
1293 r->r_addend = inst;
1294 break;
1295 }
1296 return 0;
1297}
1298
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001299static void section_rela(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001300 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001301{
1302 Elf_Sym *sym;
1303 Elf_Rela *rela;
1304 Elf_Rela r;
1305 unsigned int r_sym;
1306 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001307
Sam Ravnborgff13f922008-01-23 19:54:27 +01001308 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001309 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1310
Sam Ravnborgff13f922008-01-23 19:54:27 +01001311 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001312 fromsec += strlen(".rela");
1313 /* if from section (name) is know good then skip it */
Sam Ravnborg10668222008-01-13 22:21:31 +01001314 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001315 return;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001316 for (rela = start; rela < stop; rela++) {
1317 r.r_offset = TO_NATIVE(rela->r_offset);
1318#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001319 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001320 unsigned int r_typ;
1321 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1322 r_sym = TO_NATIVE(r_sym);
1323 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1324 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1325 } else {
1326 r.r_info = TO_NATIVE(rela->r_info);
1327 r_sym = ELF_R_SYM(r.r_info);
1328 }
1329#else
1330 r.r_info = TO_NATIVE(rela->r_info);
1331 r_sym = ELF_R_SYM(r.r_info);
1332#endif
1333 r.r_addend = TO_NATIVE(rela->r_addend);
1334 sym = elf->symtab_start + r_sym;
1335 /* Skip special sections */
1336 if (sym->st_shndx >= SHN_LORESERVE)
1337 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001338 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001339 }
1340}
1341
1342static void section_rel(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001343 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001344{
1345 Elf_Sym *sym;
1346 Elf_Rel *rel;
1347 Elf_Rela r;
1348 unsigned int r_sym;
1349 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001350
Sam Ravnborgff13f922008-01-23 19:54:27 +01001351 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001352 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1353
Sam Ravnborgff13f922008-01-23 19:54:27 +01001354 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001355 fromsec += strlen(".rel");
1356 /* if from section (name) is know good then skip it */
Sam Ravnborg10668222008-01-13 22:21:31 +01001357 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001358 return;
1359
1360 for (rel = start; rel < stop; rel++) {
1361 r.r_offset = TO_NATIVE(rel->r_offset);
1362#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001363 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001364 unsigned int r_typ;
1365 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1366 r_sym = TO_NATIVE(r_sym);
1367 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1368 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1369 } else {
1370 r.r_info = TO_NATIVE(rel->r_info);
1371 r_sym = ELF_R_SYM(r.r_info);
1372 }
1373#else
1374 r.r_info = TO_NATIVE(rel->r_info);
1375 r_sym = ELF_R_SYM(r.r_info);
1376#endif
1377 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001378 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001379 case EM_386:
1380 if (addend_386_rel(elf, sechdr, &r))
1381 continue;
1382 break;
1383 case EM_ARM:
1384 if (addend_arm_rel(elf, sechdr, &r))
1385 continue;
1386 break;
1387 case EM_MIPS:
1388 if (addend_mips_rel(elf, sechdr, &r))
1389 continue;
1390 break;
1391 }
1392 sym = elf->symtab_start + r_sym;
1393 /* Skip special sections */
1394 if (sym->st_shndx >= SHN_LORESERVE)
1395 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001396 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001397 }
1398}
1399
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001400/**
1401 * A module includes a number of sections that are discarded
1402 * either when loaded or when used as built-in.
1403 * For loaded modules all functions marked __init and all data
1404 * marked __initdata will be discarded when the module has been intialized.
1405 * Likewise for modules used built-in the sections marked __exit
1406 * are discarded because __exit marked function are supposed to be called
1407 * only when a moduel is unloaded which never happes for built-in modules.
1408 * The check_sec_ref() function traverses all relocation records
1409 * to find all references to a section that reference a section that will
1410 * be discarded and warns about it.
1411 **/
1412static void check_sec_ref(struct module *mod, const char *modname,
Sam Ravnborg10668222008-01-13 22:21:31 +01001413 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001414{
1415 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001416 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001417
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001418 /* Walk through all sections */
Sam Ravnborgff13f922008-01-23 19:54:27 +01001419 for (i = 0; i < elf->hdr->e_shnum; i++) {
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001420 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001421 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001422 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001423 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001424 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001425 }
1426}
1427
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001428static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 const char *symname;
1431 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001432 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 struct module *mod;
1434 struct elf_info info = { };
1435 Elf_Sym *sym;
1436
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001437 if (!parse_elf(&info, modname))
1438 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 mod = new_module(modname);
1441
1442 /* When there's no vmlinux, don't print warnings about
1443 * unresolved symbols (since there'll be too many ;) */
1444 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 mod->skip = 1;
1447 }
1448
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001449 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1450 while (license) {
1451 if (license_is_gpl_compatible(license))
1452 mod->gpl_compatible = 1;
1453 else {
1454 mod->gpl_compatible = 0;
1455 break;
1456 }
1457 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1458 "license", license);
1459 }
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1462 symname = info.strtab + sym->st_name;
1463
1464 handle_modversions(mod, &info, sym, symname);
1465 handle_moddevtable(mod, &info, sym, symname);
1466 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001467 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001468 (is_vmlinux(modname) && vmlinux_section_warnings))
1469 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1472 if (version)
1473 maybe_frob_rcs_version(modname, version, info.modinfo,
1474 version - (char *)info.hdr);
1475 if (version || (all_versions && !is_vmlinux(modname)))
1476 get_src_version(modname, mod->srcversion,
1477 sizeof(mod->srcversion)-1);
1478
1479 parse_elf_finish(&info);
1480
1481 /* Our trick to get versioning for struct_module - it's
1482 * never passed as an argument to an exported function, so
1483 * the automatic versioning doesn't pick it up, but it's really
1484 * important anyhow */
1485 if (modversions)
1486 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1487}
1488
1489#define SZ 500
1490
1491/* We first write the generated file into memory using the
1492 * following helper, then compare to the file on disk and
1493 * only update the later if anything changed */
1494
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001495void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1496 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 char tmp[SZ];
1499 int len;
1500 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 va_start(ap, fmt);
1503 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001504 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 va_end(ap);
1506}
1507
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001508void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001511 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 buf->p = realloc(buf->p, buf->size);
1513 }
1514 strncpy(buf->p + buf->pos, s, len);
1515 buf->pos += len;
1516}
1517
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001518static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1519{
1520 const char *e = is_vmlinux(m) ?"":".ko";
1521
1522 switch (exp) {
1523 case export_gpl:
1524 fatal("modpost: GPL-incompatible module %s%s "
1525 "uses GPL-only symbol '%s'\n", m, e, s);
1526 break;
1527 case export_unused_gpl:
1528 fatal("modpost: GPL-incompatible module %s%s "
1529 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1530 break;
1531 case export_gpl_future:
1532 warn("modpost: GPL-incompatible module %s%s "
1533 "uses future GPL-only symbol '%s'\n", m, e, s);
1534 break;
1535 case export_plain:
1536 case export_unused:
1537 case export_unknown:
1538 /* ignore */
1539 break;
1540 }
1541}
1542
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001543static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001544{
1545 const char *e = is_vmlinux(m) ?"":".ko";
1546
1547 switch (exp) {
1548 case export_unused:
1549 case export_unused_gpl:
1550 warn("modpost: module %s%s "
1551 "uses symbol '%s' marked UNUSED\n", m, e, s);
1552 break;
1553 default:
1554 /* ignore */
1555 break;
1556 }
1557}
1558
1559static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001560{
1561 struct symbol *s, *exp;
1562
1563 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001564 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001565 exp = find_symbol(s->name);
1566 if (!exp || exp->module == mod)
1567 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001568 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001569 if (basename)
1570 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001571 else
1572 basename = mod->name;
1573 if (!mod->gpl_compatible)
1574 check_for_gpl_usage(exp->export, basename, exp->name);
1575 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001576 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001577}
1578
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001579/**
1580 * Header for the generated file
1581 **/
1582static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 buf_printf(b, "#include <linux/module.h>\n");
1585 buf_printf(b, "#include <linux/vermagic.h>\n");
1586 buf_printf(b, "#include <linux/compiler.h>\n");
1587 buf_printf(b, "\n");
1588 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1589 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 buf_printf(b, "struct module __this_module\n");
1591 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001592 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (mod->has_init)
1594 buf_printf(b, " .init = init_module,\n");
1595 if (mod->has_cleanup)
1596 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1597 " .exit = cleanup_module,\n"
1598 "#endif\n");
Roman Zippele61a1c12007-05-09 02:35:15 -07001599 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 buf_printf(b, "};\n");
1601}
1602
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001603/**
1604 * Record CRCs for unresolved symbols
1605 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001606static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001609 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 for (s = mod->unres; s; s = s->next) {
1612 exp = find_symbol(s->name);
1613 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001614 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001615 if (warn_unresolved) {
1616 warn("\"%s\" [%s.ko] undefined!\n",
1617 s->name, mod->name);
1618 } else {
1619 merror("\"%s\" [%s.ko] undefined!\n",
1620 s->name, mod->name);
1621 err = 1;
1622 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 continue;
1625 }
1626 s->module = exp->module;
1627 s->crc_valid = exp->crc_valid;
1628 s->crc = exp->crc;
1629 }
1630
1631 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001632 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 buf_printf(b, "\n");
1635 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001636 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1638
1639 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001640 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001643 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 s->name, mod->name);
1645 continue;
1646 }
1647 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1648 }
1649
1650 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001651
1652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001655static void add_depends(struct buffer *b, struct module *mod,
1656 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 struct symbol *s;
1659 struct module *m;
1660 int first = 1;
1661
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001662 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 buf_printf(b, "\n");
1666 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001667 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1669 buf_printf(b, "\"depends=");
1670 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001671 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (!s->module)
1673 continue;
1674
1675 if (s->module->seen)
1676 continue;
1677
1678 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001679 p = strrchr(s->module->name, '/');
1680 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001681 p++;
1682 else
1683 p = s->module->name;
1684 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 first = 0;
1686 }
1687 buf_printf(b, "\";\n");
1688}
1689
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001690static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
1692 if (mod->srcversion[0]) {
1693 buf_printf(b, "\n");
1694 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1695 mod->srcversion);
1696 }
1697}
1698
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001699static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 char *tmp;
1702 FILE *file;
1703 struct stat st;
1704
1705 file = fopen(fname, "r");
1706 if (!file)
1707 goto write;
1708
1709 if (fstat(fileno(file), &st) < 0)
1710 goto close_write;
1711
1712 if (st.st_size != b->pos)
1713 goto close_write;
1714
1715 tmp = NOFAIL(malloc(b->pos));
1716 if (fread(tmp, 1, b->pos, file) != b->pos)
1717 goto free_write;
1718
1719 if (memcmp(tmp, b->p, b->pos) != 0)
1720 goto free_write;
1721
1722 free(tmp);
1723 fclose(file);
1724 return;
1725
1726 free_write:
1727 free(tmp);
1728 close_write:
1729 fclose(file);
1730 write:
1731 file = fopen(fname, "w");
1732 if (!file) {
1733 perror(fname);
1734 exit(1);
1735 }
1736 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1737 perror(fname);
1738 exit(1);
1739 }
1740 fclose(file);
1741}
1742
Ram Paibd5cbce2006-06-08 22:12:53 -07001743/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001744 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001745 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001746static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 unsigned long size, pos = 0;
1749 void *file = grab_file(fname, &size);
1750 char *line;
1751
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001752 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /* No symbol versions, silently ignore */
1754 return;
1755
1756 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001757 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 unsigned int crc;
1759 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001760 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 if (!(symname = strchr(line, '\t')))
1763 goto fail;
1764 *symname++ = '\0';
1765 if (!(modname = strchr(symname, '\t')))
1766 goto fail;
1767 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001768 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001769 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001770 if (export && ((end = strchr(export, '\t')) != NULL))
1771 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 crc = strtoul(line, &d, 16);
1773 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1774 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001775 mod = find_module(modname);
1776 if (!mod) {
1777 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 mod = new_module(NOFAIL(strdup(modname)));
1780 mod->skip = 1;
1781 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001782 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001783 s->kernel = kernel;
1784 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001785 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 return;
1788fail:
1789 fatal("parse error in symbol dump file\n");
1790}
1791
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001792/* For normal builds always dump all symbols.
1793 * For external modules only dump symbols
1794 * that are not read from kernel Module.symvers.
1795 **/
1796static int dump_sym(struct symbol *sym)
1797{
1798 if (!external_module)
1799 return 1;
1800 if (sym->vmlinux || sym->kernel)
1801 return 0;
1802 return 1;
1803}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001804
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001805static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 struct buffer buf = { };
1808 struct symbol *symbol;
1809 int n;
1810
1811 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1812 symbol = symbolhash[n];
1813 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001814 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001815 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001816 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001817 symbol->module->name,
1818 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 symbol = symbol->next;
1820 }
1821 }
1822 write_if_changed(&buf, fname);
1823}
1824
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001825int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 struct module *mod;
1828 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001829 char *kernel_read = NULL, *module_read = NULL;
1830 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001832 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001834 while ((opt = getopt(argc, argv, "i:I:msSo:aw")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001835 switch (opt) {
1836 case 'i':
1837 kernel_read = optarg;
1838 break;
1839 case 'I':
1840 module_read = optarg;
1841 external_module = 1;
1842 break;
1843 case 'm':
1844 modversions = 1;
1845 break;
1846 case 'o':
1847 dump_write = optarg;
1848 break;
1849 case 'a':
1850 all_versions = 1;
1851 break;
1852 case 's':
1853 vmlinux_section_warnings = 0;
1854 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001855 case 'S':
1856 sec_mismatch_verbose = 0;
1857 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001858 case 'w':
1859 warn_unresolved = 1;
1860 break;
1861 default:
1862 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 }
1865
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001866 if (kernel_read)
1867 read_dump(kernel_read, 1);
1868 if (module_read)
1869 read_dump(module_read, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001871 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 for (mod = modules; mod; mod = mod->next) {
1875 if (mod->skip)
1876 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001877 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001878 }
1879
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001880 err = 0;
1881
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001882 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01001883 char fname[strlen(mod->name) + 10];
1884
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001885 if (mod->skip)
1886 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 buf.pos = 0;
1889
1890 add_header(&buf, mod);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001891 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 add_depends(&buf, mod, modules);
1893 add_moddevtable(&buf, mod);
1894 add_srcversion(&buf, mod);
1895
1896 sprintf(fname, "%s.mod.c", mod->name);
1897 write_if_changed(&buf, fname);
1898 }
1899
1900 if (dump_write)
1901 write_dump(dump_write);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001902 if (sec_mismatch_count && !sec_mismatch_verbose)
1903 fprintf(stderr, "modpost: Found %d section mismatch(es).\n"
1904 "To see additional details select \"Enable full "
1905 "Section mismatch analysis\"\n"
1906 "in the Kernel Hacking menu "
1907 "(CONFIG_SECTION_MISMATCH).\n", sec_mismatch_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001909 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}