blob: fb0f9b711af31497df9bcddbda53a9a96ecea43b [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
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
15#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <ctype.h>
17#include "modpost.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000018#include "../../include/linux/autoconf.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020019#include "../../include/linux/license.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000021/* Some toolchains use a `_' prefix for all user symbols. */
22#ifdef CONFIG_SYMBOL_PREFIX
23#define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
24#else
25#define MODULE_SYMBOL_PREFIX ""
26#endif
27
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Are we using CONFIG_MODVERSIONS? */
30int modversions = 0;
31/* Warn about undefined symbols? (do so if we have vmlinux) */
32int have_vmlinux = 0;
33/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
34static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010035/* If we are modposting external module set to 1 */
36static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020037/* Warn about section mismatch in vmlinux if set to 1 */
38static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070039/* Only warn about unresolved symbols */
40static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070041/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010042static int sec_mismatch_count = 0;
43static int sec_mismatch_verbose = 1;
44
Sam Ravnborgc96fca22006-07-01 11:44:23 +020045enum export {
46 export_plain, export_unused, export_gpl,
47 export_unused_gpl, export_gpl_future, export_unknown
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Andi Kleen6d9a89e2007-11-22 03:43:08 +010050#define PRINTF __attribute__ ((format (printf, 1, 2)))
51
52PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 va_list arglist;
55
56 fprintf(stderr, "FATAL: ");
57
58 va_start(arglist, fmt);
59 vfprintf(stderr, fmt, arglist);
60 va_end(arglist);
61
62 exit(1);
63}
64
Andi Kleen6d9a89e2007-11-22 03:43:08 +010065PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 va_list arglist;
68
69 fprintf(stderr, "WARNING: ");
70
71 va_start(arglist, fmt);
72 vfprintf(stderr, fmt, arglist);
73 va_end(arglist);
74}
75
Andi Kleen6d9a89e2007-11-22 03:43:08 +010076PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060077{
78 va_list arglist;
79
80 fprintf(stderr, "ERROR: ");
81
82 va_start(arglist, fmt);
83 vfprintf(stderr, fmt, arglist);
84 va_end(arglist);
85}
86
Sam Ravnborg040fcc82006-01-28 22:15:55 +010087static int is_vmlinux(const char *modname)
88{
89 const char *myname;
90
Sam Ravnborgdf578e72008-01-11 19:17:15 +010091 myname = strrchr(modname, '/');
92 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010093 myname++;
94 else
95 myname = modname;
96
Sam Ravnborg741f98f2007-07-17 10:54:06 +020097 return (strcmp(myname, "vmlinux") == 0) ||
98 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +010099}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101void *do_nofail(void *ptr, const char *expr)
102{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100103 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return ptr;
107}
108
109/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static struct module *modules;
111
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100112static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct module *mod;
115
116 for (mod = modules; mod; mod = mod->next)
117 if (strcmp(mod->name, modname) == 0)
118 break;
119 return mod;
120}
121
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100122static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct module *mod;
125 char *p, *s;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 mod = NOFAIL(malloc(sizeof(*mod)));
128 memset(mod, 0, sizeof(*mod));
129 p = NOFAIL(strdup(modname));
130
131 /* strip trailing .o */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100132 s = strrchr(p, '.');
133 if (s != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (strcmp(s, ".o") == 0)
135 *s = '\0';
136
137 /* add to list */
138 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200139 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 mod->next = modules;
141 modules = mod;
142
143 return mod;
144}
145
146/* A hash of all exported symbols,
147 * struct symbol is also used for lists of unresolved symbols */
148
149#define SYMBOL_HASH_SIZE 1024
150
151struct symbol {
152 struct symbol *next;
153 struct module *module;
154 unsigned int crc;
155 int crc_valid;
156 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100157 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
158 unsigned int kernel:1; /* 1 if symbol is from kernel
159 * (only for external modules) **/
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100160 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Ram Paibd5cbce2006-06-08 22:12:53 -0700161 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 char name[0];
163};
164
165static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
166
167/* This is based on the hash agorithm from gdbm, via tdb */
168static inline unsigned int tdb_hash(const char *name)
169{
170 unsigned value; /* Used to compute the hash value. */
171 unsigned i; /* Used to cycle through random values. */
172
173 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100174 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
176
177 return (1103515243 * value + 12345);
178}
179
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100180/**
181 * Allocate a new symbols for use in the hash of exported symbols or
182 * the list of unresolved symbols per module
183 **/
184static struct symbol *alloc_symbol(const char *name, unsigned int weak,
185 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
188
189 memset(s, 0, sizeof(*s));
190 strcpy(s->name, name);
191 s->weak = weak;
192 s->next = next;
193 return s;
194}
195
196/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700197static struct symbol *new_symbol(const char *name, struct module *module,
198 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 unsigned int hash;
201 struct symbol *new;
202
203 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
204 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
205 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700206 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100207 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100210static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct symbol *s;
213
214 /* For our purposes, .foo matches foo. PPC64 needs this. */
215 if (name[0] == '.')
216 name++;
217
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100218 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (strcmp(s->name, name) == 0)
220 return s;
221 }
222 return NULL;
223}
224
Ram Paibd5cbce2006-06-08 22:12:53 -0700225static struct {
226 const char *str;
227 enum export export;
228} export_list[] = {
229 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200230 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700231 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200232 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700233 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
234 { .str = "(unknown)", .export = export_unknown },
235};
236
237
238static const char *export_str(enum export ex)
239{
240 return export_list[ex].str;
241}
242
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100243static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700244{
245 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100246
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200247 if (!s)
248 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700249 for (i = 0; export_list[i].export != export_unknown; i++) {
250 if (strcmp(export_list[i].str, s) == 0)
251 return export_list[i].export;
252 }
253 return export_unknown;
254}
255
256static enum export export_from_sec(struct elf_info *elf, Elf_Section sec)
257{
258 if (sec == elf->export_sec)
259 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200260 else if (sec == elf->export_unused_sec)
261 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700262 else if (sec == elf->export_gpl_sec)
263 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200264 else if (sec == elf->export_unused_gpl_sec)
265 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700266 else if (sec == elf->export_gpl_future_sec)
267 return export_gpl_future;
268 else
269 return export_unknown;
270}
271
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100272/**
273 * Add an exported symbol - it may have already been added without a
274 * CRC, in this case just update the CRC
275 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700276static struct symbol *sym_add_exported(const char *name, struct module *mod,
277 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct symbol *s = find_symbol(name);
280
281 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700282 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100283 } else {
284 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100285 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100286 "was in %s%s\n", mod->name, name,
287 s->module->name,
288 is_vmlinux(s->module->name) ?"":".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700289 } else {
290 /* In case Modules.symvers was out of date */
291 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100294 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100295 s->vmlinux = is_vmlinux(mod->name);
296 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700297 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100298 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100301static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700302 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100303{
304 struct symbol *s = find_symbol(name);
305
306 if (!s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700307 s = new_symbol(name, mod, export);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100308 s->crc = crc;
309 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100312void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 struct stat st;
315 void *map;
316 int fd;
317
318 fd = open(filename, O_RDONLY);
319 if (fd < 0 || fstat(fd, &st) != 0)
320 return NULL;
321
322 *size = st.st_size;
323 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
324 close(fd);
325
326 if (map == MAP_FAILED)
327 return NULL;
328 return map;
329}
330
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100331/**
332 * Return a copy of the next line in a mmap'ed file.
333 * spaces in the beginning of the line is trimmed away.
334 * Return a pointer to a static buffer.
335 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100336char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 static char line[4096];
339 int skip = 1;
340 size_t len = 0;
341 signed char *p = (signed char *)file + *pos;
342 char *s = line;
343
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100344 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (skip && isspace(*p)) {
346 p++;
347 continue;
348 }
349 skip = 0;
350 if (*p != '\n' && (*pos < size)) {
351 len++;
352 *s++ = *p++;
353 if (len > 4095)
354 break; /* Too long, stop */
355 } else {
356 /* End of string */
357 *s = '\0';
358 return line;
359 }
360 }
361 /* End of buffer */
362 return NULL;
363}
364
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100365void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 munmap(file, size);
368}
369
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100370static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100373 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 Elf_Shdr *sechdrs;
375 Elf_Sym *sym;
376
377 hdr = grab_file(filename, &info->size);
378 if (!hdr) {
379 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200380 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100383 if (info->size < sizeof(*hdr)) {
384 /* file too small, assume this is an empty .o file */
385 return 0;
386 }
387 /* Is this a valid ELF file? */
388 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
389 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
390 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
391 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
392 /* Not an ELF file - silently ignore it */
393 return 0;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200396 hdr->e_type = TO_NATIVE(hdr->e_type);
397 hdr->e_machine = TO_NATIVE(hdr->e_machine);
398 hdr->e_version = TO_NATIVE(hdr->e_version);
399 hdr->e_entry = TO_NATIVE(hdr->e_entry);
400 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
401 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
402 hdr->e_flags = TO_NATIVE(hdr->e_flags);
403 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
404 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
405 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
406 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
407 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
408 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 sechdrs = (void *)hdr + hdr->e_shoff;
410 info->sechdrs = sechdrs;
411
Petr Stetiara83710e2007-08-27 12:15:07 +0200412 /* Check if file offset is correct */
413 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100414 fatal("section header offset=%lu in file '%s' is bigger than "
415 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
416 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200417 return 0;
418 }
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* Fix endianness in section headers */
421 for (i = 0; i < hdr->e_shnum; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200422 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
423 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
424 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
425 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
426 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
427 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
428 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
429 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
430 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
431 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 /* Find symbol table. */
434 for (i = 1; i < hdr->e_shnum; i++) {
435 const char *secstrings
436 = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
Ram Paibd5cbce2006-06-08 22:12:53 -0700437 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900438 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Tejun Heo56fc82c2009-02-06 00:48:02 +0900440 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100441 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
442 "sizeof(*hrd)=%zu\n", filename,
443 (unsigned long)sechdrs[i].sh_offset,
444 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100445 return 0;
446 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700447 secname = secstrings + sechdrs[i].sh_name;
448 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900449 if (nobits)
450 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
452 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700453 } else if (strcmp(secname, "__ksymtab") == 0)
454 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200455 else if (strcmp(secname, "__ksymtab_unused") == 0)
456 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700457 else if (strcmp(secname, "__ksymtab_gpl") == 0)
458 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200459 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
460 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700461 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
462 info->export_gpl_future_sec = i;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -0800463 else if (strcmp(secname, "__markers_strings") == 0)
464 info->markers_strings_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (sechdrs[i].sh_type != SHT_SYMTAB)
467 continue;
468
469 info->symtab_start = (void *)hdr + sechdrs[i].sh_offset;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100470 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 + sechdrs[i].sh_size;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100472 info->strtab = (void *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 sechdrs[sechdrs[i].sh_link].sh_offset;
474 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100475 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100476 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* Fix endianness in symbols */
479 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
480 sym->st_shndx = TO_NATIVE(sym->st_shndx);
481 sym->st_name = TO_NATIVE(sym->st_name);
482 sym->st_value = TO_NATIVE(sym->st_value);
483 sym->st_size = TO_NATIVE(sym->st_size);
484 }
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100485 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100488static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 release_file(info->hdr, info->size);
491}
492
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200493static int ignore_undef_symbol(struct elf_info *info, const char *symname)
494{
495 /* ignore __this_module, it will be resolved shortly */
496 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
497 return 1;
498 /* ignore global offset table */
499 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
500 return 1;
501 if (info->hdr->e_machine == EM_PPC)
502 /* Special register function linked on all modules during final link of .ko */
503 if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
504 strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
505 strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
506 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
507 return 1;
508 /* Do not ignore this symbol */
509 return 0;
510}
511
Luke Yangf7b05e62006-03-02 18:35:31 +0800512#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
513#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100515static void handle_modversions(struct module *mod, struct elf_info *info,
516 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 unsigned int crc;
Ram Paibd5cbce2006-06-08 22:12:53 -0700519 enum export export = export_from_sec(info, sym->st_shndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 switch (sym->st_shndx) {
522 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100523 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
525 case SHN_ABS:
526 /* CRC'd symbol */
527 if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
528 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700529 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
530 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 break;
533 case SHN_UNDEF:
534 /* undefined symbol */
535 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
536 ELF_ST_BIND(sym->st_info) != STB_WEAK)
537 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200538 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
Ben Colline8d529012005-08-19 13:44:57 -0700540/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
541#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
542/* add compatibility with older glibc */
543#ifndef STT_SPARC_REGISTER
544#define STT_SPARC_REGISTER STT_REGISTER
545#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (info->hdr->e_machine == EM_SPARC ||
547 info->hdr->e_machine == EM_SPARCV9) {
548 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700549 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100551 if (symname[0] == '.') {
552 char *munged = strdup(symname);
553 munged[0] = '_';
554 munged[1] = toupper(munged[1]);
555 symname = munged;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100561 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
562 mod->unres =
563 alloc_symbol(symname +
564 strlen(MODULE_SYMBOL_PREFIX),
565 ELF_ST_BIND(sym->st_info) == STB_WEAK,
566 mod->unres);
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 break;
569 default:
570 /* All exported symbols */
571 if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700572 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
573 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
576 mod->has_init = 1;
577 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
578 mod->has_cleanup = 1;
579 break;
580 }
581}
582
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100583/**
584 * Parse tag=value strings from .modinfo section
585 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static char *next_string(char *string, unsigned long *secsize)
587{
588 /* Skip non-zero chars */
589 while (string[0]) {
590 string++;
591 if ((*secsize)-- <= 1)
592 return NULL;
593 }
594
595 /* Skip any zero padding. */
596 while (!string[0]) {
597 string++;
598 if ((*secsize)-- <= 1)
599 return NULL;
600 }
601 return string;
602}
603
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200604static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
605 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 char *p;
608 unsigned int taglen = strlen(tag);
609 unsigned long size = modinfo_len;
610
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200611 if (info) {
612 size -= info - (char *)modinfo;
613 modinfo = next_string(info, &size);
614 }
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 for (p = modinfo; p; p = next_string(p, &size)) {
617 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
618 return p + taglen + 1;
619 }
620 return NULL;
621}
622
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200623static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
624 const char *tag)
625
626{
627 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
628}
629
Sam Ravnborg93684d32006-02-19 11:53:35 +0100630/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100631 * Test if string s ends in string sub
632 * return 0 if match
633 **/
634static int strrcmp(const char *s, const char *sub)
635{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100636 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100637
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100638 if (!s || !sub)
639 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100640
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100641 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100642 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100643
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100644 if ((slen == 0) || (sublen == 0))
645 return 1;
646
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100647 if (sublen > slen)
648 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100649
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100650 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100651}
652
Sam Ravnborgff13f922008-01-23 19:54:27 +0100653static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
654{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100655 if (sym)
656 return elf->strtab + sym->st_name;
657 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100658 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100659}
660
661static const char *sec_name(struct elf_info *elf, int shndx)
662{
663 Elf_Shdr *sechdrs = elf->sechdrs;
664 return (void *)elf->hdr +
665 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
666 sechdrs[shndx].sh_name;
667}
668
669static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
670{
671 return (void *)elf->hdr +
672 elf->sechdrs[elf->hdr->e_shstrndx].sh_offset +
673 sechdr->sh_name;
674}
675
Sam Ravnborg10668222008-01-13 22:21:31 +0100676/* if sym is empty or point to a string
677 * like ".[0-9]+" then return 1.
678 * This is the optional prefix added by ld to some sections
679 */
680static int number_prefix(const char *sym)
681{
682 if (*sym++ == '\0')
683 return 1;
684 if (*sym != '.')
685 return 0;
686 do {
687 char c = *sym++;
688 if (c < '0' || c > '9')
689 return 0;
690 } while (*sym);
691 return 1;
692}
693
694/* The pattern is an array of simple patterns.
695 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100696 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100697 * "foo*" will match a string that begins with "foo"
698 * "foo$" will match a string equal to "foo" or "foo.1"
699 * where the '1' can be any number including several digits.
700 * The $ syntax is for sections where ld append a dot number
701 * to make section name unique.
702 */
Trevor Keith5c725132009-09-22 16:43:38 -0700703static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100704{
705 const char *p;
706 while (*pat) {
707 p = *pat++;
708 const char *endp = p + strlen(p) - 1;
709
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100710 /* "*foo" */
711 if (*p == '*') {
712 if (strrcmp(sym, p + 1) == 0)
713 return 1;
714 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100715 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100716 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100717 if (strncmp(sym, p, strlen(p) - 1) == 0)
718 return 1;
719 }
720 /* "foo$" */
721 else if (*endp == '$') {
722 if (strncmp(sym, p, strlen(p) - 1) == 0) {
723 if (number_prefix(sym + strlen(p) - 1))
724 return 1;
725 }
726 }
727 /* no wildcards */
728 else {
729 if (strcmp(p, sym) == 0)
730 return 1;
731 }
732 }
733 /* no match */
734 return 0;
735}
736
Sam Ravnborg10668222008-01-13 22:21:31 +0100737/* sections that we do not want to do full section mismatch check on */
738static const char *section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200739{
740 ".comment*",
741 ".debug*",
742 ".mdebug*", /* alpha, score, mips etc. */
743 ".pdr", /* alpha, score, mips etc. */
744 ".stab*",
745 ".note*",
746 ".got*",
747 ".toc*",
748 NULL
749};
Sam Ravnborg10668222008-01-13 22:21:31 +0100750
Sam Ravnborge241a632008-01-28 20:13:13 +0100751/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400752 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100753 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400754 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100755 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400756static void check_section(const char *modname, struct elf_info *elf,
757 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100758{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400759 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100760
Anders Kaseorgb614a692009-04-23 16:49:33 -0400761 if (sechdr->sh_type == SHT_PROGBITS &&
762 !(sechdr->sh_flags & SHF_ALLOC) &&
763 !match(sec, section_white_list)) {
764 warn("%s (%s): unexpected non-allocatable section.\n"
765 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
766 "Note that for example <linux/init.h> contains\n"
767 "section definitions for use in .S files.\n\n",
768 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100769 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100770}
771
772
773
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100774#define ALL_INIT_DATA_SECTIONS \
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000775 ".init.setup$", ".init.rodata$", \
776 ".devinit.rodata$", ".cpuinit.rodata$", ".meminit.rodata$" \
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100777 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
778#define ALL_EXIT_DATA_SECTIONS \
779 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100780
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100781#define ALL_INIT_TEXT_SECTIONS \
782 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
783#define ALL_EXIT_TEXT_SECTIONS \
784 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100785
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000786#define ALL_INIT_SECTIONS INIT_SECTIONS, DEV_INIT_SECTIONS, \
787 CPU_INIT_SECTIONS, MEM_INIT_SECTIONS
788#define ALL_EXIT_SECTIONS EXIT_SECTIONS, DEV_EXIT_SECTIONS, \
789 CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100790
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100791#define DATA_SECTIONS ".data$", ".data.rel$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100792#define TEXT_SECTIONS ".text$"
793
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000794#define INIT_SECTIONS ".init.*"
795#define DEV_INIT_SECTIONS ".devinit.*"
796#define CPU_INIT_SECTIONS ".cpuinit.*"
797#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100798
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000799#define EXIT_SECTIONS ".exit.*"
800#define DEV_EXIT_SECTIONS ".devexit.*"
801#define CPU_EXIT_SECTIONS ".cpuexit.*"
802#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100803
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100804/* init data sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100805static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100806
807/* all init sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100808static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100809
810/* All init and exit sections (code + data) */
811static const char *init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100812 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100813
814/* data section */
815static const char *data_sections[] = { DATA_SECTIONS, NULL };
816
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100817
818/* symbols in .data that may refer to init/exit sections */
819static const char *symbol_white_list[] =
820{
821 "*driver",
822 "*_template", /* scsi uses *_template a lot */
823 "*_timer", /* arm uses ops structures named _timer a lot */
824 "*_sht", /* scsi also used *_sht to some extent */
825 "*_ops",
826 "*_probe",
827 "*_probe_one",
828 "*_console",
829 NULL
830};
831
832static const char *head_sections[] = { ".head.text*", NULL };
833static const char *linker_symbols[] =
834 { "__init_begin", "_sinittext", "_einittext", NULL };
835
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100836enum mismatch {
837 NO_MISMATCH,
838 TEXT_TO_INIT,
839 DATA_TO_INIT,
840 TEXT_TO_EXIT,
841 DATA_TO_EXIT,
842 XXXINIT_TO_INIT,
843 XXXEXIT_TO_EXIT,
844 INIT_TO_EXIT,
845 EXIT_TO_INIT,
846 EXPORT_TO_INIT_EXIT,
847};
848
Sam Ravnborg10668222008-01-13 22:21:31 +0100849struct sectioncheck {
850 const char *fromsec[20];
851 const char *tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100852 enum mismatch mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100853};
854
855const struct sectioncheck sectioncheck[] = {
856/* Do not reference init/exit code/data from
857 * normal code and data
858 */
859{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100860 .fromsec = { TEXT_SECTIONS, NULL },
861 .tosec = { ALL_INIT_SECTIONS, NULL },
862 .mismatch = TEXT_TO_INIT,
863},
864{
865 .fromsec = { DATA_SECTIONS, NULL },
866 .tosec = { ALL_INIT_SECTIONS, NULL },
867 .mismatch = DATA_TO_INIT,
868},
869{
870 .fromsec = { TEXT_SECTIONS, NULL },
871 .tosec = { ALL_EXIT_SECTIONS, NULL },
872 .mismatch = TEXT_TO_EXIT,
873},
874{
875 .fromsec = { DATA_SECTIONS, NULL },
876 .tosec = { ALL_EXIT_SECTIONS, NULL },
877 .mismatch = DATA_TO_EXIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100878},
879/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
880{
881 .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100882 .tosec = { INIT_SECTIONS, NULL },
883 .mismatch = XXXINIT_TO_INIT,
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100884},
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000885/* Do not reference cpuinit code/data from meminit code/data */
886{
887 .fromsec = { MEM_INIT_SECTIONS, NULL },
888 .tosec = { CPU_INIT_SECTIONS, NULL },
889 .mismatch = XXXINIT_TO_INIT,
890},
891/* Do not reference meminit code/data from cpuinit code/data */
892{
893 .fromsec = { CPU_INIT_SECTIONS, NULL },
894 .tosec = { MEM_INIT_SECTIONS, NULL },
895 .mismatch = XXXINIT_TO_INIT,
896},
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100897/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
898{
899 .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100900 .tosec = { EXIT_SECTIONS, NULL },
901 .mismatch = XXXEXIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100902},
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000903/* Do not reference cpuexit code/data from memexit code/data */
904{
905 .fromsec = { MEM_EXIT_SECTIONS, NULL },
906 .tosec = { CPU_EXIT_SECTIONS, NULL },
907 .mismatch = XXXEXIT_TO_EXIT,
908},
909/* Do not reference memexit code/data from cpuexit code/data */
910{
911 .fromsec = { CPU_EXIT_SECTIONS, NULL },
912 .tosec = { MEM_EXIT_SECTIONS, NULL },
913 .mismatch = XXXEXIT_TO_EXIT,
914},
Sam Ravnborg10668222008-01-13 22:21:31 +0100915/* Do not use exit code/data from init code */
916{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100917 .fromsec = { ALL_INIT_SECTIONS, NULL },
918 .tosec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100919 .mismatch = INIT_TO_EXIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100920},
921/* Do not use init code/data from exit code */
922{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100923 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100924 .tosec = { ALL_INIT_SECTIONS, NULL },
925 .mismatch = EXIT_TO_INIT,
Sam Ravnborg10668222008-01-13 22:21:31 +0100926},
927/* Do not export init/exit functions or data */
928{
929 .fromsec = { "__ksymtab*", NULL },
Sam Ravnborgfa95eb12008-02-02 23:30:22 +0100930 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100931 .mismatch = EXPORT_TO_INIT_EXIT
Sam Ravnborg10668222008-01-13 22:21:31 +0100932}
933};
934
935static int section_mismatch(const char *fromsec, const char *tosec)
936{
937 int i;
938 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
939 const struct sectioncheck *check = &sectioncheck[0];
940
941 for (i = 0; i < elems; i++) {
942 if (match(fromsec, check->fromsec) &&
943 match(tosec, check->tosec))
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100944 return check->mismatch;
Sam Ravnborg10668222008-01-13 22:21:31 +0100945 check++;
946 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100947 return NO_MISMATCH;
Sam Ravnborg10668222008-01-13 22:21:31 +0100948}
949
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100950/**
951 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +0200952 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100953 * Pattern 1:
954 * If a module parameter is declared __initdata and permissions=0
955 * then this is legal despite the warning generated.
956 * We cannot see value of permissions here, so just ignore
957 * this pattern.
958 * The pattern is identified by:
959 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +0100960 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100961 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100962 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100963 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -0700964 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100965 * add, remove, probe functions etc.
966 * These functions may often be marked __init and we do not want to
967 * warn here.
968 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +0200969 * tosec = init or exit section
970 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100971 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
972 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +0100973 *
974 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +0200975 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100976 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +0200977 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +0100978 * Some symbols belong to init section but still it is ok to reference
979 * these from non-init sections as these symbols don't have any memory
980 * allocated for them and symbol address and value are same. So even
981 * if init section is freed, its ok to reference those symbols.
982 * For ex. symbols marking the init section boundaries.
983 * This pattern is identified by
984 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +0100985 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100986 **/
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100987static int secref_whitelist(const char *fromsec, const char *fromsym,
988 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100989{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100990 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100991 if (match(tosec, init_data_sections) &&
992 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100993 (strncmp(fromsym, "__param", strlen("__param")) == 0))
994 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100995
996 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100997 if (match(tosec, init_exit_sections) &&
998 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100999 match(fromsym, symbol_white_list))
1000 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001001
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001002 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001003 if (match(fromsec, head_sections) &&
1004 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001005 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001006
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001007 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001008 if (match(tosym, linker_symbols))
1009 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001010
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001011 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001012}
1013
1014/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001015 * Find symbol based on relocation record info.
1016 * In some cases the symbol supplied is a valid symbol so
1017 * return refsym. If st_name != 0 we assume this is a valid symbol.
1018 * In other cases the symbol needs to be looked up in the symbol table
1019 * based on section and address.
1020 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001021static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001022 Elf_Sym *relsym)
1023{
1024 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001025 Elf_Sym *near = NULL;
1026 Elf64_Sword distance = 20;
1027 Elf64_Sword d;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001028
1029 if (relsym->st_name != 0)
1030 return relsym;
1031 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1032 if (sym->st_shndx != relsym->st_shndx)
1033 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001034 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1035 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001036 if (sym->st_value == addr)
1037 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001038 /* Find a symbol nearby - addr are maybe negative */
1039 d = sym->st_value - addr;
1040 if (d < 0)
1041 d = addr - sym->st_value;
1042 if (d < distance) {
1043 distance = d;
1044 near = sym;
1045 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001046 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001047 /* We need a close match */
1048 if (distance < 20)
1049 return near;
1050 else
1051 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001052}
1053
David Brownellda68d612007-02-20 13:58:16 -08001054static inline int is_arm_mapping_symbol(const char *str)
1055{
1056 return str[0] == '$' && strchr("atd", str[1])
1057 && (str[2] == '\0' || str[2] == '.');
1058}
1059
1060/*
1061 * If there's no name there, ignore it; likewise, ignore it if it's
1062 * one of the magic symbols emitted used by current ARM tools.
1063 *
1064 * Otherwise if find_symbols_between() returns those symbols, they'll
1065 * fail the whitelist tests and cause lots of false alarms ... fixable
1066 * only by merging __exit and __init sections into __text, bloating
1067 * the kernel (which is especially evil on embedded platforms).
1068 */
1069static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1070{
1071 const char *name = elf->strtab + sym->st_name;
1072
1073 if (!name || !strlen(name))
1074 return 0;
1075 return !is_arm_mapping_symbol(name);
1076}
1077
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001078/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001079 * Find symbols before or equal addr and after addr - in the section sec.
1080 * If we find two symbols with equal offset prefer one with a valid name.
1081 * The ELF format may have a better way to detect what type of symbol
1082 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001083 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001084static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1085 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001086{
1087 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001088 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001089 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001090
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001091 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1092 const char *symsec;
1093
1094 if (sym->st_shndx >= SHN_LORESERVE)
1095 continue;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001096 symsec = sec_name(elf, sym->st_shndx);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001097 if (strcmp(symsec, sec) != 0)
1098 continue;
David Brownellda68d612007-02-20 13:58:16 -08001099 if (!is_valid_name(elf, sym))
1100 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001101 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001102 if ((addr - sym->st_value) < distance) {
1103 distance = addr - sym->st_value;
1104 near = sym;
1105 } else if ((addr - sym->st_value) == distance) {
1106 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001107 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001108 }
1109 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001110 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001111}
1112
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001113/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001114 * Convert a section name to the function/data attribute
1115 * .init.text => __init
1116 * .cpuinit.data => __cpudata
1117 * .memexitconst => __memconst
1118 * etc.
1119*/
1120static char *sec2annotation(const char *s)
1121{
1122 if (match(s, init_exit_sections)) {
1123 char *p = malloc(20);
1124 char *r = p;
1125
1126 *p++ = '_';
1127 *p++ = '_';
1128 if (*s == '.')
1129 s++;
1130 while (*s && *s != '.')
1131 *p++ = *s++;
1132 *p = '\0';
1133 if (*s == '.')
1134 s++;
1135 if (strstr(s, "rodata") != NULL)
1136 strcat(p, "const ");
1137 else if (strstr(s, "data") != NULL)
1138 strcat(p, "data ");
1139 else
1140 strcat(p, " ");
1141 return r; /* we leak her but we do not care */
1142 } else {
1143 return "";
1144 }
1145}
1146
1147static int is_function(Elf_Sym *sym)
1148{
1149 if (sym)
1150 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1151 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001152 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001153}
1154
1155/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001156 * Print a warning about a section mismatch.
1157 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001158 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001159 */
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001160static void report_sec_mismatch(const char *modname, enum mismatch mismatch,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001161 const char *fromsec,
1162 unsigned long long fromaddr,
1163 const char *fromsym,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001164 int from_is_func,
1165 const char *tosec, const char *tosym,
1166 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001167{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001168 const char *from, *from_p;
1169 const char *to, *to_p;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001170
1171 switch (from_is_func) {
1172 case 0: from = "variable"; from_p = ""; break;
1173 case 1: from = "function"; from_p = "()"; break;
1174 default: from = "(unknown reference)"; from_p = ""; break;
1175 }
1176 switch (to_is_func) {
1177 case 0: to = "variable"; to_p = ""; break;
1178 case 1: to = "function"; to_p = "()"; break;
1179 default: to = "(unknown reference)"; to_p = ""; break;
1180 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001181
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001182 sec_mismatch_count++;
1183 if (!sec_mismatch_verbose)
1184 return;
1185
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001186 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1187 "to the %s %s:%s%s\n",
1188 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1189 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001190
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001191 switch (mismatch) {
1192 case TEXT_TO_INIT:
1193 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001194 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001195 "the %s %s%s%s.\n"
1196 "This is often because %s lacks a %s\n"
1197 "annotation or the annotation of %s is wrong.\n",
1198 sec2annotation(fromsec), fromsym,
1199 to, sec2annotation(tosec), tosym, to_p,
1200 fromsym, sec2annotation(tosec), tosym);
1201 break;
1202 case DATA_TO_INIT: {
1203 const char **s = symbol_white_list;
1204 fprintf(stderr,
1205 "The variable %s references\n"
1206 "the %s %s%s%s\n"
1207 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001208 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001209 "or name the variable:\n",
1210 fromsym, to, sec2annotation(tosec), tosym, to_p);
1211 while (*s)
1212 fprintf(stderr, "%s, ", *s++);
1213 fprintf(stderr, "\n");
1214 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001215 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001216 case TEXT_TO_EXIT:
1217 fprintf(stderr,
1218 "The function %s() references a %s in an exit section.\n"
1219 "Often the %s %s%s has valid usage outside the exit section\n"
1220 "and the fix is to remove the %sannotation of %s.\n",
1221 fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym);
1222 break;
1223 case DATA_TO_EXIT: {
1224 const char **s = symbol_white_list;
1225 fprintf(stderr,
1226 "The variable %s references\n"
1227 "the %s %s%s%s\n"
1228 "If the reference is valid then annotate the\n"
1229 "variable with __exit* (see linux/init.h) or "
1230 "name the variable:\n",
1231 fromsym, to, sec2annotation(tosec), tosym, to_p);
1232 while (*s)
1233 fprintf(stderr, "%s, ", *s++);
1234 fprintf(stderr, "\n");
1235 break;
1236 }
1237 case XXXINIT_TO_INIT:
1238 case XXXEXIT_TO_EXIT:
1239 fprintf(stderr,
1240 "The %s %s%s%s references\n"
1241 "a %s %s%s%s.\n"
1242 "If %s is only used by %s then\n"
1243 "annotate %s with a matching annotation.\n",
1244 from, sec2annotation(fromsec), fromsym, from_p,
1245 to, sec2annotation(tosec), tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001246 tosym, fromsym, tosym);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001247 break;
1248 case INIT_TO_EXIT:
1249 fprintf(stderr,
1250 "The %s %s%s%s references\n"
1251 "a %s %s%s%s.\n"
1252 "This is often seen when error handling "
1253 "in the init function\n"
1254 "uses functionality in the exit path.\n"
1255 "The fix is often to remove the %sannotation of\n"
1256 "%s%s so it may be used outside an exit section.\n",
1257 from, sec2annotation(fromsec), fromsym, from_p,
1258 to, sec2annotation(tosec), tosym, to_p,
1259 sec2annotation(tosec), tosym, to_p);
1260 break;
1261 case EXIT_TO_INIT:
1262 fprintf(stderr,
1263 "The %s %s%s%s references\n"
1264 "a %s %s%s%s.\n"
1265 "This is often seen when error handling "
1266 "in the exit function\n"
1267 "uses functionality in the init path.\n"
1268 "The fix is often to remove the %sannotation of\n"
1269 "%s%s so it may be used outside an init section.\n",
1270 from, sec2annotation(fromsec), fromsym, from_p,
1271 to, sec2annotation(tosec), tosym, to_p,
1272 sec2annotation(tosec), tosym, to_p);
1273 break;
1274 case EXPORT_TO_INIT_EXIT:
1275 fprintf(stderr,
1276 "The symbol %s is exported and annotated %s\n"
1277 "Fix this by removing the %sannotation of %s "
1278 "or drop the export.\n",
1279 tosym, sec2annotation(tosec), sec2annotation(tosec), tosym);
1280 case NO_MISMATCH:
1281 /* To get warnings on missing members */
1282 break;
1283 }
1284 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001285}
1286
1287static void check_section_mismatch(const char *modname, struct elf_info *elf,
1288 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1289{
1290 const char *tosec;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001291 enum mismatch mismatch;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001292
1293 tosec = sec_name(elf, sym->st_shndx);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001294 mismatch = section_mismatch(fromsec, tosec);
1295 if (mismatch != NO_MISMATCH) {
1296 Elf_Sym *to;
1297 Elf_Sym *from;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001298 const char *tosym;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001299 const char *fromsym;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001300
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001301 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1302 fromsym = sym_name(elf, from);
1303 to = find_elf_symbol(elf, r->r_addend, sym);
1304 tosym = sym_name(elf, to);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001305
1306 /* check whitelist - we may ignore it */
1307 if (secref_whitelist(fromsec, fromsym, tosec, tosym)) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001308 report_sec_mismatch(modname, mismatch,
1309 fromsec, r->r_offset, fromsym,
1310 is_function(from), tosec, tosym,
1311 is_function(to));
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001312 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001313 }
1314}
1315
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001316static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001317 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001318{
1319 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001320 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001321
1322 return (void *)elf->hdr + sechdrs[section].sh_offset +
1323 (r->r_offset - sechdrs[section].sh_addr);
1324}
1325
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001326static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001327{
1328 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001329 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001330
1331 switch (r_typ) {
1332 case R_386_32:
1333 r->r_addend = TO_NATIVE(*location);
1334 break;
1335 case R_386_PC32:
1336 r->r_addend = TO_NATIVE(*location) + 4;
1337 /* For CONFIG_RELOCATABLE=y */
1338 if (elf->hdr->e_type == ET_EXEC)
1339 r->r_addend += r->r_offset;
1340 break;
1341 }
1342 return 0;
1343}
1344
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001345static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001346{
1347 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1348
1349 switch (r_typ) {
1350 case R_ARM_ABS32:
1351 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001352 r->r_addend = (int)(long)
1353 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001354 break;
1355 case R_ARM_PC24:
1356 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001357 r->r_addend = (int)(long)(elf->hdr +
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001358 sechdr->sh_offset +
1359 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001360 break;
1361 default:
1362 return 1;
1363 }
1364 return 0;
1365}
1366
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001367static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001368{
1369 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001370 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001371 unsigned int inst;
1372
1373 if (r_typ == R_MIPS_HI16)
1374 return 1; /* skip this */
1375 inst = TO_NATIVE(*location);
1376 switch (r_typ) {
1377 case R_MIPS_LO16:
1378 r->r_addend = inst & 0xffff;
1379 break;
1380 case R_MIPS_26:
1381 r->r_addend = (inst & 0x03ffffff) << 2;
1382 break;
1383 case R_MIPS_32:
1384 r->r_addend = inst;
1385 break;
1386 }
1387 return 0;
1388}
1389
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001390static void section_rela(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001391 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001392{
1393 Elf_Sym *sym;
1394 Elf_Rela *rela;
1395 Elf_Rela r;
1396 unsigned int r_sym;
1397 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001398
Sam Ravnborgff13f922008-01-23 19:54:27 +01001399 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001400 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1401
Sam Ravnborgff13f922008-01-23 19:54:27 +01001402 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001403 fromsec += strlen(".rela");
1404 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001405 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001406 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001407
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001408 for (rela = start; rela < stop; rela++) {
1409 r.r_offset = TO_NATIVE(rela->r_offset);
1410#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001411 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001412 unsigned int r_typ;
1413 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1414 r_sym = TO_NATIVE(r_sym);
1415 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1416 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1417 } else {
1418 r.r_info = TO_NATIVE(rela->r_info);
1419 r_sym = ELF_R_SYM(r.r_info);
1420 }
1421#else
1422 r.r_info = TO_NATIVE(rela->r_info);
1423 r_sym = ELF_R_SYM(r.r_info);
1424#endif
1425 r.r_addend = TO_NATIVE(rela->r_addend);
1426 sym = elf->symtab_start + r_sym;
1427 /* Skip special sections */
1428 if (sym->st_shndx >= SHN_LORESERVE)
1429 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001430 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001431 }
1432}
1433
1434static void section_rel(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001435 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001436{
1437 Elf_Sym *sym;
1438 Elf_Rel *rel;
1439 Elf_Rela r;
1440 unsigned int r_sym;
1441 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001442
Sam Ravnborgff13f922008-01-23 19:54:27 +01001443 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001444 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1445
Sam Ravnborgff13f922008-01-23 19:54:27 +01001446 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001447 fromsec += strlen(".rel");
1448 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001449 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001450 return;
1451
1452 for (rel = start; rel < stop; rel++) {
1453 r.r_offset = TO_NATIVE(rel->r_offset);
1454#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001455 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001456 unsigned int r_typ;
1457 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1458 r_sym = TO_NATIVE(r_sym);
1459 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1460 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1461 } else {
1462 r.r_info = TO_NATIVE(rel->r_info);
1463 r_sym = ELF_R_SYM(r.r_info);
1464 }
1465#else
1466 r.r_info = TO_NATIVE(rel->r_info);
1467 r_sym = ELF_R_SYM(r.r_info);
1468#endif
1469 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001470 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001471 case EM_386:
1472 if (addend_386_rel(elf, sechdr, &r))
1473 continue;
1474 break;
1475 case EM_ARM:
1476 if (addend_arm_rel(elf, sechdr, &r))
1477 continue;
1478 break;
1479 case EM_MIPS:
1480 if (addend_mips_rel(elf, sechdr, &r))
1481 continue;
1482 break;
1483 }
1484 sym = elf->symtab_start + r_sym;
1485 /* Skip special sections */
1486 if (sym->st_shndx >= SHN_LORESERVE)
1487 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001488 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001489 }
1490}
1491
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001492/**
1493 * A module includes a number of sections that are discarded
1494 * either when loaded or when used as built-in.
1495 * For loaded modules all functions marked __init and all data
1496 * marked __initdata will be discarded when the module has been intialized.
1497 * Likewise for modules used built-in the sections marked __exit
1498 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001499 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001500 * The check_sec_ref() function traverses all relocation records
1501 * to find all references to a section that reference a section that will
1502 * be discarded and warns about it.
1503 **/
1504static void check_sec_ref(struct module *mod, const char *modname,
Sam Ravnborg10668222008-01-13 22:21:31 +01001505 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001506{
1507 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001508 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001509
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001510 /* Walk through all sections */
Sam Ravnborgff13f922008-01-23 19:54:27 +01001511 for (i = 0; i < elf->hdr->e_shnum; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001512 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001513 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001514 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001515 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001516 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001517 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001518 }
1519}
1520
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001521static void get_markers(struct elf_info *info, struct module *mod)
1522{
1523 const Elf_Shdr *sh = &info->sechdrs[info->markers_strings_sec];
1524 const char *strings = (const char *) info->hdr + sh->sh_offset;
1525 const Elf_Sym *sym, *first_sym, *last_sym;
1526 size_t n;
1527
1528 if (!info->markers_strings_sec)
1529 return;
1530
1531 /*
1532 * First count the strings. We look for all the symbols defined
1533 * in the __markers_strings section named __mstrtab_*. For
1534 * these local names, the compiler puts a random .NNN suffix on,
1535 * so the names don't correspond exactly.
1536 */
1537 first_sym = last_sym = NULL;
1538 n = 0;
1539 for (sym = info->symtab_start; sym < info->symtab_stop; sym++)
1540 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1541 sym->st_shndx == info->markers_strings_sec &&
1542 !strncmp(info->strtab + sym->st_name,
1543 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1544 if (first_sym == NULL)
1545 first_sym = sym;
1546 last_sym = sym;
1547 ++n;
1548 }
1549
1550 if (n == 0)
1551 return;
1552
1553 /*
1554 * Now collect each name and format into a line for the output.
1555 * Lines look like:
1556 * marker_name vmlinux marker %s format %d
1557 * The format string after the second \t can use whitespace.
1558 */
1559 mod->markers = NOFAIL(malloc(sizeof mod->markers[0] * n));
1560 mod->nmarkers = n;
1561
1562 n = 0;
1563 for (sym = first_sym; sym <= last_sym; sym++)
1564 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT &&
1565 sym->st_shndx == info->markers_strings_sec &&
1566 !strncmp(info->strtab + sym->st_name,
1567 "__mstrtab_", sizeof "__mstrtab_" - 1)) {
1568 const char *name = strings + sym->st_value;
1569 const char *fmt = strchr(name, '\0') + 1;
1570 char *line = NULL;
1571 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1572 NOFAIL(line);
1573 mod->markers[n++] = line;
1574 }
1575}
1576
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001577static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 const char *symname;
1580 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001581 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 struct module *mod;
1583 struct elf_info info = { };
1584 Elf_Sym *sym;
1585
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001586 if (!parse_elf(&info, modname))
1587 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 mod = new_module(modname);
1590
1591 /* When there's no vmlinux, don't print warnings about
1592 * unresolved symbols (since there'll be too many ;) */
1593 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 mod->skip = 1;
1596 }
1597
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001598 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
Sam Ravnborg2fa36562008-04-26 21:07:26 +02001599 if (info.modinfo && !license && !is_vmlinux(modname))
1600 warn("modpost: missing MODULE_LICENSE() in %s\n"
1601 "see include/linux/module.h for "
1602 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001603 while (license) {
1604 if (license_is_gpl_compatible(license))
1605 mod->gpl_compatible = 1;
1606 else {
1607 mod->gpl_compatible = 0;
1608 break;
1609 }
1610 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1611 "license", license);
1612 }
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1615 symname = info.strtab + sym->st_name;
1616
1617 handle_modversions(mod, &info, sym, symname);
1618 handle_moddevtable(mod, &info, sym, symname);
1619 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001620 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001621 (is_vmlinux(modname) && vmlinux_section_warnings))
1622 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1625 if (version)
1626 maybe_frob_rcs_version(modname, version, info.modinfo,
1627 version - (char *)info.hdr);
1628 if (version || (all_versions && !is_vmlinux(modname)))
1629 get_src_version(modname, mod->srcversion,
1630 sizeof(mod->srcversion)-1);
1631
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001632 get_markers(&info, mod);
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 parse_elf_finish(&info);
1635
Rusty Russell8c8ef422009-03-31 13:05:34 -06001636 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 * never passed as an argument to an exported function, so
1638 * the automatic versioning doesn't pick it up, but it's really
1639 * important anyhow */
1640 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06001641 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
1644#define SZ 500
1645
1646/* We first write the generated file into memory using the
1647 * following helper, then compare to the file on disk and
1648 * only update the later if anything changed */
1649
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001650void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1651 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 char tmp[SZ];
1654 int len;
1655 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 va_start(ap, fmt);
1658 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001659 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 va_end(ap);
1661}
1662
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001663void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001666 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 buf->p = realloc(buf->p, buf->size);
1668 }
1669 strncpy(buf->p + buf->pos, s, len);
1670 buf->pos += len;
1671}
1672
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001673static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1674{
1675 const char *e = is_vmlinux(m) ?"":".ko";
1676
1677 switch (exp) {
1678 case export_gpl:
1679 fatal("modpost: GPL-incompatible module %s%s "
1680 "uses GPL-only symbol '%s'\n", m, e, s);
1681 break;
1682 case export_unused_gpl:
1683 fatal("modpost: GPL-incompatible module %s%s "
1684 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1685 break;
1686 case export_gpl_future:
1687 warn("modpost: GPL-incompatible module %s%s "
1688 "uses future GPL-only symbol '%s'\n", m, e, s);
1689 break;
1690 case export_plain:
1691 case export_unused:
1692 case export_unknown:
1693 /* ignore */
1694 break;
1695 }
1696}
1697
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001698static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001699{
1700 const char *e = is_vmlinux(m) ?"":".ko";
1701
1702 switch (exp) {
1703 case export_unused:
1704 case export_unused_gpl:
1705 warn("modpost: module %s%s "
1706 "uses symbol '%s' marked UNUSED\n", m, e, s);
1707 break;
1708 default:
1709 /* ignore */
1710 break;
1711 }
1712}
1713
1714static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001715{
1716 struct symbol *s, *exp;
1717
1718 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001719 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001720 exp = find_symbol(s->name);
1721 if (!exp || exp->module == mod)
1722 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001723 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001724 if (basename)
1725 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001726 else
1727 basename = mod->name;
1728 if (!mod->gpl_compatible)
1729 check_for_gpl_usage(exp->export, basename, exp->name);
1730 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001731 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001732}
1733
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001734/**
1735 * Header for the generated file
1736 **/
1737static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 buf_printf(b, "#include <linux/module.h>\n");
1740 buf_printf(b, "#include <linux/vermagic.h>\n");
1741 buf_printf(b, "#include <linux/compiler.h>\n");
1742 buf_printf(b, "\n");
1743 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1744 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 buf_printf(b, "struct module __this_module\n");
1746 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001747 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (mod->has_init)
1749 buf_printf(b, " .init = init_module,\n");
1750 if (mod->has_cleanup)
1751 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1752 " .exit = cleanup_module,\n"
1753 "#endif\n");
Roman Zippele61a1c12007-05-09 02:35:15 -07001754 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 buf_printf(b, "};\n");
1756}
1757
Trevor Keith5c725132009-09-22 16:43:38 -07001758static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07001759{
1760 static const char *staging_dir = "drivers/staging";
1761
1762 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
1763 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1764}
1765
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001766/**
1767 * Record CRCs for unresolved symbols
1768 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001769static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001772 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 for (s = mod->unres; s; s = s->next) {
1775 exp = find_symbol(s->name);
1776 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001777 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001778 if (warn_unresolved) {
1779 warn("\"%s\" [%s.ko] undefined!\n",
1780 s->name, mod->name);
1781 } else {
1782 merror("\"%s\" [%s.ko] undefined!\n",
1783 s->name, mod->name);
1784 err = 1;
1785 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 continue;
1788 }
1789 s->module = exp->module;
1790 s->crc_valid = exp->crc_valid;
1791 s->crc = exp->crc;
1792 }
1793
1794 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001795 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 buf_printf(b, "\n");
1798 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001799 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1801
1802 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001803 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001806 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 s->name, mod->name);
1808 continue;
1809 }
1810 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1811 }
1812
1813 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001814
1815 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001818static void add_depends(struct buffer *b, struct module *mod,
1819 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 struct symbol *s;
1822 struct module *m;
1823 int first = 1;
1824
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001825 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 buf_printf(b, "\n");
1829 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001830 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1832 buf_printf(b, "\"depends=");
1833 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001834 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (!s->module)
1836 continue;
1837
1838 if (s->module->seen)
1839 continue;
1840
1841 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001842 p = strrchr(s->module->name, '/');
1843 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001844 p++;
1845 else
1846 p = s->module->name;
1847 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 first = 0;
1849 }
1850 buf_printf(b, "\";\n");
1851}
1852
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001853static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 if (mod->srcversion[0]) {
1856 buf_printf(b, "\n");
1857 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1858 mod->srcversion);
1859 }
1860}
1861
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001862static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 char *tmp;
1865 FILE *file;
1866 struct stat st;
1867
1868 file = fopen(fname, "r");
1869 if (!file)
1870 goto write;
1871
1872 if (fstat(fileno(file), &st) < 0)
1873 goto close_write;
1874
1875 if (st.st_size != b->pos)
1876 goto close_write;
1877
1878 tmp = NOFAIL(malloc(b->pos));
1879 if (fread(tmp, 1, b->pos, file) != b->pos)
1880 goto free_write;
1881
1882 if (memcmp(tmp, b->p, b->pos) != 0)
1883 goto free_write;
1884
1885 free(tmp);
1886 fclose(file);
1887 return;
1888
1889 free_write:
1890 free(tmp);
1891 close_write:
1892 fclose(file);
1893 write:
1894 file = fopen(fname, "w");
1895 if (!file) {
1896 perror(fname);
1897 exit(1);
1898 }
1899 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
1900 perror(fname);
1901 exit(1);
1902 }
1903 fclose(file);
1904}
1905
Ram Paibd5cbce2006-06-08 22:12:53 -07001906/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001907 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07001908 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001909static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 unsigned long size, pos = 0;
1912 void *file = grab_file(fname, &size);
1913 char *line;
1914
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001915 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /* No symbol versions, silently ignore */
1917 return;
1918
1919 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001920 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 unsigned int crc;
1922 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001923 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (!(symname = strchr(line, '\t')))
1926 goto fail;
1927 *symname++ = '\0';
1928 if (!(modname = strchr(symname, '\t')))
1929 goto fail;
1930 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02001931 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07001932 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02001933 if (export && ((end = strchr(export, '\t')) != NULL))
1934 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 crc = strtoul(line, &d, 16);
1936 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1937 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001938 mod = find_module(modname);
1939 if (!mod) {
1940 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00001942 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 mod->skip = 1;
1944 }
Ram Paibd5cbce2006-06-08 22:12:53 -07001945 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001946 s->kernel = kernel;
1947 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07001948 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 return;
1951fail:
1952 fatal("parse error in symbol dump file\n");
1953}
1954
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001955/* For normal builds always dump all symbols.
1956 * For external modules only dump symbols
1957 * that are not read from kernel Module.symvers.
1958 **/
1959static int dump_sym(struct symbol *sym)
1960{
1961 if (!external_module)
1962 return 1;
1963 if (sym->vmlinux || sym->kernel)
1964 return 0;
1965 return 1;
1966}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001967
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001968static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 struct buffer buf = { };
1971 struct symbol *symbol;
1972 int n;
1973
1974 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1975 symbol = symbolhash[n];
1976 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001977 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07001978 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001979 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07001980 symbol->module->name,
1981 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 symbol = symbol->next;
1983 }
1984 }
1985 write_if_changed(&buf, fname);
1986}
1987
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08001988static void add_marker(struct module *mod, const char *name, const char *fmt)
1989{
1990 char *line = NULL;
1991 asprintf(&line, "%s\t%s\t%s\n", name, mod->name, fmt);
1992 NOFAIL(line);
1993
1994 mod->markers = NOFAIL(realloc(mod->markers, ((mod->nmarkers + 1) *
1995 sizeof mod->markers[0])));
1996 mod->markers[mod->nmarkers++] = line;
1997}
1998
1999static void read_markers(const char *fname)
2000{
2001 unsigned long size, pos = 0;
2002 void *file = grab_file(fname, &size);
2003 char *line;
2004
2005 if (!file) /* No old markers, silently ignore */
2006 return;
2007
2008 while ((line = get_next_line(&pos, file, size))) {
2009 char *marker, *modname, *fmt;
2010 struct module *mod;
2011
2012 marker = line;
2013 modname = strchr(marker, '\t');
2014 if (!modname)
2015 goto fail;
2016 *modname++ = '\0';
2017 fmt = strchr(modname, '\t');
2018 if (!fmt)
2019 goto fail;
2020 *fmt++ = '\0';
2021 if (*marker == '\0' || *modname == '\0')
2022 goto fail;
2023
2024 mod = find_module(modname);
2025 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002026 mod = new_module(modname);
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002027 mod->skip = 1;
2028 }
Mathieu Desnoyers87f3b6b2008-10-06 09:30:12 -04002029 if (is_vmlinux(modname)) {
2030 have_vmlinux = 1;
2031 mod->skip = 0;
2032 }
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002033
Mathieu Desnoyersd35cb362008-07-21 14:21:38 -07002034 if (!mod->skip)
2035 add_marker(mod, marker, fmt);
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002036 }
Cedric Hombourger99e3a1e2009-04-25 09:38:21 +02002037 release_file(file, size);
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002038 return;
2039fail:
2040 fatal("parse error in markers list file\n");
2041}
2042
2043static int compare_strings(const void *a, const void *b)
2044{
2045 return strcmp(*(const char **) a, *(const char **) b);
2046}
2047
2048static void write_markers(const char *fname)
2049{
2050 struct buffer buf = { };
2051 struct module *mod;
2052 size_t i;
2053
2054 for (mod = modules; mod; mod = mod->next)
2055 if ((!external_module || !mod->skip) && mod->markers != NULL) {
2056 /*
2057 * Sort the strings so we can skip duplicates when
2058 * we write them out.
2059 */
2060 qsort(mod->markers, mod->nmarkers,
2061 sizeof mod->markers[0], &compare_strings);
2062 for (i = 0; i < mod->nmarkers; ++i) {
2063 char *line = mod->markers[i];
2064 buf_write(&buf, line, strlen(line));
2065 while (i + 1 < mod->nmarkers &&
2066 !strcmp(mod->markers[i],
2067 mod->markers[i + 1]))
2068 free(mod->markers[i++]);
2069 free(mod->markers[i]);
2070 }
2071 free(mod->markers);
2072 mod->markers = NULL;
2073 }
2074
2075 write_if_changed(&buf, fname);
2076}
2077
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002078struct ext_sym_list {
2079 struct ext_sym_list *next;
2080 const char *file;
2081};
2082
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002083int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 struct module *mod;
2086 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002087 char *kernel_read = NULL, *module_read = NULL;
2088 char *dump_write = NULL;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002089 char *markers_read = NULL;
2090 char *markers_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002092 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002093 struct ext_sym_list *extsym_iter;
2094 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002096 while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002097 switch (opt) {
2098 case 'i':
2099 kernel_read = optarg;
2100 break;
2101 case 'I':
2102 module_read = optarg;
2103 external_module = 1;
2104 break;
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +01002105 case 'c':
2106 cross_build = 1;
2107 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002108 case 'e':
2109 external_module = 1;
2110 extsym_iter =
2111 NOFAIL(malloc(sizeof(*extsym_iter)));
2112 extsym_iter->next = extsym_start;
2113 extsym_iter->file = optarg;
2114 extsym_start = extsym_iter;
2115 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002116 case 'm':
2117 modversions = 1;
2118 break;
2119 case 'o':
2120 dump_write = optarg;
2121 break;
2122 case 'a':
2123 all_versions = 1;
2124 break;
2125 case 's':
2126 vmlinux_section_warnings = 0;
2127 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002128 case 'S':
2129 sec_mismatch_verbose = 0;
2130 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002131 case 'w':
2132 warn_unresolved = 1;
2133 break;
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002134 case 'M':
2135 markers_write = optarg;
2136 break;
2137 case 'K':
2138 markers_read = optarg;
2139 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002140 default:
2141 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 }
2144
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002145 if (kernel_read)
2146 read_dump(kernel_read, 1);
2147 if (module_read)
2148 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002149 while (extsym_start) {
2150 read_dump(extsym_start->file, 0);
2151 extsym_iter = extsym_start->next;
2152 free(extsym_start);
2153 extsym_start = extsym_iter;
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002156 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 for (mod = modules; mod; mod = mod->next) {
2160 if (mod->skip)
2161 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002162 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002163 }
2164
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002165 err = 0;
2166
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002167 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01002168 char fname[strlen(mod->name) + 10];
2169
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002170 if (mod->skip)
2171 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 buf.pos = 0;
2174
2175 add_header(&buf, mod);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002176 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002177 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 add_depends(&buf, mod, modules);
2179 add_moddevtable(&buf, mod);
2180 add_srcversion(&buf, mod);
2181
2182 sprintf(fname, "%s.mod.c", mod->name);
2183 write_if_changed(&buf, fname);
2184 }
2185
2186 if (dump_write)
2187 write_dump(dump_write);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002188 if (sec_mismatch_count && !sec_mismatch_verbose)
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01002189 warn("modpost: Found %d section mismatch(es).\n"
2190 "To see full details build your kernel with:\n"
2191 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2192 sec_mismatch_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -08002194 if (markers_read)
2195 read_markers(markers_read);
2196
2197 if (markers_write)
2198 write_markers(markers_write);
2199
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}