blob: 46660a4f9e224b44c5934ad132eadfa0101b4771 [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 Ravnborg2f5ee612007-07-25 21:46:40 +0200608/*
609 * Functions used only during module init is marked __init and is stored in
610 * a .init.text section. Likewise data is marked __initdata and stored in
611 * a .init.data section.
612 * If this section is one of these sections return 1
613 * See include/linux/init.h for the details
614 */
615static int init_section(const char *name)
616{
617 if (strcmp(name, ".init") == 0)
618 return 1;
619 if (strncmp(name, ".init.", strlen(".init.")) == 0)
620 return 1;
621 return 0;
622}
623
624/*
625 * Functions used only during module exit is marked __exit and is stored in
626 * a .exit.text section. Likewise data is marked __exitdata and stored in
627 * a .exit.data section.
628 * If this section is one of these sections return 1
629 * See include/linux/init.h for the details
630 **/
631static int exit_section(const char *name)
632{
633 if (strcmp(name, ".exit.text") == 0)
634 return 1;
635 if (strcmp(name, ".exit.data") == 0)
636 return 1;
637 return 0;
638
639}
640
641/*
642 * Data sections are named like this:
643 * .data | .data.rel | .data.rel.*
644 * Return 1 if the specified section is a data section
645 */
646static int data_section(const char *name)
647{
648 if ((strcmp(name, ".data") == 0) ||
649 (strcmp(name, ".data.rel") == 0) ||
650 (strncmp(name, ".data.rel.", strlen(".data.rel.")) == 0))
651 return 1;
652 else
653 return 0;
654}
655
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100656/**
657 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200658 *
659 * Pattern 0:
660 * Do not warn if funtion/data are marked with __init_refok/__initdata_refok.
661 * The pattern is identified by:
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200662 * fromsec = .text.init.refok* | .data.init.refok*
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200663 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100664 * Pattern 1:
665 * If a module parameter is declared __initdata and permissions=0
666 * then this is legal despite the warning generated.
667 * We cannot see value of permissions here, so just ignore
668 * this pattern.
669 * The pattern is identified by:
670 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100671 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100672 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100673 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100674 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700675 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100676 * add, remove, probe functions etc.
677 * These functions may often be marked __init and we do not want to
678 * warn here.
679 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +0200680 * tosec = init or exit section
681 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100682 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
683 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +0100684 *
685 * Pattern 3:
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100686 * Whitelist all refereces from .text.head to .init.data
687 * Whitelist all refereces from .text.head to .init.text
688 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200689 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +0100690 * Some symbols belong to init section but still it is ok to reference
691 * these from non-init sections as these symbols don't have any memory
692 * allocated for them and symbol address and value are same. So even
693 * if init section is freed, its ok to reference those symbols.
694 * For ex. symbols marking the init section boundaries.
695 * This pattern is identified by
696 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100697 *
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200698 * Pattern 5:
699 * Xtensa uses literal sections for constants that are accessed PC-relative.
700 * Literal sections may safely reference their text sections.
701 * (Note that the name for the literal section omits any trailing '.text')
702 * tosec = <section>[.text]
703 * fromsec = <section>.literal
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100704 **/
Magnus Damm9e157a52006-08-08 17:32:11 +0900705static int secref_whitelist(const char *modname, const char *tosec,
Vivek Goyalee6a8542007-01-11 01:52:44 +0100706 const char *fromsec, const char *atsym,
707 const char *refsymname)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100708{
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200709 int len;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100710 const char **s;
711 const char *pat2sym[] = {
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700712 "driver",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200713 "_template", /* scsi uses *_template a lot */
Sam Ravnborg1e29a702007-06-03 22:19:24 +0200714 "_timer", /* arm uses ops structures named _timer a lot */
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +0200715 "_sht", /* scsi also used *_sht to some extent */
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100716 "_ops",
717 "_probe",
718 "_probe_one",
Vivek Goyal118c0ac2007-01-11 01:52:44 +0100719 "_console",
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100720 NULL
721 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100722
Vivek Goyalee6a8542007-01-11 01:52:44 +0100723 const char *pat3refsym[] = {
724 "__init_begin",
725 "_sinittext",
726 "_einittext",
727 NULL
728 };
729
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200730 /* Check for pattern 0 */
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200731 if ((strncmp(fromsec, ".text.init.refok", strlen(".text.init.refok")) == 0) ||
Pavel Emelyanov46650792007-10-08 20:38:39 -0700732 (strncmp(fromsec, ".exit.text.refok", strlen(".exit.text.refok")) == 0) ||
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200733 (strncmp(fromsec, ".data.init.refok", strlen(".data.init.refok")) == 0))
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200734 return 1;
735
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100736 /* Check for pattern 1 */
Sam Ravnborg83cda2b2007-07-25 21:52:31 +0200737 if ((strcmp(tosec, ".init.data") == 0) &&
738 (strncmp(fromsec, ".data", strlen(".data")) == 0) &&
739 (strncmp(atsym, "__param", strlen("__param")) == 0))
740 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100741
742 /* Check for pattern 2 */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100743 if ((init_section(tosec) || exit_section(tosec))
744 && data_section(fromsec))
Sam Ravnborg83cda2b2007-07-25 21:52:31 +0200745 for (s = pat2sym; *s; s++)
746 if (strrcmp(atsym, *s) == 0)
747 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100748
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100749 /* Check for pattern 3 */
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100750 if ((strcmp(fromsec, ".text.head") == 0) &&
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100751 ((strcmp(tosec, ".init.data") == 0) ||
752 (strcmp(tosec, ".init.text") == 0)))
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100753 return 1;
754
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200755 /* Check for pattern 4 */
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100756 for (s = pat3refsym; *s; s++)
757 if (strcmp(refsymname, *s) == 0)
758 return 1;
759
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200760 /* Check for pattern 5 */
761 if (strrcmp(tosec, ".text") == 0)
762 len = strlen(tosec) - strlen(".text");
763 else
764 len = strlen(tosec);
765 if ((strncmp(tosec, fromsec, len) == 0) && (strlen(fromsec) > len) &&
766 (strcmp(fromsec + len, ".literal") == 0))
767 return 1;
768
Sam Ravnborg93659af2006-08-09 08:23:55 +0200769 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100770}
771
772/**
Sam Ravnborg93684d32006-02-19 11:53:35 +0100773 * Find symbol based on relocation record info.
774 * In some cases the symbol supplied is a valid symbol so
775 * return refsym. If st_name != 0 we assume this is a valid symbol.
776 * In other cases the symbol needs to be looked up in the symbol table
777 * based on section and address.
778 * **/
779static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
780 Elf_Sym *relsym)
781{
782 Elf_Sym *sym;
783
784 if (relsym->st_name != 0)
785 return relsym;
786 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
787 if (sym->st_shndx != relsym->st_shndx)
788 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900789 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
790 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +0100791 if (sym->st_value == addr)
792 return sym;
793 }
794 return NULL;
795}
796
David Brownellda68d612007-02-20 13:58:16 -0800797static inline int is_arm_mapping_symbol(const char *str)
798{
799 return str[0] == '$' && strchr("atd", str[1])
800 && (str[2] == '\0' || str[2] == '.');
801}
802
803/*
804 * If there's no name there, ignore it; likewise, ignore it if it's
805 * one of the magic symbols emitted used by current ARM tools.
806 *
807 * Otherwise if find_symbols_between() returns those symbols, they'll
808 * fail the whitelist tests and cause lots of false alarms ... fixable
809 * only by merging __exit and __init sections into __text, bloating
810 * the kernel (which is especially evil on embedded platforms).
811 */
812static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
813{
814 const char *name = elf->strtab + sym->st_name;
815
816 if (!name || !strlen(name))
817 return 0;
818 return !is_arm_mapping_symbol(name);
819}
820
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100821/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100822 * Find symbols before or equal addr and after addr - in the section sec.
823 * If we find two symbols with equal offset prefer one with a valid name.
824 * The ELF format may have a better way to detect what type of symbol
825 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100826 **/
827static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
828 const char *sec,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100829 Elf_Sym **before, Elf_Sym **after)
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100830{
831 Elf_Sym *sym;
832 Elf_Ehdr *hdr = elf->hdr;
833 Elf_Addr beforediff = ~0;
834 Elf_Addr afterdiff = ~0;
835 const char *secstrings = (void *)hdr +
836 elf->sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100837
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100838 *before = NULL;
839 *after = NULL;
840
841 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
842 const char *symsec;
843
844 if (sym->st_shndx >= SHN_LORESERVE)
845 continue;
846 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
847 if (strcmp(symsec, sec) != 0)
848 continue;
David Brownellda68d612007-02-20 13:58:16 -0800849 if (!is_valid_name(elf, sym))
850 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100851 if (sym->st_value <= addr) {
852 if ((addr - sym->st_value) < beforediff) {
853 beforediff = addr - sym->st_value;
854 *before = sym;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100855 } else if ((addr - sym->st_value) == beforediff) {
David Brownellda68d612007-02-20 13:58:16 -0800856 *before = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +0100857 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100858 } else {
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100859 if ((sym->st_value - addr) < afterdiff) {
860 afterdiff = sym->st_value - addr;
861 *after = sym;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100862 } else if ((sym->st_value - addr) == afterdiff)
David Brownellda68d612007-02-20 13:58:16 -0800863 *after = sym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100864 }
865 }
866}
867
868/**
869 * Print a warning about a section mismatch.
870 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100871 * Check whitelist before warning - it may be a false positive.
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100872 **/
873static void warn_sec_mismatch(const char *modname, const char *fromsec,
874 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
875{
Sam Ravnborg93684d32006-02-19 11:53:35 +0100876 const char *refsymname = "";
877 Elf_Sym *before, *after;
878 Elf_Sym *refsym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100879 Elf_Ehdr *hdr = elf->hdr;
880 Elf_Shdr *sechdrs = elf->sechdrs;
881 const char *secstrings = (void *)hdr +
882 sechdrs[hdr->e_shstrndx].sh_offset;
883 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100884
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100885 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
886
Sam Ravnborg93684d32006-02-19 11:53:35 +0100887 refsym = find_elf_symbol(elf, r.r_addend, sym);
888 if (refsym && strlen(elf->strtab + refsym->st_name))
889 refsymname = elf->strtab + refsym->st_name;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100890
891 /* check whitelist - we may ignore it */
Sam Ravnborgcb7e51d2007-07-25 22:24:52 +0200892 if (secref_whitelist(modname, secname, fromsec,
893 before ? elf->strtab + before->st_name : "",
894 refsymname))
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100895 return;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100896
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100897 if (before && after) {
Russell King256012092007-05-10 23:03:25 +0100898 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
899 "(between '%s' and '%s')\n",
900 modname, fromsec, (unsigned long long)r.r_offset,
901 secname, refsymname,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100902 elf->strtab + before->st_name,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100903 elf->strtab + after->st_name);
904 } else if (before) {
Russell King256012092007-05-10 23:03:25 +0100905 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
906 "(after '%s')\n",
907 modname, fromsec, (unsigned long long)r.r_offset,
908 secname, refsymname,
909 elf->strtab + before->st_name);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100910 } else if (after) {
Russell King256012092007-05-10 23:03:25 +0100911 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s "
Sam Ravnborg93684d32006-02-19 11:53:35 +0100912 "before '%s' (at offset -0x%llx)\n",
Russell King256012092007-05-10 23:03:25 +0100913 modname, fromsec, (unsigned long long)r.r_offset,
914 secname, refsymname,
Andi Kleen58b7a682007-11-22 03:43:09 +0100915 elf->strtab + after->st_name,
916 (unsigned long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100917 } else {
Russell King256012092007-05-10 23:03:25 +0100918 warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n",
919 modname, fromsec, (unsigned long long)r.r_offset,
920 secname, refsymname);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100921 }
922}
923
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900924static unsigned int *reloc_location(struct elf_info *elf,
925 int rsection, Elf_Rela *r)
926{
927 Elf_Shdr *sechdrs = elf->sechdrs;
928 int section = sechdrs[rsection].sh_info;
929
930 return (void *)elf->hdr + sechdrs[section].sh_offset +
931 (r->r_offset - sechdrs[section].sh_addr);
932}
933
934static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
935{
936 unsigned int r_typ = ELF_R_TYPE(r->r_info);
937 unsigned int *location = reloc_location(elf, rsection, r);
938
939 switch (r_typ) {
940 case R_386_32:
941 r->r_addend = TO_NATIVE(*location);
942 break;
943 case R_386_PC32:
944 r->r_addend = TO_NATIVE(*location) + 4;
945 /* For CONFIG_RELOCATABLE=y */
946 if (elf->hdr->e_type == ET_EXEC)
947 r->r_addend += r->r_offset;
948 break;
949 }
950 return 0;
951}
952
Sam Ravnborg56a974f2007-07-16 22:39:35 +0200953static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
954{
955 unsigned int r_typ = ELF_R_TYPE(r->r_info);
956
957 switch (r_typ) {
958 case R_ARM_ABS32:
959 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100960 r->r_addend = (int)(long)
961 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +0200962 break;
963 case R_ARM_PC24:
964 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100965 r->r_addend = (int)(long)(elf->hdr +
966 elf->sechdrs[rsection].sh_offset +
967 (r->r_offset - elf->sechdrs[rsection].sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +0200968 break;
969 default:
970 return 1;
971 }
972 return 0;
973}
974
Atsushi Nemotoae4ac122007-05-22 18:27:39 +0900975static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r)
976{
977 unsigned int r_typ = ELF_R_TYPE(r->r_info);
978 unsigned int *location = reloc_location(elf, rsection, r);
979 unsigned int inst;
980
981 if (r_typ == R_MIPS_HI16)
982 return 1; /* skip this */
983 inst = TO_NATIVE(*location);
984 switch (r_typ) {
985 case R_MIPS_LO16:
986 r->r_addend = inst & 0xffff;
987 break;
988 case R_MIPS_26:
989 r->r_addend = (inst & 0x03ffffff) << 2;
990 break;
991 case R_MIPS_32:
992 r->r_addend = inst;
993 break;
994 }
995 return 0;
996}
997
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100998/**
999 * A module includes a number of sections that are discarded
1000 * either when loaded or when used as built-in.
1001 * For loaded modules all functions marked __init and all data
1002 * marked __initdata will be discarded when the module has been intialized.
1003 * Likewise for modules used built-in the sections marked __exit
1004 * are discarded because __exit marked function are supposed to be called
1005 * only when a moduel is unloaded which never happes for built-in modules.
1006 * The check_sec_ref() function traverses all relocation records
1007 * to find all references to a section that reference a section that will
1008 * be discarded and warns about it.
1009 **/
1010static void check_sec_ref(struct module *mod, const char *modname,
1011 struct elf_info *elf,
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001012 int section(const char *),
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001013 int section_ref_ok(const char *))
1014{
1015 int i;
1016 Elf_Sym *sym;
1017 Elf_Ehdr *hdr = elf->hdr;
1018 Elf_Shdr *sechdrs = elf->sechdrs;
1019 const char *secstrings = (void *)hdr +
1020 sechdrs[hdr->e_shstrndx].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001021
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001022 /* Walk through all sections */
1023 for (i = 0; i < hdr->e_shnum; i++) {
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -07001024 const char *name = secstrings + sechdrs[i].sh_name;
1025 const char *secname;
1026 Elf_Rela r;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -07001027 unsigned int r_sym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001028 /* We want to process only relocation sections and not .init */
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -07001029 if (sechdrs[i].sh_type == SHT_RELA) {
1030 Elf_Rela *rela;
1031 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001032 Elf_Rela *stop = (void *)start + sechdrs[i].sh_size;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -07001033 name += strlen(".rela");
1034 if (section_ref_ok(name))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001035 continue;
1036
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -07001037 for (rela = start; rela < stop; rela++) {
1038 r.r_offset = TO_NATIVE(rela->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -07001039#if KERNEL_ELFCLASS == ELFCLASS64
1040 if (hdr->e_machine == EM_MIPS) {
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001041 unsigned int r_typ;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -07001042 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1043 r_sym = TO_NATIVE(r_sym);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001044 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1045 r.r_info = ELF64_R_INFO(r_sym, r_typ);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -07001046 } else {
1047 r.r_info = TO_NATIVE(rela->r_info);
1048 r_sym = ELF_R_SYM(r.r_info);
1049 }
1050#else
1051 r.r_info = TO_NATIVE(rela->r_info);
1052 r_sym = ELF_R_SYM(r.r_info);
1053#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -07001054 r.r_addend = TO_NATIVE(rela->r_addend);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -07001055 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -07001056 /* Skip special sections */
1057 if (sym->st_shndx >= SHN_LORESERVE)
1058 continue;
1059
1060 secname = secstrings +
1061 sechdrs[sym->st_shndx].sh_name;
1062 if (section(secname))
1063 warn_sec_mismatch(modname, name,
1064 elf, sym, r);
1065 }
1066 } else if (sechdrs[i].sh_type == SHT_REL) {
1067 Elf_Rel *rel;
1068 Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001069 Elf_Rel *stop = (void *)start + sechdrs[i].sh_size;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -07001070 name += strlen(".rel");
1071 if (section_ref_ok(name))
1072 continue;
1073
1074 for (rel = start; rel < stop; rel++) {
1075 r.r_offset = TO_NATIVE(rel->r_offset);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -07001076#if KERNEL_ELFCLASS == ELFCLASS64
1077 if (hdr->e_machine == EM_MIPS) {
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001078 unsigned int r_typ;
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -07001079 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1080 r_sym = TO_NATIVE(r_sym);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001081 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1082 r.r_info = ELF64_R_INFO(r_sym, r_typ);
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -07001083 } else {
1084 r.r_info = TO_NATIVE(rel->r_info);
1085 r_sym = ELF_R_SYM(r.r_info);
1086 }
1087#else
1088 r.r_info = TO_NATIVE(rel->r_info);
1089 r_sym = ELF_R_SYM(r.r_info);
1090#endif
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -07001091 r.r_addend = 0;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001092 switch (hdr->e_machine) {
1093 case EM_386:
1094 if (addend_386_rel(elf, i, &r))
1095 continue;
1096 break;
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001097 case EM_ARM:
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001098 if (addend_arm_rel(elf, i, &r))
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001099 continue;
1100 break;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001101 case EM_MIPS:
1102 if (addend_mips_rel(elf, i, &r))
1103 continue;
1104 break;
1105 }
Atsushi Nemotoeae07ac2006-05-20 15:00:28 -07001106 sym = elf->symtab_start + r_sym;
Atsushi Nemoto2c1a51f2006-05-20 15:00:26 -07001107 /* Skip special sections */
1108 if (sym->st_shndx >= SHN_LORESERVE)
1109 continue;
1110
1111 secname = secstrings +
1112 sechdrs[sym->st_shndx].sh_name;
1113 if (section(secname))
1114 warn_sec_mismatch(modname, name,
1115 elf, sym, r);
1116 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001117 }
1118 }
1119}
1120
Sam Ravnborg10872472007-06-02 21:18:51 +02001121/*
1122 * Identify sections from which references to either a
1123 * .init or a .exit section is OK.
1124 *
1125 * [OPD] Keith Ownes <kaos@sgi.com> commented:
1126 * For our future {in}sanity, add a comment that this is the ppc .opd
1127 * section, not the ia64 .opd section.
1128 * ia64 .opd should not point to discarded sections.
1129 * [.rodata] like for .init.text we ignore .rodata references -same reason
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001130 */
Sam Ravnborg10872472007-06-02 21:18:51 +02001131static int initexit_section_ref_ok(const char *name)
1132{
1133 const char **s;
1134 /* Absolute section names */
1135 const char *namelist1[] = {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001136 "__bug_table", /* used by powerpc for BUG() */
Sam Ravnborg10872472007-06-02 21:18:51 +02001137 "__ex_table",
1138 ".altinstructions",
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001139 ".cranges", /* used by sh64 */
Sam Ravnborg10872472007-06-02 21:18:51 +02001140 ".fixup",
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001141 ".machvec", /* ia64 + powerpc uses these */
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001142 ".machine.desc",
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001143 ".opd", /* See comment [OPD] */
Ralf Baechlead0b1422007-07-31 00:38:47 -07001144 "__dbe_table",
Sam Ravnborg10872472007-06-02 21:18:51 +02001145 ".parainstructions",
1146 ".pdr",
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001147 ".plt", /* seen on ARCH=um build on x86_64. Harmless */
Sam Ravnborg10872472007-06-02 21:18:51 +02001148 ".smp_locks",
1149 ".stab",
Al Viro3a5df1d2007-07-20 04:32:48 +01001150 ".m68k_fixup",
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001151 ".xt.prop", /* xtensa informational section */
1152 ".xt.lit", /* xtensa informational section */
Sam Ravnborg10872472007-06-02 21:18:51 +02001153 NULL
1154 };
1155 /* Start of section names */
1156 const char *namelist2[] = {
1157 ".debug",
1158 ".eh_frame",
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001159 ".note", /* ignore ELF notes - may contain anything */
1160 ".got", /* powerpc - global offset table */
1161 ".toc", /* powerpc - table of contents */
Sam Ravnborg10872472007-06-02 21:18:51 +02001162 NULL
1163 };
1164 /* part of section name */
1165 const char *namelist3 [] = {
1166 ".unwind", /* Sample: IA_64.unwind.exit.text */
1167 NULL
1168 };
1169
1170 for (s = namelist1; *s; s++)
1171 if (strcmp(*s, name) == 0)
1172 return 1;
1173 for (s = namelist2; *s; s++)
1174 if (strncmp(*s, name, strlen(*s)) == 0)
1175 return 1;
1176 for (s = namelist3; *s; s++)
1177 if (strstr(name, *s) != NULL)
1178 return 1;
1179 return 0;
1180}
1181
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001182
Sam Ravnborg10872472007-06-02 21:18:51 +02001183/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001184 * Identify sections from which references to a .init section is OK.
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001185 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001186 * Unfortunately references to read only data that referenced .init
1187 * sections had to be excluded. Almost all of these are false
1188 * positives, they are created by gcc. The downside of excluding rodata
1189 * is that there really are some user references from rodata to
1190 * init code, e.g. drivers/video/vgacon.c:
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001191 *
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001192 * const struct consw vga_con = {
1193 * con_startup: vgacon_startup,
1194 *
1195 * where vgacon_startup is __init. If you want to wade through the false
1196 * positives, take out the check for rodata.
Sam Ravnborg10872472007-06-02 21:18:51 +02001197 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001198static int init_section_ref_ok(const char *name)
1199{
1200 const char **s;
1201 /* Absolute section names */
1202 const char *namelist1[] = {
Ralf Baechleeec73e82007-07-10 09:16:32 +01001203 "__dbe_table", /* MIPS generate these */
Benjamin Herrenschmidt21c4ff82006-10-20 11:47:19 +10001204 "__ftr_fixup", /* powerpc cpu feature fixup */
1205 "__fw_ftr_fixup", /* powerpc firmware feature fixup */
Sam Ravnborg10872472007-06-02 21:18:51 +02001206 "__param",
1207 ".data.rel.ro", /* used by parisc64 */
1208 ".init",
1209 ".text.lock",
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001210 NULL
1211 };
1212 /* Start of section names */
1213 const char *namelist2[] = {
1214 ".init.",
Sam Ravnborg10872472007-06-02 21:18:51 +02001215 ".pci_fixup",
Matthew Wilcox742433b2007-02-05 16:34:00 -08001216 ".rodata",
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001217 NULL
1218 };
Sam Ravnborg10872472007-06-02 21:18:51 +02001219
1220 if (initexit_section_ref_ok(name))
1221 return 1;
Sam Ravnborg6e101332006-02-22 21:24:50 +01001222
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001223 for (s = namelist1; *s; s++)
1224 if (strcmp(*s, name) == 0)
1225 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001226 for (s = namelist2; *s; s++)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001227 if (strncmp(*s, name, strlen(*s)) == 0)
1228 return 1;
Sam Ravnborg10872472007-06-02 21:18:51 +02001229
1230 /* If section name ends with ".init" we allow references
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001231 * as is the case with .initcallN.init, .early_param.init,
1232 * .taglist.init etc
Sam Ravnborg10872472007-06-02 21:18:51 +02001233 */
Al Viro468d9492006-06-23 23:22:43 +01001234 if (strrcmp(name, ".init") == 0)
1235 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001236 return 0;
1237}
1238
1239/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001240 * Identify sections from which references to a .exit section is OK.
Sam Ravnborg10872472007-06-02 21:18:51 +02001241 */
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001242static int exit_section_ref_ok(const char *name)
1243{
1244 const char **s;
1245 /* Absolute section names */
1246 const char *namelist1[] = {
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001247 ".exit.data",
Sam Ravnborg10872472007-06-02 21:18:51 +02001248 ".exit.text",
1249 ".exitcall.exit",
Sam Ravnborg5ecdd0f2006-04-14 23:54:13 +02001250 ".rodata",
Sam Ravnborg6e101332006-02-22 21:24:50 +01001251 NULL
1252 };
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001253
Sam Ravnborg10872472007-06-02 21:18:51 +02001254 if (initexit_section_ref_ok(name))
1255 return 1;
1256
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001257 for (s = namelist1; *s; s++)
1258 if (strcmp(*s, name) == 0)
1259 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001260 return 0;
1261}
1262
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001263static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 const char *symname;
1266 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001267 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct module *mod;
1269 struct elf_info info = { };
1270 Elf_Sym *sym;
1271
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001272 if (!parse_elf(&info, modname))
1273 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 mod = new_module(modname);
1276
1277 /* When there's no vmlinux, don't print warnings about
1278 * unresolved symbols (since there'll be too many ;) */
1279 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 mod->skip = 1;
1282 }
1283
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001284 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
1285 while (license) {
1286 if (license_is_gpl_compatible(license))
1287 mod->gpl_compatible = 1;
1288 else {
1289 mod->gpl_compatible = 0;
1290 break;
1291 }
1292 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1293 "license", license);
1294 }
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1297 symname = info.strtab + sym->st_name;
1298
1299 handle_modversions(mod, &info, sym, symname);
1300 handle_moddevtable(mod, &info, sym, symname);
1301 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001302 if (!is_vmlinux(modname) ||
1303 (is_vmlinux(modname) && vmlinux_section_warnings)) {
Sam Ravnborg8d8d8282007-07-20 22:36:56 +02001304 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
1305 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1309 if (version)
1310 maybe_frob_rcs_version(modname, version, info.modinfo,
1311 version - (char *)info.hdr);
1312 if (version || (all_versions && !is_vmlinux(modname)))
1313 get_src_version(modname, mod->srcversion,
1314 sizeof(mod->srcversion)-1);
1315
1316 parse_elf_finish(&info);
1317
1318 /* Our trick to get versioning for struct_module - it's
1319 * never passed as an argument to an exported function, so
1320 * the automatic versioning doesn't pick it up, but it's really
1321 * important anyhow */
1322 if (modversions)
1323 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
1324}
1325
1326#define SZ 500
1327
1328/* We first write the generated file into memory using the
1329 * following helper, then compare to the file on disk and
1330 * only update the later if anything changed */
1331
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001332void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1333 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 char tmp[SZ];
1336 int len;
1337 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 va_start(ap, fmt);
1340 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001341 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 va_end(ap);
1343}
1344
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001345void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001348 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 buf->p = realloc(buf->p, buf->size);
1350 }
1351 strncpy(buf->p + buf->pos, s, len);
1352 buf->pos += len;
1353}
1354
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001355static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1356{
1357 const char *e = is_vmlinux(m) ?"":".ko";
1358
1359 switch (exp) {
1360 case export_gpl:
1361 fatal("modpost: GPL-incompatible module %s%s "
1362 "uses GPL-only symbol '%s'\n", m, e, s);
1363 break;
1364 case export_unused_gpl:
1365 fatal("modpost: GPL-incompatible module %s%s "
1366 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1367 break;
1368 case export_gpl_future:
1369 warn("modpost: GPL-incompatible module %s%s "
1370 "uses future GPL-only symbol '%s'\n", m, e, s);
1371 break;
1372 case export_plain:
1373 case export_unused:
1374 case export_unknown:
1375 /* ignore */
1376 break;
1377 }
1378}
1379
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001380static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001381{
1382 const char *e = is_vmlinux(m) ?"":".ko";
1383
1384 switch (exp) {
1385 case export_unused:
1386 case export_unused_gpl:
1387 warn("modpost: module %s%s "
1388 "uses symbol '%s' marked UNUSED\n", m, e, s);
1389 break;
1390 default:
1391 /* ignore */
1392 break;
1393 }
1394}
1395
1396static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001397{
1398 struct symbol *s, *exp;
1399
1400 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001401 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001402 exp = find_symbol(s->name);
1403 if (!exp || exp->module == mod)
1404 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001405 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001406 if (basename)
1407 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001408 else
1409 basename = mod->name;
1410 if (!mod->gpl_compatible)
1411 check_for_gpl_usage(exp->export, basename, exp->name);
1412 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001413 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001414}
1415
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001416/**
1417 * Header for the generated file
1418 **/
1419static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 buf_printf(b, "#include <linux/module.h>\n");
1422 buf_printf(b, "#include <linux/vermagic.h>\n");
1423 buf_printf(b, "#include <linux/compiler.h>\n");
1424 buf_printf(b, "\n");
1425 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1426 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 buf_printf(b, "struct module __this_module\n");
1428 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001429 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (mod->has_init)
1431 buf_printf(b, " .init = init_module,\n");
1432 if (mod->has_cleanup)
1433 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1434 " .exit = cleanup_module,\n"
1435 "#endif\n");
Roman Zippele61a1c12007-05-09 02:35:15 -07001436 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 buf_printf(b, "};\n");
1438}
1439
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001440/**
1441 * Record CRCs for unresolved symbols
1442 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001443static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
1445 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001446 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 for (s = mod->unres; s; s = s->next) {
1449 exp = find_symbol(s->name);
1450 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001451 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001452 if (warn_unresolved) {
1453 warn("\"%s\" [%s.ko] undefined!\n",
1454 s->name, mod->name);
1455 } else {
1456 merror("\"%s\" [%s.ko] undefined!\n",
1457 s->name, mod->name);
1458 err = 1;
1459 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 continue;
1462 }
1463 s->module = exp->module;
1464 s->crc_valid = exp->crc_valid;
1465 s->crc = exp->crc;
1466 }
1467
1468 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 buf_printf(b, "\n");
1472 buf_printf(b, "static const struct modversion_info ____versions[]\n");
1473 buf_printf(b, "__attribute_used__\n");
1474 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1475
1476 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001477 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001480 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 s->name, mod->name);
1482 continue;
1483 }
1484 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1485 }
1486
1487 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001488
1489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001492static void add_depends(struct buffer *b, struct module *mod,
1493 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 struct symbol *s;
1496 struct module *m;
1497 int first = 1;
1498
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001499 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 buf_printf(b, "\n");
1503 buf_printf(b, "static const char __module_depends[]\n");
1504 buf_printf(b, "__attribute_used__\n");
1505 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1506 buf_printf(b, "\"depends=");
1507 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001508 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (!s->module)
1510 continue;
1511
1512 if (s->module->seen)
1513 continue;
1514
1515 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001516 p = strrchr(s->module->name, '/');
1517 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001518 p++;
1519 else
1520 p = s->module->name;
1521 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 first = 0;
1523 }
1524 buf_printf(b, "\";\n");
1525}
1526
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001527static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 if (mod->srcversion[0]) {
1530 buf_printf(b, "\n");
1531 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1532 mod->srcversion);
1533 }
1534}
1535
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001536static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 char *tmp;
1539 FILE *file;
1540 struct stat st;
1541
1542 file = fopen(fname, "r");
1543 if (!file)
1544 goto write;
1545
1546 if (fstat(fileno(file), &st) < 0)
1547 goto close_write;
1548
1549 if (st.st_size != b->pos)
1550 goto close_write;
1551
1552 tmp = NOFAIL(malloc(b->pos));
1553 if (fread(tmp, 1, b->pos, file) != b->pos)
1554 goto free_write;
1555
1556 if (memcmp(tmp, b->p, b->pos) != 0)
1557 goto free_write;
1558
1559 free(tmp);
1560 fclose(file);
1561 return;
1562
1563 free_write:
1564 free(tmp);
1565 close_write:
1566 fclose(file);
1567 write:
1568 file = fopen(fname, "w");
1569 if (!file) {
1570 perror(fname);
1571 exit(1);
1572 }
1573 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1574 perror(fname);
1575 exit(1);
1576 }
1577 fclose(file);
1578}
1579
Ram Paibd5cbce2006-06-08 22:12:53 -07001580/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001581 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001582 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001583static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 unsigned long size, pos = 0;
1586 void *file = grab_file(fname, &size);
1587 char *line;
1588
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001589 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /* No symbol versions, silently ignore */
1591 return;
1592
1593 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001594 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 unsigned int crc;
1596 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001597 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 if (!(symname = strchr(line, '\t')))
1600 goto fail;
1601 *symname++ = '\0';
1602 if (!(modname = strchr(symname, '\t')))
1603 goto fail;
1604 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001605 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001606 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001607 if (export && ((end = strchr(export, '\t')) != NULL))
1608 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 crc = strtoul(line, &d, 16);
1610 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1611 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001612 mod = find_module(modname);
1613 if (!mod) {
1614 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 mod = new_module(NOFAIL(strdup(modname)));
1617 mod->skip = 1;
1618 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001619 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001620 s->kernel = kernel;
1621 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001622 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624 return;
1625fail:
1626 fatal("parse error in symbol dump file\n");
1627}
1628
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001629/* For normal builds always dump all symbols.
1630 * For external modules only dump symbols
1631 * that are not read from kernel Module.symvers.
1632 **/
1633static int dump_sym(struct symbol *sym)
1634{
1635 if (!external_module)
1636 return 1;
1637 if (sym->vmlinux || sym->kernel)
1638 return 0;
1639 return 1;
1640}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001641
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001642static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 struct buffer buf = { };
1645 struct symbol *symbol;
1646 int n;
1647
1648 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1649 symbol = symbolhash[n];
1650 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001651 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001652 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001653 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001654 symbol->module->name,
1655 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 symbol = symbol->next;
1657 }
1658 }
1659 write_if_changed(&buf, fname);
1660}
1661
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001662int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 struct module *mod;
1665 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001666 char *kernel_read = NULL, *module_read = NULL;
1667 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001669 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Sam Ravnborg8d8d8282007-07-20 22:36:56 +02001671 while ((opt = getopt(argc, argv, "i:I:mso:aw")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001672 switch (opt) {
1673 case 'i':
1674 kernel_read = optarg;
1675 break;
1676 case 'I':
1677 module_read = optarg;
1678 external_module = 1;
1679 break;
1680 case 'm':
1681 modversions = 1;
1682 break;
1683 case 'o':
1684 dump_write = optarg;
1685 break;
1686 case 'a':
1687 all_versions = 1;
1688 break;
1689 case 's':
1690 vmlinux_section_warnings = 0;
1691 break;
1692 case 'w':
1693 warn_unresolved = 1;
1694 break;
1695 default:
1696 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698 }
1699
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001700 if (kernel_read)
1701 read_dump(kernel_read, 1);
1702 if (module_read)
1703 read_dump(module_read, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001705 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 for (mod = modules; mod; mod = mod->next) {
1709 if (mod->skip)
1710 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001711 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001712 }
1713
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001714 err = 0;
1715
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001716 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01001717 char fname[strlen(mod->name) + 10];
1718
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001719 if (mod->skip)
1720 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 buf.pos = 0;
1723
1724 add_header(&buf, mod);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001725 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 add_depends(&buf, mod, modules);
1727 add_moddevtable(&buf, mod);
1728 add_srcversion(&buf, mod);
1729
1730 sprintf(fname, "%s.mod.c", mod->name);
1731 write_if_changed(&buf, fname);
1732 }
1733
1734 if (dump_write)
1735 write_dump(dump_write);
1736
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001737 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}