blob: 76c221dd9b2b62d39037a75c8bd36091b4829de1 [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"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020022#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093025static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093027static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010030/* If we are modposting external module set to 1 */
31static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020032/* Warn about section mismatch in vmlinux if set to 1 */
33static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070034/* Only warn about unresolved symbols */
35static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070036/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010037static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103038static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093039/* ignore missing files */
40static int ignore_missing_files;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010041
Sam Ravnborgc96fca22006-07-01 11:44:23 +020042enum export {
43 export_plain, export_unused, export_gpl,
44 export_unused_gpl, export_gpl_future, export_unknown
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080047/* In kernel, this size is defined in linux/module.h;
48 * here we use Elf_Addr instead of long for covering cross-compile
49 */
50
51#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
52
Andi Kleen6d9a89e2007-11-22 03:43:08 +010053#define PRINTF __attribute__ ((format (printf, 1, 2)))
54
55PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
59 fprintf(stderr, "FATAL: ");
60
61 va_start(arglist, fmt);
62 vfprintf(stderr, fmt, arglist);
63 va_end(arglist);
64
65 exit(1);
66}
67
Andi Kleen6d9a89e2007-11-22 03:43:08 +010068PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 va_list arglist;
71
72 fprintf(stderr, "WARNING: ");
73
74 va_start(arglist, fmt);
75 vfprintf(stderr, fmt, arglist);
76 va_end(arglist);
77}
78
Andi Kleen6d9a89e2007-11-22 03:43:08 +010079PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060080{
81 va_list arglist;
82
83 fprintf(stderr, "ERROR: ");
84
85 va_start(arglist, fmt);
86 vfprintf(stderr, fmt, arglist);
87 va_end(arglist);
88}
89
Rusty Russelld4ef1c32013-04-04 17:37:32 +103090static inline bool strends(const char *str, const char *postfix)
91{
92 if (strlen(str) < strlen(postfix))
93 return false;
94
95 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
96}
97
Sam Ravnborg040fcc82006-01-28 22:15:55 +010098static int is_vmlinux(const char *modname)
99{
100 const char *myname;
101
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100102 myname = strrchr(modname, '/');
103 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100104 myname++;
105 else
106 myname = modname;
107
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200108 return (strcmp(myname, "vmlinux") == 0) ||
109 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void *do_nofail(void *ptr, const char *expr)
113{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100114 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ptr;
118}
119
120/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct module *modules;
122
Masahiro Yamada8b185742018-05-09 18:50:40 +0900123static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct module *mod;
126
127 for (mod = modules; mod; mod = mod->next)
128 if (strcmp(mod->name, modname) == 0)
129 break;
130 return mod;
131}
132
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030133static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030136 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 mod = NOFAIL(malloc(sizeof(*mod)));
139 memset(mod, 0, sizeof(*mod));
140 p = NOFAIL(strdup(modname));
141
142 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030143 if (strends(p, ".o")) {
144 p[strlen(p) - 2] = '\0';
145 mod->is_dot_o = 1;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* add to list */
149 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200150 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 mod->next = modules;
152 modules = mod;
153
154 return mod;
155}
156
157/* A hash of all exported symbols,
158 * struct symbol is also used for lists of unresolved symbols */
159
160#define SYMBOL_HASH_SIZE 1024
161
162struct symbol {
163 struct symbol *next;
164 struct module *module;
165 unsigned int crc;
166 int crc_valid;
167 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100168 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
169 unsigned int kernel:1; /* 1 if symbol is from kernel
170 * (only for external modules) **/
Rusty Russellb6568b12013-11-07 12:09:13 +1030171 unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */
Denis Efremov15bfc232019-08-01 09:06:57 +0300172 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700173 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 char name[0];
175};
176
177static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
178
179/* This is based on the hash agorithm from gdbm, via tdb */
180static inline unsigned int tdb_hash(const char *name)
181{
182 unsigned value; /* Used to compute the hash value. */
183 unsigned i; /* Used to cycle through random values. */
184
185 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100186 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
188
189 return (1103515243 * value + 12345);
190}
191
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100192/**
193 * Allocate a new symbols for use in the hash of exported symbols or
194 * the list of unresolved symbols per module
195 **/
196static struct symbol *alloc_symbol(const char *name, unsigned int weak,
197 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
200
201 memset(s, 0, sizeof(*s));
202 strcpy(s->name, name);
203 s->weak = weak;
204 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300205 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return s;
207}
208
209/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700210static struct symbol *new_symbol(const char *name, struct module *module,
211 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 unsigned int hash;
214 struct symbol *new;
215
216 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
217 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
218 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700219 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100220 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100223static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct symbol *s;
226
227 /* For our purposes, .foo matches foo. PPC64 needs this. */
228 if (name[0] == '.')
229 name++;
230
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100231 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (strcmp(s->name, name) == 0)
233 return s;
234 }
235 return NULL;
236}
237
Mathias Krause7a3ee752014-08-27 20:28:53 +0930238static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700239 const char *str;
240 enum export export;
241} export_list[] = {
242 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200243 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700244 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200245 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700246 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
247 { .str = "(unknown)", .export = export_unknown },
248};
249
250
251static const char *export_str(enum export ex)
252{
253 return export_list[ex].str;
254}
255
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100256static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700257{
258 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100259
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200260 if (!s)
261 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700262 for (i = 0; export_list[i].export != export_unknown; i++) {
263 if (strcmp(export_list[i].str, s) == 0)
264 return export_list[i].export;
265 }
266 return export_unknown;
267}
268
Masahiro Yamada6124c042017-09-06 16:19:05 -0700269static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
270{
271 return (void *)elf->hdr +
272 elf->sechdrs[elf->secindex_strings].sh_offset +
273 sechdr->sh_name;
274}
275
276static const char *sec_name(struct elf_info *elf, int secindex)
277{
278 return sech_name(elf, &elf->sechdrs[secindex]);
279}
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200280
281#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
282
283static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
284{
285 const char *secname = sec_name(elf, sec);
286
287 if (strstarts(secname, "___ksymtab+"))
288 return export_plain;
289 else if (strstarts(secname, "___ksymtab_unused+"))
290 return export_unused;
291 else if (strstarts(secname, "___ksymtab_gpl+"))
292 return export_gpl;
293 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
294 return export_unused_gpl;
295 else if (strstarts(secname, "___ksymtab_gpl_future+"))
296 return export_gpl_future;
297 else
298 return export_unknown;
299}
300
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200301static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700302{
303 if (sec == elf->export_sec)
304 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200305 else if (sec == elf->export_unused_sec)
306 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700307 else if (sec == elf->export_gpl_sec)
308 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200309 else if (sec == elf->export_unused_gpl_sec)
310 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700311 else if (sec == elf->export_gpl_future_sec)
312 return export_gpl_future;
313 else
314 return export_unknown;
315}
316
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100317/**
318 * Add an exported symbol - it may have already been added without a
319 * CRC, in this case just update the CRC
320 **/
Ram Paibd5cbce2006-06-08 22:12:53 -0700321static struct symbol *sym_add_exported(const char *name, struct module *mod,
322 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct symbol *s = find_symbol(name);
325
326 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700327 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100328 } else {
329 if (!s->preloaded) {
Sam Ravnborg7b75b132006-03-05 13:48:58 +0100330 warn("%s: '%s' exported twice. Previous export "
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100331 "was in %s%s\n", mod->name, name,
332 s->module->name,
333 is_vmlinux(s->module->name) ?"":".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700334 } else {
Paul Bollebaec30e2014-04-16 18:05:35 +0200335 /* In case Module.symvers was out of date */
Trent Piepho4b219602007-10-11 16:40:10 -0700336 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100339 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100340 s->vmlinux = is_vmlinux(mod->name);
341 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700342 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100343 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100346static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700347 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100348{
349 struct symbol *s = find_symbol(name);
350
Rusty Russellb6568b12013-11-07 12:09:13 +1030351 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700352 s = new_symbol(name, mod, export);
Rusty Russellb6568b12013-11-07 12:09:13 +1030353 /* Don't complain when we find it later. */
354 s->preloaded = 1;
355 }
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100356 s->crc = crc;
357 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100360void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930363 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int fd;
365
366 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930367 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930369 if (fstat(fd, &st))
370 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 *size = st.st_size;
373 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930375failed:
376 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (map == MAP_FAILED)
378 return NULL;
379 return map;
380}
381
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100382/**
383 * Return a copy of the next line in a mmap'ed file.
384 * spaces in the beginning of the line is trimmed away.
385 * Return a pointer to a static buffer.
386 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100387char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 static char line[4096];
390 int skip = 1;
391 size_t len = 0;
392 signed char *p = (signed char *)file + *pos;
393 char *s = line;
394
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100395 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (skip && isspace(*p)) {
397 p++;
398 continue;
399 }
400 skip = 0;
401 if (*p != '\n' && (*pos < size)) {
402 len++;
403 *s++ = *p++;
404 if (len > 4095)
405 break; /* Too long, stop */
406 } else {
407 /* End of string */
408 *s = '\0';
409 return line;
410 }
411 }
412 /* End of buffer */
413 return NULL;
414}
415
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100416void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 munmap(file, size);
419}
420
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100421static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100424 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 Elf_Shdr *sechdrs;
426 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200427 const char *secstrings;
428 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 hdr = grab_file(filename, &info->size);
431 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930432 if (ignore_missing_files) {
433 fprintf(stderr, "%s: %s (ignored)\n", filename,
434 strerror(errno));
435 return 0;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200438 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100441 if (info->size < sizeof(*hdr)) {
442 /* file too small, assume this is an empty .o file */
443 return 0;
444 }
445 /* Is this a valid ELF file? */
446 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
447 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
448 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
449 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
450 /* Not an ELF file - silently ignore it */
451 return 0;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200454 hdr->e_type = TO_NATIVE(hdr->e_type);
455 hdr->e_machine = TO_NATIVE(hdr->e_machine);
456 hdr->e_version = TO_NATIVE(hdr->e_version);
457 hdr->e_entry = TO_NATIVE(hdr->e_entry);
458 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
459 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
460 hdr->e_flags = TO_NATIVE(hdr->e_flags);
461 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
462 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
463 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
464 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
465 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
466 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 sechdrs = (void *)hdr + hdr->e_shoff;
468 info->sechdrs = sechdrs;
469
Petr Stetiara83710e2007-08-27 12:15:07 +0200470 /* Check if file offset is correct */
471 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100472 fatal("section header offset=%lu in file '%s' is bigger than "
473 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
474 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200475 return 0;
476 }
477
Anders Kaseorg68457562011-05-19 16:55:27 -0600478 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200479 /*
480 * There are more than 64k sections,
481 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200482 */
483 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
484 }
485 else {
486 info->num_sections = hdr->e_shnum;
487 }
488 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600489 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200490 }
491 else {
492 info->secindex_strings = hdr->e_shstrndx;
493 }
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200496 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200497 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
498 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
499 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
500 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
501 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
502 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
503 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
504 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
505 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
506 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200509 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
510 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700511 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900512 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Tejun Heo56fc82c2009-02-06 00:48:02 +0900514 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100515 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
516 "sizeof(*hrd)=%zu\n", filename,
517 (unsigned long)sechdrs[i].sh_offset,
518 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100519 return 0;
520 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700521 secname = secstrings + sechdrs[i].sh_name;
522 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900523 if (nobits)
524 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
526 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700527 } else if (strcmp(secname, "__ksymtab") == 0)
528 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200529 else if (strcmp(secname, "__ksymtab_unused") == 0)
530 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700531 else if (strcmp(secname, "__ksymtab_gpl") == 0)
532 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200533 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
534 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700535 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
536 info->export_gpl_future_sec = i;
537
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200538 if (sechdrs[i].sh_type == SHT_SYMTAB) {
539 unsigned int sh_link_idx;
540 symtab_idx = i;
541 info->symtab_start = (void *)hdr +
542 sechdrs[i].sh_offset;
543 info->symtab_stop = (void *)hdr +
544 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600545 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200546 info->strtab = (void *)hdr +
547 sechdrs[sh_link_idx].sh_offset;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200550 /* 32bit section no. table? ("more than 64k sections") */
551 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
552 symtab_shndx_idx = i;
553 info->symtab_shndx_start = (void *)hdr +
554 sechdrs[i].sh_offset;
555 info->symtab_shndx_stop = (void *)hdr +
556 sechdrs[i].sh_offset + sechdrs[i].sh_size;
557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100559 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100560 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Fix endianness in symbols */
563 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
564 sym->st_shndx = TO_NATIVE(sym->st_shndx);
565 sym->st_name = TO_NATIVE(sym->st_name);
566 sym->st_value = TO_NATIVE(sym->st_value);
567 sym->st_size = TO_NATIVE(sym->st_size);
568 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200569
570 if (symtab_shndx_idx != ~0U) {
571 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600572 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200573 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600574 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200575 symtab_idx);
576 /* Fix endianness */
577 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
578 p++)
579 *p = TO_NATIVE(*p);
580 }
581
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100582 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100585static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 release_file(info->hdr, info->size);
588}
589
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200590static int ignore_undef_symbol(struct elf_info *info, const char *symname)
591{
592 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900593 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200594 return 1;
595 /* ignore global offset table */
596 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
597 return 1;
598 if (info->hdr->e_machine == EM_PPC)
599 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900600 if (strstarts(symname, "_restgpr_") ||
601 strstarts(symname, "_savegpr_") ||
602 strstarts(symname, "_rest32gpr_") ||
603 strstarts(symname, "_save32gpr_") ||
604 strstarts(symname, "_restvr_") ||
605 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200606 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000607 if (info->hdr->e_machine == EM_PPC64)
608 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900609 if (strstarts(symname, "_restgpr0_") ||
610 strstarts(symname, "_savegpr0_") ||
611 strstarts(symname, "_restvr_") ||
612 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100613 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000614 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200615 /* Do not ignore this symbol */
616 return 0;
617}
618
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100619static void handle_modversions(struct module *mod, struct elf_info *info,
620 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200623 enum export export;
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100624 bool is_crc = false;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200625
Frank Rowand258f7422012-04-09 17:59:03 -0700626 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900627 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200628 export = export_from_secname(info, get_secindex(info, sym));
629 else
630 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Andi Kleenb5064652013-11-12 15:08:38 -0800632 /* CRC'd symbol */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900633 if (strstarts(symname, "__crc_")) {
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100634 is_crc = true;
Andi Kleenb5064652013-11-12 15:08:38 -0800635 crc = (unsigned int) sym->st_value;
Ard Biesheuvel56067812017-02-03 09:54:05 +0000636 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
637 unsigned int *crcp;
638
639 /* symbol points to the CRC in the ELF object */
640 crcp = (void *)info->hdr + sym->st_value +
641 info->sechdrs[sym->st_shndx].sh_offset -
642 (info->hdr->e_type != ET_REL ?
643 info->sechdrs[sym->st_shndx].sh_addr : 0);
Fredrik Noring54a71512019-03-27 19:12:50 +0100644 crc = TO_NATIVE(*crcp);
Ard Biesheuvel56067812017-02-03 09:54:05 +0000645 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900646 sym_update_crc(symname + strlen("__crc_"), mod, crc,
Andi Kleenb5064652013-11-12 15:08:38 -0800647 export);
648 }
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 switch (sym->st_shndx) {
651 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900652 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100653 /* Should warn here, but modpost runs before the linker */
654 } else
655 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 case SHN_UNDEF:
658 /* undefined symbol */
659 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
660 ELF_ST_BIND(sym->st_info) != STB_WEAK)
661 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200662 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 break;
Ben Colline8d529012005-08-19 13:44:57 -0700664/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
665#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
666/* add compatibility with older glibc */
667#ifndef STT_SPARC_REGISTER
668#define STT_SPARC_REGISTER STT_REGISTER
669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (info->hdr->e_machine == EM_SPARC ||
671 info->hdr->e_machine == EM_SPARCV9) {
672 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700673 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100675 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700676 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100677 munged[0] = '_';
678 munged[1] = toupper(munged[1]);
679 symname = munged;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100683
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100684 if (is_crc) {
685 const char *e = is_vmlinux(mod->name) ?"":".ko";
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900686 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
687 symname + strlen("__crc_"), mod->name, e);
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100688 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030689 mod->unres = alloc_symbol(symname,
690 ELF_ST_BIND(sym->st_info) == STB_WEAK,
691 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 break;
693 default:
694 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900695 if (strstarts(symname, "__ksymtab_")) {
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900696 sym_add_exported(symname + strlen("__ksymtab_"), mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700697 export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900699 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900701 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 mod->has_cleanup = 1;
703 break;
704 }
705}
706
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100707/**
708 * Parse tag=value strings from .modinfo section
709 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710static char *next_string(char *string, unsigned long *secsize)
711{
712 /* Skip non-zero chars */
713 while (string[0]) {
714 string++;
715 if ((*secsize)-- <= 1)
716 return NULL;
717 }
718
719 /* Skip any zero padding. */
720 while (!string[0]) {
721 string++;
722 if ((*secsize)-- <= 1)
723 return NULL;
724 }
725 return string;
726}
727
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900728static char *get_next_modinfo(struct elf_info *info, const char *tag,
729 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 char *p;
732 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900733 char *modinfo = info->modinfo;
734 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900736 if (prev) {
737 size -= prev - modinfo;
738 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200739 }
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 for (p = modinfo; p; p = next_string(p, &size)) {
742 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
743 return p + taglen + 1;
744 }
745 return NULL;
746}
747
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900748static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200749
750{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900751 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200752}
753
Sam Ravnborg93684d32006-02-19 11:53:35 +0100754/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100755 * Test if string s ends in string sub
756 * return 0 if match
757 **/
758static int strrcmp(const char *s, const char *sub)
759{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100760 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100761
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100762 if (!s || !sub)
763 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100764
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100765 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100766 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100767
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100768 if ((slen == 0) || (sublen == 0))
769 return 1;
770
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100771 if (sublen > slen)
772 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100773
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100774 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100775}
776
Sam Ravnborgff13f922008-01-23 19:54:27 +0100777static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
778{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100779 if (sym)
780 return elf->strtab + sym->st_name;
781 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100782 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100783}
784
Sam Ravnborg10668222008-01-13 22:21:31 +0100785/* The pattern is an array of simple patterns.
786 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100787 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100788 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930789 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100790 */
Trevor Keith5c725132009-09-22 16:43:38 -0700791static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100792{
793 const char *p;
794 while (*pat) {
795 p = *pat++;
796 const char *endp = p + strlen(p) - 1;
797
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930798 /* "*foo*" */
799 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300800 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
801 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930802
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930803 free(bare);
804 if (here != NULL)
805 return 1;
806 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100807 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930808 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100809 if (strrcmp(sym, p + 1) == 0)
810 return 1;
811 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100812 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100813 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100814 if (strncmp(sym, p, strlen(p) - 1) == 0)
815 return 1;
816 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100817 /* no wildcards */
818 else {
819 if (strcmp(p, sym) == 0)
820 return 1;
821 }
822 }
823 /* no match */
824 return 0;
825}
826
Sam Ravnborg10668222008-01-13 22:21:31 +0100827/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930828static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200829{
830 ".comment*",
831 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930832 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800833 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000834 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200835 ".mdebug*", /* alpha, score, mips etc. */
836 ".pdr", /* alpha, score, mips etc. */
837 ".stab*",
838 ".note*",
839 ".got*",
840 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400841 ".xt.prop", /* xtensa */
842 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030843 ".arcextmap*", /* arc */
844 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030845 ".cmem*", /* EZchip */
846 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100847 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600848 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200849 NULL
850};
Sam Ravnborg10668222008-01-13 22:21:31 +0100851
Sam Ravnborge241a632008-01-28 20:13:13 +0100852/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400853 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100854 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400855 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100856 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400857static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900858 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100859{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400860 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100861
Anders Kaseorgb614a692009-04-23 16:49:33 -0400862 if (sechdr->sh_type == SHT_PROGBITS &&
863 !(sechdr->sh_flags & SHF_ALLOC) &&
864 !match(sec, section_white_list)) {
865 warn("%s (%s): unexpected non-allocatable section.\n"
866 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
867 "Note that for example <linux/init.h> contains\n"
868 "section definitions for use in .S files.\n\n",
869 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100870 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100871}
872
873
874
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100875#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930876 ".init.setup", ".init.rodata", ".meminit.rodata", \
877 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100878#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930879 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100880
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100881#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930882 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100883#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930884 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100885
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200886#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930887 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
888 ".pci_fixup_enable", ".pci_fixup_resume", \
889 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200890
Paul Gortmakere24f6622013-06-19 19:30:48 -0400891#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
892#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100893
894#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
895#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100896
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930897#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930898#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700899 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930900#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400901 ".fixup", ".entry.text", ".exception.text", ".text.*", \
902 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100903
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000904#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000905#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100906
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000907#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000908#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100909
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930910#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
911 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
912
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100913/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930914static const char *const init_data_sections[] =
915 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100916
917/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930918static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100919
920/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930921static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100922 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100923
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930924/* all text sections */
925static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
926
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100927/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930928static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100929
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100930
931/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100932#define DEFAULT_SYMBOL_WHITE_LIST \
933 "*driver", \
934 "*_template", /* scsi uses *_template a lot */ \
935 "*_timer", /* arm uses ops structures named _timer a lot */ \
936 "*_sht", /* scsi also used *_sht to some extent */ \
937 "*_ops", \
938 "*_probe", \
939 "*_probe_one", \
940 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100941
Mathias Krause7a3ee752014-08-27 20:28:53 +0930942static const char *const head_sections[] = { ".head.text*", NULL };
943static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100944 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930945static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100946
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100947enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100948 TEXT_TO_ANY_INIT,
949 DATA_TO_ANY_INIT,
950 TEXT_TO_ANY_EXIT,
951 DATA_TO_ANY_EXIT,
952 XXXINIT_TO_SOME_INIT,
953 XXXEXIT_TO_SOME_EXIT,
954 ANY_INIT_TO_ANY_EXIT,
955 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100956 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930957 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100958};
959
Quentin Casasnovase5d8f592015-04-13 20:55:15 +0930960/**
961 * Describe how to match sections on different criterias:
962 *
963 * @fromsec: Array of sections to be matched.
964 *
965 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
966 * this array is forbidden (black-list). Can be empty.
967 *
968 * @good_tosec: Relocations applied to a section in @fromsec must be
969 * targetting sections in this array (white-list). Can be empty.
970 *
971 * @mismatch: Type of mismatch.
972 *
973 * @symbol_white_list: Do not match a relocation to a symbol in this list
974 * even if it is targetting a section in @bad_to_sec.
975 *
976 * @handler: Specific handler to call when a match is found. If NULL,
977 * default_mismatch_handler() will be called.
978 *
979 */
Sam Ravnborg10668222008-01-13 22:21:31 +0100980struct sectioncheck {
981 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +0930982 const char *bad_tosec[20];
983 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100984 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100985 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +0930986 void (*handler)(const char *modname, struct elf_info *elf,
987 const struct sectioncheck* const mismatch,
988 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
989
Sam Ravnborg10668222008-01-13 22:21:31 +0100990};
991
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930992static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
993 const struct sectioncheck* const mismatch,
994 Elf_Rela *r, Elf_Sym *sym,
995 const char *fromsec);
996
Mathias Krause7a3ee752014-08-27 20:28:53 +0930997static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +0100998/* Do not reference init/exit code/data from
999 * normal code and data
1000 */
1001{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001002 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301003 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001004 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001005 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001006},
1007{
1008 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301009 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001010 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001011 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001012},
1013{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001014 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301015 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001016 .mismatch = DATA_TO_ANY_INIT,
1017 .symbol_white_list = {
1018 "*_template", "*_timer", "*_sht", "*_ops",
1019 "*_probe", "*_probe_one", "*_console", NULL
1020 },
1021},
1022{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001023 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301024 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001025 .mismatch = TEXT_TO_ANY_EXIT,
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{
1029 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301030 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001031 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001032 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001033},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001034/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001035{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001036 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301037 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001038 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001039 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001040},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001041/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001042{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001043 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301044 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001045 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001046 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001047},
1048/* Do not use exit code/data from init code */
1049{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001050 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301051 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001052 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001053 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001054},
1055/* Do not use init code/data from exit code */
1056{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001057 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301058 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001059 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001060 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001061},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001062{
1063 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301064 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001065 .mismatch = ANY_INIT_TO_ANY_EXIT,
1066 .symbol_white_list = { NULL },
1067},
Sam Ravnborg10668222008-01-13 22:21:31 +01001068/* Do not export init/exit functions or data */
1069{
1070 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301071 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001072 .mismatch = EXPORT_TO_INIT_EXIT,
1073 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301074},
1075{
1076 .fromsec = { "__ex_table", NULL },
1077 /* If you're adding any new black-listed sections in here, consider
1078 * adding a special 'printer' for them in scripts/check_extable.
1079 */
1080 .bad_tosec = { ".altinstr_replacement", NULL },
1081 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1082 .mismatch = EXTABLE_TO_NON_TEXT,
1083 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001084}
1085};
1086
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001087static const struct sectioncheck *section_mismatch(
1088 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001089{
1090 int i;
1091 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1092 const struct sectioncheck *check = &sectioncheck[0];
1093
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301094 /*
1095 * The target section could be the SHT_NUL section when we're
1096 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001097 * doesn't make much sense and causes build failures on parisc
1098 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301099 */
1100 if (*tosec == '\0')
1101 return NULL;
1102
Sam Ravnborg10668222008-01-13 22:21:31 +01001103 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301104 if (match(fromsec, check->fromsec)) {
1105 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1106 return check;
1107 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1108 return check;
1109 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001110 check++;
1111 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001112 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001113}
1114
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001115/**
1116 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001117 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001118 * Pattern 1:
1119 * If a module parameter is declared __initdata and permissions=0
1120 * then this is legal despite the warning generated.
1121 * We cannot see value of permissions here, so just ignore
1122 * this pattern.
1123 * The pattern is identified by:
1124 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001125 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001126 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001127 *
Rusty Russell6a841522010-08-11 23:04:16 -06001128 * Pattern 1a:
1129 * module_param_call() ops can refer to __init set function if permissions=0
1130 * The pattern is identified by:
1131 * tosec = .init.text
1132 * fromsec = .data*
1133 * atsym = __param_ops_*
1134 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001135 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001136 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001137 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001138 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001139 * tosec = init or exit section
1140 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001141 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1142 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001143 *
1144 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001145 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001146 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001147 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001148 * Some symbols belong to init section but still it is ok to reference
1149 * these from non-init sections as these symbols don't have any memory
1150 * allocated for them and symbol address and value are same. So even
1151 * if init section is freed, its ok to reference those symbols.
1152 * For ex. symbols marking the init section boundaries.
1153 * This pattern is identified by
1154 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001155 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301156 * Pattern 5:
1157 * GCC may optimize static inlines when fed constant arg(s) resulting
1158 * in functions like cpumask_empty() -- generating an associated symbol
1159 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1160 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1161 * meaningless section warning. May need to add isra symbols too...
1162 * This pattern is identified by
1163 * tosec = init section
1164 * fromsec = text section
1165 * refsymname = *.constprop.*
1166 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001167 * Pattern 6:
1168 * Hide section mismatch warnings for ELF local symbols. The goal
1169 * is to eliminate false positive modpost warnings caused by
1170 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1171 * Autogenerated symbol names bypass modpost's "Pattern 2"
1172 * whitelisting, which relies on pattern-matching against symbol
1173 * names to work. (One situation where gcc can autogenerate ELF
1174 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001175 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001176static int secref_whitelist(const struct sectioncheck *mismatch,
1177 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001178 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001179{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001180 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001181 if (match(tosec, init_data_sections) &&
1182 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001183 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001184 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001185
Rusty Russell6a841522010-08-11 23:04:16 -06001186 /* Check for pattern 1a */
1187 if (strcmp(tosec, ".init.text") == 0 &&
1188 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001189 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001190 return 0;
1191
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001192 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001193 if (match(tosec, init_exit_sections) &&
1194 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001195 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001196 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001197
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001198 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001199 if (match(fromsec, head_sections) &&
1200 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001201 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001202
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001203 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001204 if (match(tosym, linker_symbols))
1205 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001206
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301207 /* Check for pattern 5 */
1208 if (match(fromsec, text_sections) &&
1209 match(tosec, init_sections) &&
1210 match(fromsym, optim_symbols))
1211 return 0;
1212
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001213 /* Check for pattern 6 */
1214 if (strstarts(fromsym, ".L"))
1215 return 0;
1216
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001217 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001218}
1219
Sami Tolvanen5818c682018-10-23 15:15:35 -07001220static inline int is_arm_mapping_symbol(const char *str)
1221{
1222 return str[0] == '$' && strchr("axtd", str[1])
1223 && (str[2] == '\0' || str[2] == '.');
1224}
1225
1226/*
1227 * If there's no name there, ignore it; likewise, ignore it if it's
1228 * one of the magic symbols emitted used by current ARM tools.
1229 *
1230 * Otherwise if find_symbols_between() returns those symbols, they'll
1231 * fail the whitelist tests and cause lots of false alarms ... fixable
1232 * only by merging __exit and __init sections into __text, bloating
1233 * the kernel (which is especially evil on embedded platforms).
1234 */
1235static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1236{
1237 const char *name = elf->strtab + sym->st_name;
1238
1239 if (!name || !strlen(name))
1240 return 0;
1241 return !is_arm_mapping_symbol(name);
1242}
1243
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001244/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001245 * Find symbol based on relocation record info.
1246 * In some cases the symbol supplied is a valid symbol so
1247 * return refsym. If st_name != 0 we assume this is a valid symbol.
1248 * In other cases the symbol needs to be looked up in the symbol table
1249 * based on section and address.
1250 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001251static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001252 Elf_Sym *relsym)
1253{
1254 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001255 Elf_Sym *near = NULL;
1256 Elf64_Sword distance = 20;
1257 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001258 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001259
1260 if (relsym->st_name != 0)
1261 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001262
1263 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001264 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001265 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001266 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001267 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1268 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001269 if (!is_valid_name(elf, sym))
1270 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001271 if (sym->st_value == addr)
1272 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001273 /* Find a symbol nearby - addr are maybe negative */
1274 d = sym->st_value - addr;
1275 if (d < 0)
1276 d = addr - sym->st_value;
1277 if (d < distance) {
1278 distance = d;
1279 near = sym;
1280 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001281 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001282 /* We need a close match */
1283 if (distance < 20)
1284 return near;
1285 else
1286 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001287}
1288
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001289/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001290 * Find symbols before or equal addr and after addr - in the section sec.
1291 * If we find two symbols with equal offset prefer one with a valid name.
1292 * The ELF format may have a better way to detect what type of symbol
1293 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001294 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001295static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1296 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001297{
1298 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001299 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001300 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001301
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001302 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1303 const char *symsec;
1304
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001305 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001306 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001307 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001308 if (strcmp(symsec, sec) != 0)
1309 continue;
David Brownellda68d612007-02-20 13:58:16 -08001310 if (!is_valid_name(elf, sym))
1311 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001312 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001313 if ((addr - sym->st_value) < distance) {
1314 distance = addr - sym->st_value;
1315 near = sym;
1316 } else if ((addr - sym->st_value) == distance) {
1317 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001318 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001319 }
1320 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001321 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001322}
1323
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001324/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001325 * Convert a section name to the function/data attribute
1326 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001327 * .memexitconst => __memconst
1328 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001329 *
1330 * The memory of returned value has been allocated on a heap. The user of this
1331 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001332*/
1333static char *sec2annotation(const char *s)
1334{
1335 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001336 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001337 char *r = p;
1338
1339 *p++ = '_';
1340 *p++ = '_';
1341 if (*s == '.')
1342 s++;
1343 while (*s && *s != '.')
1344 *p++ = *s++;
1345 *p = '\0';
1346 if (*s == '.')
1347 s++;
1348 if (strstr(s, "rodata") != NULL)
1349 strcat(p, "const ");
1350 else if (strstr(s, "data") != NULL)
1351 strcat(p, "data ");
1352 else
1353 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001354 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001355 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001356 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001357 }
1358}
1359
1360static int is_function(Elf_Sym *sym)
1361{
1362 if (sym)
1363 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1364 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001365 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001366}
1367
Randy Dunlap00759c02011-03-15 14:13:47 -07001368static void print_section_list(const char * const list[20])
1369{
1370 const char *const *s = list;
1371
1372 while (*s) {
1373 fprintf(stderr, "%s", *s);
1374 s++;
1375 if (*s)
1376 fprintf(stderr, ", ");
1377 }
1378 fprintf(stderr, "\n");
1379}
1380
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301381static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1382{
1383 switch (is_func) {
1384 case 0: *name = "variable"; *name_p = ""; break;
1385 case 1: *name = "function"; *name_p = "()"; break;
1386 default: *name = "(unknown reference)"; *name_p = ""; break;
1387 }
1388}
1389
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001390/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001391 * Print a warning about a section mismatch.
1392 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001393 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001394 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001395static void report_sec_mismatch(const char *modname,
1396 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001397 const char *fromsec,
1398 unsigned long long fromaddr,
1399 const char *fromsym,
1400 int from_is_func,
1401 const char *tosec, const char *tosym,
1402 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001403{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001404 const char *from, *from_p;
1405 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001406 char *prl_from;
1407 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001408
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001409 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001410
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301411 get_pretty_name(from_is_func, &from, &from_p);
1412 get_pretty_name(to_is_func, &to, &to_p);
1413
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001414 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1415 "to the %s %s:%s%s\n",
1416 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1417 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001418
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001419 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001420 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001421 prl_from = sec2annotation(fromsec);
1422 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001423 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001424 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001425 "the %s %s%s%s.\n"
1426 "This is often because %s lacks a %s\n"
1427 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001428 prl_from, fromsym,
1429 to, prl_to, tosym, to_p,
1430 fromsym, prl_to, tosym);
1431 free(prl_from);
1432 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001433 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001434 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001435 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001436 fprintf(stderr,
1437 "The variable %s references\n"
1438 "the %s %s%s%s\n"
1439 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001440 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001441 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001442 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001443 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001444 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001445 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001446 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001447 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001448 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001449 fprintf(stderr,
1450 "The function %s() references a %s in an exit section.\n"
1451 "Often the %s %s%s has valid usage outside the exit section\n"
1452 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001453 fromsym, to, to, tosym, to_p, prl_to, tosym);
1454 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001455 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001456 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001457 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001458 fprintf(stderr,
1459 "The variable %s references\n"
1460 "the %s %s%s%s\n"
1461 "If the reference is valid then annotate the\n"
1462 "variable with __exit* (see linux/init.h) or "
1463 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001464 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001465 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001466 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001467 break;
1468 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001469 case XXXINIT_TO_SOME_INIT:
1470 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001471 prl_from = sec2annotation(fromsec);
1472 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001473 fprintf(stderr,
1474 "The %s %s%s%s references\n"
1475 "a %s %s%s%s.\n"
1476 "If %s is only used by %s then\n"
1477 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001478 from, prl_from, fromsym, from_p,
1479 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001480 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001481 free(prl_from);
1482 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001483 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001484 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001485 prl_from = sec2annotation(fromsec);
1486 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001487 fprintf(stderr,
1488 "The %s %s%s%s references\n"
1489 "a %s %s%s%s.\n"
1490 "This is often seen when error handling "
1491 "in the init function\n"
1492 "uses functionality in the exit path.\n"
1493 "The fix is often to remove the %sannotation of\n"
1494 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001495 from, prl_from, fromsym, from_p,
1496 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001497 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001498 free(prl_from);
1499 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001500 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001501 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001502 prl_from = sec2annotation(fromsec);
1503 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001504 fprintf(stderr,
1505 "The %s %s%s%s references\n"
1506 "a %s %s%s%s.\n"
1507 "This is often seen when error handling "
1508 "in the exit function\n"
1509 "uses functionality in the init path.\n"
1510 "The fix is often to remove the %sannotation of\n"
1511 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001512 from, prl_from, fromsym, from_p,
1513 to, prl_to, tosym, to_p,
1514 prl_to, tosym, to_p);
1515 free(prl_from);
1516 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001517 break;
1518 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001519 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001520 fprintf(stderr,
1521 "The symbol %s is exported and annotated %s\n"
1522 "Fix this by removing the %sannotation of %s "
1523 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001524 tosym, prl_to, prl_to, tosym);
1525 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001526 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301527 case EXTABLE_TO_NON_TEXT:
1528 fatal("There's a special handler for this mismatch type, "
1529 "we should never get here.");
1530 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001531 }
1532 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001533}
1534
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301535static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1536 const struct sectioncheck* const mismatch,
1537 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1538{
1539 const char *tosec;
1540 Elf_Sym *to;
1541 Elf_Sym *from;
1542 const char *tosym;
1543 const char *fromsym;
1544
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301545 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1546 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301547
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001548 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301549 return;
1550
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301551 tosec = sec_name(elf, get_secindex(elf, sym));
1552 to = find_elf_symbol(elf, r->r_addend, sym);
1553 tosym = sym_name(elf, to);
1554
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301555 /* check whitelist - we may ignore it */
1556 if (secref_whitelist(mismatch,
1557 fromsec, fromsym, tosec, tosym)) {
1558 report_sec_mismatch(modname, mismatch,
1559 fromsec, r->r_offset, fromsym,
1560 is_function(from), tosec, tosym,
1561 is_function(to));
1562 }
1563}
1564
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301565static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1566{
1567 if (section_index > elf->num_sections)
1568 fatal("section_index is outside elf->num_sections!\n");
1569
1570 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1571}
1572
1573/*
1574 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1575 * to know the sizeof(struct exception_table_entry) for the target architecture.
1576 */
1577static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301578static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301579{
1580 /*
1581 * If we're currently checking the second relocation within __ex_table,
1582 * that relocation offset tells us the offsetof(struct
1583 * exception_table_entry, fixup) which is equal to sizeof(struct
1584 * exception_table_entry) divided by two. We use that to our advantage
1585 * since there's no portable way to get that size as every architecture
1586 * seems to go with different sized types. Not pretty but better than
1587 * hard-coding the size for every architecture..
1588 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301589 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301590 extable_entry_size = r->r_offset * 2;
1591}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301592
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301593static inline bool is_extable_fault_address(Elf_Rela *r)
1594{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301595 /*
1596 * extable_entry_size is only discovered after we've handled the
1597 * _second_ relocation in __ex_table, so only abort when we're not
1598 * handling the first reloc and extable_entry_size is zero.
1599 */
1600 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301601 fatal("extable_entry size hasn't been discovered!\n");
1602
1603 return ((r->r_offset == 0) ||
1604 (r->r_offset % extable_entry_size == 0));
1605}
1606
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301607#define is_second_extable_reloc(Start, Cur, Sec) \
1608 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1609
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301610static void report_extable_warnings(const char* modname, struct elf_info* elf,
1611 const struct sectioncheck* const mismatch,
1612 Elf_Rela* r, Elf_Sym* sym,
1613 const char* fromsec, const char* tosec)
1614{
1615 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1616 const char* fromsym_name = sym_name(elf, fromsym);
1617 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1618 const char* tosym_name = sym_name(elf, tosym);
1619 const char* from_pretty_name;
1620 const char* from_pretty_name_p;
1621 const char* to_pretty_name;
1622 const char* to_pretty_name_p;
1623
1624 get_pretty_name(is_function(fromsym),
1625 &from_pretty_name, &from_pretty_name_p);
1626 get_pretty_name(is_function(tosym),
1627 &to_pretty_name, &to_pretty_name_p);
1628
1629 warn("%s(%s+0x%lx): Section mismatch in reference"
1630 " from the %s %s%s to the %s %s:%s%s\n",
1631 modname, fromsec, (long)r->r_offset, from_pretty_name,
1632 fromsym_name, from_pretty_name_p,
1633 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1634
1635 if (!match(tosec, mismatch->bad_tosec) &&
1636 is_executable_section(elf, get_secindex(elf, sym)))
1637 fprintf(stderr,
1638 "The relocation at %s+0x%lx references\n"
1639 "section \"%s\" which is not in the list of\n"
1640 "authorized sections. If you're adding a new section\n"
1641 "and/or if this reference is valid, add \"%s\" to the\n"
1642 "list of authorized sections to jump to on fault.\n"
1643 "This can be achieved by adding \"%s\" to \n"
1644 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1645 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1646}
1647
1648static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1649 const struct sectioncheck* const mismatch,
1650 Elf_Rela* r, Elf_Sym* sym,
1651 const char *fromsec)
1652{
1653 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1654
1655 sec_mismatch_count++;
1656
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001657 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301658
1659 if (match(tosec, mismatch->bad_tosec))
1660 fatal("The relocation at %s+0x%lx references\n"
1661 "section \"%s\" which is black-listed.\n"
1662 "Something is seriously wrong and should be fixed.\n"
1663 "You might get more information about where this is\n"
1664 "coming from by using scripts/check_extable.sh %s\n",
1665 fromsec, (long)r->r_offset, tosec, modname);
1666 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1667 if (is_extable_fault_address(r))
1668 fatal("The relocation at %s+0x%lx references\n"
1669 "section \"%s\" which is not executable, IOW\n"
1670 "it is not possible for the kernel to fault\n"
1671 "at that address. Something is seriously wrong\n"
1672 "and should be fixed.\n",
1673 fromsec, (long)r->r_offset, tosec);
1674 else
1675 fatal("The relocation at %s+0x%lx references\n"
1676 "section \"%s\" which is not executable, IOW\n"
1677 "the kernel will fault if it ever tries to\n"
1678 "jump to it. Something is seriously wrong\n"
1679 "and should be fixed.\n",
1680 fromsec, (long)r->r_offset, tosec);
1681 }
1682}
1683
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001684static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001685 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001686{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001687 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301688 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001689
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001690 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301691 if (mismatch->handler)
1692 mismatch->handler(modname, elf, mismatch,
1693 r, sym, fromsec);
1694 else
1695 default_mismatch_handler(modname, elf, mismatch,
1696 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001697 }
1698}
1699
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001700static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001701 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001702{
1703 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001704 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001705
1706 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001707 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001708}
1709
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001710static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001711{
1712 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001713 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001714
1715 switch (r_typ) {
1716 case R_386_32:
1717 r->r_addend = TO_NATIVE(*location);
1718 break;
1719 case R_386_PC32:
1720 r->r_addend = TO_NATIVE(*location) + 4;
1721 /* For CONFIG_RELOCATABLE=y */
1722 if (elf->hdr->e_type == ET_EXEC)
1723 r->r_addend += r->r_offset;
1724 break;
1725 }
1726 return 0;
1727}
1728
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001729#ifndef R_ARM_CALL
1730#define R_ARM_CALL 28
1731#endif
1732#ifndef R_ARM_JUMP24
1733#define R_ARM_JUMP24 29
1734#endif
1735
David A. Longc9698e52014-02-14 22:41:18 +01001736#ifndef R_ARM_THM_CALL
1737#define R_ARM_THM_CALL 10
1738#endif
1739#ifndef R_ARM_THM_JUMP24
1740#define R_ARM_THM_JUMP24 30
1741#endif
1742#ifndef R_ARM_THM_JUMP19
1743#define R_ARM_THM_JUMP19 51
1744#endif
1745
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001746static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001747{
1748 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1749
1750 switch (r_typ) {
1751 case R_ARM_ABS32:
1752 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001753 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001754 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001755 break;
1756 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001757 case R_ARM_CALL:
1758 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001759 case R_ARM_THM_CALL:
1760 case R_ARM_THM_JUMP24:
1761 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001762 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001763 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001764 sechdr->sh_offset +
1765 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001766 break;
1767 default:
1768 return 1;
1769 }
1770 return 0;
1771}
1772
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001773static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001774{
1775 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001776 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001777 unsigned int inst;
1778
1779 if (r_typ == R_MIPS_HI16)
1780 return 1; /* skip this */
1781 inst = TO_NATIVE(*location);
1782 switch (r_typ) {
1783 case R_MIPS_LO16:
1784 r->r_addend = inst & 0xffff;
1785 break;
1786 case R_MIPS_26:
1787 r->r_addend = (inst & 0x03ffffff) << 2;
1788 break;
1789 case R_MIPS_32:
1790 r->r_addend = inst;
1791 break;
1792 }
1793 return 0;
1794}
1795
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001796static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001797 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001798{
1799 Elf_Sym *sym;
1800 Elf_Rela *rela;
1801 Elf_Rela r;
1802 unsigned int r_sym;
1803 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001804
Sam Ravnborgff13f922008-01-23 19:54:27 +01001805 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001806 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1807
Sam Ravnborgff13f922008-01-23 19:54:27 +01001808 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001809 fromsec += strlen(".rela");
1810 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001811 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001812 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001813
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001814 for (rela = start; rela < stop; rela++) {
1815 r.r_offset = TO_NATIVE(rela->r_offset);
1816#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001817 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001818 unsigned int r_typ;
1819 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1820 r_sym = TO_NATIVE(r_sym);
1821 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1822 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1823 } else {
1824 r.r_info = TO_NATIVE(rela->r_info);
1825 r_sym = ELF_R_SYM(r.r_info);
1826 }
1827#else
1828 r.r_info = TO_NATIVE(rela->r_info);
1829 r_sym = ELF_R_SYM(r.r_info);
1830#endif
1831 r.r_addend = TO_NATIVE(rela->r_addend);
1832 sym = elf->symtab_start + r_sym;
1833 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001834 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001835 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301836 if (is_second_extable_reloc(start, rela, fromsec))
1837 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001838 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001839 }
1840}
1841
1842static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001843 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001844{
1845 Elf_Sym *sym;
1846 Elf_Rel *rel;
1847 Elf_Rela r;
1848 unsigned int r_sym;
1849 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001850
Sam Ravnborgff13f922008-01-23 19:54:27 +01001851 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001852 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1853
Sam Ravnborgff13f922008-01-23 19:54:27 +01001854 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001855 fromsec += strlen(".rel");
1856 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001857 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858 return;
1859
1860 for (rel = start; rel < stop; rel++) {
1861 r.r_offset = TO_NATIVE(rel->r_offset);
1862#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001863 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001864 unsigned int r_typ;
1865 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1866 r_sym = TO_NATIVE(r_sym);
1867 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1868 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1869 } else {
1870 r.r_info = TO_NATIVE(rel->r_info);
1871 r_sym = ELF_R_SYM(r.r_info);
1872 }
1873#else
1874 r.r_info = TO_NATIVE(rel->r_info);
1875 r_sym = ELF_R_SYM(r.r_info);
1876#endif
1877 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001878 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001879 case EM_386:
1880 if (addend_386_rel(elf, sechdr, &r))
1881 continue;
1882 break;
1883 case EM_ARM:
1884 if (addend_arm_rel(elf, sechdr, &r))
1885 continue;
1886 break;
1887 case EM_MIPS:
1888 if (addend_mips_rel(elf, sechdr, &r))
1889 continue;
1890 break;
1891 }
1892 sym = elf->symtab_start + r_sym;
1893 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001894 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001895 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301896 if (is_second_extable_reloc(start, rel, fromsec))
1897 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001898 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001899 }
1900}
1901
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001902/**
1903 * A module includes a number of sections that are discarded
1904 * either when loaded or when used as built-in.
1905 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001906 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001907 * Likewise for modules used built-in the sections marked __exit
1908 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001909 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001910 * The check_sec_ref() function traverses all relocation records
1911 * to find all references to a section that reference a section that will
1912 * be discarded and warns about it.
1913 **/
1914static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001915 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001916{
1917 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001918 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001919
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001920 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001921 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001922 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001923 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001924 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001925 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001926 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001927 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001928 }
1929}
1930
Andi Kleen7d02b492014-02-08 09:01:12 +01001931static char *remove_dot(char *s)
1932{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301933 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001934
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301935 if (n && s[n]) {
1936 size_t m = strspn(s + n + 1, "0123456789");
1937 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001938 s[n] = 0;
1939 }
1940 return s;
1941}
1942
Masahiro Yamada8b185742018-05-09 18:50:40 +09001943static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 const char *symname;
1946 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001947 char *license;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct module *mod;
1949 struct elf_info info = { };
1950 Elf_Sym *sym;
1951
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001952 if (!parse_elf(&info, modname))
1953 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 mod = new_module(modname);
1956
1957 /* When there's no vmlinux, don't print warnings about
1958 * unresolved symbols (since there'll be too many ;) */
1959 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 mod->skip = 1;
1962 }
1963
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09001964 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08001965 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02001966 warn("modpost: missing MODULE_LICENSE() in %s\n"
1967 "see include/linux/module.h for "
1968 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001969 while (license) {
1970 if (license_is_gpl_compatible(license))
1971 mod->gpl_compatible = 1;
1972 else {
1973 mod->gpl_compatible = 0;
1974 break;
1975 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09001976 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001977 }
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01001980 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 handle_modversions(mod, &info, sym, symname);
1983 handle_moddevtable(mod, &info, sym, symname);
1984 }
Denis Efremov15bfc232019-08-01 09:06:57 +03001985
1986 // check for static EXPORT_SYMBOL_* functions && global vars
1987 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
1988 unsigned char bind = ELF_ST_BIND(sym->st_info);
1989
1990 if (bind == STB_GLOBAL || bind == STB_WEAK) {
1991 struct symbol *s =
1992 find_symbol(remove_dot(info.strtab +
1993 sym->st_name));
1994
1995 if (s)
1996 s->is_static = 0;
1997 }
1998 }
1999
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002000 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002001 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002003 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (version)
2005 maybe_frob_rcs_version(modname, version, info.modinfo,
2006 version - (char *)info.hdr);
2007 if (version || (all_versions && !is_vmlinux(modname)))
2008 get_src_version(modname, mod->srcversion,
2009 sizeof(mod->srcversion)-1);
2010
2011 parse_elf_finish(&info);
2012
Rusty Russell8c8ef422009-03-31 13:05:34 -06002013 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 * never passed as an argument to an exported function, so
2015 * the automatic versioning doesn't pick it up, but it's really
2016 * important anyhow */
2017 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002018 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Rusty Russell712f9b42013-04-04 17:37:38 +10302021static void read_symbols_from_files(const char *filename)
2022{
2023 FILE *in = stdin;
2024 char fname[PATH_MAX];
2025
2026 if (strcmp(filename, "-") != 0) {
2027 in = fopen(filename, "r");
2028 if (!in)
2029 fatal("Can't open filenames file %s: %m", filename);
2030 }
2031
2032 while (fgets(fname, PATH_MAX, in) != NULL) {
2033 if (strends(fname, "\n"))
2034 fname[strlen(fname)-1] = '\0';
2035 read_symbols(fname);
2036 }
2037
2038 if (in != stdin)
2039 fclose(in);
2040}
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042#define SZ 500
2043
2044/* We first write the generated file into memory using the
2045 * following helper, then compare to the file on disk and
2046 * only update the later if anything changed */
2047
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002048void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2049 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 char tmp[SZ];
2052 int len;
2053 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 va_start(ap, fmt);
2056 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08002057 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 va_end(ap);
2059}
2060
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002061void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08002064 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002065 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
2067 strncpy(buf->p + buf->pos, s, len);
2068 buf->pos += len;
2069}
2070
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002071static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2072{
2073 const char *e = is_vmlinux(m) ?"":".ko";
2074
2075 switch (exp) {
2076 case export_gpl:
2077 fatal("modpost: GPL-incompatible module %s%s "
2078 "uses GPL-only symbol '%s'\n", m, e, s);
2079 break;
2080 case export_unused_gpl:
2081 fatal("modpost: GPL-incompatible module %s%s "
2082 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2083 break;
2084 case export_gpl_future:
2085 warn("modpost: GPL-incompatible module %s%s "
2086 "uses future GPL-only symbol '%s'\n", m, e, s);
2087 break;
2088 case export_plain:
2089 case export_unused:
2090 case export_unknown:
2091 /* ignore */
2092 break;
2093 }
2094}
2095
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002096static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002097{
2098 const char *e = is_vmlinux(m) ?"":".ko";
2099
2100 switch (exp) {
2101 case export_unused:
2102 case export_unused_gpl:
2103 warn("modpost: module %s%s "
2104 "uses symbol '%s' marked UNUSED\n", m, e, s);
2105 break;
2106 default:
2107 /* ignore */
2108 break;
2109 }
2110}
2111
Masahiro Yamada3b415282018-11-23 16:57:23 +09002112static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002113{
2114 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002115 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002116
2117 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002118 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002119 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002120 if (!exp || exp->module == mod) {
2121 if (have_vmlinux && !s->weak) {
2122 if (warn_unresolved) {
2123 warn("\"%s\" [%s.ko] undefined!\n",
2124 s->name, mod->name);
2125 } else {
2126 merror("\"%s\" [%s.ko] undefined!\n",
2127 s->name, mod->name);
2128 err = 1;
2129 }
2130 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002131 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002132 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002133 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002134 if (basename)
2135 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002136 else
2137 basename = mod->name;
2138 if (!mod->gpl_compatible)
2139 check_for_gpl_usage(exp->export, basename, exp->name);
2140 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002141 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002142
2143 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002144}
2145
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002146static int check_modname_len(struct module *mod)
2147{
2148 const char *mod_name;
2149
2150 mod_name = strrchr(mod->name, '/');
2151 if (mod_name == NULL)
2152 mod_name = mod->name;
2153 else
2154 mod_name++;
2155 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2156 merror("module name is too long [%s.ko]\n", mod->name);
2157 return 1;
2158 }
2159
2160 return 0;
2161}
2162
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002163/**
2164 * Header for the generated file
2165 **/
2166static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Laura Abbott9afb7192018-07-05 17:49:37 -07002168 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 buf_printf(b, "#include <linux/module.h>\n");
2170 buf_printf(b, "#include <linux/vermagic.h>\n");
2171 buf_printf(b, "#include <linux/compiler.h>\n");
2172 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002173 buf_printf(b, "BUILD_SALT;\n");
2174 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002176 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302178 buf_printf(b, "__visible struct module __this_module\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002180 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002182 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (mod->has_cleanup)
2184 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002185 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002187 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 buf_printf(b, "};\n");
2189}
2190
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002191static void add_intree_flag(struct buffer *b, int is_intree)
2192{
2193 if (is_intree)
2194 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2195}
2196
Andi Kleencaf75012018-01-25 15:50:28 -08002197/* Cannot check for assembler */
2198static void add_retpoline(struct buffer *b)
2199{
WANG Chaoe4f35892018-12-11 00:37:25 +08002200 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002201 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2202 buf_printf(b, "#endif\n");
2203}
2204
Trevor Keith5c725132009-09-22 16:43:38 -07002205static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002206{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002207 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002208 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2209}
2210
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002211/**
2212 * Record CRCs for unresolved symbols
2213 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002214static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002217 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 for (s = mod->unres; s; s = s->next) {
2220 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002221 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 s->module = exp->module;
2224 s->crc_valid = exp->crc_valid;
2225 s->crc = exp->crc;
2226 }
2227
2228 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002229 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 buf_printf(b, "\n");
2232 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01002233 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
2235
2236 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002237 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002240 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 s->name, mod->name);
2242 continue;
2243 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302244 if (strlen(s->name) >= MODULE_NAME_LEN) {
2245 merror("too long symbol \"%s\" [%s.ko]\n",
2246 s->name, mod->name);
2247 err = 1;
2248 break;
2249 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002250 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302251 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253
2254 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002255
2256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002259static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 int first = 1;
2263
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002264 /* Clear ->seen flag of modules that own symbols needed by this. */
2265 for (s = mod->unres; s; s = s->next)
2266 if (s->module)
2267 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 buf_printf(b, "\n");
2270 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01002271 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
2273 buf_printf(b, "\"depends=");
2274 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002275 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (!s->module)
2277 continue;
2278
2279 if (s->module->seen)
2280 continue;
2281
2282 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002283 p = strrchr(s->module->name, '/');
2284 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002285 p++;
2286 else
2287 p = s->module->name;
2288 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 first = 0;
2290 }
2291 buf_printf(b, "\";\n");
2292}
2293
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002294static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 if (mod->srcversion[0]) {
2297 buf_printf(b, "\n");
2298 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2299 mod->srcversion);
2300 }
2301}
2302
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002303static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 char *tmp;
2306 FILE *file;
2307 struct stat st;
2308
2309 file = fopen(fname, "r");
2310 if (!file)
2311 goto write;
2312
2313 if (fstat(fileno(file), &st) < 0)
2314 goto close_write;
2315
2316 if (st.st_size != b->pos)
2317 goto close_write;
2318
2319 tmp = NOFAIL(malloc(b->pos));
2320 if (fread(tmp, 1, b->pos, file) != b->pos)
2321 goto free_write;
2322
2323 if (memcmp(tmp, b->p, b->pos) != 0)
2324 goto free_write;
2325
2326 free(tmp);
2327 fclose(file);
2328 return;
2329
2330 free_write:
2331 free(tmp);
2332 close_write:
2333 fclose(file);
2334 write:
2335 file = fopen(fname, "w");
2336 if (!file) {
2337 perror(fname);
2338 exit(1);
2339 }
2340 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2341 perror(fname);
2342 exit(1);
2343 }
2344 fclose(file);
2345}
2346
Ram Paibd5cbce2006-06-08 22:12:53 -07002347/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002348 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002349 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002350static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 unsigned long size, pos = 0;
2353 void *file = grab_file(fname, &size);
2354 char *line;
2355
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002356 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 /* No symbol versions, silently ignore */
2358 return;
2359
2360 while ((line = get_next_line(&pos, file, size))) {
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002361 char *symname, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 unsigned int crc;
2363 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002364 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 if (!(symname = strchr(line, '\t')))
2367 goto fail;
2368 *symname++ = '\0';
2369 if (!(modname = strchr(symname, '\t')))
2370 goto fail;
2371 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002372 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002373 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002374 if (export && ((end = strchr(export, '\t')) != NULL))
2375 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 crc = strtoul(line, &d, 16);
2377 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2378 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002379 mod = find_module(modname);
2380 if (!mod) {
2381 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002383 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 mod->skip = 1;
2385 }
Ram Paibd5cbce2006-06-08 22:12:53 -07002386 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002387 s->kernel = kernel;
2388 s->preloaded = 1;
Denis Efremov15bfc232019-08-01 09:06:57 +03002389 s->is_static = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -07002390 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302392 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return;
2394fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302395 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 fatal("parse error in symbol dump file\n");
2397}
2398
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002399/* For normal builds always dump all symbols.
2400 * For external modules only dump symbols
2401 * that are not read from kernel Module.symvers.
2402 **/
2403static int dump_sym(struct symbol *sym)
2404{
2405 if (!external_module)
2406 return 1;
2407 if (sym->vmlinux || sym->kernel)
2408 return 0;
2409 return 1;
2410}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002411
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002412static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
2414 struct buffer buf = { };
2415 struct symbol *symbol;
2416 int n;
2417
2418 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2419 symbol = symbolhash[n];
2420 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002421 if (dump_sym(symbol))
Ram Paibd5cbce2006-06-08 22:12:53 -07002422 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n",
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002423 symbol->crc, symbol->name,
Ram Paibd5cbce2006-06-08 22:12:53 -07002424 symbol->module->name,
2425 export_str(symbol->export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 symbol = symbol->next;
2427 }
2428 }
2429 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002430 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002433struct ext_sym_list {
2434 struct ext_sym_list *next;
2435 const char *file;
2436};
2437
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002438int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 struct module *mod;
2441 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002442 char *kernel_read = NULL, *module_read = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302443 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002445 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002446 int n;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002447 struct ext_sym_list *extsym_iter;
2448 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002450 while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002451 switch (opt) {
2452 case 'i':
2453 kernel_read = optarg;
2454 break;
2455 case 'I':
2456 module_read = optarg;
2457 external_module = 1;
2458 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002459 case 'e':
2460 external_module = 1;
2461 extsym_iter =
2462 NOFAIL(malloc(sizeof(*extsym_iter)));
2463 extsym_iter->next = extsym_start;
2464 extsym_iter->file = optarg;
2465 extsym_start = extsym_iter;
2466 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002467 case 'm':
2468 modversions = 1;
2469 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302470 case 'n':
2471 ignore_missing_files = 1;
2472 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002473 case 'o':
2474 dump_write = optarg;
2475 break;
2476 case 'a':
2477 all_versions = 1;
2478 break;
2479 case 's':
2480 vmlinux_section_warnings = 0;
2481 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302482 case 'T':
2483 files_source = optarg;
2484 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002485 case 'w':
2486 warn_unresolved = 1;
2487 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302488 case 'E':
2489 sec_mismatch_fatal = 1;
2490 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002491 default:
2492 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494 }
2495
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002496 if (kernel_read)
2497 read_dump(kernel_read, 1);
2498 if (module_read)
2499 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002500 while (extsym_start) {
2501 read_dump(extsym_start->file, 0);
2502 extsym_iter = extsym_start->next;
2503 free(extsym_start);
2504 extsym_start = extsym_iter;
2505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002507 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Rusty Russell712f9b42013-04-04 17:37:38 +10302510 if (files_source)
2511 read_symbols_from_files(files_source);
2512
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002513 err = 0;
2514
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002515 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302516 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002517
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002518 if (mod->skip)
2519 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 buf.pos = 0;
2522
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002523 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002524 err |= check_exports(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002526 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002527 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002528 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002529 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002530 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 add_moddevtable(&buf, mod);
2532 add_srcversion(&buf, mod);
2533
2534 sprintf(fname, "%s.mod.c", mod->name);
2535 write_if_changed(&buf, fname);
2536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (dump_write)
2538 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002539 if (sec_mismatch_count && sec_mismatch_fatal)
2540 fatal("modpost: Section mismatches detected.\n"
2541 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002542 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
2543 struct symbol *s = symbolhash[n];
2544
2545 while (s) {
2546 if (s->is_static)
2547 warn("\"%s\" [%s] is a static %s\n",
2548 s->name, s->module->name,
2549 export_str(s->export));
2550
2551 s = s->next;
2552 }
2553 }
2554
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002555 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}