blob: be72da25fe7caa0395fbc18209aee7a3755dd4fc [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;
Matthias Maennich1d082772019-09-06 11:32:31 +010041/* write namespace dependencies */
42static int write_namespace_deps;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010043
Sam Ravnborgc96fca22006-07-01 11:44:23 +020044enum export {
45 export_plain, export_unused, export_gpl,
46 export_unused_gpl, export_gpl_future, export_unknown
47};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080049/* In kernel, this size is defined in linux/module.h;
50 * here we use Elf_Addr instead of long for covering cross-compile
51 */
52
53#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
54
Andi Kleen6d9a89e2007-11-22 03:43:08 +010055#define PRINTF __attribute__ ((format (printf, 1, 2)))
56
57PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 va_list arglist;
60
61 fprintf(stderr, "FATAL: ");
62
63 va_start(arglist, fmt);
64 vfprintf(stderr, fmt, arglist);
65 va_end(arglist);
66
67 exit(1);
68}
69
Andi Kleen6d9a89e2007-11-22 03:43:08 +010070PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 va_list arglist;
73
74 fprintf(stderr, "WARNING: ");
75
76 va_start(arglist, fmt);
77 vfprintf(stderr, fmt, arglist);
78 va_end(arglist);
79}
80
Andi Kleen6d9a89e2007-11-22 03:43:08 +010081PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060082{
83 va_list arglist;
84
85 fprintf(stderr, "ERROR: ");
86
87 va_start(arglist, fmt);
88 vfprintf(stderr, fmt, arglist);
89 va_end(arglist);
90}
91
Rusty Russelld4ef1c32013-04-04 17:37:32 +103092static inline bool strends(const char *str, const char *postfix)
93{
94 if (strlen(str) < strlen(postfix))
95 return false;
96
97 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
98}
99
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100100static int is_vmlinux(const char *modname)
101{
102 const char *myname;
103
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100104 myname = strrchr(modname, '/');
105 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100106 myname++;
107 else
108 myname = modname;
109
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200110 return (strcmp(myname, "vmlinux") == 0) ||
111 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114void *do_nofail(void *ptr, const char *expr)
115{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100116 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return ptr;
120}
121
122/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static struct module *modules;
124
Masahiro Yamada8b185742018-05-09 18:50:40 +0900125static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct module *mod;
128
129 for (mod = modules; mod; mod = mod->next)
130 if (strcmp(mod->name, modname) == 0)
131 break;
132 return mod;
133}
134
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030135static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030138 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 mod = NOFAIL(malloc(sizeof(*mod)));
141 memset(mod, 0, sizeof(*mod));
142 p = NOFAIL(strdup(modname));
143
144 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030145 if (strends(p, ".o")) {
146 p[strlen(p) - 2] = '\0';
147 mod->is_dot_o = 1;
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* add to list */
151 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200152 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 mod->next = modules;
154 modules = mod;
155
156 return mod;
157}
158
159/* A hash of all exported symbols,
160 * struct symbol is also used for lists of unresolved symbols */
161
162#define SYMBOL_HASH_SIZE 1024
163
164struct symbol {
165 struct symbol *next;
166 struct module *module;
167 unsigned int crc;
168 int crc_valid;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100169 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100171 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
172 unsigned int kernel:1; /* 1 if symbol is from kernel
173 * (only for external modules) **/
Rusty Russellb6568b12013-11-07 12:09:13 +1030174 unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */
Ram Paibd5cbce2006-06-08 22:12:53 -0700175 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 char name[0];
177};
178
179static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
180
181/* This is based on the hash agorithm from gdbm, via tdb */
182static inline unsigned int tdb_hash(const char *name)
183{
184 unsigned value; /* Used to compute the hash value. */
185 unsigned i; /* Used to cycle through random values. */
186
187 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100188 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
190
191 return (1103515243 * value + 12345);
192}
193
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100194/**
195 * Allocate a new symbols for use in the hash of exported symbols or
196 * the list of unresolved symbols per module
197 **/
198static struct symbol *alloc_symbol(const char *name, unsigned int weak,
199 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
202
203 memset(s, 0, sizeof(*s));
204 strcpy(s->name, name);
205 s->weak = weak;
206 s->next = next;
207 return s;
208}
209
210/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700211static struct symbol *new_symbol(const char *name, struct module *module,
212 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned int hash;
215 struct symbol *new;
216
217 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
218 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
219 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700220 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100221 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100224static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct symbol *s;
227
228 /* For our purposes, .foo matches foo. PPC64 needs this. */
229 if (name[0] == '.')
230 name++;
231
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100232 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (strcmp(s->name, name) == 0)
234 return s;
235 }
236 return NULL;
237}
238
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100239static bool contains_namespace(struct namespace_list *list,
240 const char *namespace)
241{
242 struct namespace_list *ns_entry;
243
244 for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
245 if (strcmp(ns_entry->namespace, namespace) == 0)
246 return true;
247
248 return false;
249}
250
251static void add_namespace(struct namespace_list **list, const char *namespace)
252{
253 struct namespace_list *ns_entry;
254
255 if (!contains_namespace(*list, namespace)) {
256 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
257 strlen(namespace) + 1));
258 strcpy(ns_entry->namespace, namespace);
259 ns_entry->next = *list;
260 *list = ns_entry;
261 }
262}
263
264static bool module_imports_namespace(struct module *module,
265 const char *namespace)
266{
267 return contains_namespace(module->imported_namespaces, namespace);
268}
269
Mathias Krause7a3ee752014-08-27 20:28:53 +0930270static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700271 const char *str;
272 enum export export;
273} export_list[] = {
274 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200275 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700276 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200277 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700278 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
279 { .str = "(unknown)", .export = export_unknown },
280};
281
282
283static const char *export_str(enum export ex)
284{
285 return export_list[ex].str;
286}
287
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100288static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700289{
290 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100291
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200292 if (!s)
293 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700294 for (i = 0; export_list[i].export != export_unknown; i++) {
295 if (strcmp(export_list[i].str, s) == 0)
296 return export_list[i].export;
297 }
298 return export_unknown;
299}
300
Masahiro Yamada6124c042017-09-06 16:19:05 -0700301static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
302{
303 return (void *)elf->hdr +
304 elf->sechdrs[elf->secindex_strings].sh_offset +
305 sechdr->sh_name;
306}
307
308static const char *sec_name(struct elf_info *elf, int secindex)
309{
310 return sech_name(elf, &elf->sechdrs[secindex]);
311}
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200312
313#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
314
315static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
316{
317 const char *secname = sec_name(elf, sec);
318
319 if (strstarts(secname, "___ksymtab+"))
320 return export_plain;
321 else if (strstarts(secname, "___ksymtab_unused+"))
322 return export_unused;
323 else if (strstarts(secname, "___ksymtab_gpl+"))
324 return export_gpl;
325 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
326 return export_unused_gpl;
327 else if (strstarts(secname, "___ksymtab_gpl_future+"))
328 return export_gpl_future;
329 else
330 return export_unknown;
331}
332
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200333static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700334{
335 if (sec == elf->export_sec)
336 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200337 else if (sec == elf->export_unused_sec)
338 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700339 else if (sec == elf->export_gpl_sec)
340 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200341 else if (sec == elf->export_unused_gpl_sec)
342 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700343 else if (sec == elf->export_gpl_future_sec)
344 return export_gpl_future;
345 else
346 return export_unknown;
347}
348
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100349static const char *sym_extract_namespace(const char **symname)
350{
351 size_t n;
352 char *dupsymname;
353
354 n = strcspn(*symname, ".");
355 if (n < strlen(*symname) - 1) {
356 dupsymname = NOFAIL(strdup(*symname));
357 dupsymname[n] = '\0';
358 *symname = dupsymname;
359 return dupsymname + n + 1;
360 }
361
362 return NULL;
363}
364
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100365/**
366 * Add an exported symbol - it may have already been added without a
367 * CRC, in this case just update the CRC
368 **/
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100369static struct symbol *sym_add_exported(const char *name, const char *namespace,
370 struct module *mod, enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct symbol *s = find_symbol(name);
373
374 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700375 s = new_symbol(name, mod, export);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100376 s->namespace = namespace;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100377 } else {
378 if (!s->preloaded) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100379 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
380 mod->name, name, s->module->name,
381 is_vmlinux(s->module->name) ? "" : ".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700382 } else {
Paul Bollebaec30e2014-04-16 18:05:35 +0200383 /* In case Module.symvers was out of date */
Trent Piepho4b219602007-10-11 16:40:10 -0700384 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100387 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100388 s->vmlinux = is_vmlinux(mod->name);
389 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700390 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100391 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100394static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700395 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100396{
397 struct symbol *s = find_symbol(name);
398
Rusty Russellb6568b12013-11-07 12:09:13 +1030399 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700400 s = new_symbol(name, mod, export);
Rusty Russellb6568b12013-11-07 12:09:13 +1030401 /* Don't complain when we find it later. */
402 s->preloaded = 1;
403 }
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100404 s->crc = crc;
405 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100408void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930411 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int fd;
413
414 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930415 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930417 if (fstat(fd, &st))
418 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 *size = st.st_size;
421 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930423failed:
424 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (map == MAP_FAILED)
426 return NULL;
427 return map;
428}
429
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100430/**
431 * Return a copy of the next line in a mmap'ed file.
432 * spaces in the beginning of the line is trimmed away.
433 * Return a pointer to a static buffer.
434 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100435char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 static char line[4096];
438 int skip = 1;
439 size_t len = 0;
440 signed char *p = (signed char *)file + *pos;
441 char *s = line;
442
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100443 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (skip && isspace(*p)) {
445 p++;
446 continue;
447 }
448 skip = 0;
449 if (*p != '\n' && (*pos < size)) {
450 len++;
451 *s++ = *p++;
452 if (len > 4095)
453 break; /* Too long, stop */
454 } else {
455 /* End of string */
456 *s = '\0';
457 return line;
458 }
459 }
460 /* End of buffer */
461 return NULL;
462}
463
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100464void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 munmap(file, size);
467}
468
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100469static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100472 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 Elf_Shdr *sechdrs;
474 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200475 const char *secstrings;
476 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 hdr = grab_file(filename, &info->size);
479 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930480 if (ignore_missing_files) {
481 fprintf(stderr, "%s: %s (ignored)\n", filename,
482 strerror(errno));
483 return 0;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200486 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100489 if (info->size < sizeof(*hdr)) {
490 /* file too small, assume this is an empty .o file */
491 return 0;
492 }
493 /* Is this a valid ELF file? */
494 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
495 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
496 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
497 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
498 /* Not an ELF file - silently ignore it */
499 return 0;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200502 hdr->e_type = TO_NATIVE(hdr->e_type);
503 hdr->e_machine = TO_NATIVE(hdr->e_machine);
504 hdr->e_version = TO_NATIVE(hdr->e_version);
505 hdr->e_entry = TO_NATIVE(hdr->e_entry);
506 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
507 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
508 hdr->e_flags = TO_NATIVE(hdr->e_flags);
509 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
510 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
511 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
512 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
513 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
514 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 sechdrs = (void *)hdr + hdr->e_shoff;
516 info->sechdrs = sechdrs;
517
Petr Stetiara83710e2007-08-27 12:15:07 +0200518 /* Check if file offset is correct */
519 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100520 fatal("section header offset=%lu in file '%s' is bigger than "
521 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
522 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200523 return 0;
524 }
525
Anders Kaseorg68457562011-05-19 16:55:27 -0600526 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200527 /*
528 * There are more than 64k sections,
529 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200530 */
531 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
532 }
533 else {
534 info->num_sections = hdr->e_shnum;
535 }
536 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600537 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200538 }
539 else {
540 info->secindex_strings = hdr->e_shstrndx;
541 }
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200544 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200545 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
546 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
547 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
548 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
549 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
550 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
551 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
552 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
553 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
554 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200557 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
558 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700559 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900560 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Tejun Heo56fc82c2009-02-06 00:48:02 +0900562 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100563 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
564 "sizeof(*hrd)=%zu\n", filename,
565 (unsigned long)sechdrs[i].sh_offset,
566 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100567 return 0;
568 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700569 secname = secstrings + sechdrs[i].sh_name;
570 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900571 if (nobits)
572 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
574 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700575 } else if (strcmp(secname, "__ksymtab") == 0)
576 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200577 else if (strcmp(secname, "__ksymtab_unused") == 0)
578 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700579 else if (strcmp(secname, "__ksymtab_gpl") == 0)
580 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200581 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
582 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700583 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
584 info->export_gpl_future_sec = i;
585
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200586 if (sechdrs[i].sh_type == SHT_SYMTAB) {
587 unsigned int sh_link_idx;
588 symtab_idx = i;
589 info->symtab_start = (void *)hdr +
590 sechdrs[i].sh_offset;
591 info->symtab_stop = (void *)hdr +
592 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600593 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200594 info->strtab = (void *)hdr +
595 sechdrs[sh_link_idx].sh_offset;
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200598 /* 32bit section no. table? ("more than 64k sections") */
599 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
600 symtab_shndx_idx = i;
601 info->symtab_shndx_start = (void *)hdr +
602 sechdrs[i].sh_offset;
603 info->symtab_shndx_stop = (void *)hdr +
604 sechdrs[i].sh_offset + sechdrs[i].sh_size;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100607 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100608 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Fix endianness in symbols */
611 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
612 sym->st_shndx = TO_NATIVE(sym->st_shndx);
613 sym->st_name = TO_NATIVE(sym->st_name);
614 sym->st_value = TO_NATIVE(sym->st_value);
615 sym->st_size = TO_NATIVE(sym->st_size);
616 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200617
618 if (symtab_shndx_idx != ~0U) {
619 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600620 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200621 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600622 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200623 symtab_idx);
624 /* Fix endianness */
625 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
626 p++)
627 *p = TO_NATIVE(*p);
628 }
629
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100630 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100633static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 release_file(info->hdr, info->size);
636}
637
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200638static int ignore_undef_symbol(struct elf_info *info, const char *symname)
639{
640 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900641 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200642 return 1;
643 /* ignore global offset table */
644 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
645 return 1;
646 if (info->hdr->e_machine == EM_PPC)
647 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900648 if (strstarts(symname, "_restgpr_") ||
649 strstarts(symname, "_savegpr_") ||
650 strstarts(symname, "_rest32gpr_") ||
651 strstarts(symname, "_save32gpr_") ||
652 strstarts(symname, "_restvr_") ||
653 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200654 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000655 if (info->hdr->e_machine == EM_PPC64)
656 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900657 if (strstarts(symname, "_restgpr0_") ||
658 strstarts(symname, "_savegpr0_") ||
659 strstarts(symname, "_restvr_") ||
660 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100661 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000662 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200663 /* Do not ignore this symbol */
664 return 0;
665}
666
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100667static void handle_modversions(struct module *mod, struct elf_info *info,
668 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200671 enum export export;
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100672 bool is_crc = false;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100673 const char *name, *namespace;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200674
Frank Rowand258f7422012-04-09 17:59:03 -0700675 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900676 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200677 export = export_from_secname(info, get_secindex(info, sym));
678 else
679 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Andi Kleenb5064652013-11-12 15:08:38 -0800681 /* CRC'd symbol */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900682 if (strstarts(symname, "__crc_")) {
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100683 is_crc = true;
Andi Kleenb5064652013-11-12 15:08:38 -0800684 crc = (unsigned int) sym->st_value;
Ard Biesheuvel56067812017-02-03 09:54:05 +0000685 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
686 unsigned int *crcp;
687
688 /* symbol points to the CRC in the ELF object */
689 crcp = (void *)info->hdr + sym->st_value +
690 info->sechdrs[sym->st_shndx].sh_offset -
691 (info->hdr->e_type != ET_REL ?
692 info->sechdrs[sym->st_shndx].sh_addr : 0);
Fredrik Noring54a71512019-03-27 19:12:50 +0100693 crc = TO_NATIVE(*crcp);
Ard Biesheuvel56067812017-02-03 09:54:05 +0000694 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900695 sym_update_crc(symname + strlen("__crc_"), mod, crc,
Andi Kleenb5064652013-11-12 15:08:38 -0800696 export);
697 }
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 switch (sym->st_shndx) {
700 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900701 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100702 /* Should warn here, but modpost runs before the linker */
703 } else
704 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 case SHN_UNDEF:
707 /* undefined symbol */
708 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
709 ELF_ST_BIND(sym->st_info) != STB_WEAK)
710 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200711 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
Ben Colline8d529012005-08-19 13:44:57 -0700713/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
714#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
715/* add compatibility with older glibc */
716#ifndef STT_SPARC_REGISTER
717#define STT_SPARC_REGISTER STT_REGISTER
718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (info->hdr->e_machine == EM_SPARC ||
720 info->hdr->e_machine == EM_SPARCV9) {
721 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700722 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100724 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700725 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100726 munged[0] = '_';
727 munged[1] = toupper(munged[1]);
728 symname = munged;
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100732
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100733 if (is_crc) {
734 const char *e = is_vmlinux(mod->name) ?"":".ko";
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900735 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
736 symname + strlen("__crc_"), mod->name, e);
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100737 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030738 mod->unres = alloc_symbol(symname,
739 ELF_ST_BIND(sym->st_info) == STB_WEAK,
740 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742 default:
743 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900744 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100745 name = symname + strlen("__ksymtab_");
746 namespace = sym_extract_namespace(&name);
747 sym_add_exported(name, namespace, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900749 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900751 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 mod->has_cleanup = 1;
753 break;
754 }
755}
756
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100757/**
758 * Parse tag=value strings from .modinfo section
759 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static char *next_string(char *string, unsigned long *secsize)
761{
762 /* Skip non-zero chars */
763 while (string[0]) {
764 string++;
765 if ((*secsize)-- <= 1)
766 return NULL;
767 }
768
769 /* Skip any zero padding. */
770 while (!string[0]) {
771 string++;
772 if ((*secsize)-- <= 1)
773 return NULL;
774 }
775 return string;
776}
777
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900778static char *get_next_modinfo(struct elf_info *info, const char *tag,
779 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 char *p;
782 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900783 char *modinfo = info->modinfo;
784 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900786 if (prev) {
787 size -= prev - modinfo;
788 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200789 }
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 for (p = modinfo; p; p = next_string(p, &size)) {
792 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
793 return p + taglen + 1;
794 }
795 return NULL;
796}
797
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900798static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200799
800{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900801 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200802}
803
Sam Ravnborg93684d32006-02-19 11:53:35 +0100804/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100805 * Test if string s ends in string sub
806 * return 0 if match
807 **/
808static int strrcmp(const char *s, const char *sub)
809{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100810 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100811
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100812 if (!s || !sub)
813 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100814
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100815 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100816 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100817
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100818 if ((slen == 0) || (sublen == 0))
819 return 1;
820
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100821 if (sublen > slen)
822 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100823
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100824 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100825}
826
Sam Ravnborgff13f922008-01-23 19:54:27 +0100827static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
828{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100829 if (sym)
830 return elf->strtab + sym->st_name;
831 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100832 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100833}
834
Sam Ravnborg10668222008-01-13 22:21:31 +0100835/* The pattern is an array of simple patterns.
836 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100837 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100838 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930839 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100840 */
Trevor Keith5c725132009-09-22 16:43:38 -0700841static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100842{
843 const char *p;
844 while (*pat) {
845 p = *pat++;
846 const char *endp = p + strlen(p) - 1;
847
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930848 /* "*foo*" */
849 if (*p == '*' && *endp == '*') {
850 char *here, *bare = strndup(p + 1, strlen(p) - 2);
851
852 here = strstr(sym, bare);
853 free(bare);
854 if (here != NULL)
855 return 1;
856 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100857 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930858 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100859 if (strrcmp(sym, p + 1) == 0)
860 return 1;
861 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100862 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100863 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100864 if (strncmp(sym, p, strlen(p) - 1) == 0)
865 return 1;
866 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100867 /* no wildcards */
868 else {
869 if (strcmp(p, sym) == 0)
870 return 1;
871 }
872 }
873 /* no match */
874 return 0;
875}
876
Sam Ravnborg10668222008-01-13 22:21:31 +0100877/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930878static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200879{
880 ".comment*",
881 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930882 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800883 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000884 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200885 ".mdebug*", /* alpha, score, mips etc. */
886 ".pdr", /* alpha, score, mips etc. */
887 ".stab*",
888 ".note*",
889 ".got*",
890 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400891 ".xt.prop", /* xtensa */
892 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030893 ".arcextmap*", /* arc */
894 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030895 ".cmem*", /* EZchip */
896 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100897 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600898 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200899 NULL
900};
Sam Ravnborg10668222008-01-13 22:21:31 +0100901
Sam Ravnborge241a632008-01-28 20:13:13 +0100902/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400903 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100904 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400905 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100906 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400907static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900908 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100909{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400910 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100911
Anders Kaseorgb614a692009-04-23 16:49:33 -0400912 if (sechdr->sh_type == SHT_PROGBITS &&
913 !(sechdr->sh_flags & SHF_ALLOC) &&
914 !match(sec, section_white_list)) {
915 warn("%s (%s): unexpected non-allocatable section.\n"
916 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
917 "Note that for example <linux/init.h> contains\n"
918 "section definitions for use in .S files.\n\n",
919 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100920 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100921}
922
923
924
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100925#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930926 ".init.setup", ".init.rodata", ".meminit.rodata", \
927 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100928#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930929 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100930
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100931#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930932 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100933#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930934 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100935
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200936#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930937 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
938 ".pci_fixup_enable", ".pci_fixup_resume", \
939 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200940
Paul Gortmakere24f6622013-06-19 19:30:48 -0400941#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
942#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100943
944#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
945#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100946
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930947#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930948#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700949 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930950#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400951 ".fixup", ".entry.text", ".exception.text", ".text.*", \
952 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100953
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000954#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000955#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100956
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000957#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000958#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100959
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930960#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
961 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
962
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100963/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930964static const char *const init_data_sections[] =
965 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100966
967/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930968static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100969
970/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930971static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100972 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100973
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930974/* all text sections */
975static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
976
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100977/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930978static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100979
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100980
981/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100982#define DEFAULT_SYMBOL_WHITE_LIST \
983 "*driver", \
984 "*_template", /* scsi uses *_template a lot */ \
985 "*_timer", /* arm uses ops structures named _timer a lot */ \
986 "*_sht", /* scsi also used *_sht to some extent */ \
987 "*_ops", \
988 "*_probe", \
989 "*_probe_one", \
990 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100991
Mathias Krause7a3ee752014-08-27 20:28:53 +0930992static const char *const head_sections[] = { ".head.text*", NULL };
993static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100994 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930995static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100996
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100997enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100998 TEXT_TO_ANY_INIT,
999 DATA_TO_ANY_INIT,
1000 TEXT_TO_ANY_EXIT,
1001 DATA_TO_ANY_EXIT,
1002 XXXINIT_TO_SOME_INIT,
1003 XXXEXIT_TO_SOME_EXIT,
1004 ANY_INIT_TO_ANY_EXIT,
1005 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001006 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301007 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001008};
1009
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301010/**
1011 * Describe how to match sections on different criterias:
1012 *
1013 * @fromsec: Array of sections to be matched.
1014 *
1015 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1016 * this array is forbidden (black-list). Can be empty.
1017 *
1018 * @good_tosec: Relocations applied to a section in @fromsec must be
1019 * targetting sections in this array (white-list). Can be empty.
1020 *
1021 * @mismatch: Type of mismatch.
1022 *
1023 * @symbol_white_list: Do not match a relocation to a symbol in this list
1024 * even if it is targetting a section in @bad_to_sec.
1025 *
1026 * @handler: Specific handler to call when a match is found. If NULL,
1027 * default_mismatch_handler() will be called.
1028 *
1029 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001030struct sectioncheck {
1031 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301032 const char *bad_tosec[20];
1033 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001034 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001035 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301036 void (*handler)(const char *modname, struct elf_info *elf,
1037 const struct sectioncheck* const mismatch,
1038 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1039
Sam Ravnborg10668222008-01-13 22:21:31 +01001040};
1041
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301042static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1043 const struct sectioncheck* const mismatch,
1044 Elf_Rela *r, Elf_Sym *sym,
1045 const char *fromsec);
1046
Mathias Krause7a3ee752014-08-27 20:28:53 +09301047static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001048/* Do not reference init/exit code/data from
1049 * normal code and data
1050 */
1051{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001052 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301053 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001054 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001055 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001056},
1057{
1058 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301059 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001060 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001061 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001062},
1063{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001064 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301065 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001066 .mismatch = DATA_TO_ANY_INIT,
1067 .symbol_white_list = {
1068 "*_template", "*_timer", "*_sht", "*_ops",
1069 "*_probe", "*_probe_one", "*_console", NULL
1070 },
1071},
1072{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001073 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301074 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001075 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001076 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001077},
1078{
1079 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301080 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001081 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001082 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001083},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001084/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001085{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001086 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301087 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001088 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001089 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001090},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001091/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001092{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001093 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301094 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001095 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001096 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001097},
1098/* Do not use exit code/data from init code */
1099{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001100 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301101 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001102 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001103 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001104},
1105/* Do not use init code/data from exit code */
1106{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001107 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301108 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001109 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001110 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001111},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001112{
1113 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301114 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001115 .mismatch = ANY_INIT_TO_ANY_EXIT,
1116 .symbol_white_list = { NULL },
1117},
Sam Ravnborg10668222008-01-13 22:21:31 +01001118/* Do not export init/exit functions or data */
1119{
1120 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301121 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001122 .mismatch = EXPORT_TO_INIT_EXIT,
1123 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301124},
1125{
1126 .fromsec = { "__ex_table", NULL },
1127 /* If you're adding any new black-listed sections in here, consider
1128 * adding a special 'printer' for them in scripts/check_extable.
1129 */
1130 .bad_tosec = { ".altinstr_replacement", NULL },
1131 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1132 .mismatch = EXTABLE_TO_NON_TEXT,
1133 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001134}
1135};
1136
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001137static const struct sectioncheck *section_mismatch(
1138 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001139{
1140 int i;
1141 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1142 const struct sectioncheck *check = &sectioncheck[0];
1143
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301144 /*
1145 * The target section could be the SHT_NUL section when we're
1146 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001147 * doesn't make much sense and causes build failures on parisc
1148 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301149 */
1150 if (*tosec == '\0')
1151 return NULL;
1152
Sam Ravnborg10668222008-01-13 22:21:31 +01001153 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301154 if (match(fromsec, check->fromsec)) {
1155 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1156 return check;
1157 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1158 return check;
1159 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001160 check++;
1161 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001162 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001163}
1164
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001165/**
1166 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001167 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001168 * Pattern 1:
1169 * If a module parameter is declared __initdata and permissions=0
1170 * then this is legal despite the warning generated.
1171 * We cannot see value of permissions here, so just ignore
1172 * this pattern.
1173 * The pattern is identified by:
1174 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001175 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001176 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001177 *
Rusty Russell6a841522010-08-11 23:04:16 -06001178 * Pattern 1a:
1179 * module_param_call() ops can refer to __init set function if permissions=0
1180 * The pattern is identified by:
1181 * tosec = .init.text
1182 * fromsec = .data*
1183 * atsym = __param_ops_*
1184 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001185 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001186 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001187 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001188 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001189 * tosec = init or exit section
1190 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001191 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1192 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001193 *
1194 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001195 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001196 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001197 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001198 * Some symbols belong to init section but still it is ok to reference
1199 * these from non-init sections as these symbols don't have any memory
1200 * allocated for them and symbol address and value are same. So even
1201 * if init section is freed, its ok to reference those symbols.
1202 * For ex. symbols marking the init section boundaries.
1203 * This pattern is identified by
1204 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001205 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301206 * Pattern 5:
1207 * GCC may optimize static inlines when fed constant arg(s) resulting
1208 * in functions like cpumask_empty() -- generating an associated symbol
1209 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1210 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1211 * meaningless section warning. May need to add isra symbols too...
1212 * This pattern is identified by
1213 * tosec = init section
1214 * fromsec = text section
1215 * refsymname = *.constprop.*
1216 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001217 * Pattern 6:
1218 * Hide section mismatch warnings for ELF local symbols. The goal
1219 * is to eliminate false positive modpost warnings caused by
1220 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1221 * Autogenerated symbol names bypass modpost's "Pattern 2"
1222 * whitelisting, which relies on pattern-matching against symbol
1223 * names to work. (One situation where gcc can autogenerate ELF
1224 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001225 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001226static int secref_whitelist(const struct sectioncheck *mismatch,
1227 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001228 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001229{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001230 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001231 if (match(tosec, init_data_sections) &&
1232 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001233 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001234 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001235
Rusty Russell6a841522010-08-11 23:04:16 -06001236 /* Check for pattern 1a */
1237 if (strcmp(tosec, ".init.text") == 0 &&
1238 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001239 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001240 return 0;
1241
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001242 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001243 if (match(tosec, init_exit_sections) &&
1244 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001245 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001246 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001247
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001248 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001249 if (match(fromsec, head_sections) &&
1250 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001251 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001252
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001253 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001254 if (match(tosym, linker_symbols))
1255 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001256
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301257 /* Check for pattern 5 */
1258 if (match(fromsec, text_sections) &&
1259 match(tosec, init_sections) &&
1260 match(fromsym, optim_symbols))
1261 return 0;
1262
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001263 /* Check for pattern 6 */
1264 if (strstarts(fromsym, ".L"))
1265 return 0;
1266
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001267 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001268}
1269
Sami Tolvanen5818c682018-10-23 15:15:35 -07001270static inline int is_arm_mapping_symbol(const char *str)
1271{
1272 return str[0] == '$' && strchr("axtd", str[1])
1273 && (str[2] == '\0' || str[2] == '.');
1274}
1275
1276/*
1277 * If there's no name there, ignore it; likewise, ignore it if it's
1278 * one of the magic symbols emitted used by current ARM tools.
1279 *
1280 * Otherwise if find_symbols_between() returns those symbols, they'll
1281 * fail the whitelist tests and cause lots of false alarms ... fixable
1282 * only by merging __exit and __init sections into __text, bloating
1283 * the kernel (which is especially evil on embedded platforms).
1284 */
1285static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1286{
1287 const char *name = elf->strtab + sym->st_name;
1288
1289 if (!name || !strlen(name))
1290 return 0;
1291 return !is_arm_mapping_symbol(name);
1292}
1293
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001294/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001295 * Find symbol based on relocation record info.
1296 * In some cases the symbol supplied is a valid symbol so
1297 * return refsym. If st_name != 0 we assume this is a valid symbol.
1298 * In other cases the symbol needs to be looked up in the symbol table
1299 * based on section and address.
1300 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001301static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001302 Elf_Sym *relsym)
1303{
1304 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001305 Elf_Sym *near = NULL;
1306 Elf64_Sword distance = 20;
1307 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001308 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001309
1310 if (relsym->st_name != 0)
1311 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001312
1313 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001314 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001315 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001316 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001317 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1318 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001319 if (!is_valid_name(elf, sym))
1320 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001321 if (sym->st_value == addr)
1322 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001323 /* Find a symbol nearby - addr are maybe negative */
1324 d = sym->st_value - addr;
1325 if (d < 0)
1326 d = addr - sym->st_value;
1327 if (d < distance) {
1328 distance = d;
1329 near = sym;
1330 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001331 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001332 /* We need a close match */
1333 if (distance < 20)
1334 return near;
1335 else
1336 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001337}
1338
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001339/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001340 * Find symbols before or equal addr and after addr - in the section sec.
1341 * If we find two symbols with equal offset prefer one with a valid name.
1342 * The ELF format may have a better way to detect what type of symbol
1343 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001344 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001345static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1346 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001347{
1348 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001349 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001350 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001351
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001352 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1353 const char *symsec;
1354
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001355 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001356 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001357 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001358 if (strcmp(symsec, sec) != 0)
1359 continue;
David Brownellda68d612007-02-20 13:58:16 -08001360 if (!is_valid_name(elf, sym))
1361 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001362 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001363 if ((addr - sym->st_value) < distance) {
1364 distance = addr - sym->st_value;
1365 near = sym;
1366 } else if ((addr - sym->st_value) == distance) {
1367 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001368 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001369 }
1370 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001371 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001372}
1373
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001374/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001375 * Convert a section name to the function/data attribute
1376 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001377 * .memexitconst => __memconst
1378 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001379 *
1380 * The memory of returned value has been allocated on a heap. The user of this
1381 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001382*/
1383static char *sec2annotation(const char *s)
1384{
1385 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001386 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001387 char *r = p;
1388
1389 *p++ = '_';
1390 *p++ = '_';
1391 if (*s == '.')
1392 s++;
1393 while (*s && *s != '.')
1394 *p++ = *s++;
1395 *p = '\0';
1396 if (*s == '.')
1397 s++;
1398 if (strstr(s, "rodata") != NULL)
1399 strcat(p, "const ");
1400 else if (strstr(s, "data") != NULL)
1401 strcat(p, "data ");
1402 else
1403 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001404 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001405 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001406 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001407 }
1408}
1409
1410static int is_function(Elf_Sym *sym)
1411{
1412 if (sym)
1413 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1414 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001415 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001416}
1417
Randy Dunlap00759c02011-03-15 14:13:47 -07001418static void print_section_list(const char * const list[20])
1419{
1420 const char *const *s = list;
1421
1422 while (*s) {
1423 fprintf(stderr, "%s", *s);
1424 s++;
1425 if (*s)
1426 fprintf(stderr, ", ");
1427 }
1428 fprintf(stderr, "\n");
1429}
1430
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301431static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1432{
1433 switch (is_func) {
1434 case 0: *name = "variable"; *name_p = ""; break;
1435 case 1: *name = "function"; *name_p = "()"; break;
1436 default: *name = "(unknown reference)"; *name_p = ""; break;
1437 }
1438}
1439
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001440/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001441 * Print a warning about a section mismatch.
1442 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001443 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001444 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001445static void report_sec_mismatch(const char *modname,
1446 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001447 const char *fromsec,
1448 unsigned long long fromaddr,
1449 const char *fromsym,
1450 int from_is_func,
1451 const char *tosec, const char *tosym,
1452 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001453{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001454 const char *from, *from_p;
1455 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001456 char *prl_from;
1457 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001458
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001459 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001460
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301461 get_pretty_name(from_is_func, &from, &from_p);
1462 get_pretty_name(to_is_func, &to, &to_p);
1463
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001464 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1465 "to the %s %s:%s%s\n",
1466 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1467 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001468
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001469 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001470 case TEXT_TO_ANY_INIT:
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,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001474 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001475 "the %s %s%s%s.\n"
1476 "This is often because %s lacks a %s\n"
1477 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001478 prl_from, fromsym,
1479 to, prl_to, tosym, to_p,
1480 fromsym, prl_to, tosym);
1481 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 DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001485 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001486 fprintf(stderr,
1487 "The variable %s references\n"
1488 "the %s %s%s%s\n"
1489 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001490 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001491 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001492 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001493 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001494 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001495 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001496 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001497 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001498 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001499 fprintf(stderr,
1500 "The function %s() references a %s in an exit section.\n"
1501 "Often the %s %s%s has valid usage outside the exit section\n"
1502 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001503 fromsym, to, to, tosym, to_p, prl_to, tosym);
1504 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001505 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001506 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001507 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001508 fprintf(stderr,
1509 "The variable %s references\n"
1510 "the %s %s%s%s\n"
1511 "If the reference is valid then annotate the\n"
1512 "variable with __exit* (see linux/init.h) or "
1513 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001514 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001515 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001516 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001517 break;
1518 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001519 case XXXINIT_TO_SOME_INIT:
1520 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001521 prl_from = sec2annotation(fromsec);
1522 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001523 fprintf(stderr,
1524 "The %s %s%s%s references\n"
1525 "a %s %s%s%s.\n"
1526 "If %s is only used by %s then\n"
1527 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001528 from, prl_from, fromsym, from_p,
1529 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001530 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001531 free(prl_from);
1532 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001533 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001534 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001535 prl_from = sec2annotation(fromsec);
1536 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001537 fprintf(stderr,
1538 "The %s %s%s%s references\n"
1539 "a %s %s%s%s.\n"
1540 "This is often seen when error handling "
1541 "in the init function\n"
1542 "uses functionality in the exit path.\n"
1543 "The fix is often to remove the %sannotation of\n"
1544 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001545 from, prl_from, fromsym, from_p,
1546 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001547 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001548 free(prl_from);
1549 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001550 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001551 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001552 prl_from = sec2annotation(fromsec);
1553 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001554 fprintf(stderr,
1555 "The %s %s%s%s references\n"
1556 "a %s %s%s%s.\n"
1557 "This is often seen when error handling "
1558 "in the exit function\n"
1559 "uses functionality in the init path.\n"
1560 "The fix is often to remove the %sannotation of\n"
1561 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001562 from, prl_from, fromsym, from_p,
1563 to, prl_to, tosym, to_p,
1564 prl_to, tosym, to_p);
1565 free(prl_from);
1566 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001567 break;
1568 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001569 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001570 fprintf(stderr,
1571 "The symbol %s is exported and annotated %s\n"
1572 "Fix this by removing the %sannotation of %s "
1573 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001574 tosym, prl_to, prl_to, tosym);
1575 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001576 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301577 case EXTABLE_TO_NON_TEXT:
1578 fatal("There's a special handler for this mismatch type, "
1579 "we should never get here.");
1580 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001581 }
1582 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001583}
1584
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301585static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1586 const struct sectioncheck* const mismatch,
1587 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1588{
1589 const char *tosec;
1590 Elf_Sym *to;
1591 Elf_Sym *from;
1592 const char *tosym;
1593 const char *fromsym;
1594
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301595 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1596 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301597
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001598 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301599 return;
1600
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301601 tosec = sec_name(elf, get_secindex(elf, sym));
1602 to = find_elf_symbol(elf, r->r_addend, sym);
1603 tosym = sym_name(elf, to);
1604
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301605 /* check whitelist - we may ignore it */
1606 if (secref_whitelist(mismatch,
1607 fromsec, fromsym, tosec, tosym)) {
1608 report_sec_mismatch(modname, mismatch,
1609 fromsec, r->r_offset, fromsym,
1610 is_function(from), tosec, tosym,
1611 is_function(to));
1612 }
1613}
1614
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301615static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1616{
1617 if (section_index > elf->num_sections)
1618 fatal("section_index is outside elf->num_sections!\n");
1619
1620 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1621}
1622
1623/*
1624 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1625 * to know the sizeof(struct exception_table_entry) for the target architecture.
1626 */
1627static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301628static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301629{
1630 /*
1631 * If we're currently checking the second relocation within __ex_table,
1632 * that relocation offset tells us the offsetof(struct
1633 * exception_table_entry, fixup) which is equal to sizeof(struct
1634 * exception_table_entry) divided by two. We use that to our advantage
1635 * since there's no portable way to get that size as every architecture
1636 * seems to go with different sized types. Not pretty but better than
1637 * hard-coding the size for every architecture..
1638 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301639 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301640 extable_entry_size = r->r_offset * 2;
1641}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301642
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301643static inline bool is_extable_fault_address(Elf_Rela *r)
1644{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301645 /*
1646 * extable_entry_size is only discovered after we've handled the
1647 * _second_ relocation in __ex_table, so only abort when we're not
1648 * handling the first reloc and extable_entry_size is zero.
1649 */
1650 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301651 fatal("extable_entry size hasn't been discovered!\n");
1652
1653 return ((r->r_offset == 0) ||
1654 (r->r_offset % extable_entry_size == 0));
1655}
1656
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301657#define is_second_extable_reloc(Start, Cur, Sec) \
1658 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1659
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301660static void report_extable_warnings(const char* modname, struct elf_info* elf,
1661 const struct sectioncheck* const mismatch,
1662 Elf_Rela* r, Elf_Sym* sym,
1663 const char* fromsec, const char* tosec)
1664{
1665 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1666 const char* fromsym_name = sym_name(elf, fromsym);
1667 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1668 const char* tosym_name = sym_name(elf, tosym);
1669 const char* from_pretty_name;
1670 const char* from_pretty_name_p;
1671 const char* to_pretty_name;
1672 const char* to_pretty_name_p;
1673
1674 get_pretty_name(is_function(fromsym),
1675 &from_pretty_name, &from_pretty_name_p);
1676 get_pretty_name(is_function(tosym),
1677 &to_pretty_name, &to_pretty_name_p);
1678
1679 warn("%s(%s+0x%lx): Section mismatch in reference"
1680 " from the %s %s%s to the %s %s:%s%s\n",
1681 modname, fromsec, (long)r->r_offset, from_pretty_name,
1682 fromsym_name, from_pretty_name_p,
1683 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1684
1685 if (!match(tosec, mismatch->bad_tosec) &&
1686 is_executable_section(elf, get_secindex(elf, sym)))
1687 fprintf(stderr,
1688 "The relocation at %s+0x%lx references\n"
1689 "section \"%s\" which is not in the list of\n"
1690 "authorized sections. If you're adding a new section\n"
1691 "and/or if this reference is valid, add \"%s\" to the\n"
1692 "list of authorized sections to jump to on fault.\n"
1693 "This can be achieved by adding \"%s\" to \n"
1694 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1695 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1696}
1697
1698static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1699 const struct sectioncheck* const mismatch,
1700 Elf_Rela* r, Elf_Sym* sym,
1701 const char *fromsec)
1702{
1703 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1704
1705 sec_mismatch_count++;
1706
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001707 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301708
1709 if (match(tosec, mismatch->bad_tosec))
1710 fatal("The relocation at %s+0x%lx references\n"
1711 "section \"%s\" which is black-listed.\n"
1712 "Something is seriously wrong and should be fixed.\n"
1713 "You might get more information about where this is\n"
1714 "coming from by using scripts/check_extable.sh %s\n",
1715 fromsec, (long)r->r_offset, tosec, modname);
1716 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1717 if (is_extable_fault_address(r))
1718 fatal("The relocation at %s+0x%lx references\n"
1719 "section \"%s\" which is not executable, IOW\n"
1720 "it is not possible for the kernel to fault\n"
1721 "at that address. Something is seriously wrong\n"
1722 "and should be fixed.\n",
1723 fromsec, (long)r->r_offset, tosec);
1724 else
1725 fatal("The relocation at %s+0x%lx references\n"
1726 "section \"%s\" which is not executable, IOW\n"
1727 "the kernel will fault if it ever tries to\n"
1728 "jump to it. Something is seriously wrong\n"
1729 "and should be fixed.\n",
1730 fromsec, (long)r->r_offset, tosec);
1731 }
1732}
1733
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001734static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001735 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001736{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001737 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301738 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001739
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001740 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301741 if (mismatch->handler)
1742 mismatch->handler(modname, elf, mismatch,
1743 r, sym, fromsec);
1744 else
1745 default_mismatch_handler(modname, elf, mismatch,
1746 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001747 }
1748}
1749
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001750static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001751 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001752{
1753 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001754 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001755
1756 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001757 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001758}
1759
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001760static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001761{
1762 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001763 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001764
1765 switch (r_typ) {
1766 case R_386_32:
1767 r->r_addend = TO_NATIVE(*location);
1768 break;
1769 case R_386_PC32:
1770 r->r_addend = TO_NATIVE(*location) + 4;
1771 /* For CONFIG_RELOCATABLE=y */
1772 if (elf->hdr->e_type == ET_EXEC)
1773 r->r_addend += r->r_offset;
1774 break;
1775 }
1776 return 0;
1777}
1778
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001779#ifndef R_ARM_CALL
1780#define R_ARM_CALL 28
1781#endif
1782#ifndef R_ARM_JUMP24
1783#define R_ARM_JUMP24 29
1784#endif
1785
David A. Longc9698e52014-02-14 22:41:18 +01001786#ifndef R_ARM_THM_CALL
1787#define R_ARM_THM_CALL 10
1788#endif
1789#ifndef R_ARM_THM_JUMP24
1790#define R_ARM_THM_JUMP24 30
1791#endif
1792#ifndef R_ARM_THM_JUMP19
1793#define R_ARM_THM_JUMP19 51
1794#endif
1795
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001796static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001797{
1798 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1799
1800 switch (r_typ) {
1801 case R_ARM_ABS32:
1802 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001803 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001804 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001805 break;
1806 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001807 case R_ARM_CALL:
1808 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001809 case R_ARM_THM_CALL:
1810 case R_ARM_THM_JUMP24:
1811 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001812 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001813 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001814 sechdr->sh_offset +
1815 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001816 break;
1817 default:
1818 return 1;
1819 }
1820 return 0;
1821}
1822
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001823static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001824{
1825 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001826 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001827 unsigned int inst;
1828
1829 if (r_typ == R_MIPS_HI16)
1830 return 1; /* skip this */
1831 inst = TO_NATIVE(*location);
1832 switch (r_typ) {
1833 case R_MIPS_LO16:
1834 r->r_addend = inst & 0xffff;
1835 break;
1836 case R_MIPS_26:
1837 r->r_addend = (inst & 0x03ffffff) << 2;
1838 break;
1839 case R_MIPS_32:
1840 r->r_addend = inst;
1841 break;
1842 }
1843 return 0;
1844}
1845
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001846static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001847 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001848{
1849 Elf_Sym *sym;
1850 Elf_Rela *rela;
1851 Elf_Rela r;
1852 unsigned int r_sym;
1853 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001854
Sam Ravnborgff13f922008-01-23 19:54:27 +01001855 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001856 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1857
Sam Ravnborgff13f922008-01-23 19:54:27 +01001858 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001859 fromsec += strlen(".rela");
1860 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001861 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001862 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001863
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001864 for (rela = start; rela < stop; rela++) {
1865 r.r_offset = TO_NATIVE(rela->r_offset);
1866#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001867 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001868 unsigned int r_typ;
1869 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1870 r_sym = TO_NATIVE(r_sym);
1871 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1872 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1873 } else {
1874 r.r_info = TO_NATIVE(rela->r_info);
1875 r_sym = ELF_R_SYM(r.r_info);
1876 }
1877#else
1878 r.r_info = TO_NATIVE(rela->r_info);
1879 r_sym = ELF_R_SYM(r.r_info);
1880#endif
1881 r.r_addend = TO_NATIVE(rela->r_addend);
1882 sym = elf->symtab_start + r_sym;
1883 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001884 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001885 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301886 if (is_second_extable_reloc(start, rela, fromsec))
1887 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001888 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001889 }
1890}
1891
1892static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001893 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001894{
1895 Elf_Sym *sym;
1896 Elf_Rel *rel;
1897 Elf_Rela r;
1898 unsigned int r_sym;
1899 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001900
Sam Ravnborgff13f922008-01-23 19:54:27 +01001901 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001902 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1903
Sam Ravnborgff13f922008-01-23 19:54:27 +01001904 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001905 fromsec += strlen(".rel");
1906 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001907 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001908 return;
1909
1910 for (rel = start; rel < stop; rel++) {
1911 r.r_offset = TO_NATIVE(rel->r_offset);
1912#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001913 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001914 unsigned int r_typ;
1915 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1916 r_sym = TO_NATIVE(r_sym);
1917 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1918 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1919 } else {
1920 r.r_info = TO_NATIVE(rel->r_info);
1921 r_sym = ELF_R_SYM(r.r_info);
1922 }
1923#else
1924 r.r_info = TO_NATIVE(rel->r_info);
1925 r_sym = ELF_R_SYM(r.r_info);
1926#endif
1927 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001928 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001929 case EM_386:
1930 if (addend_386_rel(elf, sechdr, &r))
1931 continue;
1932 break;
1933 case EM_ARM:
1934 if (addend_arm_rel(elf, sechdr, &r))
1935 continue;
1936 break;
1937 case EM_MIPS:
1938 if (addend_mips_rel(elf, sechdr, &r))
1939 continue;
1940 break;
1941 }
1942 sym = elf->symtab_start + r_sym;
1943 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001944 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001945 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301946 if (is_second_extable_reloc(start, rel, fromsec))
1947 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001948 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001949 }
1950}
1951
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001952/**
1953 * A module includes a number of sections that are discarded
1954 * either when loaded or when used as built-in.
1955 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001956 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001957 * Likewise for modules used built-in the sections marked __exit
1958 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001959 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001960 * The check_sec_ref() function traverses all relocation records
1961 * to find all references to a section that reference a section that will
1962 * be discarded and warns about it.
1963 **/
1964static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001965 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001966{
1967 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001968 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001969
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001970 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001971 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001972 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001973 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001974 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001975 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001976 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001977 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001978 }
1979}
1980
Andi Kleen7d02b492014-02-08 09:01:12 +01001981static char *remove_dot(char *s)
1982{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301983 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001984
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301985 if (n && s[n]) {
1986 size_t m = strspn(s + n + 1, "0123456789");
1987 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001988 s[n] = 0;
1989 }
1990 return s;
1991}
1992
Masahiro Yamada8b185742018-05-09 18:50:40 +09001993static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 const char *symname;
1996 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001997 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001998 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 struct module *mod;
2000 struct elf_info info = { };
2001 Elf_Sym *sym;
2002
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002003 if (!parse_elf(&info, modname))
2004 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 mod = new_module(modname);
2007
2008 /* When there's no vmlinux, don't print warnings about
2009 * unresolved symbols (since there'll be too many ;) */
2010 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 mod->skip = 1;
2013 }
2014
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002015 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002016 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002017 warn("modpost: missing MODULE_LICENSE() in %s\n"
2018 "see include/linux/module.h for "
2019 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002020 while (license) {
2021 if (license_is_gpl_compatible(license))
2022 mod->gpl_compatible = 1;
2023 else {
2024 mod->gpl_compatible = 0;
2025 break;
2026 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002027 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002028 }
2029
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002030 namespace = get_modinfo(&info, "import_ns");
2031 while (namespace) {
2032 add_namespace(&mod->imported_namespaces, namespace);
2033 namespace = get_next_modinfo(&info, "import_ns", namespace);
2034 }
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002037 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 handle_modversions(mod, &info, sym, symname);
2040 handle_moddevtable(mod, &info, sym, symname);
2041 }
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002042 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002043 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002045 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (version)
2047 maybe_frob_rcs_version(modname, version, info.modinfo,
2048 version - (char *)info.hdr);
2049 if (version || (all_versions && !is_vmlinux(modname)))
2050 get_src_version(modname, mod->srcversion,
2051 sizeof(mod->srcversion)-1);
2052
2053 parse_elf_finish(&info);
2054
Rusty Russell8c8ef422009-03-31 13:05:34 -06002055 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 * never passed as an argument to an exported function, so
2057 * the automatic versioning doesn't pick it up, but it's really
2058 * important anyhow */
2059 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002060 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
Rusty Russell712f9b42013-04-04 17:37:38 +10302063static void read_symbols_from_files(const char *filename)
2064{
2065 FILE *in = stdin;
2066 char fname[PATH_MAX];
2067
2068 if (strcmp(filename, "-") != 0) {
2069 in = fopen(filename, "r");
2070 if (!in)
2071 fatal("Can't open filenames file %s: %m", filename);
2072 }
2073
2074 while (fgets(fname, PATH_MAX, in) != NULL) {
2075 if (strends(fname, "\n"))
2076 fname[strlen(fname)-1] = '\0';
2077 read_symbols(fname);
2078 }
2079
2080 if (in != stdin)
2081 fclose(in);
2082}
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084#define SZ 500
2085
2086/* We first write the generated file into memory using the
2087 * following helper, then compare to the file on disk and
2088 * only update the later if anything changed */
2089
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002090void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2091 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 char tmp[SZ];
2094 int len;
2095 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 va_start(ap, fmt);
2098 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002099 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 va_end(ap);
2101}
2102
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002103void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002106 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002107 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109 strncpy(buf->p + buf->pos, s, len);
2110 buf->pos += len;
2111}
2112
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002113static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2114{
2115 const char *e = is_vmlinux(m) ?"":".ko";
2116
2117 switch (exp) {
2118 case export_gpl:
2119 fatal("modpost: GPL-incompatible module %s%s "
2120 "uses GPL-only symbol '%s'\n", m, e, s);
2121 break;
2122 case export_unused_gpl:
2123 fatal("modpost: GPL-incompatible module %s%s "
2124 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2125 break;
2126 case export_gpl_future:
2127 warn("modpost: GPL-incompatible module %s%s "
2128 "uses future GPL-only symbol '%s'\n", m, e, s);
2129 break;
2130 case export_plain:
2131 case export_unused:
2132 case export_unknown:
2133 /* ignore */
2134 break;
2135 }
2136}
2137
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002138static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002139{
2140 const char *e = is_vmlinux(m) ?"":".ko";
2141
2142 switch (exp) {
2143 case export_unused:
2144 case export_unused_gpl:
2145 warn("modpost: module %s%s "
2146 "uses symbol '%s' marked UNUSED\n", m, e, s);
2147 break;
2148 default:
2149 /* ignore */
2150 break;
2151 }
2152}
2153
Masahiro Yamada3b415282018-11-23 16:57:23 +09002154static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002155{
2156 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002157 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002158
2159 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002160 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002161 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002162 if (!exp || exp->module == mod) {
2163 if (have_vmlinux && !s->weak) {
2164 if (warn_unresolved) {
2165 warn("\"%s\" [%s.ko] undefined!\n",
2166 s->name, mod->name);
2167 } else {
2168 merror("\"%s\" [%s.ko] undefined!\n",
2169 s->name, mod->name);
2170 err = 1;
2171 }
2172 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002173 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002174 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002175 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002176 if (basename)
2177 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002178 else
2179 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002180
Matthias Maennich1d082772019-09-06 11:32:31 +01002181 if (exp->namespace) {
2182 add_namespace(&mod->required_namespaces,
2183 exp->namespace);
2184
2185 if (!write_namespace_deps &&
2186 !module_imports_namespace(mod, exp->namespace)) {
2187 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2188 basename, exp->name, exp->namespace);
2189 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002190 }
2191
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002192 if (!mod->gpl_compatible)
2193 check_for_gpl_usage(exp->export, basename, exp->name);
2194 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002195 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002196
2197 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002198}
2199
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002200static int check_modname_len(struct module *mod)
2201{
2202 const char *mod_name;
2203
2204 mod_name = strrchr(mod->name, '/');
2205 if (mod_name == NULL)
2206 mod_name = mod->name;
2207 else
2208 mod_name++;
2209 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2210 merror("module name is too long [%s.ko]\n", mod->name);
2211 return 1;
2212 }
2213
2214 return 0;
2215}
2216
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002217/**
2218 * Header for the generated file
2219 **/
2220static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Laura Abbott9afb7192018-07-05 17:49:37 -07002222 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 buf_printf(b, "#include <linux/module.h>\n");
2224 buf_printf(b, "#include <linux/vermagic.h>\n");
2225 buf_printf(b, "#include <linux/compiler.h>\n");
2226 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002227 buf_printf(b, "BUILD_SALT;\n");
2228 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002230 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302232 buf_printf(b, "__visible struct module __this_module\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002234 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002236 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (mod->has_cleanup)
2238 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002239 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002241 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 buf_printf(b, "};\n");
2243}
2244
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002245static void add_intree_flag(struct buffer *b, int is_intree)
2246{
2247 if (is_intree)
2248 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2249}
2250
Andi Kleencaf75012018-01-25 15:50:28 -08002251/* Cannot check for assembler */
2252static void add_retpoline(struct buffer *b)
2253{
WANG Chaoe4f35892018-12-11 00:37:25 +08002254 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002255 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2256 buf_printf(b, "#endif\n");
2257}
2258
Trevor Keith5c725132009-09-22 16:43:38 -07002259static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002260{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002261 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002262 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2263}
2264
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002265/**
2266 * Record CRCs for unresolved symbols
2267 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002268static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002271 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 for (s = mod->unres; s; s = s->next) {
2274 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002275 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 s->module = exp->module;
2278 s->crc_valid = exp->crc_valid;
2279 s->crc = exp->crc;
2280 }
2281
2282 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002283 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 buf_printf(b, "\n");
2286 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01002287 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
2289
2290 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002291 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002294 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 s->name, mod->name);
2296 continue;
2297 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302298 if (strlen(s->name) >= MODULE_NAME_LEN) {
2299 merror("too long symbol \"%s\" [%s.ko]\n",
2300 s->name, mod->name);
2301 err = 1;
2302 break;
2303 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002304 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302305 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307
2308 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002309
2310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002313static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 int first = 1;
2317
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002318 /* Clear ->seen flag of modules that own symbols needed by this. */
2319 for (s = mod->unres; s; s = s->next)
2320 if (s->module)
2321 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 buf_printf(b, "\n");
2324 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01002325 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
2327 buf_printf(b, "\"depends=");
2328 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002329 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (!s->module)
2331 continue;
2332
2333 if (s->module->seen)
2334 continue;
2335
2336 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002337 p = strrchr(s->module->name, '/');
2338 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002339 p++;
2340 else
2341 p = s->module->name;
2342 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 first = 0;
2344 }
2345 buf_printf(b, "\";\n");
2346}
2347
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002348static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 if (mod->srcversion[0]) {
2351 buf_printf(b, "\n");
2352 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2353 mod->srcversion);
2354 }
2355}
2356
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002357static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 char *tmp;
2360 FILE *file;
2361 struct stat st;
2362
2363 file = fopen(fname, "r");
2364 if (!file)
2365 goto write;
2366
2367 if (fstat(fileno(file), &st) < 0)
2368 goto close_write;
2369
2370 if (st.st_size != b->pos)
2371 goto close_write;
2372
2373 tmp = NOFAIL(malloc(b->pos));
2374 if (fread(tmp, 1, b->pos, file) != b->pos)
2375 goto free_write;
2376
2377 if (memcmp(tmp, b->p, b->pos) != 0)
2378 goto free_write;
2379
2380 free(tmp);
2381 fclose(file);
2382 return;
2383
2384 free_write:
2385 free(tmp);
2386 close_write:
2387 fclose(file);
2388 write:
2389 file = fopen(fname, "w");
2390 if (!file) {
2391 perror(fname);
2392 exit(1);
2393 }
2394 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2395 perror(fname);
2396 exit(1);
2397 }
2398 fclose(file);
2399}
2400
Ram Paibd5cbce2006-06-08 22:12:53 -07002401/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002402 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002403 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002404static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 unsigned long size, pos = 0;
2407 void *file = grab_file(fname, &size);
2408 char *line;
2409
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002410 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /* No symbol versions, silently ignore */
2412 return;
2413
2414 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002415 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 unsigned int crc;
2417 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002418 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 if (!(symname = strchr(line, '\t')))
2421 goto fail;
2422 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002423 if (!(namespace = strchr(symname, '\t')))
2424 goto fail;
2425 *namespace++ = '\0';
2426 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 goto fail;
2428 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002429 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002430 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002431 if (export && ((end = strchr(export, '\t')) != NULL))
2432 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 crc = strtoul(line, &d, 16);
2434 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2435 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002436 mod = find_module(modname);
2437 if (!mod) {
2438 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002440 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 mod->skip = 1;
2442 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002443 s = sym_add_exported(symname, namespace, mod,
2444 export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002445 s->kernel = kernel;
2446 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07002447 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302449 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return;
2451fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302452 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 fatal("parse error in symbol dump file\n");
2454}
2455
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002456/* For normal builds always dump all symbols.
2457 * For external modules only dump symbols
2458 * that are not read from kernel Module.symvers.
2459 **/
2460static int dump_sym(struct symbol *sym)
2461{
2462 if (!external_module)
2463 return 1;
2464 if (sym->vmlinux || sym->kernel)
2465 return 0;
2466 return 1;
2467}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002468
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002469static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 struct buffer buf = { };
2472 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002473 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 int n;
2475
2476 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2477 symbol = symbolhash[n];
2478 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002479 if (dump_sym(symbol)) {
2480 namespace = symbol->namespace;
2481 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2482 symbol->crc, symbol->name,
2483 namespace ? namespace : "",
2484 symbol->module->name,
2485 export_str(symbol->export));
2486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 symbol = symbol->next;
2488 }
2489 }
2490 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002491 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Matthias Maennich1d082772019-09-06 11:32:31 +01002494static void write_namespace_deps_files(void)
2495{
2496 struct module *mod;
2497 struct namespace_list *ns;
2498 struct buffer ns_deps_buf = {};
2499
2500 for (mod = modules; mod; mod = mod->next) {
2501 char fname[PATH_MAX];
2502
2503 if (mod->skip)
2504 continue;
2505
2506 ns_deps_buf.pos = 0;
2507
2508 for (ns = mod->required_namespaces; ns; ns = ns->next)
2509 buf_printf(&ns_deps_buf, "%s\n", ns->namespace);
2510
2511 if (ns_deps_buf.pos == 0)
2512 continue;
2513
2514 sprintf(fname, "%s.ns_deps", mod->name);
2515 write_if_changed(&ns_deps_buf, fname);
2516 }
2517}
2518
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002519struct ext_sym_list {
2520 struct ext_sym_list *next;
2521 const char *file;
2522};
2523
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002524int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 struct module *mod;
2527 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002528 char *kernel_read = NULL, *module_read = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302529 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002531 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002532 struct ext_sym_list *extsym_iter;
2533 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Matthias Maennich1d082772019-09-06 11:32:31 +01002535 while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awEd")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002536 switch (opt) {
2537 case 'i':
2538 kernel_read = optarg;
2539 break;
2540 case 'I':
2541 module_read = optarg;
2542 external_module = 1;
2543 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002544 case 'e':
2545 external_module = 1;
2546 extsym_iter =
2547 NOFAIL(malloc(sizeof(*extsym_iter)));
2548 extsym_iter->next = extsym_start;
2549 extsym_iter->file = optarg;
2550 extsym_start = extsym_iter;
2551 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002552 case 'm':
2553 modversions = 1;
2554 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302555 case 'n':
2556 ignore_missing_files = 1;
2557 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002558 case 'o':
2559 dump_write = optarg;
2560 break;
2561 case 'a':
2562 all_versions = 1;
2563 break;
2564 case 's':
2565 vmlinux_section_warnings = 0;
2566 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302567 case 'T':
2568 files_source = optarg;
2569 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002570 case 'w':
2571 warn_unresolved = 1;
2572 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302573 case 'E':
2574 sec_mismatch_fatal = 1;
2575 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002576 case 'd':
2577 write_namespace_deps = 1;
2578 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002579 default:
2580 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582 }
2583
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002584 if (kernel_read)
2585 read_dump(kernel_read, 1);
2586 if (module_read)
2587 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002588 while (extsym_start) {
2589 read_dump(extsym_start->file, 0);
2590 extsym_iter = extsym_start->next;
2591 free(extsym_start);
2592 extsym_start = extsym_iter;
2593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002595 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Rusty Russell712f9b42013-04-04 17:37:38 +10302598 if (files_source)
2599 read_symbols_from_files(files_source);
2600
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002601 err = 0;
2602
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002603 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302604 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002605
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002606 if (mod->skip)
2607 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 buf.pos = 0;
2610
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002611 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002612 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002613 if (write_namespace_deps)
2614 continue;
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002617 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002618 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002619 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002620 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002621 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 add_moddevtable(&buf, mod);
2623 add_srcversion(&buf, mod);
2624
2625 sprintf(fname, "%s.mod.c", mod->name);
2626 write_if_changed(&buf, fname);
2627 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002628
2629 if (write_namespace_deps) {
2630 write_namespace_deps_files();
2631 return 0;
2632 }
2633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (dump_write)
2635 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002636 if (sec_mismatch_count && sec_mismatch_fatal)
2637 fatal("modpost: Section mismatches detected.\n"
2638 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002639 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}