blob: 78b70d8af600a96d2d416b30484f7db8dd48c69a [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>
Andrew Morton5003bab2010-08-11 00:42:26 -070017#include <string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "modpost.h"
Linus Torvalds5a865c02009-12-17 07:23:42 -080019#include "../../include/generated/autoconf.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020020#include "../../include/linux/license.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000022/* Some toolchains use a `_' prefix for all user symbols. */
23#ifdef CONFIG_SYMBOL_PREFIX
24#define MODULE_SYMBOL_PREFIX CONFIG_SYMBOL_PREFIX
25#else
26#define MODULE_SYMBOL_PREFIX ""
27#endif
28
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/* Are we using CONFIG_MODVERSIONS? */
31int modversions = 0;
32/* Warn about undefined symbols? (do so if we have vmlinux) */
33int have_vmlinux = 0;
34/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
35static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010036/* If we are modposting external module set to 1 */
37static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020038/* Warn about section mismatch in vmlinux if set to 1 */
39static int vmlinux_section_warnings = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040/* Exit with an error when there is a section mismatch if set to 1 */
41static int section_error_on_mismatch;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070042/* Only warn about unresolved symbols */
43static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070044/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010045static int sec_mismatch_count = 0;
46static int sec_mismatch_verbose = 1;
47
Sam Ravnborgc96fca22006-07-01 11:44:23 +020048enum export {
49 export_plain, export_unused, export_gpl,
50 export_unused_gpl, export_gpl_future, export_unknown
51};
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Andi Kleen6d9a89e2007-11-22 03:43:08 +010053#define PRINTF __attribute__ ((format (printf, 1, 2)))
54
55PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
59 fprintf(stderr, "FATAL: ");
60
61 va_start(arglist, fmt);
62 vfprintf(stderr, fmt, arglist);
63 va_end(arglist);
64
65 exit(1);
66}
67
Andi Kleen6d9a89e2007-11-22 03:43:08 +010068PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 va_list arglist;
71
72 fprintf(stderr, "WARNING: ");
73
74 va_start(arglist, fmt);
75 vfprintf(stderr, fmt, arglist);
76 va_end(arglist);
77}
78
Andi Kleen6d9a89e2007-11-22 03:43:08 +010079PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060080{
81 va_list arglist;
82
83 fprintf(stderr, "ERROR: ");
84
85 va_start(arglist, fmt);
86 vfprintf(stderr, fmt, arglist);
87 va_end(arglist);
88}
89
Sam Ravnborg040fcc82006-01-28 22:15:55 +010090static int is_vmlinux(const char *modname)
91{
92 const char *myname;
93
Sam Ravnborgdf578e72008-01-11 19:17:15 +010094 myname = strrchr(modname, '/');
95 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010096 myname++;
97 else
98 myname = modname;
99
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200100 return (strcmp(myname, "vmlinux") == 0) ||
101 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104void *do_nofail(void *ptr, const char *expr)
105{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100106 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return ptr;
110}
111
112/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static struct module *modules;
114
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100115static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct module *mod;
118
119 for (mod = modules; mod; mod = mod->next)
120 if (strcmp(mod->name, modname) == 0)
121 break;
122 return mod;
123}
124
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100125static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct module *mod;
128 char *p, *s;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 mod = NOFAIL(malloc(sizeof(*mod)));
131 memset(mod, 0, sizeof(*mod));
132 p = NOFAIL(strdup(modname));
133
134 /* strip trailing .o */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100135 s = strrchr(p, '.');
136 if (s != NULL)
Frank Rowand258f7422012-04-09 17:59:03 -0700137 if (strcmp(s, ".o") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *s = '\0';
Frank Rowand258f7422012-04-09 17:59:03 -0700139 mod->is_dot_o = 1;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /* add to list */
143 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200144 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 mod->next = modules;
146 modules = mod;
147
148 return mod;
149}
150
151/* A hash of all exported symbols,
152 * struct symbol is also used for lists of unresolved symbols */
153
154#define SYMBOL_HASH_SIZE 1024
155
156struct symbol {
157 struct symbol *next;
158 struct module *module;
159 unsigned int crc;
160 int crc_valid;
161 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100162 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
163 unsigned int kernel:1; /* 1 if symbol is from kernel
164 * (only for external modules) **/
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100165 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Ram Paibd5cbce2006-06-08 22:12:53 -0700166 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 char name[0];
168};
169
170static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
171
172/* This is based on the hash agorithm from gdbm, via tdb */
173static inline unsigned int tdb_hash(const char *name)
174{
175 unsigned value; /* Used to compute the hash value. */
176 unsigned i; /* Used to cycle through random values. */
177
178 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100179 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
181
182 return (1103515243 * value + 12345);
183}
184
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100185/**
186 * Allocate a new symbols for use in the hash of exported symbols or
187 * the list of unresolved symbols per module
188 **/
189static struct symbol *alloc_symbol(const char *name, unsigned int weak,
190 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
193
194 memset(s, 0, sizeof(*s));
195 strcpy(s->name, name);
196 s->weak = weak;
197 s->next = next;
198 return s;
199}
200
201/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700202static struct symbol *new_symbol(const char *name, struct module *module,
203 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 unsigned int hash;
206 struct symbol *new;
207
208 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
209 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
210 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700211 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100212 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100215static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct symbol *s;
218
219 /* For our purposes, .foo matches foo. PPC64 needs this. */
220 if (name[0] == '.')
221 name++;
222
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100223 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (strcmp(s->name, name) == 0)
225 return s;
226 }
227 return NULL;
228}
229
Ram Paibd5cbce2006-06-08 22:12:53 -0700230static struct {
231 const char *str;
232 enum export export;
233} export_list[] = {
234 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200235 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700236 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200237 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700238 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
239 { .str = "(unknown)", .export = export_unknown },
240};
241
242
243static const char *export_str(enum export ex)
244{
245 return export_list[ex].str;
246}
247
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100248static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700249{
250 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100251
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200252 if (!s)
253 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700254 for (i = 0; export_list[i].export != export_unknown; i++) {
255 if (strcmp(export_list[i].str, s) == 0)
256 return export_list[i].export;
257 }
258 return export_unknown;
259}
260
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200261static const char *sec_name(struct elf_info *elf, int secindex);
262
263#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
264
265static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
266{
267 const char *secname = sec_name(elf, sec);
268
269 if (strstarts(secname, "___ksymtab+"))
270 return export_plain;
271 else if (strstarts(secname, "___ksymtab_unused+"))
272 return export_unused;
273 else if (strstarts(secname, "___ksymtab_gpl+"))
274 return export_gpl;
275 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
276 return export_unused_gpl;
277 else if (strstarts(secname, "___ksymtab_gpl_future+"))
278 return export_gpl_future;
279 else
280 return export_unknown;
281}
282
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200283static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700284{
285 if (sec == elf->export_sec)
286 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200287 else if (sec == elf->export_unused_sec)
288 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700289 else if (sec == elf->export_gpl_sec)
290 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200291 else if (sec == elf->export_unused_gpl_sec)
292 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700293 else if (sec == elf->export_gpl_future_sec)
294 return export_gpl_future;
295 else
296 return export_unknown;
297}
298
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100299/**
300 * Add an exported symbol - it may have already been added without a
301 * CRC, in this case just update the CRC
302 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700303static struct symbol *sym_add_exported(const char *name, struct module *mod,
304 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 struct symbol *s = find_symbol(name);
307
308 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700309 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100310 } else {
311 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100312 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100313 "was in %s%s\n", mod->name, name,
314 s->module->name,
315 is_vmlinux(s->module->name) ?"":".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700316 } else {
317 /* In case Modules.symvers was out of date */
318 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100321 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100322 s->vmlinux = is_vmlinux(mod->name);
323 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700324 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100325 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100328static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700329 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100330{
331 struct symbol *s = find_symbol(name);
332
333 if (!s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700334 s = new_symbol(name, mod, export);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100335 s->crc = crc;
336 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100339void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct stat st;
342 void *map;
343 int fd;
344
345 fd = open(filename, O_RDONLY);
346 if (fd < 0 || fstat(fd, &st) != 0)
347 return NULL;
348
349 *size = st.st_size;
350 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
351 close(fd);
352
353 if (map == MAP_FAILED)
354 return NULL;
355 return map;
356}
357
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100358/**
359 * Return a copy of the next line in a mmap'ed file.
360 * spaces in the beginning of the line is trimmed away.
361 * Return a pointer to a static buffer.
362 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100363char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 static char line[4096];
366 int skip = 1;
367 size_t len = 0;
368 signed char *p = (signed char *)file + *pos;
369 char *s = line;
370
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100371 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (skip && isspace(*p)) {
373 p++;
374 continue;
375 }
376 skip = 0;
377 if (*p != '\n' && (*pos < size)) {
378 len++;
379 *s++ = *p++;
380 if (len > 4095)
381 break; /* Too long, stop */
382 } else {
383 /* End of string */
384 *s = '\0';
385 return line;
386 }
387 }
388 /* End of buffer */
389 return NULL;
390}
391
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100392void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 munmap(file, size);
395}
396
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100397static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100400 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 Elf_Shdr *sechdrs;
402 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200403 const char *secstrings;
404 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 hdr = grab_file(filename, &info->size);
407 if (!hdr) {
408 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200409 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100412 if (info->size < sizeof(*hdr)) {
413 /* file too small, assume this is an empty .o file */
414 return 0;
415 }
416 /* Is this a valid ELF file? */
417 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
418 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
419 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
420 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
421 /* Not an ELF file - silently ignore it */
422 return 0;
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200425 hdr->e_type = TO_NATIVE(hdr->e_type);
426 hdr->e_machine = TO_NATIVE(hdr->e_machine);
427 hdr->e_version = TO_NATIVE(hdr->e_version);
428 hdr->e_entry = TO_NATIVE(hdr->e_entry);
429 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
430 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
431 hdr->e_flags = TO_NATIVE(hdr->e_flags);
432 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
433 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
434 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
435 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
436 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
437 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 sechdrs = (void *)hdr + hdr->e_shoff;
439 info->sechdrs = sechdrs;
440
Petr Stetiara83710e2007-08-27 12:15:07 +0200441 /* Check if file offset is correct */
442 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100443 fatal("section header offset=%lu in file '%s' is bigger than "
444 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
445 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200446 return 0;
447 }
448
Anders Kaseorg68457562011-05-19 16:55:27 -0600449 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200450 /*
451 * There are more than 64k sections,
452 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200453 */
454 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
455 }
456 else {
457 info->num_sections = hdr->e_shnum;
458 }
459 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600460 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200461 }
462 else {
463 info->secindex_strings = hdr->e_shstrndx;
464 }
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200467 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200468 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
469 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
470 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
471 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
472 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
473 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
474 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
475 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
476 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
477 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200480 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
481 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700482 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900483 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Tejun Heo56fc82c2009-02-06 00:48:02 +0900485 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100486 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
487 "sizeof(*hrd)=%zu\n", filename,
488 (unsigned long)sechdrs[i].sh_offset,
489 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100490 return 0;
491 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700492 secname = secstrings + sechdrs[i].sh_name;
493 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900494 if (nobits)
495 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
497 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700498 } else if (strcmp(secname, "__ksymtab") == 0)
499 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200500 else if (strcmp(secname, "__ksymtab_unused") == 0)
501 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700502 else if (strcmp(secname, "__ksymtab_gpl") == 0)
503 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200504 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
505 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700506 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
507 info->export_gpl_future_sec = i;
508
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200509 if (sechdrs[i].sh_type == SHT_SYMTAB) {
510 unsigned int sh_link_idx;
511 symtab_idx = i;
512 info->symtab_start = (void *)hdr +
513 sechdrs[i].sh_offset;
514 info->symtab_stop = (void *)hdr +
515 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600516 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200517 info->strtab = (void *)hdr +
518 sechdrs[sh_link_idx].sh_offset;
519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200521 /* 32bit section no. table? ("more than 64k sections") */
522 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
523 symtab_shndx_idx = i;
524 info->symtab_shndx_start = (void *)hdr +
525 sechdrs[i].sh_offset;
526 info->symtab_shndx_stop = (void *)hdr +
527 sechdrs[i].sh_offset + sechdrs[i].sh_size;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100530 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100531 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* Fix endianness in symbols */
534 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
535 sym->st_shndx = TO_NATIVE(sym->st_shndx);
536 sym->st_name = TO_NATIVE(sym->st_name);
537 sym->st_value = TO_NATIVE(sym->st_value);
538 sym->st_size = TO_NATIVE(sym->st_size);
539 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200540
541 if (symtab_shndx_idx != ~0U) {
542 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600543 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200544 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600545 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200546 symtab_idx);
547 /* Fix endianness */
548 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
549 p++)
550 *p = TO_NATIVE(*p);
551 }
552
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100553 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100556static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 release_file(info->hdr, info->size);
559}
560
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200561static int ignore_undef_symbol(struct elf_info *info, const char *symname)
562{
563 /* ignore __this_module, it will be resolved shortly */
564 if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0)
565 return 1;
566 /* ignore global offset table */
567 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
568 return 1;
569 if (info->hdr->e_machine == EM_PPC)
570 /* Special register function linked on all modules during final link of .ko */
571 if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
572 strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
573 strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
574 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
575 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000576 if (info->hdr->e_machine == EM_PPC64)
577 /* Special register function linked on all modules during final link of .ko */
578 if (strncmp(symname, "_restgpr0_", sizeof("_restgpr0_") - 1) == 0 ||
579 strncmp(symname, "_savegpr0_", sizeof("_savegpr0_") - 1) == 0)
580 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200581 /* Do not ignore this symbol */
582 return 0;
583}
584
Luke Yangf7b05e62006-03-02 18:35:31 +0800585#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
586#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100588static void handle_modversions(struct module *mod, struct elf_info *info,
589 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200592 enum export export;
593
Frank Rowand258f7422012-04-09 17:59:03 -0700594 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
595 strncmp(symname, "__ksymtab", 9) == 0)
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200596 export = export_from_secname(info, get_secindex(info, sym));
597 else
598 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 switch (sym->st_shndx) {
601 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100602 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 break;
604 case SHN_ABS:
605 /* CRC'd symbol */
Michal Marek8d995132009-12-12 12:02:24 +0100606 if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700608 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
609 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611 break;
612 case SHN_UNDEF:
613 /* undefined symbol */
614 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
615 ELF_ST_BIND(sym->st_info) != STB_WEAK)
616 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200617 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
Ben Colline8d529012005-08-19 13:44:57 -0700619/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
620#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
621/* add compatibility with older glibc */
622#ifndef STT_SPARC_REGISTER
623#define STT_SPARC_REGISTER STT_REGISTER
624#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (info->hdr->e_machine == EM_SPARC ||
626 info->hdr->e_machine == EM_SPARCV9) {
627 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700628 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100630 if (symname[0] == '.') {
631 char *munged = strdup(symname);
632 munged[0] = '_';
633 munged[1] = toupper(munged[1]);
634 symname = munged;
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100640 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
641 mod->unres =
642 alloc_symbol(symname +
643 strlen(MODULE_SYMBOL_PREFIX),
644 ELF_ST_BIND(sym->st_info) == STB_WEAK,
645 mod->unres);
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
648 default:
649 /* All exported symbols */
Michal Marek8d995132009-12-12 12:02:24 +0100650 if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700651 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
652 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
655 mod->has_init = 1;
656 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
657 mod->has_cleanup = 1;
658 break;
659 }
660}
661
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100662/**
663 * Parse tag=value strings from .modinfo section
664 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665static char *next_string(char *string, unsigned long *secsize)
666{
667 /* Skip non-zero chars */
668 while (string[0]) {
669 string++;
670 if ((*secsize)-- <= 1)
671 return NULL;
672 }
673
674 /* Skip any zero padding. */
675 while (!string[0]) {
676 string++;
677 if ((*secsize)-- <= 1)
678 return NULL;
679 }
680 return string;
681}
682
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200683static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
684 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 char *p;
687 unsigned int taglen = strlen(tag);
688 unsigned long size = modinfo_len;
689
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200690 if (info) {
691 size -= info - (char *)modinfo;
692 modinfo = next_string(info, &size);
693 }
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 for (p = modinfo; p; p = next_string(p, &size)) {
696 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
697 return p + taglen + 1;
698 }
699 return NULL;
700}
701
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200702static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
703 const char *tag)
704
705{
706 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
707}
708
Sam Ravnborg93684d32006-02-19 11:53:35 +0100709/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100710 * Test if string s ends in string sub
711 * return 0 if match
712 **/
713static int strrcmp(const char *s, const char *sub)
714{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100715 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100716
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100717 if (!s || !sub)
718 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100719
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100720 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100721 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100722
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100723 if ((slen == 0) || (sublen == 0))
724 return 1;
725
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100726 if (sublen > slen)
727 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100728
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100729 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100730}
731
Sam Ravnborgff13f922008-01-23 19:54:27 +0100732static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
733{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100734 if (sym)
735 return elf->strtab + sym->st_name;
736 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100737 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100738}
739
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200740static const char *sec_name(struct elf_info *elf, int secindex)
Sam Ravnborgff13f922008-01-23 19:54:27 +0100741{
742 Elf_Shdr *sechdrs = elf->sechdrs;
743 return (void *)elf->hdr +
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200744 elf->sechdrs[elf->secindex_strings].sh_offset +
745 sechdrs[secindex].sh_name;
Sam Ravnborgff13f922008-01-23 19:54:27 +0100746}
747
748static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
749{
750 return (void *)elf->hdr +
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200751 elf->sechdrs[elf->secindex_strings].sh_offset +
752 sechdr->sh_name;
Sam Ravnborgff13f922008-01-23 19:54:27 +0100753}
754
Sam Ravnborg10668222008-01-13 22:21:31 +0100755/* if sym is empty or point to a string
756 * like ".[0-9]+" then return 1.
757 * This is the optional prefix added by ld to some sections
758 */
759static int number_prefix(const char *sym)
760{
761 if (*sym++ == '\0')
762 return 1;
763 if (*sym != '.')
764 return 0;
765 do {
766 char c = *sym++;
767 if (c < '0' || c > '9')
768 return 0;
769 } while (*sym);
770 return 1;
771}
772
773/* The pattern is an array of simple patterns.
774 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100775 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100776 * "foo*" will match a string that begins with "foo"
777 * "foo$" will match a string equal to "foo" or "foo.1"
778 * where the '1' can be any number including several digits.
779 * The $ syntax is for sections where ld append a dot number
780 * to make section name unique.
781 */
Trevor Keith5c725132009-09-22 16:43:38 -0700782static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100783{
784 const char *p;
785 while (*pat) {
786 p = *pat++;
787 const char *endp = p + strlen(p) - 1;
788
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100789 /* "*foo" */
790 if (*p == '*') {
791 if (strrcmp(sym, p + 1) == 0)
792 return 1;
793 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100794 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100795 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100796 if (strncmp(sym, p, strlen(p) - 1) == 0)
797 return 1;
798 }
799 /* "foo$" */
800 else if (*endp == '$') {
801 if (strncmp(sym, p, strlen(p) - 1) == 0) {
802 if (number_prefix(sym + strlen(p) - 1))
803 return 1;
804 }
805 }
806 /* no wildcards */
807 else {
808 if (strcmp(p, sym) == 0)
809 return 1;
810 }
811 }
812 /* no match */
813 return 0;
814}
815
Sam Ravnborg10668222008-01-13 22:21:31 +0100816/* sections that we do not want to do full section mismatch check on */
817static const char *section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200818{
819 ".comment*",
820 ".debug*",
H.J. Lu11215842010-12-15 17:11:22 -0800821 ".zdebug*", /* Compressed debug sections. */
David Howells019fca82010-08-12 16:54:47 +0100822 ".GCC-command-line", /* mn10300 */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200823 ".mdebug*", /* alpha, score, mips etc. */
824 ".pdr", /* alpha, score, mips etc. */
825 ".stab*",
826 ".note*",
827 ".got*",
828 ".toc*",
829 NULL
830};
Sam Ravnborg10668222008-01-13 22:21:31 +0100831
Sam Ravnborge241a632008-01-28 20:13:13 +0100832/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400833 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100834 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400835 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100836 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400837static void check_section(const char *modname, struct elf_info *elf,
838 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100839{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400840 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100841
Anders Kaseorgb614a692009-04-23 16:49:33 -0400842 if (sechdr->sh_type == SHT_PROGBITS &&
843 !(sechdr->sh_flags & SHF_ALLOC) &&
844 !match(sec, section_white_list)) {
845 warn("%s (%s): unexpected non-allocatable section.\n"
846 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
847 "Note that for example <linux/init.h> contains\n"
848 "section definitions for use in .S files.\n\n",
849 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100850 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100851}
852
853
854
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100855#define ALL_INIT_DATA_SECTIONS \
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000856 ".init.setup$", ".init.rodata$", \
Jan Beulich9aaf4402012-03-08 09:41:25 +0000857 ".devinit.rodata$", ".cpuinit.rodata$", ".meminit.rodata$", \
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100858 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
859#define ALL_EXIT_DATA_SECTIONS \
860 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100861
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100862#define ALL_INIT_TEXT_SECTIONS \
863 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
864#define ALL_EXIT_TEXT_SECTIONS \
865 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100866
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100867#define ALL_XXXINIT_SECTIONS DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, \
868 MEM_INIT_SECTIONS
869#define ALL_XXXEXIT_SECTIONS DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, \
870 MEM_EXIT_SECTIONS
871
872#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
873#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100874
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100875#define DATA_SECTIONS ".data$", ".data.rel$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100876#define TEXT_SECTIONS ".text$"
877
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000878#define INIT_SECTIONS ".init.*"
879#define DEV_INIT_SECTIONS ".devinit.*"
880#define CPU_INIT_SECTIONS ".cpuinit.*"
881#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100882
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000883#define EXIT_SECTIONS ".exit.*"
884#define DEV_EXIT_SECTIONS ".devexit.*"
885#define CPU_EXIT_SECTIONS ".cpuexit.*"
886#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100887
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100888/* init data sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100889static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100890
891/* all init sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100892static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100893
894/* All init and exit sections (code + data) */
895static const char *init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100896 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100897
898/* data section */
899static const char *data_sections[] = { DATA_SECTIONS, NULL };
900
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100901
902/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100903#define DEFAULT_SYMBOL_WHITE_LIST \
904 "*driver", \
905 "*_template", /* scsi uses *_template a lot */ \
906 "*_timer", /* arm uses ops structures named _timer a lot */ \
907 "*_sht", /* scsi also used *_sht to some extent */ \
908 "*_ops", \
909 "*_probe", \
910 "*_probe_one", \
911 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100912
913static const char *head_sections[] = { ".head.text*", NULL };
914static const char *linker_symbols[] =
915 { "__init_begin", "_sinittext", "_einittext", NULL };
916
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100917enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100918 TEXT_TO_ANY_INIT,
919 DATA_TO_ANY_INIT,
920 TEXT_TO_ANY_EXIT,
921 DATA_TO_ANY_EXIT,
922 XXXINIT_TO_SOME_INIT,
923 XXXEXIT_TO_SOME_EXIT,
924 ANY_INIT_TO_ANY_EXIT,
925 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100926 EXPORT_TO_INIT_EXIT,
927};
928
Sam Ravnborg10668222008-01-13 22:21:31 +0100929struct sectioncheck {
930 const char *fromsec[20];
931 const char *tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100932 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100933 const char *symbol_white_list[20];
Sam Ravnborg10668222008-01-13 22:21:31 +0100934};
935
936const struct sectioncheck sectioncheck[] = {
937/* Do not reference init/exit code/data from
938 * normal code and data
939 */
940{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100941 .fromsec = { TEXT_SECTIONS, NULL },
942 .tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100943 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100944 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100945},
946{
947 .fromsec = { DATA_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +0100948 .tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100949 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100950 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100951},
952{
Uwe Kleine-König0db252452010-01-30 21:14:23 +0100953 .fromsec = { DATA_SECTIONS, NULL },
954 .tosec = { INIT_SECTIONS, NULL },
955 .mismatch = DATA_TO_ANY_INIT,
956 .symbol_white_list = {
957 "*_template", "*_timer", "*_sht", "*_ops",
958 "*_probe", "*_probe_one", "*_console", NULL
959 },
960},
961{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100962 .fromsec = { TEXT_SECTIONS, NULL },
963 .tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100964 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100965 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100966},
967{
968 .fromsec = { DATA_SECTIONS, NULL },
969 .tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100970 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100971 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100972},
973/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
974{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100975 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100976 .tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100977 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100978 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100979},
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000980/* Do not reference cpuinit code/data from meminit code/data */
981{
982 .fromsec = { MEM_INIT_SECTIONS, NULL },
983 .tosec = { CPU_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100984 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100985 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000986},
987/* Do not reference meminit code/data from cpuinit code/data */
988{
989 .fromsec = { CPU_INIT_SECTIONS, NULL },
990 .tosec = { MEM_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100991 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100992 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000993},
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100994/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
995{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100996 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100997 .tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100998 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100999 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001000},
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001001/* Do not reference cpuexit code/data from memexit code/data */
1002{
1003 .fromsec = { MEM_EXIT_SECTIONS, NULL },
1004 .tosec = { CPU_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001005 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001006 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001007},
1008/* Do not reference memexit code/data from cpuexit code/data */
1009{
1010 .fromsec = { CPU_EXIT_SECTIONS, NULL },
1011 .tosec = { MEM_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001012 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001013 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001014},
Sam Ravnborg10668222008-01-13 22:21:31 +01001015/* Do not use exit code/data from init code */
1016{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001017 .fromsec = { ALL_INIT_SECTIONS, NULL },
1018 .tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001019 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001020 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001021},
1022/* Do not use init code/data from exit code */
1023{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001024 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001025 .tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001026 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001027 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001028},
1029/* Do not export init/exit functions or data */
1030{
1031 .fromsec = { "__ksymtab*", NULL },
Sam Ravnborgfa95eb12008-02-02 23:30:22 +01001032 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001033 .mismatch = EXPORT_TO_INIT_EXIT,
1034 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001035}
1036};
1037
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001038static const struct sectioncheck *section_mismatch(
1039 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001040{
1041 int i;
1042 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1043 const struct sectioncheck *check = &sectioncheck[0];
1044
1045 for (i = 0; i < elems; i++) {
1046 if (match(fromsec, check->fromsec) &&
1047 match(tosec, check->tosec))
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001048 return check;
Sam Ravnborg10668222008-01-13 22:21:31 +01001049 check++;
1050 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001051 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001052}
1053
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001054/**
1055 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001056 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001057 * Pattern 1:
1058 * If a module parameter is declared __initdata and permissions=0
1059 * then this is legal despite the warning generated.
1060 * We cannot see value of permissions here, so just ignore
1061 * this pattern.
1062 * The pattern is identified by:
1063 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001064 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001065 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001066 *
Rusty Russell6a841522010-08-11 23:04:16 -06001067 * Pattern 1a:
1068 * module_param_call() ops can refer to __init set function if permissions=0
1069 * The pattern is identified by:
1070 * tosec = .init.text
1071 * fromsec = .data*
1072 * atsym = __param_ops_*
1073 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001074 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001075 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001076 * add, remove, probe functions etc.
Uwe Kleine-Königb75dcab2010-01-29 11:40:38 +01001077 * These functions may often be marked __devinit and we do not want to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001078 * warn here.
1079 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001080 * tosec = init or exit section
1081 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001082 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1083 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001084 *
1085 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001086 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001087 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001088 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001089 * Some symbols belong to init section but still it is ok to reference
1090 * these from non-init sections as these symbols don't have any memory
1091 * allocated for them and symbol address and value are same. So even
1092 * if init section is freed, its ok to reference those symbols.
1093 * For ex. symbols marking the init section boundaries.
1094 * This pattern is identified by
1095 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001096 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001097 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001098static int secref_whitelist(const struct sectioncheck *mismatch,
1099 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001100 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001101{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001102 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001103 if (match(tosec, init_data_sections) &&
1104 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001105 (strncmp(fromsym, "__param", strlen("__param")) == 0))
1106 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001107
Rusty Russell6a841522010-08-11 23:04:16 -06001108 /* Check for pattern 1a */
1109 if (strcmp(tosec, ".init.text") == 0 &&
1110 match(fromsec, data_sections) &&
1111 (strncmp(fromsym, "__param_ops_", strlen("__param_ops_")) == 0))
1112 return 0;
1113
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001114 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001115 if (match(tosec, init_exit_sections) &&
1116 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001117 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001118 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001119
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001120 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001121 if (match(fromsec, head_sections) &&
1122 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001123 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001124
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001125 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001126 if (match(tosym, linker_symbols))
1127 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001128
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001129 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001130}
1131
1132/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001133 * Find symbol based on relocation record info.
1134 * In some cases the symbol supplied is a valid symbol so
1135 * return refsym. If st_name != 0 we assume this is a valid symbol.
1136 * In other cases the symbol needs to be looked up in the symbol table
1137 * based on section and address.
1138 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001139static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001140 Elf_Sym *relsym)
1141{
1142 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001143 Elf_Sym *near = NULL;
1144 Elf64_Sword distance = 20;
1145 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001146 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001147
1148 if (relsym->st_name != 0)
1149 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001150
1151 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001152 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001153 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001154 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001155 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1156 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001157 if (sym->st_value == addr)
1158 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001159 /* Find a symbol nearby - addr are maybe negative */
1160 d = sym->st_value - addr;
1161 if (d < 0)
1162 d = addr - sym->st_value;
1163 if (d < distance) {
1164 distance = d;
1165 near = sym;
1166 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001167 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001168 /* We need a close match */
1169 if (distance < 20)
1170 return near;
1171 else
1172 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001173}
1174
David Brownellda68d612007-02-20 13:58:16 -08001175static inline int is_arm_mapping_symbol(const char *str)
1176{
1177 return str[0] == '$' && strchr("atd", str[1])
1178 && (str[2] == '\0' || str[2] == '.');
1179}
1180
1181/*
1182 * If there's no name there, ignore it; likewise, ignore it if it's
1183 * one of the magic symbols emitted used by current ARM tools.
1184 *
1185 * Otherwise if find_symbols_between() returns those symbols, they'll
1186 * fail the whitelist tests and cause lots of false alarms ... fixable
1187 * only by merging __exit and __init sections into __text, bloating
1188 * the kernel (which is especially evil on embedded platforms).
1189 */
1190static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1191{
1192 const char *name = elf->strtab + sym->st_name;
1193
1194 if (!name || !strlen(name))
1195 return 0;
1196 return !is_arm_mapping_symbol(name);
1197}
1198
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001199/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001200 * Find symbols before or equal addr and after addr - in the section sec.
1201 * If we find two symbols with equal offset prefer one with a valid name.
1202 * The ELF format may have a better way to detect what type of symbol
1203 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001204 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001205static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1206 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001207{
1208 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001209 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001210 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001211
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001212 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1213 const char *symsec;
1214
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001215 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001216 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001217 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001218 if (strcmp(symsec, sec) != 0)
1219 continue;
David Brownellda68d612007-02-20 13:58:16 -08001220 if (!is_valid_name(elf, sym))
1221 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001222 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001223 if ((addr - sym->st_value) < distance) {
1224 distance = addr - sym->st_value;
1225 near = sym;
1226 } else if ((addr - sym->st_value) == distance) {
1227 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001228 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001229 }
1230 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001231 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001232}
1233
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001234/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001235 * Convert a section name to the function/data attribute
1236 * .init.text => __init
1237 * .cpuinit.data => __cpudata
1238 * .memexitconst => __memconst
1239 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001240 *
1241 * The memory of returned value has been allocated on a heap. The user of this
1242 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001243*/
1244static char *sec2annotation(const char *s)
1245{
1246 if (match(s, init_exit_sections)) {
1247 char *p = malloc(20);
1248 char *r = p;
1249
1250 *p++ = '_';
1251 *p++ = '_';
1252 if (*s == '.')
1253 s++;
1254 while (*s && *s != '.')
1255 *p++ = *s++;
1256 *p = '\0';
1257 if (*s == '.')
1258 s++;
1259 if (strstr(s, "rodata") != NULL)
1260 strcat(p, "const ");
1261 else if (strstr(s, "data") != NULL)
1262 strcat(p, "data ");
1263 else
1264 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001265 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001266 } else {
Andrew Morton5003bab2010-08-11 00:42:26 -07001267 return strdup("");
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001268 }
1269}
1270
1271static int is_function(Elf_Sym *sym)
1272{
1273 if (sym)
1274 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1275 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001276 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001277}
1278
Randy Dunlap00759c02011-03-15 14:13:47 -07001279static void print_section_list(const char * const list[20])
1280{
1281 const char *const *s = list;
1282
1283 while (*s) {
1284 fprintf(stderr, "%s", *s);
1285 s++;
1286 if (*s)
1287 fprintf(stderr, ", ");
1288 }
1289 fprintf(stderr, "\n");
1290}
1291
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001292/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001293 * Print a warning about a section mismatch.
1294 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001295 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001296 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001297static void report_sec_mismatch(const char *modname,
1298 const struct sectioncheck *mismatch,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001299 const char *fromsec,
1300 unsigned long long fromaddr,
1301 const char *fromsym,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001302 int from_is_func,
1303 const char *tosec, const char *tosym,
1304 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001305{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001306 const char *from, *from_p;
1307 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001308 char *prl_from;
1309 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001310
1311 switch (from_is_func) {
1312 case 0: from = "variable"; from_p = ""; break;
1313 case 1: from = "function"; from_p = "()"; break;
1314 default: from = "(unknown reference)"; from_p = ""; break;
1315 }
1316 switch (to_is_func) {
1317 case 0: to = "variable"; to_p = ""; break;
1318 case 1: to = "function"; to_p = "()"; break;
1319 default: to = "(unknown reference)"; to_p = ""; break;
1320 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001321
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001322 sec_mismatch_count++;
1323 if (!sec_mismatch_verbose)
1324 return;
1325
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001326 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1327 "to the %s %s:%s%s\n",
1328 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1329 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001330
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001331 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001332 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001333 prl_from = sec2annotation(fromsec);
1334 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001335 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001336 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001337 "the %s %s%s%s.\n"
1338 "This is often because %s lacks a %s\n"
1339 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001340 prl_from, fromsym,
1341 to, prl_to, tosym, to_p,
1342 fromsym, prl_to, tosym);
1343 free(prl_from);
1344 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001345 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001346 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001347 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001348 fprintf(stderr,
1349 "The variable %s references\n"
1350 "the %s %s%s%s\n"
1351 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001352 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001353 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001354 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001355 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001356 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001357 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001358 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001359 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001360 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001361 fprintf(stderr,
1362 "The function %s() references a %s in an exit section.\n"
1363 "Often the %s %s%s has valid usage outside the exit section\n"
1364 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001365 fromsym, to, to, tosym, to_p, prl_to, tosym);
1366 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001367 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001368 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001369 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001370 fprintf(stderr,
1371 "The variable %s references\n"
1372 "the %s %s%s%s\n"
1373 "If the reference is valid then annotate the\n"
1374 "variable with __exit* (see linux/init.h) or "
1375 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001376 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001377 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001378 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001379 break;
1380 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001381 case XXXINIT_TO_SOME_INIT:
1382 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001383 prl_from = sec2annotation(fromsec);
1384 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001385 fprintf(stderr,
1386 "The %s %s%s%s references\n"
1387 "a %s %s%s%s.\n"
1388 "If %s is only used by %s then\n"
1389 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001390 from, prl_from, fromsym, from_p,
1391 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001392 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001393 free(prl_from);
1394 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001395 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001396 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001397 prl_from = sec2annotation(fromsec);
1398 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001399 fprintf(stderr,
1400 "The %s %s%s%s references\n"
1401 "a %s %s%s%s.\n"
1402 "This is often seen when error handling "
1403 "in the init function\n"
1404 "uses functionality in the exit path.\n"
1405 "The fix is often to remove the %sannotation of\n"
1406 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001407 from, prl_from, fromsym, from_p,
1408 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001409 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001410 free(prl_from);
1411 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001412 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001413 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001414 prl_from = sec2annotation(fromsec);
1415 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001416 fprintf(stderr,
1417 "The %s %s%s%s references\n"
1418 "a %s %s%s%s.\n"
1419 "This is often seen when error handling "
1420 "in the exit function\n"
1421 "uses functionality in the init path.\n"
1422 "The fix is often to remove the %sannotation of\n"
1423 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001424 from, prl_from, fromsym, from_p,
1425 to, prl_to, tosym, to_p,
1426 prl_to, tosym, to_p);
1427 free(prl_from);
1428 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001429 break;
1430 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001431 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001432 fprintf(stderr,
1433 "The symbol %s is exported and annotated %s\n"
1434 "Fix this by removing the %sannotation of %s "
1435 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001436 tosym, prl_to, prl_to, tosym);
1437 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001438 break;
1439 }
1440 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001441}
1442
1443static void check_section_mismatch(const char *modname, struct elf_info *elf,
1444 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1445{
1446 const char *tosec;
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001447 const struct sectioncheck *mismatch;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001448
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001449 tosec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001450 mismatch = section_mismatch(fromsec, tosec);
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001451 if (mismatch) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001452 Elf_Sym *to;
1453 Elf_Sym *from;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001454 const char *tosym;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001455 const char *fromsym;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001456
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001457 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1458 fromsym = sym_name(elf, from);
1459 to = find_elf_symbol(elf, r->r_addend, sym);
1460 tosym = sym_name(elf, to);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001461
1462 /* check whitelist - we may ignore it */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001463 if (secref_whitelist(mismatch,
1464 fromsec, fromsym, tosec, tosym)) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001465 report_sec_mismatch(modname, mismatch,
1466 fromsec, r->r_offset, fromsym,
1467 is_function(from), tosec, tosym,
1468 is_function(to));
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001469 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001470 }
1471}
1472
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001473static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001474 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001475{
1476 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001477 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001478
1479 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001480 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001481}
1482
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001483static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001484{
1485 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001486 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001487
1488 switch (r_typ) {
1489 case R_386_32:
1490 r->r_addend = TO_NATIVE(*location);
1491 break;
1492 case R_386_PC32:
1493 r->r_addend = TO_NATIVE(*location) + 4;
1494 /* For CONFIG_RELOCATABLE=y */
1495 if (elf->hdr->e_type == ET_EXEC)
1496 r->r_addend += r->r_offset;
1497 break;
1498 }
1499 return 0;
1500}
1501
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001502#ifndef R_ARM_CALL
1503#define R_ARM_CALL 28
1504#endif
1505#ifndef R_ARM_JUMP24
1506#define R_ARM_JUMP24 29
1507#endif
1508
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001509static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001510{
1511 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1512
1513 switch (r_typ) {
1514 case R_ARM_ABS32:
1515 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001516 r->r_addend = (int)(long)
1517 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001518 break;
1519 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001520 case R_ARM_CALL:
1521 case R_ARM_JUMP24:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001522 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001523 r->r_addend = (int)(long)(elf->hdr +
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001524 sechdr->sh_offset +
1525 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001526 break;
1527 default:
1528 return 1;
1529 }
1530 return 0;
1531}
1532
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001533static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001534{
1535 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001536 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001537 unsigned int inst;
1538
1539 if (r_typ == R_MIPS_HI16)
1540 return 1; /* skip this */
1541 inst = TO_NATIVE(*location);
1542 switch (r_typ) {
1543 case R_MIPS_LO16:
1544 r->r_addend = inst & 0xffff;
1545 break;
1546 case R_MIPS_26:
1547 r->r_addend = (inst & 0x03ffffff) << 2;
1548 break;
1549 case R_MIPS_32:
1550 r->r_addend = inst;
1551 break;
1552 }
1553 return 0;
1554}
1555
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001556static void section_rela(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001557 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001558{
1559 Elf_Sym *sym;
1560 Elf_Rela *rela;
1561 Elf_Rela r;
1562 unsigned int r_sym;
1563 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001564
Sam Ravnborgff13f922008-01-23 19:54:27 +01001565 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001566 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1567
Sam Ravnborgff13f922008-01-23 19:54:27 +01001568 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001569 fromsec += strlen(".rela");
1570 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001571 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001572 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001573
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001574 for (rela = start; rela < stop; rela++) {
1575 r.r_offset = TO_NATIVE(rela->r_offset);
1576#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001577 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001578 unsigned int r_typ;
1579 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1580 r_sym = TO_NATIVE(r_sym);
1581 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1582 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1583 } else {
1584 r.r_info = TO_NATIVE(rela->r_info);
1585 r_sym = ELF_R_SYM(r.r_info);
1586 }
1587#else
1588 r.r_info = TO_NATIVE(rela->r_info);
1589 r_sym = ELF_R_SYM(r.r_info);
1590#endif
1591 r.r_addend = TO_NATIVE(rela->r_addend);
1592 sym = elf->symtab_start + r_sym;
1593 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001594 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001595 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001596 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001597 }
1598}
1599
1600static void section_rel(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001601 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001602{
1603 Elf_Sym *sym;
1604 Elf_Rel *rel;
1605 Elf_Rela r;
1606 unsigned int r_sym;
1607 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001608
Sam Ravnborgff13f922008-01-23 19:54:27 +01001609 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001610 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1611
Sam Ravnborgff13f922008-01-23 19:54:27 +01001612 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001613 fromsec += strlen(".rel");
1614 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001615 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001616 return;
1617
1618 for (rel = start; rel < stop; rel++) {
1619 r.r_offset = TO_NATIVE(rel->r_offset);
1620#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001621 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001622 unsigned int r_typ;
1623 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1624 r_sym = TO_NATIVE(r_sym);
1625 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1626 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1627 } else {
1628 r.r_info = TO_NATIVE(rel->r_info);
1629 r_sym = ELF_R_SYM(r.r_info);
1630 }
1631#else
1632 r.r_info = TO_NATIVE(rel->r_info);
1633 r_sym = ELF_R_SYM(r.r_info);
1634#endif
1635 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001636 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001637 case EM_386:
1638 if (addend_386_rel(elf, sechdr, &r))
1639 continue;
1640 break;
1641 case EM_ARM:
1642 if (addend_arm_rel(elf, sechdr, &r))
1643 continue;
1644 break;
1645 case EM_MIPS:
1646 if (addend_mips_rel(elf, sechdr, &r))
1647 continue;
1648 break;
1649 }
1650 sym = elf->symtab_start + r_sym;
1651 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001652 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001653 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001654 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001655 }
1656}
1657
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001658/**
1659 * A module includes a number of sections that are discarded
1660 * either when loaded or when used as built-in.
1661 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001662 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001663 * Likewise for modules used built-in the sections marked __exit
1664 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001665 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001666 * The check_sec_ref() function traverses all relocation records
1667 * to find all references to a section that reference a section that will
1668 * be discarded and warns about it.
1669 **/
1670static void check_sec_ref(struct module *mod, const char *modname,
Sam Ravnborg10668222008-01-13 22:21:31 +01001671 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001672{
1673 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001674 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001675
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001676 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001677 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001678 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001679 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001680 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001681 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001682 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001683 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001684 }
1685}
1686
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001687static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 const char *symname;
1690 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001691 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct module *mod;
1693 struct elf_info info = { };
1694 Elf_Sym *sym;
1695
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001696 if (!parse_elf(&info, modname))
1697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 mod = new_module(modname);
1700
1701 /* When there's no vmlinux, don't print warnings about
1702 * unresolved symbols (since there'll be too many ;) */
1703 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 mod->skip = 1;
1706 }
1707
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001708 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
Sam Ravnborg2fa36562008-04-26 21:07:26 +02001709 if (info.modinfo && !license && !is_vmlinux(modname))
1710 warn("modpost: missing MODULE_LICENSE() in %s\n"
1711 "see include/linux/module.h for "
1712 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001713 while (license) {
1714 if (license_is_gpl_compatible(license))
1715 mod->gpl_compatible = 1;
1716 else {
1717 mod->gpl_compatible = 0;
1718 break;
1719 }
1720 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1721 "license", license);
1722 }
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1725 symname = info.strtab + sym->st_name;
1726
1727 handle_modversions(mod, &info, sym, symname);
1728 handle_moddevtable(mod, &info, sym, symname);
1729 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001730 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001731 (is_vmlinux(modname) && vmlinux_section_warnings))
1732 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1735 if (version)
1736 maybe_frob_rcs_version(modname, version, info.modinfo,
1737 version - (char *)info.hdr);
1738 if (version || (all_versions && !is_vmlinux(modname)))
1739 get_src_version(modname, mod->srcversion,
1740 sizeof(mod->srcversion)-1);
1741
1742 parse_elf_finish(&info);
1743
Rusty Russell8c8ef422009-03-31 13:05:34 -06001744 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 * never passed as an argument to an exported function, so
1746 * the automatic versioning doesn't pick it up, but it's really
1747 * important anyhow */
1748 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06001749 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
1752#define SZ 500
1753
1754/* We first write the generated file into memory using the
1755 * following helper, then compare to the file on disk and
1756 * only update the later if anything changed */
1757
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001758void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1759 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 char tmp[SZ];
1762 int len;
1763 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 va_start(ap, fmt);
1766 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001767 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 va_end(ap);
1769}
1770
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001771void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001774 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 buf->p = realloc(buf->p, buf->size);
1776 }
1777 strncpy(buf->p + buf->pos, s, len);
1778 buf->pos += len;
1779}
1780
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001781static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1782{
1783 const char *e = is_vmlinux(m) ?"":".ko";
1784
1785 switch (exp) {
1786 case export_gpl:
1787 fatal("modpost: GPL-incompatible module %s%s "
1788 "uses GPL-only symbol '%s'\n", m, e, s);
1789 break;
1790 case export_unused_gpl:
1791 fatal("modpost: GPL-incompatible module %s%s "
1792 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1793 break;
1794 case export_gpl_future:
1795 warn("modpost: GPL-incompatible module %s%s "
1796 "uses future GPL-only symbol '%s'\n", m, e, s);
1797 break;
1798 case export_plain:
1799 case export_unused:
1800 case export_unknown:
1801 /* ignore */
1802 break;
1803 }
1804}
1805
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001806static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001807{
1808 const char *e = is_vmlinux(m) ?"":".ko";
1809
1810 switch (exp) {
1811 case export_unused:
1812 case export_unused_gpl:
1813 warn("modpost: module %s%s "
1814 "uses symbol '%s' marked UNUSED\n", m, e, s);
1815 break;
1816 default:
1817 /* ignore */
1818 break;
1819 }
1820}
1821
1822static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001823{
1824 struct symbol *s, *exp;
1825
1826 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001827 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001828 exp = find_symbol(s->name);
1829 if (!exp || exp->module == mod)
1830 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001831 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001832 if (basename)
1833 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001834 else
1835 basename = mod->name;
1836 if (!mod->gpl_compatible)
1837 check_for_gpl_usage(exp->export, basename, exp->name);
1838 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001839 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001840}
1841
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001842/**
1843 * Header for the generated file
1844 **/
1845static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 buf_printf(b, "#include <linux/module.h>\n");
1848 buf_printf(b, "#include <linux/vermagic.h>\n");
1849 buf_printf(b, "#include <linux/compiler.h>\n");
1850 buf_printf(b, "\n");
1851 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1852 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 buf_printf(b, "struct module __this_module\n");
1854 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001855 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (mod->has_init)
1857 buf_printf(b, " .init = init_module,\n");
1858 if (mod->has_cleanup)
1859 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1860 " .exit = cleanup_module,\n"
1861 "#endif\n");
Roman Zippele61a1c12007-05-09 02:35:15 -07001862 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 buf_printf(b, "};\n");
1864}
1865
Ben Hutchings2449b8b2011-10-24 15:12:28 +02001866static void add_intree_flag(struct buffer *b, int is_intree)
1867{
1868 if (is_intree)
1869 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
1870}
1871
Trevor Keith5c725132009-09-22 16:43:38 -07001872static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07001873{
1874 static const char *staging_dir = "drivers/staging";
1875
1876 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
1877 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1878}
1879
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001880/**
1881 * Record CRCs for unresolved symbols
1882 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001883static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001886 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 for (s = mod->unres; s; s = s->next) {
1889 exp = find_symbol(s->name);
1890 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001891 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001892 if (warn_unresolved) {
1893 warn("\"%s\" [%s.ko] undefined!\n",
1894 s->name, mod->name);
1895 } else {
1896 merror("\"%s\" [%s.ko] undefined!\n",
1897 s->name, mod->name);
1898 err = 1;
1899 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 continue;
1902 }
1903 s->module = exp->module;
1904 s->crc_valid = exp->crc_valid;
1905 s->crc = exp->crc;
1906 }
1907
1908 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001909 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 buf_printf(b, "\n");
1912 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001913 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1915
1916 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001917 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001920 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 s->name, mod->name);
1922 continue;
1923 }
1924 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1925 }
1926
1927 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001928
1929 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001932static void add_depends(struct buffer *b, struct module *mod,
1933 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 struct symbol *s;
1936 struct module *m;
1937 int first = 1;
1938
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001939 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 buf_printf(b, "\n");
1943 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001944 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1946 buf_printf(b, "\"depends=");
1947 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001948 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (!s->module)
1950 continue;
1951
1952 if (s->module->seen)
1953 continue;
1954
1955 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001956 p = strrchr(s->module->name, '/');
1957 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001958 p++;
1959 else
1960 p = s->module->name;
1961 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 first = 0;
1963 }
1964 buf_printf(b, "\";\n");
1965}
1966
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001967static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 if (mod->srcversion[0]) {
1970 buf_printf(b, "\n");
1971 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1972 mod->srcversion);
1973 }
1974}
1975
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001976static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 char *tmp;
1979 FILE *file;
1980 struct stat st;
1981
1982 file = fopen(fname, "r");
1983 if (!file)
1984 goto write;
1985
1986 if (fstat(fileno(file), &st) < 0)
1987 goto close_write;
1988
1989 if (st.st_size != b->pos)
1990 goto close_write;
1991
1992 tmp = NOFAIL(malloc(b->pos));
1993 if (fread(tmp, 1, b->pos, file) != b->pos)
1994 goto free_write;
1995
1996 if (memcmp(tmp, b->p, b->pos) != 0)
1997 goto free_write;
1998
1999 free(tmp);
2000 fclose(file);
2001 return;
2002
2003 free_write:
2004 free(tmp);
2005 close_write:
2006 fclose(file);
2007 write:
2008 file = fopen(fname, "w");
2009 if (!file) {
2010 perror(fname);
2011 exit(1);
2012 }
2013 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2014 perror(fname);
2015 exit(1);
2016 }
2017 fclose(file);
2018}
2019
Ram Paibd5cbce2006-06-08 22:12:53 -07002020/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002021 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002022 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002023static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 unsigned long size, pos = 0;
2026 void *file = grab_file(fname, &size);
2027 char *line;
2028
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002029 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* No symbol versions, silently ignore */
2031 return;
2032
2033 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002034 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 unsigned int crc;
2036 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002037 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 if (!(symname = strchr(line, '\t')))
2040 goto fail;
2041 *symname++ = '\0';
2042 if (!(modname = strchr(symname, '\t')))
2043 goto fail;
2044 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002045 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002046 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002047 if (export && ((end = strchr(export, '\t')) != NULL))
2048 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 crc = strtoul(line, &d, 16);
2050 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2051 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002052 mod = find_module(modname);
2053 if (!mod) {
2054 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002056 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 mod->skip = 1;
2058 }
Ram Paibd5cbce2006-06-08 22:12:53 -07002059 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002060 s->kernel = kernel;
2061 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07002062 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064 return;
2065fail:
2066 fatal("parse error in symbol dump file\n");
2067}
2068
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002069/* For normal builds always dump all symbols.
2070 * For external modules only dump symbols
2071 * that are not read from kernel Module.symvers.
2072 **/
2073static int dump_sym(struct symbol *sym)
2074{
2075 if (!external_module)
2076 return 1;
2077 if (sym->vmlinux || sym->kernel)
2078 return 0;
2079 return 1;
2080}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002081
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002082static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct buffer buf = { };
2085 struct symbol *symbol;
2086 int n;
2087
2088 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2089 symbol = symbolhash[n];
2090 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002091 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07002092 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002093 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07002094 symbol->module->name,
2095 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 symbol = symbol->next;
2097 }
2098 }
2099 write_if_changed(&buf, fname);
2100}
2101
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002102struct ext_sym_list {
2103 struct ext_sym_list *next;
2104 const char *file;
2105};
2106
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002107int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 struct module *mod;
2110 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002111 char *kernel_read = NULL, *module_read = NULL;
2112 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002114 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002115 struct ext_sym_list *extsym_iter;
2116 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002118 while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:E")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002119 switch (opt) {
2120 case 'i':
2121 kernel_read = optarg;
2122 break;
2123 case 'I':
2124 module_read = optarg;
2125 external_module = 1;
2126 break;
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +01002127 case 'c':
2128 cross_build = 1;
2129 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002130 case 'e':
2131 external_module = 1;
2132 extsym_iter =
2133 NOFAIL(malloc(sizeof(*extsym_iter)));
2134 extsym_iter->next = extsym_start;
2135 extsym_iter->file = optarg;
2136 extsym_start = extsym_iter;
2137 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002138 case 'm':
2139 modversions = 1;
2140 break;
2141 case 'o':
2142 dump_write = optarg;
2143 break;
2144 case 'a':
2145 all_versions = 1;
2146 break;
2147 case 's':
2148 vmlinux_section_warnings = 0;
2149 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002150 case 'S':
2151 sec_mismatch_verbose = 0;
2152 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002153 case 'w':
2154 warn_unresolved = 1;
2155 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002156 case 'E':
2157 section_error_on_mismatch = 1;
2158 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002159 default:
2160 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162 }
2163
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002164 if (kernel_read)
2165 read_dump(kernel_read, 1);
2166 if (module_read)
2167 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002168 while (extsym_start) {
2169 read_dump(extsym_start->file, 0);
2170 extsym_iter = extsym_start->next;
2171 free(extsym_start);
2172 extsym_start = extsym_iter;
2173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002175 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 for (mod = modules; mod; mod = mod->next) {
2179 if (mod->skip)
2180 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002181 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002182 }
2183
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002184 err = 0;
2185
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002186 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01002187 char fname[strlen(mod->name) + 10];
2188
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002189 if (mod->skip)
2190 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 buf.pos = 0;
2193
2194 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002195 add_intree_flag(&buf, !external_module);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002196 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002197 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 add_depends(&buf, mod, modules);
2199 add_moddevtable(&buf, mod);
2200 add_srcversion(&buf, mod);
2201
2202 sprintf(fname, "%s.mod.c", mod->name);
2203 write_if_changed(&buf, fname);
2204 }
2205
2206 if (dump_write)
2207 write_dump(dump_write);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208
2209 if (sec_mismatch_count && !sec_mismatch_verbose) {
2210 merror(
2211 "modpost: Found %d section mismatch(es).\n"
2212 "To see full details build your kernel with:\n"
2213 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2214 sec_mismatch_count);
2215
2216 }
2217
2218 if (sec_mismatch_count && section_error_on_mismatch) {
2219 err |= 1;
2220 printf(
2221 "To build the kernel despite the mismatches, "
2222 "build with:\n'make CONFIG_NO_ERROR_ON_MISMATCH=y'\n"
2223 "(NOTE: This is not recommended)\n");
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002226 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}