blob: 3187684084c9f4cede25f5c4213ae1f76fff35cd [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 ||
Andreas Schwab7dff32e2013-12-30 15:31:17 +0100574 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0 ||
575 strncmp(symname, "_restvr_", sizeof("_restvr_") - 1) == 0 ||
576 strncmp(symname, "_savevr_", sizeof("_savevr_") - 1) == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200577 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000578 if (info->hdr->e_machine == EM_PPC64)
579 /* Special register function linked on all modules during final link of .ko */
580 if (strncmp(symname, "_restgpr0_", sizeof("_restgpr0_") - 1) == 0 ||
Andreas Schwab7dff32e2013-12-30 15:31:17 +0100581 strncmp(symname, "_savegpr0_", sizeof("_savegpr0_") - 1) == 0 ||
582 strncmp(symname, "_restvr_", sizeof("_restvr_") - 1) == 0 ||
583 strncmp(symname, "_savevr_", sizeof("_savevr_") - 1) == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000584 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200585 /* Do not ignore this symbol */
586 return 0;
587}
588
Luke Yangf7b05e62006-03-02 18:35:31 +0800589#define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_"
590#define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100592static void handle_modversions(struct module *mod, struct elf_info *info,
593 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200596 enum export export;
597
Frank Rowand258f7422012-04-09 17:59:03 -0700598 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
599 strncmp(symname, "__ksymtab", 9) == 0)
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200600 export = export_from_secname(info, get_secindex(info, sym));
601 else
602 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 switch (sym->st_shndx) {
605 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100606 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 break;
608 case SHN_ABS:
609 /* CRC'd symbol */
Michal Marek8d995132009-12-12 12:02:24 +0100610 if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700612 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
613 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 break;
616 case SHN_UNDEF:
617 /* undefined symbol */
618 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
619 ELF_ST_BIND(sym->st_info) != STB_WEAK)
620 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200621 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 break;
Ben Colline8d529012005-08-19 13:44:57 -0700623/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
624#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
625/* add compatibility with older glibc */
626#ifndef STT_SPARC_REGISTER
627#define STT_SPARC_REGISTER STT_REGISTER
628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (info->hdr->e_machine == EM_SPARC ||
630 info->hdr->e_machine == EM_SPARCV9) {
631 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700632 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100634 if (symname[0] == '.') {
635 char *munged = strdup(symname);
636 munged[0] = '_';
637 munged[1] = toupper(munged[1]);
638 symname = munged;
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (memcmp(symname, MODULE_SYMBOL_PREFIX,
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100644 strlen(MODULE_SYMBOL_PREFIX)) == 0) {
645 mod->unres =
646 alloc_symbol(symname +
647 strlen(MODULE_SYMBOL_PREFIX),
648 ELF_ST_BIND(sym->st_info) == STB_WEAK,
649 mod->unres);
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
652 default:
653 /* All exported symbols */
Michal Marek8d995132009-12-12 12:02:24 +0100654 if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700655 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
656 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0)
659 mod->has_init = 1;
660 if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0)
661 mod->has_cleanup = 1;
662 break;
663 }
664}
665
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100666/**
667 * Parse tag=value strings from .modinfo section
668 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static char *next_string(char *string, unsigned long *secsize)
670{
671 /* Skip non-zero chars */
672 while (string[0]) {
673 string++;
674 if ((*secsize)-- <= 1)
675 return NULL;
676 }
677
678 /* Skip any zero padding. */
679 while (!string[0]) {
680 string++;
681 if ((*secsize)-- <= 1)
682 return NULL;
683 }
684 return string;
685}
686
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200687static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
688 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 char *p;
691 unsigned int taglen = strlen(tag);
692 unsigned long size = modinfo_len;
693
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200694 if (info) {
695 size -= info - (char *)modinfo;
696 modinfo = next_string(info, &size);
697 }
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 for (p = modinfo; p; p = next_string(p, &size)) {
700 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
701 return p + taglen + 1;
702 }
703 return NULL;
704}
705
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200706static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
707 const char *tag)
708
709{
710 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
711}
712
Sam Ravnborg93684d32006-02-19 11:53:35 +0100713/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100714 * Test if string s ends in string sub
715 * return 0 if match
716 **/
717static int strrcmp(const char *s, const char *sub)
718{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100719 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100720
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100721 if (!s || !sub)
722 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100723
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100724 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100725 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100726
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100727 if ((slen == 0) || (sublen == 0))
728 return 1;
729
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100730 if (sublen > slen)
731 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100732
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100733 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100734}
735
Sam Ravnborgff13f922008-01-23 19:54:27 +0100736static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
737{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100738 if (sym)
739 return elf->strtab + sym->st_name;
740 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100741 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100742}
743
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200744static const char *sec_name(struct elf_info *elf, int secindex)
Sam Ravnborgff13f922008-01-23 19:54:27 +0100745{
746 Elf_Shdr *sechdrs = elf->sechdrs;
747 return (void *)elf->hdr +
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200748 elf->sechdrs[elf->secindex_strings].sh_offset +
749 sechdrs[secindex].sh_name;
Sam Ravnborgff13f922008-01-23 19:54:27 +0100750}
751
752static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
753{
754 return (void *)elf->hdr +
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200755 elf->sechdrs[elf->secindex_strings].sh_offset +
756 sechdr->sh_name;
Sam Ravnborgff13f922008-01-23 19:54:27 +0100757}
758
Sam Ravnborg10668222008-01-13 22:21:31 +0100759/* if sym is empty or point to a string
760 * like ".[0-9]+" then return 1.
761 * This is the optional prefix added by ld to some sections
762 */
763static int number_prefix(const char *sym)
764{
765 if (*sym++ == '\0')
766 return 1;
767 if (*sym != '.')
768 return 0;
769 do {
770 char c = *sym++;
771 if (c < '0' || c > '9')
772 return 0;
773 } while (*sym);
774 return 1;
775}
776
777/* The pattern is an array of simple patterns.
778 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100779 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100780 * "foo*" will match a string that begins with "foo"
781 * "foo$" will match a string equal to "foo" or "foo.1"
782 * where the '1' can be any number including several digits.
783 * The $ syntax is for sections where ld append a dot number
784 * to make section name unique.
785 */
Trevor Keith5c725132009-09-22 16:43:38 -0700786static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100787{
788 const char *p;
789 while (*pat) {
790 p = *pat++;
791 const char *endp = p + strlen(p) - 1;
792
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100793 /* "*foo" */
794 if (*p == '*') {
795 if (strrcmp(sym, p + 1) == 0)
796 return 1;
797 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100798 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100799 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100800 if (strncmp(sym, p, strlen(p) - 1) == 0)
801 return 1;
802 }
803 /* "foo$" */
804 else if (*endp == '$') {
805 if (strncmp(sym, p, strlen(p) - 1) == 0) {
806 if (number_prefix(sym + strlen(p) - 1))
807 return 1;
808 }
809 }
810 /* no wildcards */
811 else {
812 if (strcmp(p, sym) == 0)
813 return 1;
814 }
815 }
816 /* no match */
817 return 0;
818}
819
Sam Ravnborg10668222008-01-13 22:21:31 +0100820/* sections that we do not want to do full section mismatch check on */
821static const char *section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200822{
823 ".comment*",
824 ".debug*",
H.J. Lu11215842010-12-15 17:11:22 -0800825 ".zdebug*", /* Compressed debug sections. */
David Howells019fca82010-08-12 16:54:47 +0100826 ".GCC-command-line", /* mn10300 */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200827 ".mdebug*", /* alpha, score, mips etc. */
828 ".pdr", /* alpha, score, mips etc. */
829 ".stab*",
830 ".note*",
831 ".got*",
832 ".toc*",
833 NULL
834};
Sam Ravnborg10668222008-01-13 22:21:31 +0100835
Sam Ravnborge241a632008-01-28 20:13:13 +0100836/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400837 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100838 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400839 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100840 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400841static void check_section(const char *modname, struct elf_info *elf,
842 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100843{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400844 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100845
Anders Kaseorgb614a692009-04-23 16:49:33 -0400846 if (sechdr->sh_type == SHT_PROGBITS &&
847 !(sechdr->sh_flags & SHF_ALLOC) &&
848 !match(sec, section_white_list)) {
849 warn("%s (%s): unexpected non-allocatable section.\n"
850 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
851 "Note that for example <linux/init.h> contains\n"
852 "section definitions for use in .S files.\n\n",
853 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100854 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100855}
856
857
858
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100859#define ALL_INIT_DATA_SECTIONS \
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000860 ".init.setup$", ".init.rodata$", \
Jan Beulich9aaf4402012-03-08 09:41:25 +0000861 ".devinit.rodata$", ".cpuinit.rodata$", ".meminit.rodata$", \
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100862 ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$"
863#define ALL_EXIT_DATA_SECTIONS \
864 ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100865
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100866#define ALL_INIT_TEXT_SECTIONS \
867 ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$"
868#define ALL_EXIT_TEXT_SECTIONS \
869 ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100870
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100871#define ALL_XXXINIT_SECTIONS DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, \
872 MEM_INIT_SECTIONS
873#define ALL_XXXEXIT_SECTIONS DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, \
874 MEM_EXIT_SECTIONS
875
876#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
877#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100878
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100879#define DATA_SECTIONS ".data$", ".data.rel$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100880#define TEXT_SECTIONS ".text$"
881
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000882#define INIT_SECTIONS ".init.*"
883#define DEV_INIT_SECTIONS ".devinit.*"
884#define CPU_INIT_SECTIONS ".cpuinit.*"
885#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100886
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000887#define EXIT_SECTIONS ".exit.*"
888#define DEV_EXIT_SECTIONS ".devexit.*"
889#define CPU_EXIT_SECTIONS ".cpuexit.*"
890#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100891
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100892/* init data sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100893static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100894
895/* all init sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100896static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100897
898/* All init and exit sections (code + data) */
899static const char *init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100900 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100901
902/* data section */
903static const char *data_sections[] = { DATA_SECTIONS, NULL };
904
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100905
906/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100907#define DEFAULT_SYMBOL_WHITE_LIST \
908 "*driver", \
909 "*_template", /* scsi uses *_template a lot */ \
910 "*_timer", /* arm uses ops structures named _timer a lot */ \
911 "*_sht", /* scsi also used *_sht to some extent */ \
912 "*_ops", \
913 "*_probe", \
914 "*_probe_one", \
915 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100916
917static const char *head_sections[] = { ".head.text*", NULL };
918static const char *linker_symbols[] =
919 { "__init_begin", "_sinittext", "_einittext", NULL };
920
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100921enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100922 TEXT_TO_ANY_INIT,
923 DATA_TO_ANY_INIT,
924 TEXT_TO_ANY_EXIT,
925 DATA_TO_ANY_EXIT,
926 XXXINIT_TO_SOME_INIT,
927 XXXEXIT_TO_SOME_EXIT,
928 ANY_INIT_TO_ANY_EXIT,
929 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100930 EXPORT_TO_INIT_EXIT,
931};
932
Sam Ravnborg10668222008-01-13 22:21:31 +0100933struct sectioncheck {
934 const char *fromsec[20];
935 const char *tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100936 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100937 const char *symbol_white_list[20];
Sam Ravnborg10668222008-01-13 22:21:31 +0100938};
939
940const struct sectioncheck sectioncheck[] = {
941/* Do not reference init/exit code/data from
942 * normal code and data
943 */
944{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100945 .fromsec = { TEXT_SECTIONS, NULL },
946 .tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100947 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100948 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100949},
950{
951 .fromsec = { DATA_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +0100952 .tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100953 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100954 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100955},
956{
Uwe Kleine-König0db252452010-01-30 21:14:23 +0100957 .fromsec = { DATA_SECTIONS, NULL },
958 .tosec = { INIT_SECTIONS, NULL },
959 .mismatch = DATA_TO_ANY_INIT,
960 .symbol_white_list = {
961 "*_template", "*_timer", "*_sht", "*_ops",
962 "*_probe", "*_probe_one", "*_console", NULL
963 },
964},
965{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100966 .fromsec = { TEXT_SECTIONS, NULL },
967 .tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100968 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100969 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100970},
971{
972 .fromsec = { DATA_SECTIONS, NULL },
973 .tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100974 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100975 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100976},
977/* Do not reference init code/data from devinit/cpuinit/meminit code/data */
978{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100979 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100980 .tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100981 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100982 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100983},
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000984/* Do not reference cpuinit code/data from meminit code/data */
985{
986 .fromsec = { MEM_INIT_SECTIONS, NULL },
987 .tosec = { CPU_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100988 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100989 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000990},
991/* Do not reference meminit code/data from cpuinit code/data */
992{
993 .fromsec = { CPU_INIT_SECTIONS, NULL },
994 .tosec = { MEM_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100995 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100996 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000997},
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100998/* Do not reference exit code/data from devexit/cpuexit/memexit code/data */
999{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001000 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001001 .tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001002 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001003 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001004},
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001005/* Do not reference cpuexit code/data from memexit code/data */
1006{
1007 .fromsec = { MEM_EXIT_SECTIONS, NULL },
1008 .tosec = { CPU_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001009 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001010 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001011},
1012/* Do not reference memexit code/data from cpuexit code/data */
1013{
1014 .fromsec = { CPU_EXIT_SECTIONS, NULL },
1015 .tosec = { MEM_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001016 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001017 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001018},
Sam Ravnborg10668222008-01-13 22:21:31 +01001019/* Do not use exit code/data from init code */
1020{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001021 .fromsec = { ALL_INIT_SECTIONS, NULL },
1022 .tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001023 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001024 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001025},
1026/* Do not use init code/data from exit code */
1027{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001028 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001029 .tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001030 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001031 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001032},
1033/* Do not export init/exit functions or data */
1034{
1035 .fromsec = { "__ksymtab*", NULL },
Sam Ravnborgfa95eb12008-02-02 23:30:22 +01001036 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001037 .mismatch = EXPORT_TO_INIT_EXIT,
1038 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001039}
1040};
1041
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001042static const struct sectioncheck *section_mismatch(
1043 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001044{
1045 int i;
1046 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1047 const struct sectioncheck *check = &sectioncheck[0];
1048
1049 for (i = 0; i < elems; i++) {
1050 if (match(fromsec, check->fromsec) &&
1051 match(tosec, check->tosec))
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001052 return check;
Sam Ravnborg10668222008-01-13 22:21:31 +01001053 check++;
1054 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001055 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001056}
1057
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001058/**
1059 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001060 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001061 * Pattern 1:
1062 * If a module parameter is declared __initdata and permissions=0
1063 * then this is legal despite the warning generated.
1064 * We cannot see value of permissions here, so just ignore
1065 * this pattern.
1066 * The pattern is identified by:
1067 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001068 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001069 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001070 *
Rusty Russell6a841522010-08-11 23:04:16 -06001071 * Pattern 1a:
1072 * module_param_call() ops can refer to __init set function if permissions=0
1073 * The pattern is identified by:
1074 * tosec = .init.text
1075 * fromsec = .data*
1076 * atsym = __param_ops_*
1077 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001078 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001079 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001080 * add, remove, probe functions etc.
Uwe Kleine-Königb75dcab2010-01-29 11:40:38 +01001081 * These functions may often be marked __devinit and we do not want to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001082 * warn here.
1083 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001084 * tosec = init or exit section
1085 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001086 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1087 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001088 *
1089 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001090 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001091 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001092 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001093 * Some symbols belong to init section but still it is ok to reference
1094 * these from non-init sections as these symbols don't have any memory
1095 * allocated for them and symbol address and value are same. So even
1096 * if init section is freed, its ok to reference those symbols.
1097 * For ex. symbols marking the init section boundaries.
1098 * This pattern is identified by
1099 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001100 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001101 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001102static int secref_whitelist(const struct sectioncheck *mismatch,
1103 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001104 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001105{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001106 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001107 if (match(tosec, init_data_sections) &&
1108 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001109 (strncmp(fromsym, "__param", strlen("__param")) == 0))
1110 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001111
Rusty Russell6a841522010-08-11 23:04:16 -06001112 /* Check for pattern 1a */
1113 if (strcmp(tosec, ".init.text") == 0 &&
1114 match(fromsec, data_sections) &&
1115 (strncmp(fromsym, "__param_ops_", strlen("__param_ops_")) == 0))
1116 return 0;
1117
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001118 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001119 if (match(tosec, init_exit_sections) &&
1120 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001121 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001122 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001123
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001124 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001125 if (match(fromsec, head_sections) &&
1126 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001127 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001128
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001129 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001130 if (match(tosym, linker_symbols))
1131 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001132
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001133 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001134}
1135
1136/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001137 * Find symbol based on relocation record info.
1138 * In some cases the symbol supplied is a valid symbol so
1139 * return refsym. If st_name != 0 we assume this is a valid symbol.
1140 * In other cases the symbol needs to be looked up in the symbol table
1141 * based on section and address.
1142 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001143static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001144 Elf_Sym *relsym)
1145{
1146 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001147 Elf_Sym *near = NULL;
1148 Elf64_Sword distance = 20;
1149 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001150 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001151
1152 if (relsym->st_name != 0)
1153 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001154
1155 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001156 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001157 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001158 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001159 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1160 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001161 if (sym->st_value == addr)
1162 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001163 /* Find a symbol nearby - addr are maybe negative */
1164 d = sym->st_value - addr;
1165 if (d < 0)
1166 d = addr - sym->st_value;
1167 if (d < distance) {
1168 distance = d;
1169 near = sym;
1170 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001171 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001172 /* We need a close match */
1173 if (distance < 20)
1174 return near;
1175 else
1176 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001177}
1178
David Brownellda68d612007-02-20 13:58:16 -08001179static inline int is_arm_mapping_symbol(const char *str)
1180{
1181 return str[0] == '$' && strchr("atd", str[1])
1182 && (str[2] == '\0' || str[2] == '.');
1183}
1184
1185/*
1186 * If there's no name there, ignore it; likewise, ignore it if it's
1187 * one of the magic symbols emitted used by current ARM tools.
1188 *
1189 * Otherwise if find_symbols_between() returns those symbols, they'll
1190 * fail the whitelist tests and cause lots of false alarms ... fixable
1191 * only by merging __exit and __init sections into __text, bloating
1192 * the kernel (which is especially evil on embedded platforms).
1193 */
1194static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1195{
1196 const char *name = elf->strtab + sym->st_name;
1197
1198 if (!name || !strlen(name))
1199 return 0;
1200 return !is_arm_mapping_symbol(name);
1201}
1202
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001203/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001204 * Find symbols before or equal addr and after addr - in the section sec.
1205 * If we find two symbols with equal offset prefer one with a valid name.
1206 * The ELF format may have a better way to detect what type of symbol
1207 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001208 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001209static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1210 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001211{
1212 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001213 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001214 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001215
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001216 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1217 const char *symsec;
1218
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001219 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001220 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001221 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001222 if (strcmp(symsec, sec) != 0)
1223 continue;
David Brownellda68d612007-02-20 13:58:16 -08001224 if (!is_valid_name(elf, sym))
1225 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001226 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001227 if ((addr - sym->st_value) < distance) {
1228 distance = addr - sym->st_value;
1229 near = sym;
1230 } else if ((addr - sym->st_value) == distance) {
1231 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001232 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001233 }
1234 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001235 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001236}
1237
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001238/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001239 * Convert a section name to the function/data attribute
1240 * .init.text => __init
1241 * .cpuinit.data => __cpudata
1242 * .memexitconst => __memconst
1243 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001244 *
1245 * The memory of returned value has been allocated on a heap. The user of this
1246 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001247*/
1248static char *sec2annotation(const char *s)
1249{
1250 if (match(s, init_exit_sections)) {
1251 char *p = malloc(20);
1252 char *r = p;
1253
1254 *p++ = '_';
1255 *p++ = '_';
1256 if (*s == '.')
1257 s++;
1258 while (*s && *s != '.')
1259 *p++ = *s++;
1260 *p = '\0';
1261 if (*s == '.')
1262 s++;
1263 if (strstr(s, "rodata") != NULL)
1264 strcat(p, "const ");
1265 else if (strstr(s, "data") != NULL)
1266 strcat(p, "data ");
1267 else
1268 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001269 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001270 } else {
Andrew Morton5003bab2010-08-11 00:42:26 -07001271 return strdup("");
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001272 }
1273}
1274
1275static int is_function(Elf_Sym *sym)
1276{
1277 if (sym)
1278 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1279 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001280 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001281}
1282
Randy Dunlap00759c02011-03-15 14:13:47 -07001283static void print_section_list(const char * const list[20])
1284{
1285 const char *const *s = list;
1286
1287 while (*s) {
1288 fprintf(stderr, "%s", *s);
1289 s++;
1290 if (*s)
1291 fprintf(stderr, ", ");
1292 }
1293 fprintf(stderr, "\n");
1294}
1295
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001296/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001297 * Print a warning about a section mismatch.
1298 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001299 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001300 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001301static void report_sec_mismatch(const char *modname,
1302 const struct sectioncheck *mismatch,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001303 const char *fromsec,
1304 unsigned long long fromaddr,
1305 const char *fromsym,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001306 int from_is_func,
1307 const char *tosec, const char *tosym,
1308 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001309{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001310 const char *from, *from_p;
1311 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001312 char *prl_from;
1313 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001314
1315 switch (from_is_func) {
1316 case 0: from = "variable"; from_p = ""; break;
1317 case 1: from = "function"; from_p = "()"; break;
1318 default: from = "(unknown reference)"; from_p = ""; break;
1319 }
1320 switch (to_is_func) {
1321 case 0: to = "variable"; to_p = ""; break;
1322 case 1: to = "function"; to_p = "()"; break;
1323 default: to = "(unknown reference)"; to_p = ""; break;
1324 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001325
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001326 sec_mismatch_count++;
1327 if (!sec_mismatch_verbose)
1328 return;
1329
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001330 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1331 "to the %s %s:%s%s\n",
1332 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1333 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001334
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001335 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001336 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001337 prl_from = sec2annotation(fromsec);
1338 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001339 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001340 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001341 "the %s %s%s%s.\n"
1342 "This is often because %s lacks a %s\n"
1343 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001344 prl_from, fromsym,
1345 to, prl_to, tosym, to_p,
1346 fromsym, prl_to, tosym);
1347 free(prl_from);
1348 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001349 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001350 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001351 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001352 fprintf(stderr,
1353 "The variable %s references\n"
1354 "the %s %s%s%s\n"
1355 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001356 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001357 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001358 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001359 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001360 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001361 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001362 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001363 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001364 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001365 fprintf(stderr,
1366 "The function %s() references a %s in an exit section.\n"
1367 "Often the %s %s%s has valid usage outside the exit section\n"
1368 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001369 fromsym, to, to, tosym, to_p, prl_to, tosym);
1370 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001371 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001372 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001373 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001374 fprintf(stderr,
1375 "The variable %s references\n"
1376 "the %s %s%s%s\n"
1377 "If the reference is valid then annotate the\n"
1378 "variable with __exit* (see linux/init.h) or "
1379 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001380 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001381 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001382 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001383 break;
1384 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001385 case XXXINIT_TO_SOME_INIT:
1386 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001387 prl_from = sec2annotation(fromsec);
1388 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001389 fprintf(stderr,
1390 "The %s %s%s%s references\n"
1391 "a %s %s%s%s.\n"
1392 "If %s is only used by %s then\n"
1393 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001394 from, prl_from, fromsym, from_p,
1395 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001396 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001397 free(prl_from);
1398 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001399 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001400 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001401 prl_from = sec2annotation(fromsec);
1402 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001403 fprintf(stderr,
1404 "The %s %s%s%s references\n"
1405 "a %s %s%s%s.\n"
1406 "This is often seen when error handling "
1407 "in the init function\n"
1408 "uses functionality in the exit path.\n"
1409 "The fix is often to remove the %sannotation of\n"
1410 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001411 from, prl_from, fromsym, from_p,
1412 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001413 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001414 free(prl_from);
1415 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001416 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001417 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001418 prl_from = sec2annotation(fromsec);
1419 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001420 fprintf(stderr,
1421 "The %s %s%s%s references\n"
1422 "a %s %s%s%s.\n"
1423 "This is often seen when error handling "
1424 "in the exit function\n"
1425 "uses functionality in the init path.\n"
1426 "The fix is often to remove the %sannotation of\n"
1427 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001428 from, prl_from, fromsym, from_p,
1429 to, prl_to, tosym, to_p,
1430 prl_to, tosym, to_p);
1431 free(prl_from);
1432 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001433 break;
1434 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001435 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001436 fprintf(stderr,
1437 "The symbol %s is exported and annotated %s\n"
1438 "Fix this by removing the %sannotation of %s "
1439 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001440 tosym, prl_to, prl_to, tosym);
1441 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001442 break;
1443 }
1444 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001445}
1446
1447static void check_section_mismatch(const char *modname, struct elf_info *elf,
1448 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1449{
1450 const char *tosec;
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001451 const struct sectioncheck *mismatch;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001452
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001453 tosec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001454 mismatch = section_mismatch(fromsec, tosec);
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001455 if (mismatch) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001456 Elf_Sym *to;
1457 Elf_Sym *from;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001458 const char *tosym;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001459 const char *fromsym;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001460
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001461 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1462 fromsym = sym_name(elf, from);
1463 to = find_elf_symbol(elf, r->r_addend, sym);
1464 tosym = sym_name(elf, to);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001465
1466 /* check whitelist - we may ignore it */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001467 if (secref_whitelist(mismatch,
1468 fromsec, fromsym, tosec, tosym)) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001469 report_sec_mismatch(modname, mismatch,
1470 fromsec, r->r_offset, fromsym,
1471 is_function(from), tosec, tosym,
1472 is_function(to));
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001473 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001474 }
1475}
1476
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001477static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001478 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001479{
1480 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001481 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001482
1483 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001484 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001485}
1486
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001487static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001488{
1489 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001490 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001491
1492 switch (r_typ) {
1493 case R_386_32:
1494 r->r_addend = TO_NATIVE(*location);
1495 break;
1496 case R_386_PC32:
1497 r->r_addend = TO_NATIVE(*location) + 4;
1498 /* For CONFIG_RELOCATABLE=y */
1499 if (elf->hdr->e_type == ET_EXEC)
1500 r->r_addend += r->r_offset;
1501 break;
1502 }
1503 return 0;
1504}
1505
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001506#ifndef R_ARM_CALL
1507#define R_ARM_CALL 28
1508#endif
1509#ifndef R_ARM_JUMP24
1510#define R_ARM_JUMP24 29
1511#endif
1512
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001513static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001514{
1515 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1516
1517 switch (r_typ) {
1518 case R_ARM_ABS32:
1519 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001520 r->r_addend = (int)(long)
1521 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001522 break;
1523 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001524 case R_ARM_CALL:
1525 case R_ARM_JUMP24:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001526 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001527 r->r_addend = (int)(long)(elf->hdr +
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001528 sechdr->sh_offset +
1529 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001530 break;
1531 default:
1532 return 1;
1533 }
1534 return 0;
1535}
1536
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001537static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001538{
1539 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001540 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001541 unsigned int inst;
1542
1543 if (r_typ == R_MIPS_HI16)
1544 return 1; /* skip this */
1545 inst = TO_NATIVE(*location);
1546 switch (r_typ) {
1547 case R_MIPS_LO16:
1548 r->r_addend = inst & 0xffff;
1549 break;
1550 case R_MIPS_26:
1551 r->r_addend = (inst & 0x03ffffff) << 2;
1552 break;
1553 case R_MIPS_32:
1554 r->r_addend = inst;
1555 break;
1556 }
1557 return 0;
1558}
1559
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001560static void section_rela(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001561 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001562{
1563 Elf_Sym *sym;
1564 Elf_Rela *rela;
1565 Elf_Rela r;
1566 unsigned int r_sym;
1567 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001568
Sam Ravnborgff13f922008-01-23 19:54:27 +01001569 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001570 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1571
Sam Ravnborgff13f922008-01-23 19:54:27 +01001572 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001573 fromsec += strlen(".rela");
1574 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001575 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001576 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001577
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001578 for (rela = start; rela < stop; rela++) {
1579 r.r_offset = TO_NATIVE(rela->r_offset);
1580#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001581 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001582 unsigned int r_typ;
1583 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1584 r_sym = TO_NATIVE(r_sym);
1585 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1586 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1587 } else {
1588 r.r_info = TO_NATIVE(rela->r_info);
1589 r_sym = ELF_R_SYM(r.r_info);
1590 }
1591#else
1592 r.r_info = TO_NATIVE(rela->r_info);
1593 r_sym = ELF_R_SYM(r.r_info);
1594#endif
1595 r.r_addend = TO_NATIVE(rela->r_addend);
1596 sym = elf->symtab_start + r_sym;
1597 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001598 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001599 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001600 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001601 }
1602}
1603
1604static void section_rel(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001605 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001606{
1607 Elf_Sym *sym;
1608 Elf_Rel *rel;
1609 Elf_Rela r;
1610 unsigned int r_sym;
1611 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001612
Sam Ravnborgff13f922008-01-23 19:54:27 +01001613 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001614 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1615
Sam Ravnborgff13f922008-01-23 19:54:27 +01001616 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001617 fromsec += strlen(".rel");
1618 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001619 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001620 return;
1621
1622 for (rel = start; rel < stop; rel++) {
1623 r.r_offset = TO_NATIVE(rel->r_offset);
1624#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001625 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001626 unsigned int r_typ;
1627 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1628 r_sym = TO_NATIVE(r_sym);
1629 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1630 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1631 } else {
1632 r.r_info = TO_NATIVE(rel->r_info);
1633 r_sym = ELF_R_SYM(r.r_info);
1634 }
1635#else
1636 r.r_info = TO_NATIVE(rel->r_info);
1637 r_sym = ELF_R_SYM(r.r_info);
1638#endif
1639 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001640 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001641 case EM_386:
1642 if (addend_386_rel(elf, sechdr, &r))
1643 continue;
1644 break;
1645 case EM_ARM:
1646 if (addend_arm_rel(elf, sechdr, &r))
1647 continue;
1648 break;
1649 case EM_MIPS:
1650 if (addend_mips_rel(elf, sechdr, &r))
1651 continue;
1652 break;
1653 }
1654 sym = elf->symtab_start + r_sym;
1655 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001656 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001657 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001658 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001659 }
1660}
1661
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001662/**
1663 * A module includes a number of sections that are discarded
1664 * either when loaded or when used as built-in.
1665 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001666 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001667 * Likewise for modules used built-in the sections marked __exit
1668 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001669 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001670 * The check_sec_ref() function traverses all relocation records
1671 * to find all references to a section that reference a section that will
1672 * be discarded and warns about it.
1673 **/
1674static void check_sec_ref(struct module *mod, const char *modname,
Sam Ravnborg10668222008-01-13 22:21:31 +01001675 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001676{
1677 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001678 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001679
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001680 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001681 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001682 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001683 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001684 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001685 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001686 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001687 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001688 }
1689}
1690
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001691static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 const char *symname;
1694 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001695 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 struct module *mod;
1697 struct elf_info info = { };
1698 Elf_Sym *sym;
1699
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001700 if (!parse_elf(&info, modname))
1701 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 mod = new_module(modname);
1704
1705 /* When there's no vmlinux, don't print warnings about
1706 * unresolved symbols (since there'll be too many ;) */
1707 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 mod->skip = 1;
1710 }
1711
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001712 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
Sam Ravnborg2fa36562008-04-26 21:07:26 +02001713 if (info.modinfo && !license && !is_vmlinux(modname))
1714 warn("modpost: missing MODULE_LICENSE() in %s\n"
1715 "see include/linux/module.h for "
1716 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001717 while (license) {
1718 if (license_is_gpl_compatible(license))
1719 mod->gpl_compatible = 1;
1720 else {
1721 mod->gpl_compatible = 0;
1722 break;
1723 }
1724 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1725 "license", license);
1726 }
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1729 symname = info.strtab + sym->st_name;
1730
1731 handle_modversions(mod, &info, sym, symname);
1732 handle_moddevtable(mod, &info, sym, symname);
1733 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001734 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001735 (is_vmlinux(modname) && vmlinux_section_warnings))
1736 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1739 if (version)
1740 maybe_frob_rcs_version(modname, version, info.modinfo,
1741 version - (char *)info.hdr);
1742 if (version || (all_versions && !is_vmlinux(modname)))
1743 get_src_version(modname, mod->srcversion,
1744 sizeof(mod->srcversion)-1);
1745
1746 parse_elf_finish(&info);
1747
Rusty Russell8c8ef422009-03-31 13:05:34 -06001748 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 * never passed as an argument to an exported function, so
1750 * the automatic versioning doesn't pick it up, but it's really
1751 * important anyhow */
1752 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06001753 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756#define SZ 500
1757
1758/* We first write the generated file into memory using the
1759 * following helper, then compare to the file on disk and
1760 * only update the later if anything changed */
1761
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001762void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1763 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 char tmp[SZ];
1766 int len;
1767 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 va_start(ap, fmt);
1770 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001771 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 va_end(ap);
1773}
1774
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001775void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001778 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 buf->p = realloc(buf->p, buf->size);
1780 }
1781 strncpy(buf->p + buf->pos, s, len);
1782 buf->pos += len;
1783}
1784
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001785static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1786{
1787 const char *e = is_vmlinux(m) ?"":".ko";
1788
1789 switch (exp) {
1790 case export_gpl:
1791 fatal("modpost: GPL-incompatible module %s%s "
1792 "uses GPL-only symbol '%s'\n", m, e, s);
1793 break;
1794 case export_unused_gpl:
1795 fatal("modpost: GPL-incompatible module %s%s "
1796 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1797 break;
1798 case export_gpl_future:
1799 warn("modpost: GPL-incompatible module %s%s "
1800 "uses future GPL-only symbol '%s'\n", m, e, s);
1801 break;
1802 case export_plain:
1803 case export_unused:
1804 case export_unknown:
1805 /* ignore */
1806 break;
1807 }
1808}
1809
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001810static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001811{
1812 const char *e = is_vmlinux(m) ?"":".ko";
1813
1814 switch (exp) {
1815 case export_unused:
1816 case export_unused_gpl:
1817 warn("modpost: module %s%s "
1818 "uses symbol '%s' marked UNUSED\n", m, e, s);
1819 break;
1820 default:
1821 /* ignore */
1822 break;
1823 }
1824}
1825
1826static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001827{
1828 struct symbol *s, *exp;
1829
1830 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001831 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001832 exp = find_symbol(s->name);
1833 if (!exp || exp->module == mod)
1834 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001835 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001836 if (basename)
1837 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001838 else
1839 basename = mod->name;
1840 if (!mod->gpl_compatible)
1841 check_for_gpl_usage(exp->export, basename, exp->name);
1842 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001843 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001844}
1845
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001846/**
1847 * Header for the generated file
1848 **/
1849static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 buf_printf(b, "#include <linux/module.h>\n");
1852 buf_printf(b, "#include <linux/vermagic.h>\n");
1853 buf_printf(b, "#include <linux/compiler.h>\n");
1854 buf_printf(b, "\n");
1855 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1856 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 buf_printf(b, "struct module __this_module\n");
1858 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +04001859 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (mod->has_init)
1861 buf_printf(b, " .init = init_module,\n");
1862 if (mod->has_cleanup)
1863 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
1864 " .exit = cleanup_module,\n"
1865 "#endif\n");
Roman Zippele61a1c12007-05-09 02:35:15 -07001866 buf_printf(b, " .arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 buf_printf(b, "};\n");
1868}
1869
Ben Hutchings2449b8b2011-10-24 15:12:28 +02001870static void add_intree_flag(struct buffer *b, int is_intree)
1871{
1872 if (is_intree)
1873 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
1874}
1875
Trevor Keith5c725132009-09-22 16:43:38 -07001876static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07001877{
1878 static const char *staging_dir = "drivers/staging";
1879
1880 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
1881 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1882}
1883
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001884/**
1885 * Record CRCs for unresolved symbols
1886 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001887static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001890 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 for (s = mod->unres; s; s = s->next) {
1893 exp = find_symbol(s->name);
1894 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001895 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001896 if (warn_unresolved) {
1897 warn("\"%s\" [%s.ko] undefined!\n",
1898 s->name, mod->name);
1899 } else {
1900 merror("\"%s\" [%s.ko] undefined!\n",
1901 s->name, mod->name);
1902 err = 1;
1903 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 continue;
1906 }
1907 s->module = exp->module;
1908 s->crc_valid = exp->crc_valid;
1909 s->crc = exp->crc;
1910 }
1911
1912 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001913 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 buf_printf(b, "\n");
1916 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001917 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1919
1920 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001921 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001924 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 s->name, mod->name);
1926 continue;
1927 }
1928 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
1929 }
1930
1931 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001932
1933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001936static void add_depends(struct buffer *b, struct module *mod,
1937 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
1939 struct symbol *s;
1940 struct module *m;
1941 int first = 1;
1942
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001943 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 buf_printf(b, "\n");
1947 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001948 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1950 buf_printf(b, "\"depends=");
1951 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001952 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (!s->module)
1954 continue;
1955
1956 if (s->module->seen)
1957 continue;
1958
1959 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001960 p = strrchr(s->module->name, '/');
1961 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001962 p++;
1963 else
1964 p = s->module->name;
1965 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 first = 0;
1967 }
1968 buf_printf(b, "\";\n");
1969}
1970
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001971static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 if (mod->srcversion[0]) {
1974 buf_printf(b, "\n");
1975 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1976 mod->srcversion);
1977 }
1978}
1979
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001980static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 char *tmp;
1983 FILE *file;
1984 struct stat st;
1985
1986 file = fopen(fname, "r");
1987 if (!file)
1988 goto write;
1989
1990 if (fstat(fileno(file), &st) < 0)
1991 goto close_write;
1992
1993 if (st.st_size != b->pos)
1994 goto close_write;
1995
1996 tmp = NOFAIL(malloc(b->pos));
1997 if (fread(tmp, 1, b->pos, file) != b->pos)
1998 goto free_write;
1999
2000 if (memcmp(tmp, b->p, b->pos) != 0)
2001 goto free_write;
2002
2003 free(tmp);
2004 fclose(file);
2005 return;
2006
2007 free_write:
2008 free(tmp);
2009 close_write:
2010 fclose(file);
2011 write:
2012 file = fopen(fname, "w");
2013 if (!file) {
2014 perror(fname);
2015 exit(1);
2016 }
2017 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2018 perror(fname);
2019 exit(1);
2020 }
2021 fclose(file);
2022}
2023
Ram Paibd5cbce2006-06-08 22:12:53 -07002024/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002025 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002026 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002027static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 unsigned long size, pos = 0;
2030 void *file = grab_file(fname, &size);
2031 char *line;
2032
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002033 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 /* No symbol versions, silently ignore */
2035 return;
2036
2037 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002038 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 unsigned int crc;
2040 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002041 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 if (!(symname = strchr(line, '\t')))
2044 goto fail;
2045 *symname++ = '\0';
2046 if (!(modname = strchr(symname, '\t')))
2047 goto fail;
2048 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002049 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002050 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002051 if (export && ((end = strchr(export, '\t')) != NULL))
2052 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 crc = strtoul(line, &d, 16);
2054 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2055 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002056 mod = find_module(modname);
2057 if (!mod) {
2058 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002060 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 mod->skip = 1;
2062 }
Ram Paibd5cbce2006-06-08 22:12:53 -07002063 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002064 s->kernel = kernel;
2065 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07002066 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068 return;
2069fail:
2070 fatal("parse error in symbol dump file\n");
2071}
2072
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002073/* For normal builds always dump all symbols.
2074 * For external modules only dump symbols
2075 * that are not read from kernel Module.symvers.
2076 **/
2077static int dump_sym(struct symbol *sym)
2078{
2079 if (!external_module)
2080 return 1;
2081 if (sym->vmlinux || sym->kernel)
2082 return 0;
2083 return 1;
2084}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002085
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002086static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 struct buffer buf = { };
2089 struct symbol *symbol;
2090 int n;
2091
2092 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2093 symbol = symbolhash[n];
2094 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002095 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07002096 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002097 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07002098 symbol->module->name,
2099 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 symbol = symbol->next;
2101 }
2102 }
2103 write_if_changed(&buf, fname);
2104}
2105
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002106struct ext_sym_list {
2107 struct ext_sym_list *next;
2108 const char *file;
2109};
2110
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002111int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 struct module *mod;
2114 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002115 char *kernel_read = NULL, *module_read = NULL;
2116 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002118 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002119 struct ext_sym_list *extsym_iter;
2120 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002122 while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:E")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002123 switch (opt) {
2124 case 'i':
2125 kernel_read = optarg;
2126 break;
2127 case 'I':
2128 module_read = optarg;
2129 external_module = 1;
2130 break;
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +01002131 case 'c':
2132 cross_build = 1;
2133 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002134 case 'e':
2135 external_module = 1;
2136 extsym_iter =
2137 NOFAIL(malloc(sizeof(*extsym_iter)));
2138 extsym_iter->next = extsym_start;
2139 extsym_iter->file = optarg;
2140 extsym_start = extsym_iter;
2141 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002142 case 'm':
2143 modversions = 1;
2144 break;
2145 case 'o':
2146 dump_write = optarg;
2147 break;
2148 case 'a':
2149 all_versions = 1;
2150 break;
2151 case 's':
2152 vmlinux_section_warnings = 0;
2153 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002154 case 'S':
2155 sec_mismatch_verbose = 0;
2156 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002157 case 'w':
2158 warn_unresolved = 1;
2159 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002160 case 'E':
2161 section_error_on_mismatch = 1;
2162 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002163 default:
2164 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166 }
2167
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002168 if (kernel_read)
2169 read_dump(kernel_read, 1);
2170 if (module_read)
2171 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002172 while (extsym_start) {
2173 read_dump(extsym_start->file, 0);
2174 extsym_iter = extsym_start->next;
2175 free(extsym_start);
2176 extsym_start = extsym_iter;
2177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002179 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 for (mod = modules; mod; mod = mod->next) {
2183 if (mod->skip)
2184 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002185 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002186 }
2187
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002188 err = 0;
2189
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002190 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01002191 char fname[strlen(mod->name) + 10];
2192
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002193 if (mod->skip)
2194 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 buf.pos = 0;
2197
2198 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002199 add_intree_flag(&buf, !external_module);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002200 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002201 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 add_depends(&buf, mod, modules);
2203 add_moddevtable(&buf, mod);
2204 add_srcversion(&buf, mod);
2205
2206 sprintf(fname, "%s.mod.c", mod->name);
2207 write_if_changed(&buf, fname);
2208 }
2209
2210 if (dump_write)
2211 write_dump(dump_write);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002212
2213 if (sec_mismatch_count && !sec_mismatch_verbose) {
2214 merror(
2215 "modpost: Found %d section mismatch(es).\n"
2216 "To see full details build your kernel with:\n"
2217 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2218 sec_mismatch_count);
2219
2220 }
2221
2222 if (sec_mismatch_count && section_error_on_mismatch) {
2223 err |= 1;
2224 printf(
2225 "To build the kernel despite the mismatches, "
2226 "build with:\n'make CONFIG_NO_ERROR_ON_MISMATCH=y'\n"
2227 "(NOTE: This is not recommended)\n");
2228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002230 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}