blob: 301c27740c5c4e85237070218ecbdb7403ec90b4 [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>
Rusty Russell712f9b42013-04-04 17:37:38 +103018#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103019#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093020#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "modpost.h"
Linus Torvalds5a865c02009-12-17 07:23:42 -080022#include "../../include/generated/autoconf.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020023#include "../../include/linux/license.h"
Rusty Russellb92021b2013-03-15 15:04:17 +103024#include "../../include/linux/export.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093027static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093029static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
31static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010032/* If we are modposting external module set to 1 */
33static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020034/* Warn about section mismatch in vmlinux if set to 1 */
35static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070036/* Only warn about unresolved symbols */
37static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070038/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010039static int sec_mismatch_count = 0;
40static int sec_mismatch_verbose = 1;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103041static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093042/* ignore missing files */
43static int ignore_missing_files;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010044
Sam Ravnborgc96fca22006-07-01 11:44:23 +020045enum export {
46 export_plain, export_unused, export_gpl,
47 export_unused_gpl, export_gpl_future, export_unknown
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080050/* In kernel, this size is defined in linux/module.h;
51 * here we use Elf_Addr instead of long for covering cross-compile
52 */
53
54#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
55
Andi Kleen6d9a89e2007-11-22 03:43:08 +010056#define PRINTF __attribute__ ((format (printf, 1, 2)))
57
58PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 va_list arglist;
61
62 fprintf(stderr, "FATAL: ");
63
64 va_start(arglist, fmt);
65 vfprintf(stderr, fmt, arglist);
66 va_end(arglist);
67
68 exit(1);
69}
70
Andi Kleen6d9a89e2007-11-22 03:43:08 +010071PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 va_list arglist;
74
75 fprintf(stderr, "WARNING: ");
76
77 va_start(arglist, fmt);
78 vfprintf(stderr, fmt, arglist);
79 va_end(arglist);
80}
81
Andi Kleen6d9a89e2007-11-22 03:43:08 +010082PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060083{
84 va_list arglist;
85
86 fprintf(stderr, "ERROR: ");
87
88 va_start(arglist, fmt);
89 vfprintf(stderr, fmt, arglist);
90 va_end(arglist);
91}
92
Rusty Russelld4ef1c32013-04-04 17:37:32 +103093static inline bool strends(const char *str, const char *postfix)
94{
95 if (strlen(str) < strlen(postfix))
96 return false;
97
98 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
99}
100
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100101static int is_vmlinux(const char *modname)
102{
103 const char *myname;
104
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100105 myname = strrchr(modname, '/');
106 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100107 myname++;
108 else
109 myname = modname;
110
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200111 return (strcmp(myname, "vmlinux") == 0) ||
112 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115void *do_nofail(void *ptr, const char *expr)
116{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100117 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return ptr;
121}
122
123/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static struct module *modules;
125
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100126static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct module *mod;
129
130 for (mod = modules; mod; mod = mod->next)
131 if (strcmp(mod->name, modname) == 0)
132 break;
133 return mod;
134}
135
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030136static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030139 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 mod = NOFAIL(malloc(sizeof(*mod)));
142 memset(mod, 0, sizeof(*mod));
143 p = NOFAIL(strdup(modname));
144
145 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030146 if (strends(p, ".o")) {
147 p[strlen(p) - 2] = '\0';
148 mod->is_dot_o = 1;
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* add to list */
152 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200153 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 mod->next = modules;
155 modules = mod;
156
157 return mod;
158}
159
160/* A hash of all exported symbols,
161 * struct symbol is also used for lists of unresolved symbols */
162
163#define SYMBOL_HASH_SIZE 1024
164
165struct symbol {
166 struct symbol *next;
167 struct module *module;
168 unsigned int crc;
169 int crc_valid;
170 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100171 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
172 unsigned int kernel:1; /* 1 if symbol is from kernel
173 * (only for external modules) **/
Rusty Russellb6568b12013-11-07 12:09:13 +1030174 unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */
Ram Paibd5cbce2006-06-08 22:12:53 -0700175 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 char name[0];
177};
178
179static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
180
181/* This is based on the hash agorithm from gdbm, via tdb */
182static inline unsigned int tdb_hash(const char *name)
183{
184 unsigned value; /* Used to compute the hash value. */
185 unsigned i; /* Used to cycle through random values. */
186
187 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100188 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
190
191 return (1103515243 * value + 12345);
192}
193
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100194/**
195 * Allocate a new symbols for use in the hash of exported symbols or
196 * the list of unresolved symbols per module
197 **/
198static struct symbol *alloc_symbol(const char *name, unsigned int weak,
199 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
202
203 memset(s, 0, sizeof(*s));
204 strcpy(s->name, name);
205 s->weak = weak;
206 s->next = next;
207 return s;
208}
209
210/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700211static struct symbol *new_symbol(const char *name, struct module *module,
212 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned int hash;
215 struct symbol *new;
216
217 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
218 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
219 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700220 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100221 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100224static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct symbol *s;
227
228 /* For our purposes, .foo matches foo. PPC64 needs this. */
229 if (name[0] == '.')
230 name++;
231
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100232 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (strcmp(s->name, name) == 0)
234 return s;
235 }
236 return NULL;
237}
238
Mathias Krause7a3ee752014-08-27 20:28:53 +0930239static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700240 const char *str;
241 enum export export;
242} export_list[] = {
243 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200244 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700245 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200246 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700247 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
248 { .str = "(unknown)", .export = export_unknown },
249};
250
251
252static const char *export_str(enum export ex)
253{
254 return export_list[ex].str;
255}
256
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100257static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700258{
259 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100260
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200261 if (!s)
262 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700263 for (i = 0; export_list[i].export != export_unknown; i++) {
264 if (strcmp(export_list[i].str, s) == 0)
265 return export_list[i].export;
266 }
267 return export_unknown;
268}
269
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200270static const char *sec_name(struct elf_info *elf, int secindex);
271
272#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
273
274static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
275{
276 const char *secname = sec_name(elf, sec);
277
278 if (strstarts(secname, "___ksymtab+"))
279 return export_plain;
280 else if (strstarts(secname, "___ksymtab_unused+"))
281 return export_unused;
282 else if (strstarts(secname, "___ksymtab_gpl+"))
283 return export_gpl;
284 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
285 return export_unused_gpl;
286 else if (strstarts(secname, "___ksymtab_gpl_future+"))
287 return export_gpl_future;
288 else
289 return export_unknown;
290}
291
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200292static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700293{
294 if (sec == elf->export_sec)
295 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200296 else if (sec == elf->export_unused_sec)
297 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700298 else if (sec == elf->export_gpl_sec)
299 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200300 else if (sec == elf->export_unused_gpl_sec)
301 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700302 else if (sec == elf->export_gpl_future_sec)
303 return export_gpl_future;
304 else
305 return export_unknown;
306}
307
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100308/**
309 * Add an exported symbol - it may have already been added without a
310 * CRC, in this case just update the CRC
311 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700312static struct symbol *sym_add_exported(const char *name, struct module *mod,
313 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct symbol *s = find_symbol(name);
316
317 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700318 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100319 } else {
320 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100321 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100322 "was in %s%s\n", mod->name, name,
323 s->module->name,
324 is_vmlinux(s->module->name) ?"":".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700325 } else {
Paul Bollebaec30e2014-04-16 18:05:35 +0200326 /* In case Module.symvers was out of date */
Trent Piepho4b219602007-10-11 16:40:10 -0700327 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100330 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100331 s->vmlinux = is_vmlinux(mod->name);
332 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700333 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100334 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100337static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700338 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100339{
340 struct symbol *s = find_symbol(name);
341
Rusty Russellb6568b12013-11-07 12:09:13 +1030342 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700343 s = new_symbol(name, mod, export);
Rusty Russellb6568b12013-11-07 12:09:13 +1030344 /* Don't complain when we find it later. */
345 s->preloaded = 1;
346 }
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100347 s->crc = crc;
348 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100351void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930354 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int fd;
356
357 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930358 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930360 if (fstat(fd, &st))
361 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 *size = st.st_size;
364 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930366failed:
367 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (map == MAP_FAILED)
369 return NULL;
370 return map;
371}
372
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100373/**
374 * Return a copy of the next line in a mmap'ed file.
375 * spaces in the beginning of the line is trimmed away.
376 * Return a pointer to a static buffer.
377 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100378char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 static char line[4096];
381 int skip = 1;
382 size_t len = 0;
383 signed char *p = (signed char *)file + *pos;
384 char *s = line;
385
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100386 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (skip && isspace(*p)) {
388 p++;
389 continue;
390 }
391 skip = 0;
392 if (*p != '\n' && (*pos < size)) {
393 len++;
394 *s++ = *p++;
395 if (len > 4095)
396 break; /* Too long, stop */
397 } else {
398 /* End of string */
399 *s = '\0';
400 return line;
401 }
402 }
403 /* End of buffer */
404 return NULL;
405}
406
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100407void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 munmap(file, size);
410}
411
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100412static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100415 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 Elf_Shdr *sechdrs;
417 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200418 const char *secstrings;
419 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 hdr = grab_file(filename, &info->size);
422 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930423 if (ignore_missing_files) {
424 fprintf(stderr, "%s: %s (ignored)\n", filename,
425 strerror(errno));
426 return 0;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200429 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100432 if (info->size < sizeof(*hdr)) {
433 /* file too small, assume this is an empty .o file */
434 return 0;
435 }
436 /* Is this a valid ELF file? */
437 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
438 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
439 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
440 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
441 /* Not an ELF file - silently ignore it */
442 return 0;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200445 hdr->e_type = TO_NATIVE(hdr->e_type);
446 hdr->e_machine = TO_NATIVE(hdr->e_machine);
447 hdr->e_version = TO_NATIVE(hdr->e_version);
448 hdr->e_entry = TO_NATIVE(hdr->e_entry);
449 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
450 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
451 hdr->e_flags = TO_NATIVE(hdr->e_flags);
452 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
453 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
454 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
455 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
456 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
457 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 sechdrs = (void *)hdr + hdr->e_shoff;
459 info->sechdrs = sechdrs;
460
Petr Stetiara83710e2007-08-27 12:15:07 +0200461 /* Check if file offset is correct */
462 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100463 fatal("section header offset=%lu in file '%s' is bigger than "
464 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
465 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200466 return 0;
467 }
468
Anders Kaseorg68457562011-05-19 16:55:27 -0600469 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200470 /*
471 * There are more than 64k sections,
472 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200473 */
474 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
475 }
476 else {
477 info->num_sections = hdr->e_shnum;
478 }
479 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600480 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200481 }
482 else {
483 info->secindex_strings = hdr->e_shstrndx;
484 }
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200487 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200488 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
489 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
490 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
491 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
492 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
493 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
494 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
495 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
496 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
497 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200500 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
501 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700502 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900503 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Tejun Heo56fc82c2009-02-06 00:48:02 +0900505 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100506 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
507 "sizeof(*hrd)=%zu\n", filename,
508 (unsigned long)sechdrs[i].sh_offset,
509 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100510 return 0;
511 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700512 secname = secstrings + sechdrs[i].sh_name;
513 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900514 if (nobits)
515 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
517 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700518 } else if (strcmp(secname, "__ksymtab") == 0)
519 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200520 else if (strcmp(secname, "__ksymtab_unused") == 0)
521 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700522 else if (strcmp(secname, "__ksymtab_gpl") == 0)
523 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200524 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
525 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700526 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
527 info->export_gpl_future_sec = i;
528
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200529 if (sechdrs[i].sh_type == SHT_SYMTAB) {
530 unsigned int sh_link_idx;
531 symtab_idx = i;
532 info->symtab_start = (void *)hdr +
533 sechdrs[i].sh_offset;
534 info->symtab_stop = (void *)hdr +
535 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600536 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200537 info->strtab = (void *)hdr +
538 sechdrs[sh_link_idx].sh_offset;
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200541 /* 32bit section no. table? ("more than 64k sections") */
542 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
543 symtab_shndx_idx = i;
544 info->symtab_shndx_start = (void *)hdr +
545 sechdrs[i].sh_offset;
546 info->symtab_shndx_stop = (void *)hdr +
547 sechdrs[i].sh_offset + sechdrs[i].sh_size;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100550 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100551 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Fix endianness in symbols */
554 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
555 sym->st_shndx = TO_NATIVE(sym->st_shndx);
556 sym->st_name = TO_NATIVE(sym->st_name);
557 sym->st_value = TO_NATIVE(sym->st_value);
558 sym->st_size = TO_NATIVE(sym->st_size);
559 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200560
561 if (symtab_shndx_idx != ~0U) {
562 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600563 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200564 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600565 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200566 symtab_idx);
567 /* Fix endianness */
568 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
569 p++)
570 *p = TO_NATIVE(*p);
571 }
572
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100573 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100576static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 release_file(info->hdr, info->size);
579}
580
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200581static int ignore_undef_symbol(struct elf_info *info, const char *symname)
582{
583 /* ignore __this_module, it will be resolved shortly */
Rusty Russellb92021b2013-03-15 15:04:17 +1030584 if (strcmp(symname, VMLINUX_SYMBOL_STR(__this_module)) == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200585 return 1;
586 /* ignore global offset table */
587 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
588 return 1;
589 if (info->hdr->e_machine == EM_PPC)
590 /* Special register function linked on all modules during final link of .ko */
591 if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 ||
592 strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 ||
593 strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 ||
Andreas Schwab8fe9c932013-12-30 15:31:17 +0100594 strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0 ||
595 strncmp(symname, "_restvr_", sizeof("_restvr_") - 1) == 0 ||
596 strncmp(symname, "_savevr_", sizeof("_savevr_") - 1) == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200597 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000598 if (info->hdr->e_machine == EM_PPC64)
599 /* Special register function linked on all modules during final link of .ko */
600 if (strncmp(symname, "_restgpr0_", sizeof("_restgpr0_") - 1) == 0 ||
Andreas Schwab8fe9c932013-12-30 15:31:17 +0100601 strncmp(symname, "_savegpr0_", sizeof("_savegpr0_") - 1) == 0 ||
602 strncmp(symname, "_restvr_", sizeof("_restvr_") - 1) == 0 ||
Alan Modrac1536932016-01-15 20:52:22 +1100603 strncmp(symname, "_savevr_", sizeof("_savevr_") - 1) == 0 ||
604 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000605 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200606 /* Do not ignore this symbol */
607 return 0;
608}
609
Rusty Russellb92021b2013-03-15 15:04:17 +1030610#define CRC_PFX VMLINUX_SYMBOL_STR(__crc_)
611#define KSYMTAB_PFX VMLINUX_SYMBOL_STR(__ksymtab_)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100613static void handle_modversions(struct module *mod, struct elf_info *info,
614 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200617 enum export export;
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100618 bool is_crc = false;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200619
Frank Rowand258f7422012-04-09 17:59:03 -0700620 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
621 strncmp(symname, "__ksymtab", 9) == 0)
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200622 export = export_from_secname(info, get_secindex(info, sym));
623 else
624 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Andi Kleenb5064652013-11-12 15:08:38 -0800626 /* CRC'd symbol */
627 if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) {
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100628 is_crc = true;
Andi Kleenb5064652013-11-12 15:08:38 -0800629 crc = (unsigned int) sym->st_value;
Ard Biesheuvel56067812017-02-03 09:54:05 +0000630 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
631 unsigned int *crcp;
632
633 /* symbol points to the CRC in the ELF object */
634 crcp = (void *)info->hdr + sym->st_value +
635 info->sechdrs[sym->st_shndx].sh_offset -
636 (info->hdr->e_type != ET_REL ?
637 info->sechdrs[sym->st_shndx].sh_addr : 0);
638 crc = *crcp;
639 }
Andi Kleenb5064652013-11-12 15:08:38 -0800640 sym_update_crc(symname + strlen(CRC_PFX), mod, crc,
641 export);
642 }
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 switch (sym->st_shndx) {
645 case SHN_COMMON:
Andi Kleenef178f92014-02-08 09:01:17 +0100646 if (!strncmp(symname, "__gnu_lto_", sizeof("__gnu_lto_")-1)) {
647 /* Should warn here, but modpost runs before the linker */
648 } else
649 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 case SHN_UNDEF:
652 /* undefined symbol */
653 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
654 ELF_ST_BIND(sym->st_info) != STB_WEAK)
655 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200656 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
Ben Colline8d529012005-08-19 13:44:57 -0700658/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
659#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
660/* add compatibility with older glibc */
661#ifndef STT_SPARC_REGISTER
662#define STT_SPARC_REGISTER STT_REGISTER
663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (info->hdr->e_machine == EM_SPARC ||
665 info->hdr->e_machine == EM_SPARCV9) {
666 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700667 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100669 if (symname[0] == '.') {
670 char *munged = strdup(symname);
671 munged[0] = '_';
672 munged[1] = toupper(munged[1]);
673 symname = munged;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100677
Rusty Russellb92021b2013-03-15 15:04:17 +1030678#ifdef CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX
679 if (symname[0] != '_')
680 break;
681 else
682 symname++;
683#endif
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100684 if (is_crc) {
685 const char *e = is_vmlinux(mod->name) ?"":".ko";
686 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n", symname + strlen(CRC_PFX), mod->name, e);
687 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030688 mod->unres = alloc_symbol(symname,
689 ELF_ST_BIND(sym->st_info) == STB_WEAK,
690 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 break;
692 default:
693 /* All exported symbols */
Michal Marek8d995132009-12-12 12:02:24 +0100694 if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700695 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod,
696 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030698 if (strcmp(symname, VMLINUX_SYMBOL_STR(init_module)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 mod->has_init = 1;
Rusty Russellb92021b2013-03-15 15:04:17 +1030700 if (strcmp(symname, VMLINUX_SYMBOL_STR(cleanup_module)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 mod->has_cleanup = 1;
702 break;
703 }
704}
705
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100706/**
707 * Parse tag=value strings from .modinfo section
708 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709static char *next_string(char *string, unsigned long *secsize)
710{
711 /* Skip non-zero chars */
712 while (string[0]) {
713 string++;
714 if ((*secsize)-- <= 1)
715 return NULL;
716 }
717
718 /* Skip any zero padding. */
719 while (!string[0]) {
720 string++;
721 if ((*secsize)-- <= 1)
722 return NULL;
723 }
724 return string;
725}
726
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200727static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len,
728 const char *tag, char *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 char *p;
731 unsigned int taglen = strlen(tag);
732 unsigned long size = modinfo_len;
733
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200734 if (info) {
735 size -= info - (char *)modinfo;
736 modinfo = next_string(info, &size);
737 }
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 for (p = modinfo; p; p = next_string(p, &size)) {
740 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
741 return p + taglen + 1;
742 }
743 return NULL;
744}
745
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200746static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
747 const char *tag)
748
749{
750 return get_next_modinfo(modinfo, modinfo_len, tag, NULL);
751}
752
Sam Ravnborg93684d32006-02-19 11:53:35 +0100753/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100754 * Test if string s ends in string sub
755 * return 0 if match
756 **/
757static int strrcmp(const char *s, const char *sub)
758{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100759 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100760
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100761 if (!s || !sub)
762 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100763
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100764 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100765 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100766
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100767 if ((slen == 0) || (sublen == 0))
768 return 1;
769
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100770 if (sublen > slen)
771 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100772
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100773 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100774}
775
Sam Ravnborgff13f922008-01-23 19:54:27 +0100776static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
777{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100778 if (sym)
779 return elf->strtab + sym->st_name;
780 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100781 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100782}
783
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200784static const char *sec_name(struct elf_info *elf, int secindex)
Sam Ravnborgff13f922008-01-23 19:54:27 +0100785{
786 Elf_Shdr *sechdrs = elf->sechdrs;
787 return (void *)elf->hdr +
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200788 elf->sechdrs[elf->secindex_strings].sh_offset +
789 sechdrs[secindex].sh_name;
Sam Ravnborgff13f922008-01-23 19:54:27 +0100790}
791
792static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
793{
794 return (void *)elf->hdr +
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200795 elf->sechdrs[elf->secindex_strings].sh_offset +
796 sechdr->sh_name;
Sam Ravnborgff13f922008-01-23 19:54:27 +0100797}
798
Sam Ravnborg10668222008-01-13 22:21:31 +0100799/* The pattern is an array of simple patterns.
800 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100801 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100802 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930803 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100804 */
Trevor Keith5c725132009-09-22 16:43:38 -0700805static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100806{
807 const char *p;
808 while (*pat) {
809 p = *pat++;
810 const char *endp = p + strlen(p) - 1;
811
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930812 /* "*foo*" */
813 if (*p == '*' && *endp == '*') {
814 char *here, *bare = strndup(p + 1, strlen(p) - 2);
815
816 here = strstr(sym, bare);
817 free(bare);
818 if (here != NULL)
819 return 1;
820 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100821 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930822 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100823 if (strrcmp(sym, p + 1) == 0)
824 return 1;
825 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100826 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100827 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100828 if (strncmp(sym, p, strlen(p) - 1) == 0)
829 return 1;
830 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100831 /* no wildcards */
832 else {
833 if (strcmp(p, sym) == 0)
834 return 1;
835 }
836 }
837 /* no match */
838 return 0;
839}
840
Sam Ravnborg10668222008-01-13 22:21:31 +0100841/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930842static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200843{
844 ".comment*",
845 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930846 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800847 ".zdebug*", /* Compressed debug sections. */
David Howells019fca82010-08-12 16:54:47 +0100848 ".GCC-command-line", /* mn10300 */
Jonathan Kliegman76b27642012-10-04 16:32:19 -0400849 ".GCC.command.line", /* record-gcc-switches, non mn10300 */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200850 ".mdebug*", /* alpha, score, mips etc. */
851 ".pdr", /* alpha, score, mips etc. */
852 ".stab*",
853 ".note*",
854 ".got*",
855 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400856 ".xt.prop", /* xtensa */
857 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030858 ".arcextmap*", /* arc */
859 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030860 ".cmem*", /* EZchip */
861 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100862 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600863 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200864 NULL
865};
Sam Ravnborg10668222008-01-13 22:21:31 +0100866
Sam Ravnborge241a632008-01-28 20:13:13 +0100867/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400868 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100869 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400870 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100871 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400872static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900873 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100874{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400875 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100876
Anders Kaseorgb614a692009-04-23 16:49:33 -0400877 if (sechdr->sh_type == SHT_PROGBITS &&
878 !(sechdr->sh_flags & SHF_ALLOC) &&
879 !match(sec, section_white_list)) {
880 warn("%s (%s): unexpected non-allocatable section.\n"
881 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
882 "Note that for example <linux/init.h> contains\n"
883 "section definitions for use in .S files.\n\n",
884 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100885 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100886}
887
888
889
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100890#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930891 ".init.setup", ".init.rodata", ".meminit.rodata", \
892 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100893#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930894 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100895
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100896#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930897 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100898#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930899 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100900
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200901#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930902 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
903 ".pci_fixup_enable", ".pci_fixup_resume", \
904 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200905
Paul Gortmakere24f6622013-06-19 19:30:48 -0400906#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
907#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100908
909#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
910#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100911
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930912#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930913#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700914 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930915#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400916 ".fixup", ".entry.text", ".exception.text", ".text.*", \
917 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100918
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000919#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000920#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100921
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000922#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000923#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100924
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930925#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
926 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
927
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100928/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930929static const char *const init_data_sections[] =
930 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100931
932/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930933static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100934
935/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930936static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100937 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100938
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930939/* all text sections */
940static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
941
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100942/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930943static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100944
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100945
946/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100947#define DEFAULT_SYMBOL_WHITE_LIST \
948 "*driver", \
949 "*_template", /* scsi uses *_template a lot */ \
950 "*_timer", /* arm uses ops structures named _timer a lot */ \
951 "*_sht", /* scsi also used *_sht to some extent */ \
952 "*_ops", \
953 "*_probe", \
954 "*_probe_one", \
955 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100956
Mathias Krause7a3ee752014-08-27 20:28:53 +0930957static const char *const head_sections[] = { ".head.text*", NULL };
958static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100959 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930960static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100961
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100962enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100963 TEXT_TO_ANY_INIT,
964 DATA_TO_ANY_INIT,
965 TEXT_TO_ANY_EXIT,
966 DATA_TO_ANY_EXIT,
967 XXXINIT_TO_SOME_INIT,
968 XXXEXIT_TO_SOME_EXIT,
969 ANY_INIT_TO_ANY_EXIT,
970 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100971 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930972 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100973};
974
Quentin Casasnovase5d8f592015-04-13 20:55:15 +0930975/**
976 * Describe how to match sections on different criterias:
977 *
978 * @fromsec: Array of sections to be matched.
979 *
980 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
981 * this array is forbidden (black-list). Can be empty.
982 *
983 * @good_tosec: Relocations applied to a section in @fromsec must be
984 * targetting sections in this array (white-list). Can be empty.
985 *
986 * @mismatch: Type of mismatch.
987 *
988 * @symbol_white_list: Do not match a relocation to a symbol in this list
989 * even if it is targetting a section in @bad_to_sec.
990 *
991 * @handler: Specific handler to call when a match is found. If NULL,
992 * default_mismatch_handler() will be called.
993 *
994 */
Sam Ravnborg10668222008-01-13 22:21:31 +0100995struct sectioncheck {
996 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +0930997 const char *bad_tosec[20];
998 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100999 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001000 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301001 void (*handler)(const char *modname, struct elf_info *elf,
1002 const struct sectioncheck* const mismatch,
1003 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1004
Sam Ravnborg10668222008-01-13 22:21:31 +01001005};
1006
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301007static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1008 const struct sectioncheck* const mismatch,
1009 Elf_Rela *r, Elf_Sym *sym,
1010 const char *fromsec);
1011
Mathias Krause7a3ee752014-08-27 20:28:53 +09301012static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001013/* Do not reference init/exit code/data from
1014 * normal code and data
1015 */
1016{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001017 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301018 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001019 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001020 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001021},
1022{
1023 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301024 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001025 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001026 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001027},
1028{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001029 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301030 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001031 .mismatch = DATA_TO_ANY_INIT,
1032 .symbol_white_list = {
1033 "*_template", "*_timer", "*_sht", "*_ops",
1034 "*_probe", "*_probe_one", "*_console", NULL
1035 },
1036},
1037{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001038 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301039 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001040 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001041 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001042},
1043{
1044 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301045 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001046 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001047 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001048},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001049/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001050{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001051 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301052 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001053 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001054 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001055},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001056/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001057{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001058 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301059 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001060 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001061 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001062},
1063/* Do not use exit code/data from init code */
1064{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001065 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301066 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001067 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001068 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001069},
1070/* Do not use init code/data from exit code */
1071{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001072 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301073 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001074 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001075 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001076},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001077{
1078 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301079 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001080 .mismatch = ANY_INIT_TO_ANY_EXIT,
1081 .symbol_white_list = { NULL },
1082},
Sam Ravnborg10668222008-01-13 22:21:31 +01001083/* Do not export init/exit functions or data */
1084{
1085 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301086 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001087 .mismatch = EXPORT_TO_INIT_EXIT,
1088 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301089},
1090{
1091 .fromsec = { "__ex_table", NULL },
1092 /* If you're adding any new black-listed sections in here, consider
1093 * adding a special 'printer' for them in scripts/check_extable.
1094 */
1095 .bad_tosec = { ".altinstr_replacement", NULL },
1096 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1097 .mismatch = EXTABLE_TO_NON_TEXT,
1098 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001099}
1100};
1101
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001102static const struct sectioncheck *section_mismatch(
1103 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001104{
1105 int i;
1106 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1107 const struct sectioncheck *check = &sectioncheck[0];
1108
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301109 /*
1110 * The target section could be the SHT_NUL section when we're
1111 * handling relocations to un-resolved symbols, trying to match it
1112 * doesn't make much sense and causes build failures on parisc and
1113 * mn10300 architectures.
1114 */
1115 if (*tosec == '\0')
1116 return NULL;
1117
Sam Ravnborg10668222008-01-13 22:21:31 +01001118 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301119 if (match(fromsec, check->fromsec)) {
1120 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1121 return check;
1122 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1123 return check;
1124 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001125 check++;
1126 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001127 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001128}
1129
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001130/**
1131 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001132 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001133 * Pattern 1:
1134 * If a module parameter is declared __initdata and permissions=0
1135 * then this is legal despite the warning generated.
1136 * We cannot see value of permissions here, so just ignore
1137 * this pattern.
1138 * The pattern is identified by:
1139 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001140 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001141 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001142 *
Rusty Russell6a841522010-08-11 23:04:16 -06001143 * Pattern 1a:
1144 * module_param_call() ops can refer to __init set function if permissions=0
1145 * The pattern is identified by:
1146 * tosec = .init.text
1147 * fromsec = .data*
1148 * atsym = __param_ops_*
1149 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001150 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001151 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001152 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001153 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001154 * tosec = init or exit section
1155 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001156 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1157 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001158 *
1159 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001160 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001161 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001162 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001163 * Some symbols belong to init section but still it is ok to reference
1164 * these from non-init sections as these symbols don't have any memory
1165 * allocated for them and symbol address and value are same. So even
1166 * if init section is freed, its ok to reference those symbols.
1167 * For ex. symbols marking the init section boundaries.
1168 * This pattern is identified by
1169 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001170 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301171 * Pattern 5:
1172 * GCC may optimize static inlines when fed constant arg(s) resulting
1173 * in functions like cpumask_empty() -- generating an associated symbol
1174 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1175 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1176 * meaningless section warning. May need to add isra symbols too...
1177 * This pattern is identified by
1178 * tosec = init section
1179 * fromsec = text section
1180 * refsymname = *.constprop.*
1181 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001182 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001183static int secref_whitelist(const struct sectioncheck *mismatch,
1184 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001185 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001186{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001187 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001188 if (match(tosec, init_data_sections) &&
1189 match(fromsec, data_sections) &&
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001190 (strncmp(fromsym, "__param", strlen("__param")) == 0))
1191 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001192
Rusty Russell6a841522010-08-11 23:04:16 -06001193 /* Check for pattern 1a */
1194 if (strcmp(tosec, ".init.text") == 0 &&
1195 match(fromsec, data_sections) &&
1196 (strncmp(fromsym, "__param_ops_", strlen("__param_ops_")) == 0))
1197 return 0;
1198
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001199 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001200 if (match(tosec, init_exit_sections) &&
1201 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001202 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001203 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001204
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001205 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001206 if (match(fromsec, head_sections) &&
1207 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001208 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001209
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001210 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001211 if (match(tosym, linker_symbols))
1212 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001213
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301214 /* Check for pattern 5 */
1215 if (match(fromsec, text_sections) &&
1216 match(tosec, init_sections) &&
1217 match(fromsym, optim_symbols))
1218 return 0;
1219
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001220 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001221}
1222
1223/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001224 * Find symbol based on relocation record info.
1225 * In some cases the symbol supplied is a valid symbol so
1226 * return refsym. If st_name != 0 we assume this is a valid symbol.
1227 * In other cases the symbol needs to be looked up in the symbol table
1228 * based on section and address.
1229 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001230static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001231 Elf_Sym *relsym)
1232{
1233 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001234 Elf_Sym *near = NULL;
1235 Elf64_Sword distance = 20;
1236 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001237 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001238
1239 if (relsym->st_name != 0)
1240 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001241
1242 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001243 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001244 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001245 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001246 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1247 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001248 if (sym->st_value == addr)
1249 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001250 /* Find a symbol nearby - addr are maybe negative */
1251 d = sym->st_value - addr;
1252 if (d < 0)
1253 d = addr - sym->st_value;
1254 if (d < distance) {
1255 distance = d;
1256 near = sym;
1257 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001258 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001259 /* We need a close match */
1260 if (distance < 20)
1261 return near;
1262 else
1263 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001264}
1265
David Brownellda68d612007-02-20 13:58:16 -08001266static inline int is_arm_mapping_symbol(const char *str)
1267{
Kyle McMartin6c34f1f2014-09-16 22:37:18 +01001268 return str[0] == '$' && strchr("axtd", str[1])
David Brownellda68d612007-02-20 13:58:16 -08001269 && (str[2] == '\0' || str[2] == '.');
1270}
1271
1272/*
1273 * If there's no name there, ignore it; likewise, ignore it if it's
1274 * one of the magic symbols emitted used by current ARM tools.
1275 *
1276 * Otherwise if find_symbols_between() returns those symbols, they'll
1277 * fail the whitelist tests and cause lots of false alarms ... fixable
1278 * only by merging __exit and __init sections into __text, bloating
1279 * the kernel (which is especially evil on embedded platforms).
1280 */
1281static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1282{
1283 const char *name = elf->strtab + sym->st_name;
1284
1285 if (!name || !strlen(name))
1286 return 0;
1287 return !is_arm_mapping_symbol(name);
1288}
1289
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001290/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001291 * Find symbols before or equal addr and after addr - in the section sec.
1292 * If we find two symbols with equal offset prefer one with a valid name.
1293 * The ELF format may have a better way to detect what type of symbol
1294 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001295 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001296static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1297 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001298{
1299 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001300 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001301 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001302
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001303 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1304 const char *symsec;
1305
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001306 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001307 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001308 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001309 if (strcmp(symsec, sec) != 0)
1310 continue;
David Brownellda68d612007-02-20 13:58:16 -08001311 if (!is_valid_name(elf, sym))
1312 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001313 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001314 if ((addr - sym->st_value) < distance) {
1315 distance = addr - sym->st_value;
1316 near = sym;
1317 } else if ((addr - sym->st_value) == distance) {
1318 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001319 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001320 }
1321 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001322 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001323}
1324
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001325/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001326 * Convert a section name to the function/data attribute
1327 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001328 * .memexitconst => __memconst
1329 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001330 *
1331 * The memory of returned value has been allocated on a heap. The user of this
1332 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001333*/
1334static char *sec2annotation(const char *s)
1335{
1336 if (match(s, init_exit_sections)) {
1337 char *p = malloc(20);
1338 char *r = p;
1339
1340 *p++ = '_';
1341 *p++ = '_';
1342 if (*s == '.')
1343 s++;
1344 while (*s && *s != '.')
1345 *p++ = *s++;
1346 *p = '\0';
1347 if (*s == '.')
1348 s++;
1349 if (strstr(s, "rodata") != NULL)
1350 strcat(p, "const ");
1351 else if (strstr(s, "data") != NULL)
1352 strcat(p, "data ");
1353 else
1354 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001355 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001356 } else {
Andrew Morton5003bab2010-08-11 00:42:26 -07001357 return strdup("");
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001358 }
1359}
1360
1361static int is_function(Elf_Sym *sym)
1362{
1363 if (sym)
1364 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1365 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001366 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001367}
1368
Randy Dunlap00759c02011-03-15 14:13:47 -07001369static void print_section_list(const char * const list[20])
1370{
1371 const char *const *s = list;
1372
1373 while (*s) {
1374 fprintf(stderr, "%s", *s);
1375 s++;
1376 if (*s)
1377 fprintf(stderr, ", ");
1378 }
1379 fprintf(stderr, "\n");
1380}
1381
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301382static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1383{
1384 switch (is_func) {
1385 case 0: *name = "variable"; *name_p = ""; break;
1386 case 1: *name = "function"; *name_p = "()"; break;
1387 default: *name = "(unknown reference)"; *name_p = ""; break;
1388 }
1389}
1390
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001391/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001392 * Print a warning about a section mismatch.
1393 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001394 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001395 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001396static void report_sec_mismatch(const char *modname,
1397 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001398 const char *fromsec,
1399 unsigned long long fromaddr,
1400 const char *fromsym,
1401 int from_is_func,
1402 const char *tosec, const char *tosym,
1403 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001404{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001405 const char *from, *from_p;
1406 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001407 char *prl_from;
1408 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001409
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001410 sec_mismatch_count++;
1411 if (!sec_mismatch_verbose)
1412 return;
1413
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301414 get_pretty_name(from_is_func, &from, &from_p);
1415 get_pretty_name(to_is_func, &to, &to_p);
1416
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001417 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1418 "to the %s %s:%s%s\n",
1419 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1420 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001421
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001422 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001423 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001424 prl_from = sec2annotation(fromsec);
1425 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001426 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001427 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001428 "the %s %s%s%s.\n"
1429 "This is often because %s lacks a %s\n"
1430 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001431 prl_from, fromsym,
1432 to, prl_to, tosym, to_p,
1433 fromsym, prl_to, tosym);
1434 free(prl_from);
1435 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001436 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001437 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001438 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001439 fprintf(stderr,
1440 "The variable %s references\n"
1441 "the %s %s%s%s\n"
1442 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001443 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001444 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001445 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001446 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001447 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001448 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001449 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001450 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001451 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001452 fprintf(stderr,
1453 "The function %s() references a %s in an exit section.\n"
1454 "Often the %s %s%s has valid usage outside the exit section\n"
1455 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001456 fromsym, to, to, tosym, to_p, prl_to, tosym);
1457 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001458 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001459 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001460 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001461 fprintf(stderr,
1462 "The variable %s references\n"
1463 "the %s %s%s%s\n"
1464 "If the reference is valid then annotate the\n"
1465 "variable with __exit* (see linux/init.h) or "
1466 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001467 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001468 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001469 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001470 break;
1471 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001472 case XXXINIT_TO_SOME_INIT:
1473 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001474 prl_from = sec2annotation(fromsec);
1475 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001476 fprintf(stderr,
1477 "The %s %s%s%s references\n"
1478 "a %s %s%s%s.\n"
1479 "If %s is only used by %s then\n"
1480 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001481 from, prl_from, fromsym, from_p,
1482 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001483 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001484 free(prl_from);
1485 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001486 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001487 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001488 prl_from = sec2annotation(fromsec);
1489 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001490 fprintf(stderr,
1491 "The %s %s%s%s references\n"
1492 "a %s %s%s%s.\n"
1493 "This is often seen when error handling "
1494 "in the init function\n"
1495 "uses functionality in the exit path.\n"
1496 "The fix is often to remove the %sannotation of\n"
1497 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001498 from, prl_from, fromsym, from_p,
1499 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001500 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001501 free(prl_from);
1502 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001503 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001504 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001505 prl_from = sec2annotation(fromsec);
1506 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001507 fprintf(stderr,
1508 "The %s %s%s%s references\n"
1509 "a %s %s%s%s.\n"
1510 "This is often seen when error handling "
1511 "in the exit function\n"
1512 "uses functionality in the init path.\n"
1513 "The fix is often to remove the %sannotation of\n"
1514 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001515 from, prl_from, fromsym, from_p,
1516 to, prl_to, tosym, to_p,
1517 prl_to, tosym, to_p);
1518 free(prl_from);
1519 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001520 break;
1521 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001522 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001523 fprintf(stderr,
1524 "The symbol %s is exported and annotated %s\n"
1525 "Fix this by removing the %sannotation of %s "
1526 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001527 tosym, prl_to, prl_to, tosym);
1528 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001529 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301530 case EXTABLE_TO_NON_TEXT:
1531 fatal("There's a special handler for this mismatch type, "
1532 "we should never get here.");
1533 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001534 }
1535 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001536}
1537
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301538static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1539 const struct sectioncheck* const mismatch,
1540 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1541{
1542 const char *tosec;
1543 Elf_Sym *to;
1544 Elf_Sym *from;
1545 const char *tosym;
1546 const char *fromsym;
1547
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301548 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1549 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301550
1551 if (!strncmp(fromsym, "reference___initcall",
1552 sizeof("reference___initcall")-1))
1553 return;
1554
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301555 tosec = sec_name(elf, get_secindex(elf, sym));
1556 to = find_elf_symbol(elf, r->r_addend, sym);
1557 tosym = sym_name(elf, to);
1558
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301559 /* check whitelist - we may ignore it */
1560 if (secref_whitelist(mismatch,
1561 fromsec, fromsym, tosec, tosym)) {
1562 report_sec_mismatch(modname, mismatch,
1563 fromsec, r->r_offset, fromsym,
1564 is_function(from), tosec, tosym,
1565 is_function(to));
1566 }
1567}
1568
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301569static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1570{
1571 if (section_index > elf->num_sections)
1572 fatal("section_index is outside elf->num_sections!\n");
1573
1574 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1575}
1576
1577/*
1578 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1579 * to know the sizeof(struct exception_table_entry) for the target architecture.
1580 */
1581static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301582static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301583{
1584 /*
1585 * If we're currently checking the second relocation within __ex_table,
1586 * that relocation offset tells us the offsetof(struct
1587 * exception_table_entry, fixup) which is equal to sizeof(struct
1588 * exception_table_entry) divided by two. We use that to our advantage
1589 * since there's no portable way to get that size as every architecture
1590 * seems to go with different sized types. Not pretty but better than
1591 * hard-coding the size for every architecture..
1592 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301593 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301594 extable_entry_size = r->r_offset * 2;
1595}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301596
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301597static inline bool is_extable_fault_address(Elf_Rela *r)
1598{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301599 /*
1600 * extable_entry_size is only discovered after we've handled the
1601 * _second_ relocation in __ex_table, so only abort when we're not
1602 * handling the first reloc and extable_entry_size is zero.
1603 */
1604 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301605 fatal("extable_entry size hasn't been discovered!\n");
1606
1607 return ((r->r_offset == 0) ||
1608 (r->r_offset % extable_entry_size == 0));
1609}
1610
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301611#define is_second_extable_reloc(Start, Cur, Sec) \
1612 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1613
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301614static void report_extable_warnings(const char* modname, struct elf_info* elf,
1615 const struct sectioncheck* const mismatch,
1616 Elf_Rela* r, Elf_Sym* sym,
1617 const char* fromsec, const char* tosec)
1618{
1619 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1620 const char* fromsym_name = sym_name(elf, fromsym);
1621 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1622 const char* tosym_name = sym_name(elf, tosym);
1623 const char* from_pretty_name;
1624 const char* from_pretty_name_p;
1625 const char* to_pretty_name;
1626 const char* to_pretty_name_p;
1627
1628 get_pretty_name(is_function(fromsym),
1629 &from_pretty_name, &from_pretty_name_p);
1630 get_pretty_name(is_function(tosym),
1631 &to_pretty_name, &to_pretty_name_p);
1632
1633 warn("%s(%s+0x%lx): Section mismatch in reference"
1634 " from the %s %s%s to the %s %s:%s%s\n",
1635 modname, fromsec, (long)r->r_offset, from_pretty_name,
1636 fromsym_name, from_pretty_name_p,
1637 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1638
1639 if (!match(tosec, mismatch->bad_tosec) &&
1640 is_executable_section(elf, get_secindex(elf, sym)))
1641 fprintf(stderr,
1642 "The relocation at %s+0x%lx references\n"
1643 "section \"%s\" which is not in the list of\n"
1644 "authorized sections. If you're adding a new section\n"
1645 "and/or if this reference is valid, add \"%s\" to the\n"
1646 "list of authorized sections to jump to on fault.\n"
1647 "This can be achieved by adding \"%s\" to \n"
1648 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1649 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1650}
1651
1652static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1653 const struct sectioncheck* const mismatch,
1654 Elf_Rela* r, Elf_Sym* sym,
1655 const char *fromsec)
1656{
1657 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1658
1659 sec_mismatch_count++;
1660
1661 if (sec_mismatch_verbose)
1662 report_extable_warnings(modname, elf, mismatch, r, sym,
1663 fromsec, tosec);
1664
1665 if (match(tosec, mismatch->bad_tosec))
1666 fatal("The relocation at %s+0x%lx references\n"
1667 "section \"%s\" which is black-listed.\n"
1668 "Something is seriously wrong and should be fixed.\n"
1669 "You might get more information about where this is\n"
1670 "coming from by using scripts/check_extable.sh %s\n",
1671 fromsec, (long)r->r_offset, tosec, modname);
1672 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1673 if (is_extable_fault_address(r))
1674 fatal("The relocation at %s+0x%lx references\n"
1675 "section \"%s\" which is not executable, IOW\n"
1676 "it is not possible for the kernel to fault\n"
1677 "at that address. Something is seriously wrong\n"
1678 "and should be fixed.\n",
1679 fromsec, (long)r->r_offset, tosec);
1680 else
1681 fatal("The relocation at %s+0x%lx references\n"
1682 "section \"%s\" which is not executable, IOW\n"
1683 "the kernel will fault if it ever tries to\n"
1684 "jump to it. Something is seriously wrong\n"
1685 "and should be fixed.\n",
1686 fromsec, (long)r->r_offset, tosec);
1687 }
1688}
1689
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001690static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001691 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001692{
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301693 const char *tosec = sec_name(elf, get_secindex(elf, sym));;
1694 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001695
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001696 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301697 if (mismatch->handler)
1698 mismatch->handler(modname, elf, mismatch,
1699 r, sym, fromsec);
1700 else
1701 default_mismatch_handler(modname, elf, mismatch,
1702 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001703 }
1704}
1705
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001706static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001707 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001708{
1709 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001710 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001711
1712 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001713 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001714}
1715
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001716static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001717{
1718 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001719 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001720
1721 switch (r_typ) {
1722 case R_386_32:
1723 r->r_addend = TO_NATIVE(*location);
1724 break;
1725 case R_386_PC32:
1726 r->r_addend = TO_NATIVE(*location) + 4;
1727 /* For CONFIG_RELOCATABLE=y */
1728 if (elf->hdr->e_type == ET_EXEC)
1729 r->r_addend += r->r_offset;
1730 break;
1731 }
1732 return 0;
1733}
1734
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001735#ifndef R_ARM_CALL
1736#define R_ARM_CALL 28
1737#endif
1738#ifndef R_ARM_JUMP24
1739#define R_ARM_JUMP24 29
1740#endif
1741
David A. Longc9698e52014-02-14 22:41:18 +01001742#ifndef R_ARM_THM_CALL
1743#define R_ARM_THM_CALL 10
1744#endif
1745#ifndef R_ARM_THM_JUMP24
1746#define R_ARM_THM_JUMP24 30
1747#endif
1748#ifndef R_ARM_THM_JUMP19
1749#define R_ARM_THM_JUMP19 51
1750#endif
1751
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001752static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001753{
1754 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1755
1756 switch (r_typ) {
1757 case R_ARM_ABS32:
1758 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001759 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001760 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001761 break;
1762 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001763 case R_ARM_CALL:
1764 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001765 case R_ARM_THM_CALL:
1766 case R_ARM_THM_JUMP24:
1767 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001768 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001769 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001770 sechdr->sh_offset +
1771 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001772 break;
1773 default:
1774 return 1;
1775 }
1776 return 0;
1777}
1778
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001779static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001780{
1781 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001782 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001783 unsigned int inst;
1784
1785 if (r_typ == R_MIPS_HI16)
1786 return 1; /* skip this */
1787 inst = TO_NATIVE(*location);
1788 switch (r_typ) {
1789 case R_MIPS_LO16:
1790 r->r_addend = inst & 0xffff;
1791 break;
1792 case R_MIPS_26:
1793 r->r_addend = (inst & 0x03ffffff) << 2;
1794 break;
1795 case R_MIPS_32:
1796 r->r_addend = inst;
1797 break;
1798 }
1799 return 0;
1800}
1801
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001802static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001803 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001804{
1805 Elf_Sym *sym;
1806 Elf_Rela *rela;
1807 Elf_Rela r;
1808 unsigned int r_sym;
1809 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001810
Sam Ravnborgff13f922008-01-23 19:54:27 +01001811 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001812 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1813
Sam Ravnborgff13f922008-01-23 19:54:27 +01001814 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001815 fromsec += strlen(".rela");
1816 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001817 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001818 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001819
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001820 for (rela = start; rela < stop; rela++) {
1821 r.r_offset = TO_NATIVE(rela->r_offset);
1822#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001823 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001824 unsigned int r_typ;
1825 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1826 r_sym = TO_NATIVE(r_sym);
1827 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1828 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1829 } else {
1830 r.r_info = TO_NATIVE(rela->r_info);
1831 r_sym = ELF_R_SYM(r.r_info);
1832 }
1833#else
1834 r.r_info = TO_NATIVE(rela->r_info);
1835 r_sym = ELF_R_SYM(r.r_info);
1836#endif
1837 r.r_addend = TO_NATIVE(rela->r_addend);
1838 sym = elf->symtab_start + r_sym;
1839 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001840 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001841 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301842 if (is_second_extable_reloc(start, rela, fromsec))
1843 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001844 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001845 }
1846}
1847
1848static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001849 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001850{
1851 Elf_Sym *sym;
1852 Elf_Rel *rel;
1853 Elf_Rela r;
1854 unsigned int r_sym;
1855 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001856
Sam Ravnborgff13f922008-01-23 19:54:27 +01001857 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1859
Sam Ravnborgff13f922008-01-23 19:54:27 +01001860 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001861 fromsec += strlen(".rel");
1862 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001863 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001864 return;
1865
1866 for (rel = start; rel < stop; rel++) {
1867 r.r_offset = TO_NATIVE(rel->r_offset);
1868#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001869 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001870 unsigned int r_typ;
1871 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1872 r_sym = TO_NATIVE(r_sym);
1873 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1874 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1875 } else {
1876 r.r_info = TO_NATIVE(rel->r_info);
1877 r_sym = ELF_R_SYM(r.r_info);
1878 }
1879#else
1880 r.r_info = TO_NATIVE(rel->r_info);
1881 r_sym = ELF_R_SYM(r.r_info);
1882#endif
1883 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001884 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001885 case EM_386:
1886 if (addend_386_rel(elf, sechdr, &r))
1887 continue;
1888 break;
1889 case EM_ARM:
1890 if (addend_arm_rel(elf, sechdr, &r))
1891 continue;
1892 break;
1893 case EM_MIPS:
1894 if (addend_mips_rel(elf, sechdr, &r))
1895 continue;
1896 break;
1897 }
1898 sym = elf->symtab_start + r_sym;
1899 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001900 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001901 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301902 if (is_second_extable_reloc(start, rel, fromsec))
1903 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001904 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001905 }
1906}
1907
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001908/**
1909 * A module includes a number of sections that are discarded
1910 * either when loaded or when used as built-in.
1911 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001912 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001913 * Likewise for modules used built-in the sections marked __exit
1914 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001915 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001916 * The check_sec_ref() function traverses all relocation records
1917 * to find all references to a section that reference a section that will
1918 * be discarded and warns about it.
1919 **/
1920static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001921 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001922{
1923 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001924 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001925
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001926 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001927 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001928 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001929 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001930 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001931 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001932 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001933 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001934 }
1935}
1936
Andi Kleen7d02b492014-02-08 09:01:12 +01001937static char *remove_dot(char *s)
1938{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301939 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001940
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301941 if (n && s[n]) {
1942 size_t m = strspn(s + n + 1, "0123456789");
1943 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001944 s[n] = 0;
1945 }
1946 return s;
1947}
1948
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001949static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 const char *symname;
1952 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001953 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct module *mod;
1955 struct elf_info info = { };
1956 Elf_Sym *sym;
1957
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001958 if (!parse_elf(&info, modname))
1959 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 mod = new_module(modname);
1962
1963 /* When there's no vmlinux, don't print warnings about
1964 * unresolved symbols (since there'll be too many ;) */
1965 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 mod->skip = 1;
1968 }
1969
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001970 license = get_modinfo(info.modinfo, info.modinfo_len, "license");
Sam Ravnborg2fa36562008-04-26 21:07:26 +02001971 if (info.modinfo && !license && !is_vmlinux(modname))
1972 warn("modpost: missing MODULE_LICENSE() in %s\n"
1973 "see include/linux/module.h for "
1974 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001975 while (license) {
1976 if (license_is_gpl_compatible(license))
1977 mod->gpl_compatible = 1;
1978 else {
1979 mod->gpl_compatible = 0;
1980 break;
1981 }
1982 license = get_next_modinfo(info.modinfo, info.modinfo_len,
1983 "license", license);
1984 }
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01001987 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 handle_modversions(mod, &info, sym, symname);
1990 handle_moddevtable(mod, &info, sym, symname);
1991 }
Sam Ravnborgd1f25e62008-01-17 21:17:42 +01001992 if (!is_vmlinux(modname) ||
Sam Ravnborg10668222008-01-13 22:21:31 +01001993 (is_vmlinux(modname) && vmlinux_section_warnings))
1994 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
1997 if (version)
1998 maybe_frob_rcs_version(modname, version, info.modinfo,
1999 version - (char *)info.hdr);
2000 if (version || (all_versions && !is_vmlinux(modname)))
2001 get_src_version(modname, mod->srcversion,
2002 sizeof(mod->srcversion)-1);
2003
2004 parse_elf_finish(&info);
2005
Rusty Russell8c8ef422009-03-31 13:05:34 -06002006 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * never passed as an argument to an exported function, so
2008 * the automatic versioning doesn't pick it up, but it's really
2009 * important anyhow */
2010 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002011 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Rusty Russell712f9b42013-04-04 17:37:38 +10302014static void read_symbols_from_files(const char *filename)
2015{
2016 FILE *in = stdin;
2017 char fname[PATH_MAX];
2018
2019 if (strcmp(filename, "-") != 0) {
2020 in = fopen(filename, "r");
2021 if (!in)
2022 fatal("Can't open filenames file %s: %m", filename);
2023 }
2024
2025 while (fgets(fname, PATH_MAX, in) != NULL) {
2026 if (strends(fname, "\n"))
2027 fname[strlen(fname)-1] = '\0';
2028 read_symbols(fname);
2029 }
2030
2031 if (in != stdin)
2032 fclose(in);
2033}
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035#define SZ 500
2036
2037/* We first write the generated file into memory using the
2038 * following helper, then compare to the file on disk and
2039 * only update the later if anything changed */
2040
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002041void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2042 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 char tmp[SZ];
2045 int len;
2046 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 va_start(ap, fmt);
2049 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002050 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 va_end(ap);
2052}
2053
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002054void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002057 buf->size += len + SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 buf->p = realloc(buf->p, buf->size);
2059 }
2060 strncpy(buf->p + buf->pos, s, len);
2061 buf->pos += len;
2062}
2063
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002064static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2065{
2066 const char *e = is_vmlinux(m) ?"":".ko";
2067
2068 switch (exp) {
2069 case export_gpl:
2070 fatal("modpost: GPL-incompatible module %s%s "
2071 "uses GPL-only symbol '%s'\n", m, e, s);
2072 break;
2073 case export_unused_gpl:
2074 fatal("modpost: GPL-incompatible module %s%s "
2075 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2076 break;
2077 case export_gpl_future:
2078 warn("modpost: GPL-incompatible module %s%s "
2079 "uses future GPL-only symbol '%s'\n", m, e, s);
2080 break;
2081 case export_plain:
2082 case export_unused:
2083 case export_unknown:
2084 /* ignore */
2085 break;
2086 }
2087}
2088
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002089static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002090{
2091 const char *e = is_vmlinux(m) ?"":".ko";
2092
2093 switch (exp) {
2094 case export_unused:
2095 case export_unused_gpl:
2096 warn("modpost: module %s%s "
2097 "uses symbol '%s' marked UNUSED\n", m, e, s);
2098 break;
2099 default:
2100 /* ignore */
2101 break;
2102 }
2103}
2104
2105static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002106{
2107 struct symbol *s, *exp;
2108
2109 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002110 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002111 exp = find_symbol(s->name);
2112 if (!exp || exp->module == mod)
2113 continue;
Andrew Morton6449bd62006-06-09 20:45:06 -07002114 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002115 if (basename)
2116 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002117 else
2118 basename = mod->name;
2119 if (!mod->gpl_compatible)
2120 check_for_gpl_usage(exp->export, basename, exp->name);
2121 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002122 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002123}
2124
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002125static int check_modname_len(struct module *mod)
2126{
2127 const char *mod_name;
2128
2129 mod_name = strrchr(mod->name, '/');
2130 if (mod_name == NULL)
2131 mod_name = mod->name;
2132 else
2133 mod_name++;
2134 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2135 merror("module name is too long [%s.ko]\n", mod->name);
2136 return 1;
2137 }
2138
2139 return 0;
2140}
2141
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002142/**
2143 * Header for the generated file
2144 **/
2145static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 buf_printf(b, "#include <linux/module.h>\n");
2148 buf_printf(b, "#include <linux/vermagic.h>\n");
2149 buf_printf(b, "#include <linux/compiler.h>\n");
2150 buf_printf(b, "\n");
2151 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002152 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302154 buf_printf(b, "__visible struct module __this_module\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002156 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002158 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (mod->has_cleanup)
2160 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002161 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002163 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 buf_printf(b, "};\n");
2165}
2166
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002167static void add_intree_flag(struct buffer *b, int is_intree)
2168{
2169 if (is_intree)
2170 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2171}
2172
Trevor Keith5c725132009-09-22 16:43:38 -07002173static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002174{
2175 static const char *staging_dir = "drivers/staging";
2176
2177 if (strncmp(staging_dir, name, strlen(staging_dir)) == 0)
2178 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2179}
2180
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002181/**
2182 * Record CRCs for unresolved symbols
2183 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002184static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002187 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 for (s = mod->unres; s; s = s->next) {
2190 exp = find_symbol(s->name);
2191 if (!exp || exp->module == mod) {
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002192 if (have_vmlinux && !s->weak) {
Matthew Wilcox2a116652006-10-07 05:35:32 -06002193 if (warn_unresolved) {
2194 warn("\"%s\" [%s.ko] undefined!\n",
2195 s->name, mod->name);
2196 } else {
2197 merror("\"%s\" [%s.ko] undefined!\n",
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09002198 s->name, mod->name);
Matthew Wilcox2a116652006-10-07 05:35:32 -06002199 err = 1;
2200 }
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 continue;
2203 }
2204 s->module = exp->module;
2205 s->crc_valid = exp->crc_valid;
2206 s->crc = exp->crc;
2207 }
2208
2209 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002210 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 buf_printf(b, "\n");
2213 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01002214 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
2216
2217 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002218 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002221 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 s->name, mod->name);
2223 continue;
2224 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302225 if (strlen(s->name) >= MODULE_NAME_LEN) {
2226 merror("too long symbol \"%s\" [%s.ko]\n",
2227 s->name, mod->name);
2228 err = 1;
2229 break;
2230 }
James Hogana53a11f32013-04-29 11:36:59 +09302231 buf_printf(b, "\t{ %#8x, __VMLINUX_SYMBOL_STR(%s) },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302232 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234
2235 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002236
2237 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002240static void add_depends(struct buffer *b, struct module *mod,
2241 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 struct symbol *s;
2244 struct module *m;
2245 int first = 1;
2246
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002247 for (m = modules; m; m = m->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 m->seen = is_vmlinux(m->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 buf_printf(b, "\n");
2251 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01002252 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
2254 buf_printf(b, "\"depends=");
2255 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002256 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (!s->module)
2258 continue;
2259
2260 if (s->module->seen)
2261 continue;
2262
2263 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002264 p = strrchr(s->module->name, '/');
2265 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002266 p++;
2267 else
2268 p = s->module->name;
2269 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 first = 0;
2271 }
2272 buf_printf(b, "\";\n");
2273}
2274
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002275static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 if (mod->srcversion[0]) {
2278 buf_printf(b, "\n");
2279 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2280 mod->srcversion);
2281 }
2282}
2283
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002284static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
2286 char *tmp;
2287 FILE *file;
2288 struct stat st;
2289
2290 file = fopen(fname, "r");
2291 if (!file)
2292 goto write;
2293
2294 if (fstat(fileno(file), &st) < 0)
2295 goto close_write;
2296
2297 if (st.st_size != b->pos)
2298 goto close_write;
2299
2300 tmp = NOFAIL(malloc(b->pos));
2301 if (fread(tmp, 1, b->pos, file) != b->pos)
2302 goto free_write;
2303
2304 if (memcmp(tmp, b->p, b->pos) != 0)
2305 goto free_write;
2306
2307 free(tmp);
2308 fclose(file);
2309 return;
2310
2311 free_write:
2312 free(tmp);
2313 close_write:
2314 fclose(file);
2315 write:
2316 file = fopen(fname, "w");
2317 if (!file) {
2318 perror(fname);
2319 exit(1);
2320 }
2321 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2322 perror(fname);
2323 exit(1);
2324 }
2325 fclose(file);
2326}
2327
Ram Paibd5cbce2006-06-08 22:12:53 -07002328/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002329 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002330 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002331static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 unsigned long size, pos = 0;
2334 void *file = grab_file(fname, &size);
2335 char *line;
2336
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002337 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /* No symbol versions, silently ignore */
2339 return;
2340
2341 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002342 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 unsigned int crc;
2344 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002345 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 if (!(symname = strchr(line, '\t')))
2348 goto fail;
2349 *symname++ = '\0';
2350 if (!(modname = strchr(symname, '\t')))
2351 goto fail;
2352 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002353 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002354 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002355 if (export && ((end = strchr(export, '\t')) != NULL))
2356 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 crc = strtoul(line, &d, 16);
2358 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2359 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002360 mod = find_module(modname);
2361 if (!mod) {
2362 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002364 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 mod->skip = 1;
2366 }
Ram Paibd5cbce2006-06-08 22:12:53 -07002367 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002368 s->kernel = kernel;
2369 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07002370 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302372 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return;
2374fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302375 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 fatal("parse error in symbol dump file\n");
2377}
2378
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002379/* For normal builds always dump all symbols.
2380 * For external modules only dump symbols
2381 * that are not read from kernel Module.symvers.
2382 **/
2383static int dump_sym(struct symbol *sym)
2384{
2385 if (!external_module)
2386 return 1;
2387 if (sym->vmlinux || sym->kernel)
2388 return 0;
2389 return 1;
2390}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002391
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002392static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 struct buffer buf = { };
2395 struct symbol *symbol;
2396 int n;
2397
2398 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2399 symbol = symbolhash[n];
2400 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002401 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07002402 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002403 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07002404 symbol->module->name,
2405 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 symbol = symbol->next;
2407 }
2408 }
2409 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002410 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002413struct ext_sym_list {
2414 struct ext_sym_list *next;
2415 const char *file;
2416};
2417
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002418int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
2420 struct module *mod;
2421 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002422 char *kernel_read = NULL, *module_read = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302423 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002425 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002426 struct ext_sym_list *extsym_iter;
2427 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302429 while ((opt = getopt(argc, argv, "i:I:e:mnsST:o:awM:K:E")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002430 switch (opt) {
2431 case 'i':
2432 kernel_read = optarg;
2433 break;
2434 case 'I':
2435 module_read = optarg;
2436 external_module = 1;
2437 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002438 case 'e':
2439 external_module = 1;
2440 extsym_iter =
2441 NOFAIL(malloc(sizeof(*extsym_iter)));
2442 extsym_iter->next = extsym_start;
2443 extsym_iter->file = optarg;
2444 extsym_start = extsym_iter;
2445 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002446 case 'm':
2447 modversions = 1;
2448 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302449 case 'n':
2450 ignore_missing_files = 1;
2451 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002452 case 'o':
2453 dump_write = optarg;
2454 break;
2455 case 'a':
2456 all_versions = 1;
2457 break;
2458 case 's':
2459 vmlinux_section_warnings = 0;
2460 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01002461 case 'S':
2462 sec_mismatch_verbose = 0;
2463 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302464 case 'T':
2465 files_source = optarg;
2466 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002467 case 'w':
2468 warn_unresolved = 1;
2469 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302470 case 'E':
2471 sec_mismatch_fatal = 1;
2472 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002473 default:
2474 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476 }
2477
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002478 if (kernel_read)
2479 read_dump(kernel_read, 1);
2480 if (module_read)
2481 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002482 while (extsym_start) {
2483 read_dump(extsym_start->file, 0);
2484 extsym_iter = extsym_start->next;
2485 free(extsym_start);
2486 extsym_start = extsym_iter;
2487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002489 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Rusty Russell712f9b42013-04-04 17:37:38 +10302492 if (files_source)
2493 read_symbols_from_files(files_source);
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 for (mod = modules; mod; mod = mod->next) {
2496 if (mod->skip)
2497 continue;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002498 check_exports(mod);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002499 }
2500
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002501 err = 0;
2502
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002503 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302504 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002505
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002506 if (mod->skip)
2507 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 buf.pos = 0;
2510
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002511 err |= check_modname_len(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002513 add_intree_flag(&buf, !external_module);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002514 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002515 err |= add_versions(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 add_depends(&buf, mod, modules);
2517 add_moddevtable(&buf, mod);
2518 add_srcversion(&buf, mod);
2519
2520 sprintf(fname, "%s.mod.c", mod->name);
2521 write_if_changed(&buf, fname);
2522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (dump_write)
2524 write_dump(dump_write);
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302525 if (sec_mismatch_count) {
2526 if (!sec_mismatch_verbose) {
2527 warn("modpost: Found %d section mismatch(es).\n"
2528 "To see full details build your kernel with:\n"
2529 "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n",
2530 sec_mismatch_count);
2531 }
2532 if (sec_mismatch_fatal) {
2533 fatal("modpost: Section mismatches detected.\n"
2534 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
2535 }
2536 }
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002537 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}