blob: f6913db776278ba83d729fc5118d4f95dd0654d5 [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"
Rusty Russellb92021b2013-03-15 15:04:17 +103021#include "../../include/linux/export.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/* Are we using CONFIG_MODVERSIONS? */
24int modversions = 0;
25/* Warn about undefined symbols? (do so if we have vmlinux) */
26int have_vmlinux = 0;
27/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
28static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010029/* If we are modposting external module set to 1 */
30static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020031/* Warn about section mismatch in vmlinux if set to 1 */
32static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070033/* Only warn about unresolved symbols */
34static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070035/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010036static int sec_mismatch_count = 0;
37static int sec_mismatch_verbose = 1;
38
Sam Ravnborgc96fca22006-07-01 11:44:23 +020039enum export {
40 export_plain, export_unused, export_gpl,
41 export_unused_gpl, export_gpl_future, export_unknown
42};
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Andi Kleen6d9a89e2007-11-22 03:43:08 +010044#define PRINTF __attribute__ ((format (printf, 1, 2)))
45
46PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 va_list arglist;
49
50 fprintf(stderr, "FATAL: ");
51
52 va_start(arglist, fmt);
53 vfprintf(stderr, fmt, arglist);
54 va_end(arglist);
55
56 exit(1);
57}
58
Andi Kleen6d9a89e2007-11-22 03:43:08 +010059PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 va_list arglist;
62
63 fprintf(stderr, "WARNING: ");
64
65 va_start(arglist, fmt);
66 vfprintf(stderr, fmt, arglist);
67 va_end(arglist);
68}
69
Andi Kleen6d9a89e2007-11-22 03:43:08 +010070PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060071{
72 va_list arglist;
73
74 fprintf(stderr, "ERROR: ");
75
76 va_start(arglist, fmt);
77 vfprintf(stderr, fmt, arglist);
78 va_end(arglist);
79}
80
Sam Ravnborg040fcc82006-01-28 22:15:55 +010081static int is_vmlinux(const char *modname)
82{
83 const char *myname;
84
Sam Ravnborgdf578e72008-01-11 19:17:15 +010085 myname = strrchr(modname, '/');
86 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010087 myname++;
88 else
89 myname = modname;
90
Sam Ravnborg741f98f2007-07-17 10:54:06 +020091 return (strcmp(myname, "vmlinux") == 0) ||
92 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +010093}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095void *do_nofail(void *ptr, const char *expr)
96{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010097 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +010099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return ptr;
101}
102
103/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static struct module *modules;
105
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100106static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct module *mod;
109
110 for (mod = modules; mod; mod = mod->next)
111 if (strcmp(mod->name, modname) == 0)
112 break;
113 return mod;
114}
115
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100116static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct module *mod;
119 char *p, *s;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 mod = NOFAIL(malloc(sizeof(*mod)));
122 memset(mod, 0, sizeof(*mod));
123 p = NOFAIL(strdup(modname));
124
125 /* strip trailing .o */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100126 s = strrchr(p, '.');
127 if (s != NULL)
Frank Rowand258f7422012-04-09 17:59:03 -0700128 if (strcmp(s, ".o") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *s = '\0';
Frank Rowand258f7422012-04-09 17:59:03 -0700130 mod->is_dot_o = 1;
131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* add to list */
134 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200135 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 mod->next = modules;
137 modules = mod;
138
139 return mod;
140}
141
142/* A hash of all exported symbols,
143 * struct symbol is also used for lists of unresolved symbols */
144
145#define SYMBOL_HASH_SIZE 1024
146
147struct symbol {
148 struct symbol *next;
149 struct module *module;
150 unsigned int crc;
151 int crc_valid;
152 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100153 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
154 unsigned int kernel:1; /* 1 if symbol is from kernel
155 * (only for external modules) **/
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100156 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Ram Paibd5cbce2006-06-08 22:12:53 -0700157 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 char name[0];
159};
160
161static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
162
163/* This is based on the hash agorithm from gdbm, via tdb */
164static inline unsigned int tdb_hash(const char *name)
165{
166 unsigned value; /* Used to compute the hash value. */
167 unsigned i; /* Used to cycle through random values. */
168
169 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100170 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
172
173 return (1103515243 * value + 12345);
174}
175
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100176/**
177 * Allocate a new symbols for use in the hash of exported symbols or
178 * the list of unresolved symbols per module
179 **/
180static struct symbol *alloc_symbol(const char *name, unsigned int weak,
181 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
184
185 memset(s, 0, sizeof(*s));
186 strcpy(s->name, name);
187 s->weak = weak;
188 s->next = next;
189 return s;
190}
191
192/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700193static struct symbol *new_symbol(const char *name, struct module *module,
194 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 unsigned int hash;
197 struct symbol *new;
198
199 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
200 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
201 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700202 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100203 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100206static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct symbol *s;
209
210 /* For our purposes, .foo matches foo. PPC64 needs this. */
211 if (name[0] == '.')
212 name++;
213
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100214 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (strcmp(s->name, name) == 0)
216 return s;
217 }
218 return NULL;
219}
220
Ram Paibd5cbce2006-06-08 22:12:53 -0700221static struct {
222 const char *str;
223 enum export export;
224} export_list[] = {
225 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200226 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700227 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200228 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700229 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
230 { .str = "(unknown)", .export = export_unknown },
231};
232
233
234static const char *export_str(enum export ex)
235{
236 return export_list[ex].str;
237}
238
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100239static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700240{
241 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100242
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200243 if (!s)
244 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700245 for (i = 0; export_list[i].export != export_unknown; i++) {
246 if (strcmp(export_list[i].str, s) == 0)
247 return export_list[i].export;
248 }
249 return export_unknown;
250}
251
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200252static const char *sec_name(struct elf_info *elf, int secindex);
253
254#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
255
256static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
257{
258 const char *secname = sec_name(elf, sec);
259
260 if (strstarts(secname, "___ksymtab+"))
261 return export_plain;
262 else if (strstarts(secname, "___ksymtab_unused+"))
263 return export_unused;
264 else if (strstarts(secname, "___ksymtab_gpl+"))
265 return export_gpl;
266 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
267 return export_unused_gpl;
268 else if (strstarts(secname, "___ksymtab_gpl_future+"))
269 return export_gpl_future;
270 else
271 return export_unknown;
272}
273
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200274static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700275{
276 if (sec == elf->export_sec)
277 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200278 else if (sec == elf->export_unused_sec)
279 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700280 else if (sec == elf->export_gpl_sec)
281 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200282 else if (sec == elf->export_unused_gpl_sec)
283 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700284 else if (sec == elf->export_gpl_future_sec)
285 return export_gpl_future;
286 else
287 return export_unknown;
288}
289
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100290/**
291 * Add an exported symbol - it may have already been added without a
292 * CRC, in this case just update the CRC
293 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700294static struct symbol *sym_add_exported(const char *name, struct module *mod,
295 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct symbol *s = find_symbol(name);
298
299 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700300 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100301 } else {
302 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100303 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100304 "was in %s%s\n", mod->name, name,
305 s->module->name,
306 is_vmlinux(s->module->name) ?"":".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700307 } else {
308 /* In case Modules.symvers was out of date */
309 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100312 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100313 s->vmlinux = is_vmlinux(mod->name);
314 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700315 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100316 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100319static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700320 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100321{
322 struct symbol *s = find_symbol(name);
323
324 if (!s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700325 s = new_symbol(name, mod, export);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100326 s->crc = crc;
327 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100330void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930333 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int fd;
335
336 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930337 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930339 if (fstat(fd, &st))
340 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 *size = st.st_size;
343 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930345failed:
346 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (map == MAP_FAILED)
348 return NULL;
349 return map;
350}
351
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100352/**
353 * Return a copy of the next line in a mmap'ed file.
354 * spaces in the beginning of the line is trimmed away.
355 * Return a pointer to a static buffer.
356 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100357char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 static char line[4096];
360 int skip = 1;
361 size_t len = 0;
362 signed char *p = (signed char *)file + *pos;
363 char *s = line;
364
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100365 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (skip && isspace(*p)) {
367 p++;
368 continue;
369 }
370 skip = 0;
371 if (*p != '\n' && (*pos < size)) {
372 len++;
373 *s++ = *p++;
374 if (len > 4095)
375 break; /* Too long, stop */
376 } else {
377 /* End of string */
378 *s = '\0';
379 return line;
380 }
381 }
382 /* End of buffer */
383 return NULL;
384}
385
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100386void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 munmap(file, size);
389}
390
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100391static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100394 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 Elf_Shdr *sechdrs;
396 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200397 const char *secstrings;
398 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 hdr = grab_file(filename, &info->size);
401 if (!hdr) {
402 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200403 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100406 if (info->size < sizeof(*hdr)) {
407 /* file too small, assume this is an empty .o file */
408 return 0;
409 }
410 /* Is this a valid ELF file? */
411 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
412 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
413 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
414 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
415 /* Not an ELF file - silently ignore it */
416 return 0;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200419 hdr->e_type = TO_NATIVE(hdr->e_type);
420 hdr->e_machine = TO_NATIVE(hdr->e_machine);
421 hdr->e_version = TO_NATIVE(hdr->e_version);
422 hdr->e_entry = TO_NATIVE(hdr->e_entry);
423 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
424 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
425 hdr->e_flags = TO_NATIVE(hdr->e_flags);
426 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
427 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
428 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
429 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
430 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
431 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sechdrs = (void *)hdr + hdr->e_shoff;
433 info->sechdrs = sechdrs;
434
Petr Stetiara83710e2007-08-27 12:15:07 +0200435 /* Check if file offset is correct */
436 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100437 fatal("section header offset=%lu in file '%s' is bigger than "
438 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
439 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200440 return 0;
441 }
442
Anders Kaseorg68457562011-05-19 16:55:27 -0600443 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200444 /*
445 * There are more than 64k sections,
446 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200447 */
448 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
449 }
450 else {
451 info->num_sections = hdr->e_shnum;
452 }
453 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600454 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200455 }
456 else {
457 info->secindex_strings = hdr->e_shstrndx;
458 }
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200461 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200462 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
463 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
464 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
465 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
466 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
467 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
468 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
469 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
470 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
471 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200474 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
475 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700476 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900477 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Tejun Heo56fc82c2009-02-06 00:48:02 +0900479 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100480 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
481 "sizeof(*hrd)=%zu\n", filename,
482 (unsigned long)sechdrs[i].sh_offset,
483 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100484 return 0;
485 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700486 secname = secstrings + sechdrs[i].sh_name;
487 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900488 if (nobits)
489 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
491 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700492 } else if (strcmp(secname, "__ksymtab") == 0)
493 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200494 else if (strcmp(secname, "__ksymtab_unused") == 0)
495 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700496 else if (strcmp(secname, "__ksymtab_gpl") == 0)
497 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200498 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
499 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700500 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
501 info->export_gpl_future_sec = i;
502
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200503 if (sechdrs[i].sh_type == SHT_SYMTAB) {
504 unsigned int sh_link_idx;
505 symtab_idx = i;
506 info->symtab_start = (void *)hdr +
507 sechdrs[i].sh_offset;
508 info->symtab_stop = (void *)hdr +
509 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600510 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200511 info->strtab = (void *)hdr +
512 sechdrs[sh_link_idx].sh_offset;
513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200515 /* 32bit section no. table? ("more than 64k sections") */
516 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
517 symtab_shndx_idx = i;
518 info->symtab_shndx_start = (void *)hdr +
519 sechdrs[i].sh_offset;
520 info->symtab_shndx_stop = (void *)hdr +
521 sechdrs[i].sh_offset + sechdrs[i].sh_size;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100524 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100525 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* Fix endianness in symbols */
528 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
529 sym->st_shndx = TO_NATIVE(sym->st_shndx);
530 sym->st_name = TO_NATIVE(sym->st_name);
531 sym->st_value = TO_NATIVE(sym->st_value);
532 sym->st_size = TO_NATIVE(sym->st_size);
533 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200534
535 if (symtab_shndx_idx != ~0U) {
536 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600537 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200538 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600539 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200540 symtab_idx);
541 /* Fix endianness */
542 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
543 p++)
544 *p = TO_NATIVE(*p);
545 }
546
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100547 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100550static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 release_file(info->hdr, info->size);
553}
554
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200555static int ignore_undef_symbol(struct elf_info *info, const char *symname)
556{
557 /* ignore __this_module, it will be resolved shortly */
Rusty Russellb92021b2013-03-15 15:04:17 +1030558 if (strcmp(symname, VMLINUX_SYMBOL_STR(__this_module)) == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200559 return 1;
560 /* ignore global offset table */
561 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
562 return 1;
563 if (info->hdr->e_machine == EM_PPC)
564 /* Special register function linked on all modules during final link of .ko */
565 if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
566 strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
567 strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
568 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0)
569 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000570 if (info->hdr->e_machine == EM_PPC64)
571 /* Special register function linked on all modules during final link of .ko */
572 if (strncmp(symname, "_restgpr0_", sizeof("_restgpr0_") - 1) == 0 ||
573 strncmp(symname, "_savegpr0_", sizeof("_savegpr0_") - 1) == 0)
574 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200575 /* Do not ignore this symbol */
576 return 0;
577}
578
Rusty Russellb92021b2013-03-15 15:04:17 +1030579#define CRC_PFX VMLINUX_SYMBOL_STR(__crc_)
580#define KSYMTAB_PFX VMLINUX_SYMBOL_STR(__ksymtab_)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100582static void handle_modversions(struct module *mod, struct elf_info *info,
583 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200586 enum export export;
587
Frank Rowand258f7422012-04-09 17:59:03 -0700588 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
589 strncmp(symname, "__ksymtab", 9) == 0)
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200590 export = export_from_secname(info, get_secindex(info, sym));
591 else
592 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 switch (sym->st_shndx) {
595 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100596 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
598 case SHN_ABS:
599 /* CRC'd symbol */
Michal Marek8d995132009-12-12 12:02:24 +0100600 if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 crc = (unsigned int) sym->st_value;
Ram Paibd5cbce2006-06-08 22:12:53 -0700602 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
603 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605 break;
606 case SHN_UNDEF:
607 /* undefined symbol */
608 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
609 ELF_ST_BIND(sym->st_info) != STB_WEAK)
610 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200611 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 break;
Ben Colline8d529012005-08-19 13:44:57 -0700613/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
614#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
615/* add compatibility with older glibc */
616#ifndef STT_SPARC_REGISTER
617#define STT_SPARC_REGISTER STT_REGISTER
618#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (info->hdr->e_machine == EM_SPARC ||
620 info->hdr->e_machine == EM_SPARCV9) {
621 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700622 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100624 if (symname[0] == '.') {
625 char *munged = strdup(symname);
626 munged[0] = '_';
627 munged[1] = toupper(munged[1]);
628 symname = munged;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100632
Rusty Russellb92021b2013-03-15 15:04:17 +1030633#ifdef CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX
634 if (symname[0] != '_')
635 break;
636 else
637 symname++;
638#endif
639 mod->unres = alloc_symbol(symname,
640 ELF_ST_BIND(sym->st_info) == STB_WEAK,
641 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 default:
644 /* All exported symbols */
Michal Marek8d995132009-12-12 12:02:24 +0100645 if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700646 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
647 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030649 if (strcmp(symname, VMLINUX_SYMBOL_STR(init_module)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 mod->has_init = 1;
Rusty Russellb92021b2013-03-15 15:04:17 +1030651 if (strcmp(symname, VMLINUX_SYMBOL_STR(cleanup_module)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 mod->has_cleanup = 1;
653 break;
654 }
655}
656
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100657/**
658 * Parse tag=value strings from .modinfo section
659 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660static char *next_string(char *string, unsigned long *secsize)
661{
662 /* Skip non-zero chars */
663 while (string[0]) {
664 string++;
665 if ((*secsize)-- <= 1)
666 return NULL;
667 }
668
669 /* Skip any zero padding. */
670 while (!string[0]) {
671 string++;
672 if ((*secsize)-- <= 1)
673 return NULL;
674 }
675 return string;
676}
677
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200678static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
679 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 char *p;
682 unsigned int taglen = strlen(tag);
683 unsigned long size = modinfo_len;
684
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200685 if (info) {
686 size -= info - (char *)modinfo;
687 modinfo = next_string(info, &size);
688 }
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 for (p = modinfo; p; p = next_string(p, &size)) {
691 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
692 return p + taglen + 1;
693 }
694 return NULL;
695}
696
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200697static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
698 const char *tag)
699
700{
701 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
702}
703
Sam Ravnborg93684d32006-02-19 11:53:35 +0100704/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100705 * Test if string s ends in string sub
706 * return 0 if match
707 **/
708static int strrcmp(const char *s, const char *sub)
709{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100710 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100711
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100712 if (!s || !sub)
713 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100714
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100715 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100716 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100717
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100718 if ((slen == 0) || (sublen == 0))
719 return 1;
720
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100721 if (sublen > slen)
722 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100723
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100724 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100725}
726
Sam Ravnborgff13f922008-01-23 19:54:27 +0100727static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
728{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100729 if (sym)
730 return elf->strtab + sym->st_name;
731 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100732 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100733}
734
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200735static const char *sec_name(struct elf_info *elf, int secindex)
Sam Ravnborgff13f922008-01-23 19:54:27 +0100736{
737 Elf_Shdr *sechdrs = elf->sechdrs;
738 return (void *)elf->hdr +
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200739 elf->sechdrs[elf->secindex_strings].sh_offset +
740 sechdrs[secindex].sh_name;
Sam Ravnborgff13f922008-01-23 19:54:27 +0100741}
742
743static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
744{
745 return (void *)elf->hdr +
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200746 elf->sechdrs[elf->secindex_strings].sh_offset +
747 sechdr->sh_name;
Sam Ravnborgff13f922008-01-23 19:54:27 +0100748}
749
Sam Ravnborg10668222008-01-13 22:21:31 +0100750/* if sym is empty or point to a string
751 * like ".[0-9]+" then return 1.
752 * This is the optional prefix added by ld to some sections
753 */
754static int number_prefix(const char *sym)
755{
756 if (*sym++ == '\0')
757 return 1;
758 if (*sym != '.')
759 return 0;
760 do {
761 char c = *sym++;
762 if (c < '0' || c > '9')
763 return 0;
764 } while (*sym);
765 return 1;
766}
767
768/* The pattern is an array of simple patterns.
769 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100770 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100771 * "foo*" will match a string that begins with "foo"
772 * "foo$" will match a string equal to "foo" or "foo.1"
773 * where the '1' can be any number including several digits.
774 * The $ syntax is for sections where ld append a dot number
775 * to make section name unique.
776 */
Trevor Keith5c725132009-09-22 16:43:38 -0700777static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100778{
779 const char *p;
780 while (*pat) {
781 p = *pat++;
782 const char *endp = p + strlen(p) - 1;
783
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100784 /* "*foo" */
785 if (*p == '*') {
786 if (strrcmp(sym, p + 1) == 0)
787 return 1;
788 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100789 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100790 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100791 if (strncmp(sym, p, strlen(p) - 1) == 0)
792 return 1;
793 }
794 /* "foo$" */
795 else if (*endp == '$') {
796 if (strncmp(sym, p, strlen(p) - 1) == 0) {
797 if (number_prefix(sym + strlen(p) - 1))
798 return 1;
799 }
800 }
801 /* no wildcards */
802 else {
803 if (strcmp(p, sym) == 0)
804 return 1;
805 }
806 }
807 /* no match */
808 return 0;
809}
810
Sam Ravnborg10668222008-01-13 22:21:31 +0100811/* sections that we do not want to do full section mismatch check on */
812static const char *section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200813{
814 ".comment*",
815 ".debug*",
H.J. Lu11215842010-12-15 17:11:22 -0800816 ".zdebug*", /* Compressed debug sections. */
David Howells019fca82010-08-12 16:54:47 +0100817 ".GCC-command-line", /* mn10300 */
Jonathan Kliegman76b27642012-10-04 16:32:19 -0400818 ".GCC.command.line", /* record-gcc-switches, non mn10300 */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200819 ".mdebug*", /* alpha, score, mips etc. */
820 ".pdr", /* alpha, score, mips etc. */
821 ".stab*",
822 ".note*",
823 ".got*",
824 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400825 ".xt.prop", /* xtensa */
826 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030827 ".arcextmap*", /* arc */
828 ".gnu.linkonce.arcext*", /* arc : modules */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200829 NULL
830};
Sam Ravnborg10668222008-01-13 22:21:31 +0100831
Sam Ravnborge241a632008-01-28 20:13:13 +0100832/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400833 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100834 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400835 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100836 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400837static void check_section(const char *modname, struct elf_info *elf,
838 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100839{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400840 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100841
Anders Kaseorgb614a692009-04-23 16:49:33 -0400842 if (sechdr->sh_type == SHT_PROGBITS &&
843 !(sechdr->sh_flags & SHF_ALLOC) &&
844 !match(sec, section_white_list)) {
845 warn("%s (%s): unexpected non-allocatable section.\n"
846 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
847 "Note that for example <linux/init.h> contains\n"
848 "section definitions for use in .S files.\n\n",
849 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100850 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100851}
852
853
854
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100855#define ALL_INIT_DATA_SECTIONS \
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000856 ".init.setup$", ".init.rodata$", \
Greg Kroah-Hartman92e9e6d2012-11-29 10:45:02 -0800857 ".cpuinit.rodata$", ".meminit.rodata$", \
858 ".init.data$", ".cpuinit.data$", ".meminit.data$"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100859#define ALL_EXIT_DATA_SECTIONS \
Greg Kroah-Hartman92e9e6d2012-11-29 10:45:02 -0800860 ".exit.data$", ".cpuexit.data$", ".memexit.data$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100861
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100862#define ALL_INIT_TEXT_SECTIONS \
Greg Kroah-Hartman92e9e6d2012-11-29 10:45:02 -0800863 ".init.text$", ".cpuinit.text$", ".meminit.text$"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100864#define ALL_EXIT_TEXT_SECTIONS \
Greg Kroah-Hartman92e9e6d2012-11-29 10:45:02 -0800865 ".exit.text$", ".cpuexit.text$", ".memexit.text$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100866
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200867#define ALL_PCI_INIT_SECTIONS \
868 ".pci_fixup_early$", ".pci_fixup_header$", ".pci_fixup_final$", \
869 ".pci_fixup_enable$", ".pci_fixup_resume$", \
870 ".pci_fixup_resume_early$", ".pci_fixup_suspend$"
871
Greg Kroah-Hartman92e9e6d2012-11-29 10:45:02 -0800872#define ALL_XXXINIT_SECTIONS CPU_INIT_SECTIONS, MEM_INIT_SECTIONS
873#define ALL_XXXEXIT_SECTIONS CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100874
875#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
876#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100877
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100878#define DATA_SECTIONS ".data$", ".data.rel$"
Sam Ravnborg10668222008-01-13 22:21:31 +0100879#define TEXT_SECTIONS ".text$"
880
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000881#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000882#define CPU_INIT_SECTIONS ".cpuinit.*"
883#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100884
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000885#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000886#define CPU_EXIT_SECTIONS ".cpuexit.*"
887#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100888
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100889/* init data sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100890static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100891
892/* all init sections */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100893static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100894
895/* All init and exit sections (code + data) */
896static const char *init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100897 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100898
899/* data section */
900static const char *data_sections[] = { DATA_SECTIONS, NULL };
901
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100902
903/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100904#define DEFAULT_SYMBOL_WHITE_LIST \
905 "*driver", \
906 "*_template", /* scsi uses *_template a lot */ \
907 "*_timer", /* arm uses ops structures named _timer a lot */ \
908 "*_sht", /* scsi also used *_sht to some extent */ \
909 "*_ops", \
910 "*_probe", \
911 "*_probe_one", \
912 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100913
914static const char *head_sections[] = { ".head.text*", NULL };
915static const char *linker_symbols[] =
916 { "__init_begin", "_sinittext", "_einittext", NULL };
917
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100918enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100919 TEXT_TO_ANY_INIT,
920 DATA_TO_ANY_INIT,
921 TEXT_TO_ANY_EXIT,
922 DATA_TO_ANY_EXIT,
923 XXXINIT_TO_SOME_INIT,
924 XXXEXIT_TO_SOME_EXIT,
925 ANY_INIT_TO_ANY_EXIT,
926 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100927 EXPORT_TO_INIT_EXIT,
928};
929
Sam Ravnborg10668222008-01-13 22:21:31 +0100930struct sectioncheck {
931 const char *fromsec[20];
932 const char *tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100933 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100934 const char *symbol_white_list[20];
Sam Ravnborg10668222008-01-13 22:21:31 +0100935};
936
937const struct sectioncheck sectioncheck[] = {
938/* Do not reference init/exit code/data from
939 * normal code and data
940 */
941{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100942 .fromsec = { TEXT_SECTIONS, NULL },
943 .tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100944 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100945 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100946},
947{
948 .fromsec = { DATA_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +0100949 .tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100950 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100951 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100952},
953{
Uwe Kleine-König0db252452010-01-30 21:14:23 +0100954 .fromsec = { DATA_SECTIONS, NULL },
955 .tosec = { INIT_SECTIONS, NULL },
956 .mismatch = DATA_TO_ANY_INIT,
957 .symbol_white_list = {
958 "*_template", "*_timer", "*_sht", "*_ops",
959 "*_probe", "*_probe_one", "*_console", NULL
960 },
961},
962{
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100963 .fromsec = { TEXT_SECTIONS, NULL },
964 .tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100965 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100966 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100967},
968{
969 .fromsec = { DATA_SECTIONS, NULL },
970 .tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100971 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100972 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100973},
Greg Kroah-Hartman92e9e6d2012-11-29 10:45:02 -0800974/* Do not reference init code/data from cpuinit/meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100975{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100976 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100977 .tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100978 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100979 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100980},
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000981/* Do not reference cpuinit code/data from meminit code/data */
982{
983 .fromsec = { MEM_INIT_SECTIONS, NULL },
984 .tosec = { CPU_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100985 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100986 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000987},
988/* Do not reference meminit code/data from cpuinit code/data */
989{
990 .fromsec = { CPU_INIT_SECTIONS, NULL },
991 .tosec = { MEM_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100992 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100993 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000994},
Greg Kroah-Hartman92e9e6d2012-11-29 10:45:02 -0800995/* Do not reference exit code/data from cpuexit/memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100996{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100997 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100998 .tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100999 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001000 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001001},
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001002/* Do not reference cpuexit code/data from memexit code/data */
1003{
1004 .fromsec = { MEM_EXIT_SECTIONS, NULL },
1005 .tosec = { CPU_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001006 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001007 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001008},
1009/* Do not reference memexit code/data from cpuexit code/data */
1010{
1011 .fromsec = { CPU_EXIT_SECTIONS, NULL },
1012 .tosec = { MEM_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001013 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001014 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001015},
Sam Ravnborg10668222008-01-13 22:21:31 +01001016/* Do not use exit code/data from init code */
1017{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001018 .fromsec = { ALL_INIT_SECTIONS, NULL },
1019 .tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001020 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001021 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001022},
1023/* Do not use init code/data from exit code */
1024{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001025 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001026 .tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001027 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001028 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001029},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001030{
1031 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
1032 .tosec = { INIT_SECTIONS, NULL },
1033 .mismatch = ANY_INIT_TO_ANY_EXIT,
1034 .symbol_white_list = { NULL },
1035},
Sam Ravnborg10668222008-01-13 22:21:31 +01001036/* Do not export init/exit functions or data */
1037{
1038 .fromsec = { "__ksymtab*", NULL },
Sam Ravnborgfa95eb12008-02-02 23:30:22 +01001039 .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001040 .mismatch = EXPORT_TO_INIT_EXIT,
1041 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001042}
1043};
1044
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001045static const struct sectioncheck *section_mismatch(
1046 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001047{
1048 int i;
1049 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1050 const struct sectioncheck *check = &sectioncheck[0];
1051
1052 for (i = 0; i < elems; i++) {
1053 if (match(fromsec, check->fromsec) &&
1054 match(tosec, check->tosec))
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001055 return check;
Sam Ravnborg10668222008-01-13 22:21:31 +01001056 check++;
1057 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001058 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001059}
1060
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001061/**
1062 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001063 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001064 * Pattern 1:
1065 * If a module parameter is declared __initdata and permissions=0
1066 * then this is legal despite the warning generated.
1067 * We cannot see value of permissions here, so just ignore
1068 * this pattern.
1069 * The pattern is identified by:
1070 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001071 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001072 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001073 *
Rusty Russell6a841522010-08-11 23:04:16 -06001074 * Pattern 1a:
1075 * module_param_call() ops can refer to __init set function if permissions=0
1076 * The pattern is identified by:
1077 * tosec = .init.text
1078 * fromsec = .data*
1079 * atsym = __param_ops_*
1080 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001081 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001082 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001083 * add, remove, probe functions etc.
Greg Kroah-Hartman92e9e6d2012-11-29 10:45:02 -08001084 * These functions may often be marked __cpuinit and we do not want to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001085 * warn here.
1086 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001087 * tosec = init or exit section
1088 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001089 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1090 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001091 *
1092 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001093 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001094 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001095 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001096 * Some symbols belong to init section but still it is ok to reference
1097 * these from non-init sections as these symbols don't have any memory
1098 * allocated for them and symbol address and value are same. So even
1099 * if init section is freed, its ok to reference those symbols.
1100 * For ex. symbols marking the init section boundaries.
1101 * This pattern is identified by
1102 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001103 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001104 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001105static int secref_whitelist(const struct sectioncheck *mismatch,
1106 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001107 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001108{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001109 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001110 if (match(tosec, init_data_sections) &&
1111 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001112 (strncmp(fromsym, "__param", strlen("__param")) == 0))
1113 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001114
Rusty Russell6a841522010-08-11 23:04:16 -06001115 /* Check for pattern 1a */
1116 if (strcmp(tosec, ".init.text") == 0 &&
1117 match(fromsec, data_sections) &&
1118 (strncmp(fromsym, "__param_ops_", strlen("__param_ops_")) == 0))
1119 return 0;
1120
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001121 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001122 if (match(tosec, init_exit_sections) &&
1123 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001124 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001125 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001126
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001127 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001128 if (match(fromsec, head_sections) &&
1129 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001130 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001131
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001132 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001133 if (match(tosym, linker_symbols))
1134 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001135
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001136 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001137}
1138
1139/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001140 * Find symbol based on relocation record info.
1141 * In some cases the symbol supplied is a valid symbol so
1142 * return refsym. If st_name != 0 we assume this is a valid symbol.
1143 * In other cases the symbol needs to be looked up in the symbol table
1144 * based on section and address.
1145 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001146static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001147 Elf_Sym *relsym)
1148{
1149 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001150 Elf_Sym *near = NULL;
1151 Elf64_Sword distance = 20;
1152 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001153 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001154
1155 if (relsym->st_name != 0)
1156 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001157
1158 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001159 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001160 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001161 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001162 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1163 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001164 if (sym->st_value == addr)
1165 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001166 /* Find a symbol nearby - addr are maybe negative */
1167 d = sym->st_value - addr;
1168 if (d < 0)
1169 d = addr - sym->st_value;
1170 if (d < distance) {
1171 distance = d;
1172 near = sym;
1173 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001174 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001175 /* We need a close match */
1176 if (distance < 20)
1177 return near;
1178 else
1179 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001180}
1181
David Brownellda68d612007-02-20 13:58:16 -08001182static inline int is_arm_mapping_symbol(const char *str)
1183{
1184 return str[0] == '$' && strchr("atd", str[1])
1185 && (str[2] == '\0' || str[2] == '.');
1186}
1187
1188/*
1189 * If there's no name there, ignore it; likewise, ignore it if it's
1190 * one of the magic symbols emitted used by current ARM tools.
1191 *
1192 * Otherwise if find_symbols_between() returns those symbols, they'll
1193 * fail the whitelist tests and cause lots of false alarms ... fixable
1194 * only by merging __exit and __init sections into __text, bloating
1195 * the kernel (which is especially evil on embedded platforms).
1196 */
1197static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1198{
1199 const char *name = elf->strtab + sym->st_name;
1200
1201 if (!name || !strlen(name))
1202 return 0;
1203 return !is_arm_mapping_symbol(name);
1204}
1205
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001206/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001207 * Find symbols before or equal addr and after addr - in the section sec.
1208 * If we find two symbols with equal offset prefer one with a valid name.
1209 * The ELF format may have a better way to detect what type of symbol
1210 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001211 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001212static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1213 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001214{
1215 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001216 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001217 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001218
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001219 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1220 const char *symsec;
1221
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001222 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001223 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001224 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001225 if (strcmp(symsec, sec) != 0)
1226 continue;
David Brownellda68d612007-02-20 13:58:16 -08001227 if (!is_valid_name(elf, sym))
1228 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001229 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001230 if ((addr - sym->st_value) < distance) {
1231 distance = addr - sym->st_value;
1232 near = sym;
1233 } else if ((addr - sym->st_value) == distance) {
1234 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001235 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001236 }
1237 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001238 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001239}
1240
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001241/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001242 * Convert a section name to the function/data attribute
1243 * .init.text => __init
1244 * .cpuinit.data => __cpudata
1245 * .memexitconst => __memconst
1246 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001247 *
1248 * The memory of returned value has been allocated on a heap. The user of this
1249 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001250*/
1251static char *sec2annotation(const char *s)
1252{
1253 if (match(s, init_exit_sections)) {
1254 char *p = malloc(20);
1255 char *r = p;
1256
1257 *p++ = '_';
1258 *p++ = '_';
1259 if (*s == '.')
1260 s++;
1261 while (*s && *s != '.')
1262 *p++ = *s++;
1263 *p = '\0';
1264 if (*s == '.')
1265 s++;
1266 if (strstr(s, "rodata") != NULL)
1267 strcat(p, "const ");
1268 else if (strstr(s, "data") != NULL)
1269 strcat(p, "data ");
1270 else
1271 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001272 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001273 } else {
Andrew Morton5003bab2010-08-11 00:42:26 -07001274 return strdup("");
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001275 }
1276}
1277
1278static int is_function(Elf_Sym *sym)
1279{
1280 if (sym)
1281 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1282 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001283 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001284}
1285
Randy Dunlap00759c02011-03-15 14:13:47 -07001286static void print_section_list(const char * const list[20])
1287{
1288 const char *const *s = list;
1289
1290 while (*s) {
1291 fprintf(stderr, "%s", *s);
1292 s++;
1293 if (*s)
1294 fprintf(stderr, ", ");
1295 }
1296 fprintf(stderr, "\n");
1297}
1298
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001299/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001300 * Print a warning about a section mismatch.
1301 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001302 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001303 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001304static void report_sec_mismatch(const char *modname,
1305 const struct sectioncheck *mismatch,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001306 const char *fromsec,
1307 unsigned long long fromaddr,
1308 const char *fromsym,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001309 int from_is_func,
1310 const char *tosec, const char *tosym,
1311 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001312{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001313 const char *from, *from_p;
1314 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001315 char *prl_from;
1316 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001317
1318 switch (from_is_func) {
1319 case 0: from = "variable"; from_p = ""; break;
1320 case 1: from = "function"; from_p = "()"; break;
1321 default: from = "(unknown reference)"; from_p = ""; break;
1322 }
1323 switch (to_is_func) {
1324 case 0: to = "variable"; to_p = ""; break;
1325 case 1: to = "function"; to_p = "()"; break;
1326 default: to = "(unknown reference)"; to_p = ""; break;
1327 }
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001328
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001329 sec_mismatch_count++;
1330 if (!sec_mismatch_verbose)
1331 return;
1332
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001333 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1334 "to the %s %s:%s%s\n",
1335 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1336 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001337
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001338 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001339 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001340 prl_from = sec2annotation(fromsec);
1341 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001342 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001343 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001344 "the %s %s%s%s.\n"
1345 "This is often because %s lacks a %s\n"
1346 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001347 prl_from, fromsym,
1348 to, prl_to, tosym, to_p,
1349 fromsym, prl_to, tosym);
1350 free(prl_from);
1351 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001352 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001353 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001354 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001355 fprintf(stderr,
1356 "The variable %s references\n"
1357 "the %s %s%s%s\n"
1358 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001359 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001360 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001361 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001362 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001363 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001364 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001365 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001366 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001367 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001368 fprintf(stderr,
1369 "The function %s() references a %s in an exit section.\n"
1370 "Often the %s %s%s has valid usage outside the exit section\n"
1371 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001372 fromsym, to, to, tosym, to_p, prl_to, tosym);
1373 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001374 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001375 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001376 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001377 fprintf(stderr,
1378 "The variable %s references\n"
1379 "the %s %s%s%s\n"
1380 "If the reference is valid then annotate the\n"
1381 "variable with __exit* (see linux/init.h) or "
1382 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001383 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001384 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001385 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001386 break;
1387 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001388 case XXXINIT_TO_SOME_INIT:
1389 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001390 prl_from = sec2annotation(fromsec);
1391 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001392 fprintf(stderr,
1393 "The %s %s%s%s references\n"
1394 "a %s %s%s%s.\n"
1395 "If %s is only used by %s then\n"
1396 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001397 from, prl_from, fromsym, from_p,
1398 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001399 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001400 free(prl_from);
1401 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001402 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001403 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001404 prl_from = sec2annotation(fromsec);
1405 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001406 fprintf(stderr,
1407 "The %s %s%s%s references\n"
1408 "a %s %s%s%s.\n"
1409 "This is often seen when error handling "
1410 "in the init function\n"
1411 "uses functionality in the exit path.\n"
1412 "The fix is often to remove the %sannotation of\n"
1413 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001414 from, prl_from, fromsym, from_p,
1415 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001416 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001417 free(prl_from);
1418 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001419 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001420 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001421 prl_from = sec2annotation(fromsec);
1422 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001423 fprintf(stderr,
1424 "The %s %s%s%s references\n"
1425 "a %s %s%s%s.\n"
1426 "This is often seen when error handling "
1427 "in the exit function\n"
1428 "uses functionality in the init path.\n"
1429 "The fix is often to remove the %sannotation of\n"
1430 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001431 from, prl_from, fromsym, from_p,
1432 to, prl_to, tosym, to_p,
1433 prl_to, tosym, to_p);
1434 free(prl_from);
1435 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001436 break;
1437 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001438 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001439 fprintf(stderr,
1440 "The symbol %s is exported and annotated %s\n"
1441 "Fix this by removing the %sannotation of %s "
1442 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001443 tosym, prl_to, prl_to, tosym);
1444 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001445 break;
1446 }
1447 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001448}
1449
1450static void check_section_mismatch(const char *modname, struct elf_info *elf,
1451 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1452{
1453 const char *tosec;
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001454 const struct sectioncheck *mismatch;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001455
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001456 tosec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001457 mismatch = section_mismatch(fromsec, tosec);
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001458 if (mismatch) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001459 Elf_Sym *to;
1460 Elf_Sym *from;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001461 const char *tosym;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001462 const char *fromsym;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001463
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001464 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1465 fromsym = sym_name(elf, from);
1466 to = find_elf_symbol(elf, r->r_addend, sym);
1467 tosym = sym_name(elf, to);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001468
1469 /* check whitelist - we may ignore it */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001470 if (secref_whitelist(mismatch,
1471 fromsec, fromsym, tosec, tosym)) {
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001472 report_sec_mismatch(modname, mismatch,
1473 fromsec, r->r_offset, fromsym,
1474 is_function(from), tosec, tosym,
1475 is_function(to));
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001476 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001477 }
1478}
1479
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001480static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001481 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001482{
1483 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001484 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001485
1486 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001487 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001488}
1489
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001490static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001491{
1492 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001493 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001494
1495 switch (r_typ) {
1496 case R_386_32:
1497 r->r_addend = TO_NATIVE(*location);
1498 break;
1499 case R_386_PC32:
1500 r->r_addend = TO_NATIVE(*location) + 4;
1501 /* For CONFIG_RELOCATABLE=y */
1502 if (elf->hdr->e_type == ET_EXEC)
1503 r->r_addend += r->r_offset;
1504 break;
1505 }
1506 return 0;
1507}
1508
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001509#ifndef R_ARM_CALL
1510#define R_ARM_CALL 28
1511#endif
1512#ifndef R_ARM_JUMP24
1513#define R_ARM_JUMP24 29
1514#endif
1515
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001516static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001517{
1518 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1519
1520 switch (r_typ) {
1521 case R_ARM_ABS32:
1522 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001523 r->r_addend = (int)(long)
1524 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001525 break;
1526 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001527 case R_ARM_CALL:
1528 case R_ARM_JUMP24:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001529 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001530 r->r_addend = (int)(long)(elf->hdr +
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001531 sechdr->sh_offset +
1532 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001533 break;
1534 default:
1535 return 1;
1536 }
1537 return 0;
1538}
1539
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001540static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001541{
1542 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001543 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001544 unsigned int inst;
1545
1546 if (r_typ == R_MIPS_HI16)
1547 return 1; /* skip this */
1548 inst = TO_NATIVE(*location);
1549 switch (r_typ) {
1550 case R_MIPS_LO16:
1551 r->r_addend = inst & 0xffff;
1552 break;
1553 case R_MIPS_26:
1554 r->r_addend = (inst & 0x03ffffff) << 2;
1555 break;
1556 case R_MIPS_32:
1557 r->r_addend = inst;
1558 break;
1559 }
1560 return 0;
1561}
1562
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001563static void section_rela(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001564 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001565{
1566 Elf_Sym *sym;
1567 Elf_Rela *rela;
1568 Elf_Rela r;
1569 unsigned int r_sym;
1570 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001571
Sam Ravnborgff13f922008-01-23 19:54:27 +01001572 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001573 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1574
Sam Ravnborgff13f922008-01-23 19:54:27 +01001575 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001576 fromsec += strlen(".rela");
1577 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001578 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001579 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001580
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001581 for (rela = start; rela < stop; rela++) {
1582 r.r_offset = TO_NATIVE(rela->r_offset);
1583#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001584 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001585 unsigned int r_typ;
1586 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1587 r_sym = TO_NATIVE(r_sym);
1588 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1589 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1590 } else {
1591 r.r_info = TO_NATIVE(rela->r_info);
1592 r_sym = ELF_R_SYM(r.r_info);
1593 }
1594#else
1595 r.r_info = TO_NATIVE(rela->r_info);
1596 r_sym = ELF_R_SYM(r.r_info);
1597#endif
1598 r.r_addend = TO_NATIVE(rela->r_addend);
1599 sym = elf->symtab_start + r_sym;
1600 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001601 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001602 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001603 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001604 }
1605}
1606
1607static void section_rel(const char *modname, struct elf_info *elf,
Sam Ravnborg10668222008-01-13 22:21:31 +01001608 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001609{
1610 Elf_Sym *sym;
1611 Elf_Rel *rel;
1612 Elf_Rela r;
1613 unsigned int r_sym;
1614 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001615
Sam Ravnborgff13f922008-01-23 19:54:27 +01001616 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001617 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1618
Sam Ravnborgff13f922008-01-23 19:54:27 +01001619 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001620 fromsec += strlen(".rel");
1621 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001622 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001623 return;
1624
1625 for (rel = start; rel < stop; rel++) {
1626 r.r_offset = TO_NATIVE(rel->r_offset);
1627#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001628 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001629 unsigned int r_typ;
1630 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1631 r_sym = TO_NATIVE(r_sym);
1632 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1633 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1634 } else {
1635 r.r_info = TO_NATIVE(rel->r_info);
1636 r_sym = ELF_R_SYM(r.r_info);
1637 }
1638#else
1639 r.r_info = TO_NATIVE(rel->r_info);
1640 r_sym = ELF_R_SYM(r.r_info);
1641#endif
1642 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001643 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001644 case EM_386:
1645 if (addend_386_rel(elf, sechdr, &r))
1646 continue;
1647 break;
1648 case EM_ARM:
1649 if (addend_arm_rel(elf, sechdr, &r))
1650 continue;
1651 break;
1652 case EM_MIPS:
1653 if (addend_mips_rel(elf, sechdr, &r))
1654 continue;
1655 break;
1656 }
1657 sym = elf->symtab_start + r_sym;
1658 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001659 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001660 continue;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001661 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001662 }
1663}
1664
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001665/**
1666 * A module includes a number of sections that are discarded
1667 * either when loaded or when used as built-in.
1668 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001669 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001670 * Likewise for modules used built-in the sections marked __exit
1671 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001672 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001673 * The check_sec_ref() function traverses all relocation records
1674 * to find all references to a section that reference a section that will
1675 * be discarded and warns about it.
1676 **/
1677static void check_sec_ref(struct module *mod, const char *modname,
Sam Ravnborg10668222008-01-13 22:21:31 +01001678 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001679{
1680 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001681 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001682
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001683 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001684 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001685 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001686 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001687 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001688 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001689 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001690 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001691 }
1692}
1693
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001694static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
1696 const char *symname;
1697 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001698 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 struct module *mod;
1700 struct elf_info info = { };
1701 Elf_Sym *sym;
1702
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001703 if (!parse_elf(&info, modname))
1704 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 mod = new_module(modname);
1707
1708 /* When there's no vmlinux, don't print warnings about
1709 * unresolved symbols (since there'll be too many ;) */
1710 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 mod->skip = 1;
1713 }
1714
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001715 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
Sam Ravnborg2fa36562008-04-26 21:07:26 +02001716 if (info.modinfo && !license && !is_vmlinux(modname))
1717 warn("modpost: missing MODULE_LICENSE() in %s\n"
1718 "see include/linux/module.h for "
1719 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001720 while (license) {
1721 if (license_is_gpl_compatible(license))
1722 mod->gpl_compatible = 1;
1723 else {
1724 mod->gpl_compatible = 0;
1725 break;
1726 }
1727 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1728 "license", license);
1729 }
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1732 symname = info.strtab + sym->st_name;
1733
1734 handle_modversions(mod, &info, sym, symname);
1735 handle_moddevtable(mod, &info, sym, symname);
1736 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001737 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001738 (is_vmlinux(modname) && vmlinux_section_warnings))
1739 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1742 if (version)
1743 maybe_frob_rcs_version(modname, version, info.modinfo,
1744 version - (char *)info.hdr);
1745 if (version || (all_versions && !is_vmlinux(modname)))
1746 get_src_version(modname, mod->srcversion,
1747 sizeof(mod->srcversion)-1);
1748
1749 parse_elf_finish(&info);
1750
Rusty Russell8c8ef422009-03-31 13:05:34 -06001751 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 * never passed as an argument to an exported function, so
1753 * the automatic versioning doesn't pick it up, but it's really
1754 * important anyhow */
1755 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06001756 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
1759#define SZ 500
1760
1761/* We first write the generated file into memory using the
1762 * following helper, then compare to the file on disk and
1763 * only update the later if anything changed */
1764
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001765void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
1766 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 char tmp[SZ];
1769 int len;
1770 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 va_start(ap, fmt);
1773 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08001774 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 va_end(ap);
1776}
1777
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001778void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08001781 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 buf->p = realloc(buf->p, buf->size);
1783 }
1784 strncpy(buf->p + buf->pos, s, len);
1785 buf->pos += len;
1786}
1787
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001788static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
1789{
1790 const char *e = is_vmlinux(m) ?"":".ko";
1791
1792 switch (exp) {
1793 case export_gpl:
1794 fatal("modpost: GPL-incompatible module %s%s "
1795 "uses GPL-only symbol '%s'\n", m, e, s);
1796 break;
1797 case export_unused_gpl:
1798 fatal("modpost: GPL-incompatible module %s%s "
1799 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
1800 break;
1801 case export_gpl_future:
1802 warn("modpost: GPL-incompatible module %s%s "
1803 "uses future GPL-only symbol '%s'\n", m, e, s);
1804 break;
1805 case export_plain:
1806 case export_unused:
1807 case export_unknown:
1808 /* ignore */
1809 break;
1810 }
1811}
1812
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001813static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001814{
1815 const char *e = is_vmlinux(m) ?"":".ko";
1816
1817 switch (exp) {
1818 case export_unused:
1819 case export_unused_gpl:
1820 warn("modpost: module %s%s "
1821 "uses symbol '%s' marked UNUSED\n", m, e, s);
1822 break;
1823 default:
1824 /* ignore */
1825 break;
1826 }
1827}
1828
1829static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001830{
1831 struct symbol *s, *exp;
1832
1833 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07001834 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001835 exp = find_symbol(s->name);
1836 if (!exp || exp->module == mod)
1837 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07001838 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001839 if (basename)
1840 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02001841 else
1842 basename = mod->name;
1843 if (!mod->gpl_compatible)
1844 check_for_gpl_usage(exp->export, basename, exp->name);
1845 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001846 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001847}
1848
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001849/**
1850 * Header for the generated file
1851 **/
1852static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 buf_printf(b, "#include <linux/module.h>\n");
1855 buf_printf(b, "#include <linux/vermagic.h>\n");
1856 buf_printf(b, "#include <linux/compiler.h>\n");
1857 buf_printf(b, "\n");
1858 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
1859 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 buf_printf(b, "struct module __this_module\n");
1861 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07001862 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07001864 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (mod->has_cleanup)
1866 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07001867 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07001869 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 buf_printf(b, "};\n");
1871}
1872
Ben Hutchings2449b8b2011-10-24 15:12:28 +02001873static void add_intree_flag(struct buffer *b, int is_intree)
1874{
1875 if (is_intree)
1876 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
1877}
1878
Trevor Keith5c725132009-09-22 16:43:38 -07001879static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07001880{
1881 static const char *staging_dir = "drivers/staging";
1882
1883 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
1884 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1885}
1886
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001887/**
1888 * Record CRCs for unresolved symbols
1889 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001890static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001893 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 for (s = mod->unres; s; s = s->next) {
1896 exp = find_symbol(s->name);
1897 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001898 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06001899 if (warn_unresolved) {
1900 warn("\"%s\" [%s.ko] undefined!\n",
1901 s->name, mod->name);
1902 } else {
1903 merror("\"%s\" [%s.ko] undefined!\n",
1904 s->name, mod->name);
1905 err = 1;
1906 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 continue;
1909 }
1910 s->module = exp->module;
1911 s->crc_valid = exp->crc_valid;
1912 s->crc = exp->crc;
1913 }
1914
1915 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001916 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 buf_printf(b, "\n");
1919 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001920 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
1922
1923 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001924 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01001927 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 s->name, mod->name);
1929 continue;
1930 }
James Hogana4b6a772013-03-18 19:38:56 +10301931 buf_printf(b, "\t{ %#8x, VMLINUX_SYMBOL_STR(%s) },\n",
1932 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934
1935 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07001936
1937 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001940static void add_depends(struct buffer *b, struct module *mod,
1941 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 struct symbol *s;
1944 struct module *m;
1945 int first = 1;
1946
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001947 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 buf_printf(b, "\n");
1951 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01001952 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
1954 buf_printf(b, "\"depends=");
1955 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001956 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (!s->module)
1958 continue;
1959
1960 if (s->module->seen)
1961 continue;
1962
1963 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001964 p = strrchr(s->module->name, '/');
1965 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01001966 p++;
1967 else
1968 p = s->module->name;
1969 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 first = 0;
1971 }
1972 buf_printf(b, "\";\n");
1973}
1974
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001975static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 if (mod->srcversion[0]) {
1978 buf_printf(b, "\n");
1979 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
1980 mod->srcversion);
1981 }
1982}
1983
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001984static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 char *tmp;
1987 FILE *file;
1988 struct stat st;
1989
1990 file = fopen(fname, "r");
1991 if (!file)
1992 goto write;
1993
1994 if (fstat(fileno(file), &st) < 0)
1995 goto close_write;
1996
1997 if (st.st_size != b->pos)
1998 goto close_write;
1999
2000 tmp = NOFAIL(malloc(b->pos));
2001 if (fread(tmp, 1, b->pos, file) != b->pos)
2002 goto free_write;
2003
2004 if (memcmp(tmp, b->p, b->pos) != 0)
2005 goto free_write;
2006
2007 free(tmp);
2008 fclose(file);
2009 return;
2010
2011 free_write:
2012 free(tmp);
2013 close_write:
2014 fclose(file);
2015 write:
2016 file = fopen(fname, "w");
2017 if (!file) {
2018 perror(fname);
2019 exit(1);
2020 }
2021 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2022 perror(fname);
2023 exit(1);
2024 }
2025 fclose(file);
2026}
2027
Ram Paibd5cbce2006-06-08 22:12:53 -07002028/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002029 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002030 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002031static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 unsigned long size, pos = 0;
2034 void *file = grab_file(fname, &size);
2035 char *line;
2036
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002037 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /* No symbol versions, silently ignore */
2039 return;
2040
2041 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002042 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 unsigned int crc;
2044 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002045 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 if (!(symname = strchr(line, '\t')))
2048 goto fail;
2049 *symname++ = '\0';
2050 if (!(modname = strchr(symname, '\t')))
2051 goto fail;
2052 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002053 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002054 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002055 if (export && ((end = strchr(export, '\t')) != NULL))
2056 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 crc = strtoul(line, &d, 16);
2058 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2059 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002060 mod = find_module(modname);
2061 if (!mod) {
2062 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002064 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 mod->skip = 1;
2066 }
Ram Paibd5cbce2006-06-08 22:12:53 -07002067 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002068 s->kernel = kernel;
2069 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07002070 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 return;
2073fail:
2074 fatal("parse error in symbol dump file\n");
2075}
2076
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002077/* For normal builds always dump all symbols.
2078 * For external modules only dump symbols
2079 * that are not read from kernel Module.symvers.
2080 **/
2081static int dump_sym(struct symbol *sym)
2082{
2083 if (!external_module)
2084 return 1;
2085 if (sym->vmlinux || sym->kernel)
2086 return 0;
2087 return 1;
2088}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002089
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002090static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 struct buffer buf = { };
2093 struct symbol *symbol;
2094 int n;
2095
2096 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2097 symbol = symbolhash[n];
2098 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002099 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07002100 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002101 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07002102 symbol->module->name,
2103 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 symbol = symbol->next;
2105 }
2106 }
2107 write_if_changed(&buf, fname);
2108}
2109
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002110struct ext_sym_list {
2111 struct ext_sym_list *next;
2112 const char *file;
2113};
2114
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002115int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
2117 struct module *mod;
2118 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002119 char *kernel_read = NULL, *module_read = NULL;
2120 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002122 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002123 struct ext_sym_list *extsym_iter;
2124 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Andreas Schwab6543bec2013-01-20 17:58:47 +01002126 while ((opt = getopt(argc, argv, "i:I:e:msSo:awM:K:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002127 switch (opt) {
2128 case 'i':
2129 kernel_read = optarg;
2130 break;
2131 case 'I':
2132 module_read = optarg;
2133 external_module = 1;
2134 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002135 case 'e':
2136 external_module = 1;
2137 extsym_iter =
2138 NOFAIL(malloc(sizeof(*extsym_iter)));
2139 extsym_iter->next = extsym_start;
2140 extsym_iter->file = optarg;
2141 extsym_start = extsym_iter;
2142 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002143 case 'm':
2144 modversions = 1;
2145 break;
2146 case 'o':
2147 dump_write = optarg;
2148 break;
2149 case 'a':
2150 all_versions = 1;
2151 break;
2152 case 's':
2153 vmlinux_section_warnings = 0;
2154 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002155 case 'S':
2156 sec_mismatch_verbose = 0;
2157 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002158 case 'w':
2159 warn_unresolved = 1;
2160 break;
2161 default:
2162 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164 }
2165
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002166 if (kernel_read)
2167 read_dump(kernel_read, 1);
2168 if (module_read)
2169 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002170 while (extsym_start) {
2171 read_dump(extsym_start->file, 0);
2172 extsym_iter = extsym_start->next;
2173 free(extsym_start);
2174 extsym_start = extsym_iter;
2175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002177 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 for (mod = modules; mod; mod = mod->next) {
2181 if (mod->skip)
2182 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002183 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002184 }
2185
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002186 err = 0;
2187
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002188 for (mod = modules; mod; mod = mod->next) {
Andi Kleen666ab412007-11-22 03:43:10 +01002189 char fname[strlen(mod->name) + 10];
2190
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002191 if (mod->skip)
2192 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 buf.pos = 0;
2195
2196 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002197 add_intree_flag(&buf, !external_module);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002198 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002199 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 add_depends(&buf, mod, modules);
2201 add_moddevtable(&buf, mod);
2202 add_srcversion(&buf, mod);
2203
2204 sprintf(fname, "%s.mod.c", mod->name);
2205 write_if_changed(&buf, fname);
2206 }
2207
2208 if (dump_write)
2209 write_dump(dump_write);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002210 if (sec_mismatch_count && !sec_mismatch_verbose)
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01002211 warn("modpost: Found %d section mismatch(es).\n"
2212 "To see full details build your kernel with:\n"
2213 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2214 sec_mismatch_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002216 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}