blob: 0a80acafd2123ce3aba70e343a284dba23c7f04f [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 Ravnborgc96fca22006-07-01 11:44:23 +020031enum export {
32 export_plain, export_unused, export_gpl,
33 export_unused_gpl, export_gpl_future, export_unknown
34};
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Andi Kleen6d9a89e2007-11-22 03:43:08 +010036#define PRINTF __attribute__ ((format (printf, 1, 2)))
37
38PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 va_list arglist;
41
42 fprintf(stderr, "FATAL: ");
43
44 va_start(arglist, fmt);
45 vfprintf(stderr, fmt, arglist);
46 va_end(arglist);
47
48 exit(1);
49}
50
Andi Kleen6d9a89e2007-11-22 03:43:08 +010051PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 va_list arglist;
54
55 fprintf(stderr, "WARNING: ");
56
57 va_start(arglist, fmt);
58 vfprintf(stderr, fmt, arglist);
59 va_end(arglist);
60}
61
Andi Kleen6d9a89e2007-11-22 03:43:08 +010062PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060063{
64 va_list arglist;
65
66 fprintf(stderr, "ERROR: ");
67
68 va_start(arglist, fmt);
69 vfprintf(stderr, fmt, arglist);
70 va_end(arglist);
71}
72
Sam Ravnborg040fcc82006-01-28 22:15:55 +010073static int is_vmlinux(const char *modname)
74{
75 const char *myname;
76
Sam Ravnborgdf578e72008-01-11 19:17:15 +010077 myname = strrchr(modname, '/');
78 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010079 myname++;
80 else
81 myname = modname;
82
Sam Ravnborg741f98f2007-07-17 10:54:06 +020083 return (strcmp(myname, "vmlinux") == 0) ||
84 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +010085}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087void *do_nofail(void *ptr, const char *expr)
88{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010089 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +010091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return ptr;
93}
94
95/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static struct module *modules;
97
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010098static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct module *mod;
101
102 for (mod = modules; mod; mod = mod->next)
103 if (strcmp(mod->name, modname) == 0)
104 break;
105 return mod;
106}
107
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100108static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct module *mod;
111 char *p, *s;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 mod = NOFAIL(malloc(sizeof(*mod)));
114 memset(mod, 0, sizeof(*mod));
115 p = NOFAIL(strdup(modname));
116
117 /* strip trailing .o */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100118 s = strrchr(p, '.');
119 if (s != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (strcmp(s, ".o") == 0)
121 *s = '\0';
122
123 /* add to list */
124 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200125 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 mod->next = modules;
127 modules = mod;
128
129 return mod;
130}
131
132/* A hash of all exported symbols,
133 * struct symbol is also used for lists of unresolved symbols */
134
135#define SYMBOL_HASH_SIZE 1024
136
137struct symbol {
138 struct symbol *next;
139 struct module *module;
140 unsigned int crc;
141 int crc_valid;
142 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100143 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
144 unsigned int kernel:1; /* 1 if symbol is from kernel
145 * (only for external modules) **/
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100146 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Ram Paibd5cbce2006-06-08 22:12:53 -0700147 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 char name[0];
149};
150
151static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
152
153/* This is based on the hash agorithm from gdbm, via tdb */
154static inline unsigned int tdb_hash(const char *name)
155{
156 unsigned value; /* Used to compute the hash value. */
157 unsigned i; /* Used to cycle through random values. */
158
159 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100160 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
162
163 return (1103515243 * value + 12345);
164}
165
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100166/**
167 * Allocate a new symbols for use in the hash of exported symbols or
168 * the list of unresolved symbols per module
169 **/
170static struct symbol *alloc_symbol(const char *name, unsigned int weak,
171 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
174
175 memset(s, 0, sizeof(*s));
176 strcpy(s->name, name);
177 s->weak = weak;
178 s->next = next;
179 return s;
180}
181
182/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700183static struct symbol *new_symbol(const char *name, struct module *module,
184 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 unsigned int hash;
187 struct symbol *new;
188
189 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
190 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
191 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700192 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100193 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100196static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 struct symbol *s;
199
200 /* For our purposes, .foo matches foo. PPC64 needs this. */
201 if (name[0] == '.')
202 name++;
203
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100204 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (strcmp(s->name, name) == 0)
206 return s;
207 }
208 return NULL;
209}
210
Ram Paibd5cbce2006-06-08 22:12:53 -0700211static struct {
212 const char *str;
213 enum export export;
214} export_list[] = {
215 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200216 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700217 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200218 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700219 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
220 { .str = "(unknown)", .export = export_unknown },
221};
222
223
224static const char *export_str(enum export ex)
225{
226 return export_list[ex].str;
227}
228
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100229static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700230{
231 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100232
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200233 if (!s)
234 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700235 for (i = 0; export_list[i].export != export_unknown; i++) {
236 if (strcmp(export_list[i].str, s) == 0)
237 return export_list[i].export;
238 }
239 return export_unknown;
240}
241
242static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
243{
244 if (sec == elf->export_sec)
245 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200246 else if (sec == elf->export_unused_sec)
247 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700248 else if (sec == elf->export_gpl_sec)
249 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200250 else if (sec == elf->export_unused_gpl_sec)
251 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700252 else if (sec == elf->export_gpl_future_sec)
253 return export_gpl_future;
254 else
255 return export_unknown;
256}
257
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100258/**
259 * Add an exported symbol - it may have already been added without a
260 * CRC, in this case just update the CRC
261 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700262static struct symbol *sym_add_exported(const char *name, struct module *mod,
263 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct symbol *s = find_symbol(name);
266
267 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700268 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100269 } else {
270 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100271 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100272 "was in %s%s\n", mod->name, name,
273 s->module->name,
274 is_vmlinux(s->module->name) ?"":".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700275 } else {
276 /* In case Modules.symvers was out of date */
277 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100280 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100281 s->vmlinux = is_vmlinux(mod->name);
282 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700283 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100284 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100287static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700288 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100289{
290 struct symbol *s = find_symbol(name);
291
292 if (!s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700293 s = new_symbol(name, mod, export);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100294 s->crc = crc;
295 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100298void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct stat st;
301 void *map;
302 int fd;
303
304 fd = open(filename, O_RDONLY);
305 if (fd < 0 || fstat(fd, &st) != 0)
306 return NULL;
307
308 *size = st.st_size;
309 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
310 close(fd);
311
312 if (map == MAP_FAILED)
313 return NULL;
314 return map;
315}
316
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100317/**
318 * Return a copy of the next line in a mmap'ed file.
319 * spaces in the beginning of the line is trimmed away.
320 * Return a pointer to a static buffer.
321 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100322char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 static char line[4096];
325 int skip = 1;
326 size_t len = 0;
327 signed char *p = (signed char *)file + *pos;
328 char *s = line;
329
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100330 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (skip && isspace(*p)) {
332 p++;
333 continue;
334 }
335 skip = 0;
336 if (*p != '\n' && (*pos < size)) {
337 len++;
338 *s++ = *p++;
339 if (len > 4095)
340 break; /* Too long, stop */
341 } else {
342 /* End of string */
343 *s = '\0';
344 return line;
345 }
346 }
347 /* End of buffer */
348 return NULL;
349}
350
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100351void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 munmap(file, size);
354}
355
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100356static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100359 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 Elf_Shdr *sechdrs;
361 Elf_Sym *sym;
362
363 hdr = grab_file(filename, &info->size);
364 if (!hdr) {
365 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200366 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100369 if (info->size < sizeof(*hdr)) {
370 /* file too small, assume this is an empty .o file */
371 return 0;
372 }
373 /* Is this a valid ELF file? */
374 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
375 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
376 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
377 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
378 /* Not an ELF file - silently ignore it */
379 return 0;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /* Fix endianness in ELF header */
382 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
383 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
384 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
385 hdr->e_machine = TO_NATIVE(hdr->e_machine);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900386 hdr->e_type = TO_NATIVE(hdr->e_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sechdrs = (void *)hdr + hdr->e_shoff;
388 info->sechdrs = sechdrs;
389
Petr Stetiara83710e2007-08-27 12:15:07 +0200390 /* Check if file offset is correct */
391 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100392 fatal("section header offset=%lu in file '%s' is bigger than "
393 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
394 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200395 return 0;
396 }
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* Fix endianness in section headers */
399 for (i = 0; i < hdr->e_shnum; i++) {
400 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
401 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
402 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
403 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
404 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900405 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
406 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 /* Find symbol table. */
409 for (i = 1; i < hdr->e_shnum; i++) {
410 const char *secstrings
411 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
Ram Paibd5cbce2006-06-08 22:12:53 -0700412 const char *secname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100414 if (sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100415 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
416 "sizeof(*hrd)=%zu\n", filename,
417 (unsigned long)sechdrs[i].sh_offset,
418 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100419 return 0;
420 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700421 secname = secstrings + sechdrs[i].sh_name;
422 if (strcmp(secname, ".modinfo") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
424 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700425 } else if (strcmp(secname, "__ksymtab") == 0)
426 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200427 else if (strcmp(secname, "__ksymtab_unused") == 0)
428 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700429 else if (strcmp(secname, "__ksymtab_gpl") == 0)
430 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200431 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
432 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700433 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
434 info->export_gpl_future_sec = i;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (sechdrs[i].sh_type != SHT_SYMTAB)
437 continue;
438
439 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100440 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100442 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sechdrs[sechdrs[i].sh_link].sh_offset;
444 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100445 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100446 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* Fix endianness in symbols */
449 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
450 sym->st_shndx = TO_NATIVE(sym->st_shndx);
451 sym->st_name = TO_NATIVE(sym->st_name);
452 sym->st_value = TO_NATIVE(sym->st_value);
453 sym->st_size = TO_NATIVE(sym->st_size);
454 }
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100455 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100458static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 release_file(info->hdr, info->size);
461}
462
Luke Yangf7b05e62006-03-02 18:35:31 +0800463#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
464#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100466static void handle_modversions(struct module *mod, struct elf_info *info,
467 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700470 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 switch (sym->st_shndx) {
473 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100474 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476 case SHN_ABS:
477 /* CRC'd symbol */
478 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
479 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700480 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
481 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 break;
484 case SHN_UNDEF:
485 /* undefined symbol */
486 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
487 ELF_ST_BIND(sym->st_info) != STB_WEAK)
488 break;
489 /* ignore global offset table */
490 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
491 break;
492 /* ignore __this_module, it will be resolved shortly */
493 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
494 break;
Ben Colline8d529012005-08-19 13:44:57 -0700495/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
496#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
497/* add compatibility with older glibc */
498#ifndef STT_SPARC_REGISTER
499#define STT_SPARC_REGISTER STT_REGISTER
500#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (info->hdr->e_machine == EM_SPARC ||
502 info->hdr->e_machine == EM_SPARCV9) {
503 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700504 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100506 if (symname[0] == '.') {
507 char *munged = strdup(symname);
508 munged[0] = '_';
509 munged[1] = toupper(munged[1]);
510 symname = munged;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100516 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
517 mod->unres =
518 alloc_symbol(symname +
519 strlen(MODULE_SYMBOL_PREFIX),
520 ELF_ST_BIND(sym->st_info) == STB_WEAK,
521 mod->unres);
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
524 default:
525 /* All exported symbols */
526 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700527 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
528 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
531 mod->has_init = 1;
532 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
533 mod->has_cleanup = 1;
534 break;
535 }
536}
537
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100538/**
539 * Parse tag=value strings from .modinfo section
540 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static char *next_string(char *string, unsigned long *secsize)
542{
543 /* Skip non-zero chars */
544 while (string[0]) {
545 string++;
546 if ((*secsize)-- <= 1)
547 return NULL;
548 }
549
550 /* Skip any zero padding. */
551 while (!string[0]) {
552 string++;
553 if ((*secsize)-- <= 1)
554 return NULL;
555 }
556 return string;
557}
558
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200559static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
560 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 char *p;
563 unsigned int taglen = strlen(tag);
564 unsigned long size = modinfo_len;
565
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200566 if (info) {
567 size -= info - (char *)modinfo;
568 modinfo = next_string(info, &size);
569 }
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 for (p = modinfo; p; p = next_string(p, &size)) {
572 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
573 return p + taglen + 1;
574 }
575 return NULL;
576}
577
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200578static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
579 const char *tag)
580
581{
582 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
583}
584
Sam Ravnborg93684d32006-02-19 11:53:35 +0100585/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100586 * Test if string s ends in string sub
587 * return 0 if match
588 **/
589static int strrcmp(const char *s, const char *sub)
590{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100591 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100592
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100593 if (!s || !sub)
594 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100595
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100596 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100597 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100598
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100599 if ((slen == 0) || (sublen == 0))
600 return 1;
601
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100602 if (sublen > slen)
603 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100604
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100605 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100606}
607
Sam Ravnborgff13f922008-01-23 19:54:27 +0100608static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
609{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100610 if (sym)
611 return elf->strtab + sym->st_name;
612 else
613 return "";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100614}
615
616static const char *sec_name(struct elf_info *elf, int shndx)
617{
618 Elf_Shdr *sechdrs = elf->sechdrs;
619 return (void *)elf->hdr +
620 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
621 sechdrs[shndx].sh_name;
622}
623
624static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
625{
626 return (void *)elf->hdr +
627 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
628 sechdr->sh_name;
629}
630
Sam Ravnborg10668222008-01-13 22:21:31 +0100631/* if sym is empty or point to a string
632 * like ".[0-9]+" then return 1.
633 * This is the optional prefix added by ld to some sections
634 */
635static int number_prefix(const char *sym)
636{
637 if (*sym++ == '\0')
638 return 1;
639 if (*sym != '.')
640 return 0;
641 do {
642 char c = *sym++;
643 if (c < '0' || c > '9')
644 return 0;
645 } while (*sym);
646 return 1;
647}
648
649/* The pattern is an array of simple patterns.
650 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100651 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100652 * "foo*" will match a string that begins with "foo"
653 * "foo$" will match a string equal to "foo" or "foo.1"
654 * where the '1' can be any number including several digits.
655 * The $ syntax is for sections where ld append a dot number
656 * to make section name unique.
657 */
658int match(const char *sym, const char * const pat[])
659{
660 const char *p;
661 while (*pat) {
662 p = *pat++;
663 const char *endp = p + strlen(p) - 1;
664
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100665 /* "*foo" */
666 if (*p == '*') {
667 if (strrcmp(sym, p + 1) == 0)
668 return 1;
669 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100670 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100671 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100672 if (strncmp(sym, p, strlen(p) - 1) == 0)
673 return 1;
674 }
675 /* "foo$" */
676 else if (*endp == '$') {
677 if (strncmp(sym, p, strlen(p) - 1) == 0) {
678 if (number_prefix(sym + strlen(p) - 1))
679 return 1;
680 }
681 }
682 /* no wildcards */
683 else {
684 if (strcmp(p, sym) == 0)
685 return 1;
686 }
687 }
688 /* no match */
689 return 0;
690}
691
Sam Ravnborg10668222008-01-13 22:21:31 +0100692/* sections that we do not want to do full section mismatch check on */
693static const char *section_white_list[] =
694 { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL };
695
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100696#define ALL_INIT_DATA_SECTIONS \
697 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
698#define ALL_EXIT_DATA_SECTIONS \
699 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100700
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100701#define ALL_INIT_TEXT_SECTIONS \
702 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
703#define ALL_EXIT_TEXT_SECTIONS \
704 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100705
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100706#define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS
707#define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100708
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100709#define DATA_SECTIONS ".data$", ".data.rel$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100710#define TEXT_SECTIONS ".text$"
711
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100712#define INIT_SECTIONS ".init.data$", ".init.text$"
713#define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$"
714#define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$"
715#define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$"
716
717#define EXIT_SECTIONS ".exit.data$", ".exit.text$"
718#define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$"
719#define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$"
720#define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$"
721
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100722/* init data sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100723static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100724
725/* all init sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100726static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100727
728/* All init and exit sections (code + data) */
729static const char *init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100730 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100731
732/* data section */
733static const char *data_sections[] = { DATA_SECTIONS, NULL };
734
735/* sections that may refer to an init/exit section with no warning */
736static const char *initref_sections[] =
737{
738 ".text.init.refok*",
739 ".exit.text.refok*",
740 ".data.init.refok*",
741 NULL
742};
743
744
745/* symbols in .data that may refer to init/exit sections */
746static const char *symbol_white_list[] =
747{
748 "*driver",
749 "*_template", /* scsi uses *_template a lot */
750 "*_timer", /* arm uses ops structures named _timer a lot */
751 "*_sht", /* scsi also used *_sht to some extent */
752 "*_ops",
753 "*_probe",
754 "*_probe_one",
755 "*_console",
756 NULL
757};
758
759static const char *head_sections[] = { ".head.text*", NULL };
760static const char *linker_symbols[] =
761 { "__init_begin", "_sinittext", "_einittext", NULL };
762
Sam Ravnborg10668222008-01-13 22:21:31 +0100763struct sectioncheck {
764 const char *fromsec[20];
765 const char *tosec[20];
766};
767
768const struct sectioncheck sectioncheck[] = {
769/* Do not reference init/exit code/data from
770 * normal code and data
771 */
772{
773 .fromsec = { TEXT_SECTIONS, DATA_SECTIONS, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100774 .tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }
775},
776/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
777{
778 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
779 .tosec = { INIT_SECTIONS, NULL }
780},
781/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
782{
783 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
784 .tosec = { EXIT_SECTIONS, NULL }
Sam Ravnborg10668222008-01-13 22:21:31 +0100785},
786/* Do not use exit code/data from init code */
787{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100788 .fromsec = { ALL_INIT_SECTIONS, NULL },
789 .tosec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +0100790},
791/* Do not use init code/data from exit code */
792{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100793 .fromsec = { ALL_EXIT_SECTIONS, NULL },
794 .tosec = { ALL_INIT_SECTIONS, NULL }
Sam Ravnborg10668222008-01-13 22:21:31 +0100795},
796/* Do not export init/exit functions or data */
797{
798 .fromsec = { "__ksymtab*", NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100799 .tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }
Sam Ravnborg10668222008-01-13 22:21:31 +0100800}
801};
802
803static int section_mismatch(const char *fromsec, const char *tosec)
804{
805 int i;
806 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
807 const struct sectioncheck *check = &sectioncheck[0];
808
809 for (i = 0; i < elems; i++) {
810 if (match(fromsec, check->fromsec) &&
811 match(tosec, check->tosec))
812 return 1;
813 check++;
814 }
815 return 0;
816}
817
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100818/**
819 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200820 *
821 * Pattern 0:
822 * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
823 * The pattern is identified by:
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200824 * fromsec = .text.init.refok* | .data.init.refok*
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200825 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100826 * Pattern 1:
827 * If a module parameter is declared __initdata and permissions=0
828 * then this is legal despite the warning generated.
829 * We cannot see value of permissions here, so just ignore
830 * this pattern.
831 * The pattern is identified by:
832 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100833 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100834 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100835 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100836 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700837 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100838 * add, remove, probe functions etc.
839 * These functions may often be marked __init and we do not want to
840 * warn here.
841 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +0200842 * tosec = init or exit section
843 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100844 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
845 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +0100846 *
847 * Pattern 3:
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100848 * Whitelist all refereces from .text.head to .init.data
849 * Whitelist all refereces from .text.head to .init.text
850 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200851 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +0100852 * Some symbols belong to init section but still it is ok to reference
853 * these from non-init sections as these symbols don't have any memory
854 * allocated for them and symbol address and value are same. So even
855 * if init section is freed, its ok to reference those symbols.
856 * For ex. symbols marking the init section boundaries.
857 * This pattern is identified by
858 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100859 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100860 **/
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100861static int secref_whitelist(const char *fromsec, const char *fromsym,
862 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100863{
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200864 /* Check for pattern 0 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100865 if (match(fromsec, initref_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100866 return 0;
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200867
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100868 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100869 if (match(tosec, init_data_sections) &&
870 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100871 (strncmp(fromsym, "__param", strlen("__param")) == 0))
872 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100873
874 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100875 if (match(tosec, init_exit_sections) &&
876 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100877 match(fromsym, symbol_white_list))
878 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100879
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100880 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100881 if (match(fromsec, head_sections) &&
882 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100883 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100884
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200885 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100886 if (match(tosym, linker_symbols))
887 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100888
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100889 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100890}
891
892/**
Sam Ravnborg93684d32006-02-19 11:53:35 +0100893 * Find symbol based on relocation record info.
894 * In some cases the symbol supplied is a valid symbol so
895 * return refsym. If st_name != 0 we assume this is a valid symbol.
896 * In other cases the symbol needs to be looked up in the symbol table
897 * based on section and address.
898 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100899static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100900 Elf_Sym *relsym)
901{
902 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100903 Elf_Sym *near = NULL;
904 Elf64_Sword distance = 20;
905 Elf64_Sword d;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100906
907 if (relsym->st_name != 0)
908 return relsym;
909 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
910 if (sym->st_shndx != relsym->st_shndx)
911 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900912 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
913 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100914 if (sym->st_value == addr)
915 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100916 /* Find a symbol nearby - addr are maybe negative */
917 d = sym->st_value - addr;
918 if (d < 0)
919 d = addr - sym->st_value;
920 if (d < distance) {
921 distance = d;
922 near = sym;
923 }
Sam Ravnborg93684d32006-02-19 11:53:35 +0100924 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +0100925 /* We need a close match */
926 if (distance < 20)
927 return near;
928 else
929 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100930}
931
David Brownellda68d612007-02-20 13:58:16 -0800932static inline int is_arm_mapping_symbol(const char *str)
933{
934 return str[0] == '$' && strchr("atd", str[1])
935 && (str[2] == '\0' || str[2] == '.');
936}
937
938/*
939 * If there's no name there, ignore it; likewise, ignore it if it's
940 * one of the magic symbols emitted used by current ARM tools.
941 *
942 * Otherwise if find_symbols_between() returns those symbols, they'll
943 * fail the whitelist tests and cause lots of false alarms ... fixable
944 * only by merging __exit and __init sections into __text, bloating
945 * the kernel (which is especially evil on embedded platforms).
946 */
947static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
948{
949 const char *name = elf->strtab + sym->st_name;
950
951 if (!name || !strlen(name))
952 return 0;
953 return !is_arm_mapping_symbol(name);
954}
955
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100956/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100957 * Find symbols before or equal addr and after addr - in the section sec.
958 * If we find two symbols with equal offset prefer one with a valid name.
959 * The ELF format may have a better way to detect what type of symbol
960 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100961 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +0100962static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
963 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100964{
965 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +0100966 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +0100967 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100968
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100969 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
970 const char *symsec;
971
972 if (sym->st_shndx >= SHN_LORESERVE)
973 continue;
Sam Ravnborgff13f922008-01-23 19:54:27 +0100974 symsec = sec_name(elf, sym->st_shndx);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100975 if (strcmp(symsec, sec) != 0)
976 continue;
David Brownellda68d612007-02-20 13:58:16 -0800977 if (!is_valid_name(elf, sym))
978 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100979 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +0100980 if ((addr - sym->st_value) < distance) {
981 distance = addr - sym->st_value;
982 near = sym;
983 } else if ((addr - sym->st_value) == distance) {
984 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100985 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100986 }
987 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +0100988 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100989}
990
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100991/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100992 * Print a warning about a section mismatch.
993 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100994 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100995 */
996static void report_sec_mismatch(const char *modname,
997 const char *fromsec,
998 unsigned long long fromaddr,
999 const char *fromsym,
1000 const char *tosec, const char *tosym)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001001{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001002 if (strlen(tosym)) {
Russell King256012092007-05-10 23:03:25 +01001003 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001004 "in '%s'\n",
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001005 modname, fromsec, fromaddr,
1006 tosec, tosym, fromsym);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001007 } else {
Russell King256012092007-05-10 23:03:25 +01001008 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001009 modname, fromsec, fromaddr,
1010 tosec, tosym);
1011 }
1012}
1013
1014static void check_section_mismatch(const char *modname, struct elf_info *elf,
1015 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1016{
1017 const char *tosec;
1018
1019 tosec = sec_name(elf, sym->st_shndx);
1020 if (section_mismatch(fromsec, tosec)) {
1021 const char *fromsym;
1022 const char *tosym;
1023
1024 fromsym = sym_name(elf,
1025 find_elf_symbol2(elf, r->r_offset, fromsec));
1026 tosym = sym_name(elf,
1027 find_elf_symbol(elf, r->r_addend, sym));
1028
1029 /* check whitelist - we may ignore it */
1030 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
1031 report_sec_mismatch(modname, fromsec, r->r_offset,
1032 fromsym, tosec, tosym);
1033 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001034 }
1035}
1036
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001037static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001038 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001039{
1040 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001041 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001042
1043 return (void *)elf->hdr + sechdrs[section].sh_offset +
1044 (r->r_offset - sechdrs[section].sh_addr);
1045}
1046
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001047static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001048{
1049 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001050 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001051
1052 switch (r_typ) {
1053 case R_386_32:
1054 r->r_addend = TO_NATIVE(*location);
1055 break;
1056 case R_386_PC32:
1057 r->r_addend = TO_NATIVE(*location) + 4;
1058 /* For CONFIG_RELOCATABLE=y */
1059 if (elf->hdr->e_type == ET_EXEC)
1060 r->r_addend += r->r_offset;
1061 break;
1062 }
1063 return 0;
1064}
1065
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001066static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001067{
1068 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1069
1070 switch (r_typ) {
1071 case R_ARM_ABS32:
1072 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001073 r->r_addend = (int)(long)
1074 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001075 break;
1076 case R_ARM_PC24:
1077 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001078 r->r_addend = (int)(long)(elf->hdr +
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001079 sechdr->sh_offset +
1080 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001081 break;
1082 default:
1083 return 1;
1084 }
1085 return 0;
1086}
1087
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001088static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001089{
1090 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001091 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001092 unsigned int inst;
1093
1094 if (r_typ == R_MIPS_HI16)
1095 return 1; /* skip this */
1096 inst = TO_NATIVE(*location);
1097 switch (r_typ) {
1098 case R_MIPS_LO16:
1099 r->r_addend = inst & 0xffff;
1100 break;
1101 case R_MIPS_26:
1102 r->r_addend = (inst & 0x03ffffff) << 2;
1103 break;
1104 case R_MIPS_32:
1105 r->r_addend = inst;
1106 break;
1107 }
1108 return 0;
1109}
1110
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001111static void section_rela(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001112 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001113{
1114 Elf_Sym *sym;
1115 Elf_Rela *rela;
1116 Elf_Rela r;
1117 unsigned int r_sym;
1118 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001119
Sam Ravnborgff13f922008-01-23 19:54:27 +01001120 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001121 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1122
Sam Ravnborgff13f922008-01-23 19:54:27 +01001123 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001124 fromsec += strlen(".rela");
1125 /* if from section (name) is know good then skip it */
Sam Ravnborg10668222008-01-13 22:21:31 +01001126 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001127 return;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001128 for (rela = start; rela < stop; rela++) {
1129 r.r_offset = TO_NATIVE(rela->r_offset);
1130#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001131 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001132 unsigned int r_typ;
1133 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1134 r_sym = TO_NATIVE(r_sym);
1135 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1136 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1137 } else {
1138 r.r_info = TO_NATIVE(rela->r_info);
1139 r_sym = ELF_R_SYM(r.r_info);
1140 }
1141#else
1142 r.r_info = TO_NATIVE(rela->r_info);
1143 r_sym = ELF_R_SYM(r.r_info);
1144#endif
1145 r.r_addend = TO_NATIVE(rela->r_addend);
1146 sym = elf->symtab_start + r_sym;
1147 /* Skip special sections */
1148 if (sym->st_shndx >= SHN_LORESERVE)
1149 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001150 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001151 }
1152}
1153
1154static void section_rel(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001155 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001156{
1157 Elf_Sym *sym;
1158 Elf_Rel *rel;
1159 Elf_Rela r;
1160 unsigned int r_sym;
1161 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001162
Sam Ravnborgff13f922008-01-23 19:54:27 +01001163 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001164 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1165
Sam Ravnborgff13f922008-01-23 19:54:27 +01001166 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001167 fromsec += strlen(".rel");
1168 /* if from section (name) is know good then skip it */
Sam Ravnborg10668222008-01-13 22:21:31 +01001169 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001170 return;
1171
1172 for (rel = start; rel < stop; rel++) {
1173 r.r_offset = TO_NATIVE(rel->r_offset);
1174#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001175 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001176 unsigned int r_typ;
1177 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1178 r_sym = TO_NATIVE(r_sym);
1179 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1180 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1181 } else {
1182 r.r_info = TO_NATIVE(rel->r_info);
1183 r_sym = ELF_R_SYM(r.r_info);
1184 }
1185#else
1186 r.r_info = TO_NATIVE(rel->r_info);
1187 r_sym = ELF_R_SYM(r.r_info);
1188#endif
1189 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001190 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001191 case EM_386:
1192 if (addend_386_rel(elf, sechdr, &r))
1193 continue;
1194 break;
1195 case EM_ARM:
1196 if (addend_arm_rel(elf, sechdr, &r))
1197 continue;
1198 break;
1199 case EM_MIPS:
1200 if (addend_mips_rel(elf, sechdr, &r))
1201 continue;
1202 break;
1203 }
1204 sym = elf->symtab_start + r_sym;
1205 /* Skip special sections */
1206 if (sym->st_shndx >= SHN_LORESERVE)
1207 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001208 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001209 }
1210}
1211
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001212/**
1213 * A module includes a number of sections that are discarded
1214 * either when loaded or when used as built-in.
1215 * For loaded modules all functions marked __init and all data
1216 * marked __initdata will be discarded when the module has been intialized.
1217 * Likewise for modules used built-in the sections marked __exit
1218 * are discarded because __exit marked function are supposed to be called
1219 * only when a moduel is unloaded which never happes for built-in modules.
1220 * The check_sec_ref() function traverses all relocation records
1221 * to find all references to a section that reference a section that will
1222 * be discarded and warns about it.
1223 **/
1224static void check_sec_ref(struct module *mod, const char *modname,
Sam Ravnborg10668222008-01-13 22:21:31 +01001225 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001226{
1227 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001228 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001229
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001230 /* Walk through all sections */
Sam Ravnborgff13f922008-01-23 19:54:27 +01001231 for (i = 0; i < elf->hdr->e_shnum; i++) {
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001232 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001233 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001234 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001235 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001236 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001237 }
1238}
1239
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001240static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 const char *symname;
1243 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001244 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct module *mod;
1246 struct elf_info info = { };
1247 Elf_Sym *sym;
1248
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001249 if (!parse_elf(&info, modname))
1250 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 mod = new_module(modname);
1253
1254 /* When there's no vmlinux, don't print warnings about
1255 * unresolved symbols (since there'll be too many ;) */
1256 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 mod->skip = 1;
1259 }
1260
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001261 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1262 while (license) {
1263 if (license_is_gpl_compatible(license))
1264 mod->gpl_compatible = 1;
1265 else {
1266 mod->gpl_compatible = 0;
1267 break;
1268 }
1269 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1270 "license", license);
1271 }
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1274 symname = info.strtab + sym->st_name;
1275
1276 handle_modversions(mod, &info, sym, symname);
1277 handle_moddevtable(mod, &info, sym, symname);
1278 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001279 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001280 (is_vmlinux(modname) && vmlinux_section_warnings))
1281 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1284 if (version)
1285 maybe_frob_rcs_version(modname, version, info.modinfo,
1286 version - (char *)info.hdr);
1287 if (version || (all_versions && !is_vmlinux(modname)))
1288 get_src_version(modname, mod->srcversion,
1289 sizeof(mod->srcversion)-1);
1290
1291 parse_elf_finish(&info);
1292
1293 /* Our trick to get versioning for struct_module - it's
1294 * never passed as an argument to an exported function, so
1295 * the automatic versioning doesn't pick it up, but it's really
1296 * important anyhow */
1297 if (modversions)
1298 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1299}
1300
1301#define SZ 500
1302
1303/* We first write the generated file into memory using the
1304 * following helper, then compare to the file on disk and
1305 * only update the later if anything changed */
1306
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001307void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1308 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 char tmp[SZ];
1311 int len;
1312 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 va_start(ap, fmt);
1315 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001316 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 va_end(ap);
1318}
1319
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001320void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001323 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 buf->p = realloc(buf->p, buf->size);
1325 }
1326 strncpy(buf->p + buf->pos, s, len);
1327 buf->pos += len;
1328}
1329
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001330static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1331{
1332 const char *e = is_vmlinux(m) ?"":".ko";
1333
1334 switch (exp) {
1335 case export_gpl:
1336 fatal("modpost: GPL-incompatible module %s%s "
1337 "uses GPL-only symbol '%s'\n", m, e, s);
1338 break;
1339 case export_unused_gpl:
1340 fatal("modpost: GPL-incompatible module %s%s "
1341 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1342 break;
1343 case export_gpl_future:
1344 warn("modpost: GPL-incompatible module %s%s "
1345 "uses future GPL-only symbol '%s'\n", m, e, s);
1346 break;
1347 case export_plain:
1348 case export_unused:
1349 case export_unknown:
1350 /* ignore */
1351 break;
1352 }
1353}
1354
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001355static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001356{
1357 const char *e = is_vmlinux(m) ?"":".ko";
1358
1359 switch (exp) {
1360 case export_unused:
1361 case export_unused_gpl:
1362 warn("modpost: module %s%s "
1363 "uses symbol '%s' marked UNUSED\n", m, e, s);
1364 break;
1365 default:
1366 /* ignore */
1367 break;
1368 }
1369}
1370
1371static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001372{
1373 struct symbol *s, *exp;
1374
1375 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001376 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001377 exp = find_symbol(s->name);
1378 if (!exp || exp->module == mod)
1379 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001380 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001381 if (basename)
1382 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001383 else
1384 basename = mod->name;
1385 if (!mod->gpl_compatible)
1386 check_for_gpl_usage(exp->export, basename, exp->name);
1387 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001388 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001389}
1390
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001391/**
1392 * Header for the generated file
1393 **/
1394static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 buf_printf(b, "#include <linux/module.h>\n");
1397 buf_printf(b, "#include <linux/vermagic.h>\n");
1398 buf_printf(b, "#include <linux/compiler.h>\n");
1399 buf_printf(b, "\n");
1400 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1401 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 buf_printf(b, "struct module __this_module\n");
1403 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001404 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (mod->has_init)
1406 buf_printf(b, " .init = init_module,\n");
1407 if (mod->has_cleanup)
1408 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1409 " .exit = cleanup_module,\n"
1410 "#endif\n");
Roman Zippele61a1c12007-05-09 02:35:15 -07001411 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 buf_printf(b, "};\n");
1413}
1414
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001415/**
1416 * Record CRCs for unresolved symbols
1417 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001418static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001421 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 for (s = mod->unres; s; s = s->next) {
1424 exp = find_symbol(s->name);
1425 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001426 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001427 if (warn_unresolved) {
1428 warn("\"%s\" [%s.ko] undefined!\n",
1429 s->name, mod->name);
1430 } else {
1431 merror("\"%s\" [%s.ko] undefined!\n",
1432 s->name, mod->name);
1433 err = 1;
1434 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 continue;
1437 }
1438 s->module = exp->module;
1439 s->crc_valid = exp->crc_valid;
1440 s->crc = exp->crc;
1441 }
1442
1443 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001444 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 buf_printf(b, "\n");
1447 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1448 buf_printf(b, "__attribute_used__\n");
1449 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1450
1451 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001452 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001455 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 s->name, mod->name);
1457 continue;
1458 }
1459 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1460 }
1461
1462 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001463
1464 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001467static void add_depends(struct buffer *b, struct module *mod,
1468 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 struct symbol *s;
1471 struct module *m;
1472 int first = 1;
1473
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001474 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 buf_printf(b, "\n");
1478 buf_printf(b, "static const char __module_depends[]\n");
1479 buf_printf(b, "__attribute_used__\n");
1480 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1481 buf_printf(b, "\"depends=");
1482 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001483 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (!s->module)
1485 continue;
1486
1487 if (s->module->seen)
1488 continue;
1489
1490 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001491 p = strrchr(s->module->name, '/');
1492 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001493 p++;
1494 else
1495 p = s->module->name;
1496 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 first = 0;
1498 }
1499 buf_printf(b, "\";\n");
1500}
1501
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001502static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 if (mod->srcversion[0]) {
1505 buf_printf(b, "\n");
1506 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1507 mod->srcversion);
1508 }
1509}
1510
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001511static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 char *tmp;
1514 FILE *file;
1515 struct stat st;
1516
1517 file = fopen(fname, "r");
1518 if (!file)
1519 goto write;
1520
1521 if (fstat(fileno(file), &st) < 0)
1522 goto close_write;
1523
1524 if (st.st_size != b->pos)
1525 goto close_write;
1526
1527 tmp = NOFAIL(malloc(b->pos));
1528 if (fread(tmp, 1, b->pos, file) != b->pos)
1529 goto free_write;
1530
1531 if (memcmp(tmp, b->p, b->pos) != 0)
1532 goto free_write;
1533
1534 free(tmp);
1535 fclose(file);
1536 return;
1537
1538 free_write:
1539 free(tmp);
1540 close_write:
1541 fclose(file);
1542 write:
1543 file = fopen(fname, "w");
1544 if (!file) {
1545 perror(fname);
1546 exit(1);
1547 }
1548 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1549 perror(fname);
1550 exit(1);
1551 }
1552 fclose(file);
1553}
1554
Ram Paibd5cbce2006-06-08 22:12:53 -07001555/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001556 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001557 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001558static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 unsigned long size, pos = 0;
1561 void *file = grab_file(fname, &size);
1562 char *line;
1563
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001564 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* No symbol versions, silently ignore */
1566 return;
1567
1568 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001569 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 unsigned int crc;
1571 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001572 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 if (!(symname = strchr(line, '\t')))
1575 goto fail;
1576 *symname++ = '\0';
1577 if (!(modname = strchr(symname, '\t')))
1578 goto fail;
1579 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001580 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001581 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001582 if (export && ((end = strchr(export, '\t')) != NULL))
1583 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 crc = strtoul(line, &d, 16);
1585 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1586 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001587 mod = find_module(modname);
1588 if (!mod) {
1589 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 mod = new_module(NOFAIL(strdup(modname)));
1592 mod->skip = 1;
1593 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001594 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001595 s->kernel = kernel;
1596 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001597 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599 return;
1600fail:
1601 fatal("parse error in symbol dump file\n");
1602}
1603
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001604/* For normal builds always dump all symbols.
1605 * For external modules only dump symbols
1606 * that are not read from kernel Module.symvers.
1607 **/
1608static int dump_sym(struct symbol *sym)
1609{
1610 if (!external_module)
1611 return 1;
1612 if (sym->vmlinux || sym->kernel)
1613 return 0;
1614 return 1;
1615}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001616
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001617static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 struct buffer buf = { };
1620 struct symbol *symbol;
1621 int n;
1622
1623 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1624 symbol = symbolhash[n];
1625 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001626 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001627 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001628 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001629 symbol->module->name,
1630 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 symbol = symbol->next;
1632 }
1633 }
1634 write_if_changed(&buf, fname);
1635}
1636
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001637int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 struct module *mod;
1640 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001641 char *kernel_read = NULL, *module_read = NULL;
1642 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001644 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Sam Ravnborg8d8d8282007-07-20 22:36:56 +02001646 while ((opt = getopt(argc, argv, "i:I:mso:aw")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001647 switch (opt) {
1648 case 'i':
1649 kernel_read = optarg;
1650 break;
1651 case 'I':
1652 module_read = optarg;
1653 external_module = 1;
1654 break;
1655 case 'm':
1656 modversions = 1;
1657 break;
1658 case 'o':
1659 dump_write = optarg;
1660 break;
1661 case 'a':
1662 all_versions = 1;
1663 break;
1664 case 's':
1665 vmlinux_section_warnings = 0;
1666 break;
1667 case 'w':
1668 warn_unresolved = 1;
1669 break;
1670 default:
1671 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673 }
1674
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001675 if (kernel_read)
1676 read_dump(kernel_read, 1);
1677 if (module_read)
1678 read_dump(module_read, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001680 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 for (mod = modules; mod; mod = mod->next) {
1684 if (mod->skip)
1685 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001686 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001687 }
1688
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001689 err = 0;
1690
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001691 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01001692 char fname[strlen(mod->name) + 10];
1693
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001694 if (mod->skip)
1695 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 buf.pos = 0;
1698
1699 add_header(&buf, mod);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001700 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 add_depends(&buf, mod, modules);
1702 add_moddevtable(&buf, mod);
1703 add_srcversion(&buf, mod);
1704
1705 sprintf(fname, "%s.mod.c", mod->name);
1706 write_if_changed(&buf, fname);
1707 }
1708
1709 if (dump_write)
1710 write_dump(dump_write);
1711
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001712 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}