blob: bc00bbac50bba1851aa9babbe4d14a730e7a8dd0 [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
Masahiro Yamada5370d4a2020-01-05 00:36:51 +090015#include <elf.h>
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080016#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070018#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103019#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103020#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093021#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020023#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093026static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093028static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
30static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010031/* If we are modposting external module set to 1 */
32static int external_module = 0;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070033/* Only warn about unresolved symbols */
34static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070035/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010036static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103037static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093038/* ignore missing files */
39static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010040/* If set to 1, only warn (instead of error) about missing ns imports */
41static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010042
Sam Ravnborgc96fca22006-07-01 11:44:23 +020043enum export {
44 export_plain, export_unused, export_gpl,
45 export_unused_gpl, export_gpl_future, export_unknown
46};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080048/* In kernel, this size is defined in linux/module.h;
49 * here we use Elf_Addr instead of long for covering cross-compile
50 */
51
52#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
53
Jessica Yu93c95e52020-03-06 17:02:05 +010054void __attribute__((format(printf, 2, 3)))
55modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
Jessica Yu93c95e52020-03-06 17:02:05 +010059 switch (loglevel) {
60 case LOG_WARN:
61 fprintf(stderr, "WARNING: ");
62 break;
63 case LOG_ERROR:
64 fprintf(stderr, "ERROR: ");
65 break;
66 case LOG_FATAL:
67 fprintf(stderr, "FATAL: ");
68 break;
69 default: /* invalid loglevel, ignore */
70 break;
71 }
72
73 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 va_start(arglist, fmt);
76 vfprintf(stderr, fmt, arglist);
77 va_end(arglist);
78
Jessica Yu93c95e52020-03-06 17:02:05 +010079 if (loglevel == LOG_FATAL)
80 exit(1);
Matthew Wilcox2a116652006-10-07 05:35:32 -060081}
82
Rusty Russelld4ef1c32013-04-04 17:37:32 +103083static inline bool strends(const char *str, const char *postfix)
84{
85 if (strlen(str) < strlen(postfix))
86 return false;
87
88 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
89}
90
Sam Ravnborg040fcc82006-01-28 22:15:55 +010091static int is_vmlinux(const char *modname)
92{
93 const char *myname;
94
Sam Ravnborgdf578e72008-01-11 19:17:15 +010095 myname = strrchr(modname, '/');
96 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010097 myname++;
98 else
99 myname = modname;
100
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200101 return (strcmp(myname, "vmlinux") == 0) ||
102 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void *do_nofail(void *ptr, const char *expr)
106{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100107 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100108 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return ptr;
111}
112
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900113char *read_text_file(const char *filename)
114{
115 struct stat st;
116 size_t nbytes;
117 int fd;
118 char *buf;
119
120 fd = open(filename, O_RDONLY);
121 if (fd < 0) {
122 perror(filename);
123 exit(1);
124 }
125
126 if (fstat(fd, &st) < 0) {
127 perror(filename);
128 exit(1);
129 }
130
131 buf = NOFAIL(malloc(st.st_size + 1));
132
133 nbytes = st.st_size;
134
135 while (nbytes) {
136 ssize_t bytes_read;
137
138 bytes_read = read(fd, buf, nbytes);
139 if (bytes_read < 0) {
140 perror(filename);
141 exit(1);
142 }
143
144 nbytes -= bytes_read;
145 }
146 buf[st.st_size] = '\0';
147
148 close(fd);
149
150 return buf;
151}
152
153char *get_line(char **stringp)
154{
155 /* do not return the unwanted extra line at EOF */
156 if (*stringp && **stringp == '\0')
157 return NULL;
158
159 return strsep(stringp, "\n");
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static struct module *modules;
164
Masahiro Yamada8b185742018-05-09 18:50:40 +0900165static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct module *mod;
168
169 for (mod = modules; mod; mod = mod->next)
170 if (strcmp(mod->name, modname) == 0)
171 break;
172 return mod;
173}
174
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030175static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030178 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 mod = NOFAIL(malloc(sizeof(*mod)));
181 memset(mod, 0, sizeof(*mod));
182 p = NOFAIL(strdup(modname));
183
184 /* strip trailing .o */
Masahiro Yamada33795762020-06-01 14:57:24 +0900185 if (strends(p, ".o"))
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030186 p[strlen(p) - 2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /* add to list */
189 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200190 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 mod->next = modules;
192 modules = mod;
193
194 return mod;
195}
196
197/* A hash of all exported symbols,
198 * struct symbol is also used for lists of unresolved symbols */
199
200#define SYMBOL_HASH_SIZE 1024
201
202struct symbol {
203 struct symbol *next;
204 struct module *module;
205 unsigned int crc;
206 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900207 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300209 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700210 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500211 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
214static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
215
216/* This is based on the hash agorithm from gdbm, via tdb */
217static inline unsigned int tdb_hash(const char *name)
218{
219 unsigned value; /* Used to compute the hash value. */
220 unsigned i; /* Used to cycle through random values. */
221
222 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100223 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
225
226 return (1103515243 * value + 12345);
227}
228
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100229/**
230 * Allocate a new symbols for use in the hash of exported symbols or
231 * the list of unresolved symbols per module
232 **/
233static struct symbol *alloc_symbol(const char *name, unsigned int weak,
234 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
237
238 memset(s, 0, sizeof(*s));
239 strcpy(s->name, name);
240 s->weak = weak;
241 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300242 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return s;
244}
245
246/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700247static struct symbol *new_symbol(const char *name, struct module *module,
248 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900253 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
254
255 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100258static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct symbol *s;
261
262 /* For our purposes, .foo matches foo. PPC64 needs this. */
263 if (name[0] == '.')
264 name++;
265
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100266 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (strcmp(s->name, name) == 0)
268 return s;
269 }
270 return NULL;
271}
272
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100273static bool contains_namespace(struct namespace_list *list,
274 const char *namespace)
275{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900276 for (; list; list = list->next)
277 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100278 return true;
279
280 return false;
281}
282
283static void add_namespace(struct namespace_list **list, const char *namespace)
284{
285 struct namespace_list *ns_entry;
286
287 if (!contains_namespace(*list, namespace)) {
288 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
289 strlen(namespace) + 1));
290 strcpy(ns_entry->namespace, namespace);
291 ns_entry->next = *list;
292 *list = ns_entry;
293 }
294}
295
296static bool module_imports_namespace(struct module *module,
297 const char *namespace)
298{
299 return contains_namespace(module->imported_namespaces, namespace);
300}
301
Mathias Krause7a3ee752014-08-27 20:28:53 +0930302static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700303 const char *str;
304 enum export export;
305} export_list[] = {
306 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200307 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700308 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200309 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700310 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
311 { .str = "(unknown)", .export = export_unknown },
312};
313
314
315static const char *export_str(enum export ex)
316{
317 return export_list[ex].str;
318}
319
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100320static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700321{
322 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100323
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200324 if (!s)
325 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700326 for (i = 0; export_list[i].export != export_unknown; i++) {
327 if (strcmp(export_list[i].str, s) == 0)
328 return export_list[i].export;
329 }
330 return export_unknown;
331}
332
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900333static void *sym_get_data_by_offset(const struct elf_info *info,
334 unsigned int secindex, unsigned long offset)
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900335{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800336 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900337
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900338 if (info->hdr->e_type != ET_REL)
339 offset -= sechdr->sh_addr;
340
341 return (void *)info->hdr + sechdr->sh_offset + offset;
342}
343
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900344static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
345{
346 return sym_get_data_by_offset(info, get_secindex(info, sym),
347 sym->st_value);
348}
349
Masahiro Yamada565587d2020-05-25 14:47:05 +0900350static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
351{
352 return sym_get_data_by_offset(info, info->secindex_strings,
353 sechdr->sh_name);
354}
355
356static const char *sec_name(const struct elf_info *info, int secindex)
357{
358 return sech_name(info, &info->sechdrs[secindex]);
359}
360
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200361#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
362
363static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
364{
365 const char *secname = sec_name(elf, sec);
366
367 if (strstarts(secname, "___ksymtab+"))
368 return export_plain;
369 else if (strstarts(secname, "___ksymtab_unused+"))
370 return export_unused;
371 else if (strstarts(secname, "___ksymtab_gpl+"))
372 return export_gpl;
373 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
374 return export_unused_gpl;
375 else if (strstarts(secname, "___ksymtab_gpl_future+"))
376 return export_gpl_future;
377 else
378 return export_unknown;
379}
380
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200381static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700382{
383 if (sec == elf->export_sec)
384 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200385 else if (sec == elf->export_unused_sec)
386 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700387 else if (sec == elf->export_gpl_sec)
388 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200389 else if (sec == elf->export_unused_gpl_sec)
390 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700391 else if (sec == elf->export_gpl_future_sec)
392 return export_gpl_future;
393 else
394 return export_unknown;
395}
396
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900397static const char *namespace_from_kstrtabns(const struct elf_info *info,
398 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100399{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900400 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100401 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100402}
403
Matthias Maennicha2b11182019-10-18 10:31:40 +0100404static void sym_update_namespace(const char *symname, const char *namespace)
405{
406 struct symbol *s = find_symbol(symname);
407
408 /*
409 * That symbol should have been created earlier and thus this is
410 * actually an assertion.
411 */
412 if (!s) {
413 merror("Could not update namespace(%s) for symbol %s\n",
414 namespace, symname);
415 return;
416 }
417
418 free(s->namespace);
419 s->namespace =
420 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
421}
422
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100423/**
424 * Add an exported symbol - it may have already been added without a
425 * CRC, in this case just update the CRC
426 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100427static struct symbol *sym_add_exported(const char *name, struct module *mod,
428 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct symbol *s = find_symbol(name);
431
432 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700433 s = new_symbol(name, mod, export);
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900434 } else if (!external_module || is_vmlinux(s->module->name) ||
435 s->module == mod) {
436 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
437 mod->name, name, s->module->name,
438 is_vmlinux(s->module->name) ? "" : ".ko");
439 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900441
442 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700443 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100444 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Masahiro Yamada17436942019-11-15 02:42:24 +0900447static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100448{
449 struct symbol *s = find_symbol(name);
450
Masahiro Yamada17436942019-11-15 02:42:24 +0900451 /*
452 * Ignore stand-alone __crc_*, which might be auto-generated symbols
453 * such as __*_veneer in ARM ELF.
454 */
455 if (!s)
456 return;
457
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100458 s->crc = crc;
459 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Masahiro Yamada75893572020-06-01 14:57:21 +0900462static void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930465 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int fd;
467
468 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930469 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930471 if (fstat(fd, &st))
472 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 *size = st.st_size;
475 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930477failed:
478 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (map == MAP_FAILED)
480 return NULL;
481 return map;
482}
483
Masahiro Yamada75893572020-06-01 14:57:21 +0900484static void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 munmap(file, size);
487}
488
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100489static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100492 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 Elf_Shdr *sechdrs;
494 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200495 const char *secstrings;
496 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 hdr = grab_file(filename, &info->size);
499 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930500 if (ignore_missing_files) {
501 fprintf(stderr, "%s: %s (ignored)\n", filename,
502 strerror(errno));
503 return 0;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200506 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100509 if (info->size < sizeof(*hdr)) {
510 /* file too small, assume this is an empty .o file */
511 return 0;
512 }
513 /* Is this a valid ELF file? */
514 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
515 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
516 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
517 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
518 /* Not an ELF file - silently ignore it */
519 return 0;
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200522 hdr->e_type = TO_NATIVE(hdr->e_type);
523 hdr->e_machine = TO_NATIVE(hdr->e_machine);
524 hdr->e_version = TO_NATIVE(hdr->e_version);
525 hdr->e_entry = TO_NATIVE(hdr->e_entry);
526 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
527 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
528 hdr->e_flags = TO_NATIVE(hdr->e_flags);
529 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
530 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
531 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
532 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
533 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
534 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 sechdrs = (void *)hdr + hdr->e_shoff;
536 info->sechdrs = sechdrs;
537
Petr Stetiara83710e2007-08-27 12:15:07 +0200538 /* Check if file offset is correct */
539 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100540 fatal("section header offset=%lu in file '%s' is bigger than "
541 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
542 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200543 return 0;
544 }
545
Anders Kaseorg68457562011-05-19 16:55:27 -0600546 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200547 /*
548 * There are more than 64k sections,
549 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200550 */
551 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
552 }
553 else {
554 info->num_sections = hdr->e_shnum;
555 }
556 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600557 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200558 }
559 else {
560 info->secindex_strings = hdr->e_shstrndx;
561 }
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200564 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200565 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
566 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
567 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
568 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
569 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
570 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
571 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
572 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
573 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
574 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200577 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
578 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700579 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900580 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Tejun Heo56fc82c2009-02-06 00:48:02 +0900582 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100583 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
584 "sizeof(*hrd)=%zu\n", filename,
585 (unsigned long)sechdrs[i].sh_offset,
586 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100587 return 0;
588 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700589 secname = secstrings + sechdrs[i].sh_name;
590 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900591 if (nobits)
592 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
594 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700595 } else if (strcmp(secname, "__ksymtab") == 0)
596 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200597 else if (strcmp(secname, "__ksymtab_unused") == 0)
598 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700599 else if (strcmp(secname, "__ksymtab_gpl") == 0)
600 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200601 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
602 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700603 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
604 info->export_gpl_future_sec = i;
605
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200606 if (sechdrs[i].sh_type == SHT_SYMTAB) {
607 unsigned int sh_link_idx;
608 symtab_idx = i;
609 info->symtab_start = (void *)hdr +
610 sechdrs[i].sh_offset;
611 info->symtab_stop = (void *)hdr +
612 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600613 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200614 info->strtab = (void *)hdr +
615 sechdrs[sh_link_idx].sh_offset;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200618 /* 32bit section no. table? ("more than 64k sections") */
619 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
620 symtab_shndx_idx = i;
621 info->symtab_shndx_start = (void *)hdr +
622 sechdrs[i].sh_offset;
623 info->symtab_shndx_stop = (void *)hdr +
624 sechdrs[i].sh_offset + sechdrs[i].sh_size;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100627 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100628 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Fix endianness in symbols */
631 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
632 sym->st_shndx = TO_NATIVE(sym->st_shndx);
633 sym->st_name = TO_NATIVE(sym->st_name);
634 sym->st_value = TO_NATIVE(sym->st_value);
635 sym->st_size = TO_NATIVE(sym->st_size);
636 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200637
638 if (symtab_shndx_idx != ~0U) {
639 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600640 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200641 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600642 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200643 symtab_idx);
644 /* Fix endianness */
645 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
646 p++)
647 *p = TO_NATIVE(*p);
648 }
649
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100650 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100653static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 release_file(info->hdr, info->size);
656}
657
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200658static int ignore_undef_symbol(struct elf_info *info, const char *symname)
659{
660 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900661 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200662 return 1;
663 /* ignore global offset table */
664 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
665 return 1;
666 if (info->hdr->e_machine == EM_PPC)
667 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900668 if (strstarts(symname, "_restgpr_") ||
669 strstarts(symname, "_savegpr_") ||
670 strstarts(symname, "_rest32gpr_") ||
671 strstarts(symname, "_save32gpr_") ||
672 strstarts(symname, "_restvr_") ||
673 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200674 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000675 if (info->hdr->e_machine == EM_PPC64)
676 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900677 if (strstarts(symname, "_restgpr0_") ||
678 strstarts(symname, "_savegpr0_") ||
679 strstarts(symname, "_restvr_") ||
680 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100681 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000682 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200683 /* Do not ignore this symbol */
684 return 0;
685}
686
Masahiro Yamada17436942019-11-15 02:42:24 +0900687static void handle_modversion(const struct module *mod,
688 const struct elf_info *info,
689 const Elf_Sym *sym, const char *symname)
690{
691 unsigned int crc;
692
693 if (sym->st_shndx == SHN_UNDEF) {
694 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
695 symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
696 return;
697 }
698
699 if (sym->st_shndx == SHN_ABS) {
700 crc = sym->st_value;
701 } else {
702 unsigned int *crcp;
703
704 /* symbol points to the CRC in the ELF object */
705 crcp = sym_get_data(info, sym);
706 crc = TO_NATIVE(*crcp);
707 }
708 sym_set_crc(symname, crc);
709}
710
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900711static void handle_symbol(struct module *mod, struct elf_info *info,
712 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200714 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900715 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200716
Masahiro Yamada33795762020-06-01 14:57:24 +0900717 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200718 export = export_from_secname(info, get_secindex(info, sym));
719 else
720 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 switch (sym->st_shndx) {
723 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900724 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100725 /* Should warn here, but modpost runs before the linker */
726 } else
727 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 case SHN_UNDEF:
730 /* undefined symbol */
731 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
732 ELF_ST_BIND(sym->st_info) != STB_WEAK)
733 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200734 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (info->hdr->e_machine == EM_SPARC ||
737 info->hdr->e_machine == EM_SPARCV9) {
738 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700739 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100741 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700742 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100743 munged[0] = '_';
744 munged[1] = toupper(munged[1]);
745 symname = munged;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100748
Rusty Russellb92021b2013-03-15 15:04:17 +1030749 mod->unres = alloc_symbol(symname,
750 ELF_ST_BIND(sym->st_info) == STB_WEAK,
751 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
753 default:
754 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900755 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100756 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100757 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900759 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900761 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 mod->has_cleanup = 1;
763 break;
764 }
765}
766
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100767/**
768 * Parse tag=value strings from .modinfo section
769 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static char *next_string(char *string, unsigned long *secsize)
771{
772 /* Skip non-zero chars */
773 while (string[0]) {
774 string++;
775 if ((*secsize)-- <= 1)
776 return NULL;
777 }
778
779 /* Skip any zero padding. */
780 while (!string[0]) {
781 string++;
782 if ((*secsize)-- <= 1)
783 return NULL;
784 }
785 return string;
786}
787
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900788static char *get_next_modinfo(struct elf_info *info, const char *tag,
789 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 char *p;
792 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900793 char *modinfo = info->modinfo;
794 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900796 if (prev) {
797 size -= prev - modinfo;
798 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200799 }
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 for (p = modinfo; p; p = next_string(p, &size)) {
802 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
803 return p + taglen + 1;
804 }
805 return NULL;
806}
807
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900808static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200809
810{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900811 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200812}
813
Sam Ravnborg93684d32006-02-19 11:53:35 +0100814/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100815 * Test if string s ends in string sub
816 * return 0 if match
817 **/
818static int strrcmp(const char *s, const char *sub)
819{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100820 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100821
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100822 if (!s || !sub)
823 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100824
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100825 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100826 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100827
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100828 if ((slen == 0) || (sublen == 0))
829 return 1;
830
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100831 if (sublen > slen)
832 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100833
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100834 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100835}
836
Sam Ravnborgff13f922008-01-23 19:54:27 +0100837static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
838{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100839 if (sym)
840 return elf->strtab + sym->st_name;
841 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100842 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100843}
844
Sam Ravnborg10668222008-01-13 22:21:31 +0100845/* The pattern is an array of simple patterns.
846 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100847 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100848 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930849 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100850 */
Trevor Keith5c725132009-09-22 16:43:38 -0700851static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100852{
853 const char *p;
854 while (*pat) {
855 p = *pat++;
856 const char *endp = p + strlen(p) - 1;
857
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930858 /* "*foo*" */
859 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300860 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
861 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930862
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930863 free(bare);
864 if (here != NULL)
865 return 1;
866 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100867 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930868 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100869 if (strrcmp(sym, p + 1) == 0)
870 return 1;
871 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100872 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100873 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100874 if (strncmp(sym, p, strlen(p) - 1) == 0)
875 return 1;
876 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100877 /* no wildcards */
878 else {
879 if (strcmp(p, sym) == 0)
880 return 1;
881 }
882 }
883 /* no match */
884 return 0;
885}
886
Sam Ravnborg10668222008-01-13 22:21:31 +0100887/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930888static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200889{
890 ".comment*",
891 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930892 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800893 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000894 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200895 ".mdebug*", /* alpha, score, mips etc. */
896 ".pdr", /* alpha, score, mips etc. */
897 ".stab*",
898 ".note*",
899 ".got*",
900 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400901 ".xt.prop", /* xtensa */
902 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030903 ".arcextmap*", /* arc */
904 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030905 ".cmem*", /* EZchip */
906 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100907 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600908 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200909 NULL
910};
Sam Ravnborg10668222008-01-13 22:21:31 +0100911
Sam Ravnborge241a632008-01-28 20:13:13 +0100912/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400913 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100914 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400915 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100916 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400917static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900918 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100919{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400920 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100921
Anders Kaseorgb614a692009-04-23 16:49:33 -0400922 if (sechdr->sh_type == SHT_PROGBITS &&
923 !(sechdr->sh_flags & SHF_ALLOC) &&
924 !match(sec, section_white_list)) {
925 warn("%s (%s): unexpected non-allocatable section.\n"
926 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
927 "Note that for example <linux/init.h> contains\n"
928 "section definitions for use in .S files.\n\n",
929 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100930 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100931}
932
933
934
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100935#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930936 ".init.setup", ".init.rodata", ".meminit.rodata", \
937 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100938#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930939 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100940
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100941#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930942 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100943#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930944 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100945
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200946#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930947 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
948 ".pci_fixup_enable", ".pci_fixup_resume", \
949 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200950
Paul Gortmakere24f6622013-06-19 19:30:48 -0400951#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
952#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100953
954#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
955#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100956
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930957#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930958#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700959 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930960#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400961 ".fixup", ".entry.text", ".exception.text", ".text.*", \
962 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100963
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000964#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000965#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100966
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000967#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000968#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100969
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930970#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
971 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
972
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100973/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930974static const char *const init_data_sections[] =
975 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100976
977/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930978static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100979
980/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930981static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100982 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100983
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930984/* all text sections */
985static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
986
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100987/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930988static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100989
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100990
991/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100992#define DEFAULT_SYMBOL_WHITE_LIST \
993 "*driver", \
994 "*_template", /* scsi uses *_template a lot */ \
995 "*_timer", /* arm uses ops structures named _timer a lot */ \
996 "*_sht", /* scsi also used *_sht to some extent */ \
997 "*_ops", \
998 "*_probe", \
999 "*_probe_one", \
1000 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001001
Mathias Krause7a3ee752014-08-27 20:28:53 +09301002static const char *const head_sections[] = { ".head.text*", NULL };
1003static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001004 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301005static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001006
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001007enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001008 TEXT_TO_ANY_INIT,
1009 DATA_TO_ANY_INIT,
1010 TEXT_TO_ANY_EXIT,
1011 DATA_TO_ANY_EXIT,
1012 XXXINIT_TO_SOME_INIT,
1013 XXXEXIT_TO_SOME_EXIT,
1014 ANY_INIT_TO_ANY_EXIT,
1015 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001016 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301017 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001018};
1019
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301020/**
1021 * Describe how to match sections on different criterias:
1022 *
1023 * @fromsec: Array of sections to be matched.
1024 *
1025 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1026 * this array is forbidden (black-list). Can be empty.
1027 *
1028 * @good_tosec: Relocations applied to a section in @fromsec must be
1029 * targetting sections in this array (white-list). Can be empty.
1030 *
1031 * @mismatch: Type of mismatch.
1032 *
1033 * @symbol_white_list: Do not match a relocation to a symbol in this list
1034 * even if it is targetting a section in @bad_to_sec.
1035 *
1036 * @handler: Specific handler to call when a match is found. If NULL,
1037 * default_mismatch_handler() will be called.
1038 *
1039 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001040struct sectioncheck {
1041 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301042 const char *bad_tosec[20];
1043 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001044 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001045 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301046 void (*handler)(const char *modname, struct elf_info *elf,
1047 const struct sectioncheck* const mismatch,
1048 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1049
Sam Ravnborg10668222008-01-13 22:21:31 +01001050};
1051
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301052static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1053 const struct sectioncheck* const mismatch,
1054 Elf_Rela *r, Elf_Sym *sym,
1055 const char *fromsec);
1056
Mathias Krause7a3ee752014-08-27 20:28:53 +09301057static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001058/* Do not reference init/exit code/data from
1059 * normal code and data
1060 */
1061{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001062 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301063 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001064 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001065 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001066},
1067{
1068 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301069 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001070 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001071 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001072},
1073{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001074 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301075 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001076 .mismatch = DATA_TO_ANY_INIT,
1077 .symbol_white_list = {
1078 "*_template", "*_timer", "*_sht", "*_ops",
1079 "*_probe", "*_probe_one", "*_console", NULL
1080 },
1081},
1082{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001083 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301084 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001085 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001086 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001087},
1088{
1089 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301090 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001091 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001092 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001093},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001094/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001095{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001096 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301097 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001098 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001099 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001100},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001101/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001102{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001103 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301104 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001105 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001106 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001107},
1108/* Do not use exit code/data from init code */
1109{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001110 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301111 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001112 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001113 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001114},
1115/* Do not use init code/data from exit code */
1116{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001117 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301118 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001119 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001120 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001121},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001122{
1123 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301124 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001125 .mismatch = ANY_INIT_TO_ANY_EXIT,
1126 .symbol_white_list = { NULL },
1127},
Sam Ravnborg10668222008-01-13 22:21:31 +01001128/* Do not export init/exit functions or data */
1129{
1130 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301131 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001132 .mismatch = EXPORT_TO_INIT_EXIT,
1133 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301134},
1135{
1136 .fromsec = { "__ex_table", NULL },
1137 /* If you're adding any new black-listed sections in here, consider
1138 * adding a special 'printer' for them in scripts/check_extable.
1139 */
1140 .bad_tosec = { ".altinstr_replacement", NULL },
1141 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1142 .mismatch = EXTABLE_TO_NON_TEXT,
1143 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001144}
1145};
1146
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001147static const struct sectioncheck *section_mismatch(
1148 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001149{
1150 int i;
1151 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1152 const struct sectioncheck *check = &sectioncheck[0];
1153
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301154 /*
1155 * The target section could be the SHT_NUL section when we're
1156 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001157 * doesn't make much sense and causes build failures on parisc
1158 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301159 */
1160 if (*tosec == '\0')
1161 return NULL;
1162
Sam Ravnborg10668222008-01-13 22:21:31 +01001163 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301164 if (match(fromsec, check->fromsec)) {
1165 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1166 return check;
1167 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1168 return check;
1169 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001170 check++;
1171 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001172 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001173}
1174
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001175/**
1176 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001177 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001178 * Pattern 1:
1179 * If a module parameter is declared __initdata and permissions=0
1180 * then this is legal despite the warning generated.
1181 * We cannot see value of permissions here, so just ignore
1182 * this pattern.
1183 * The pattern is identified by:
1184 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001185 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001186 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001187 *
Rusty Russell6a841522010-08-11 23:04:16 -06001188 * Pattern 1a:
1189 * module_param_call() ops can refer to __init set function if permissions=0
1190 * The pattern is identified by:
1191 * tosec = .init.text
1192 * fromsec = .data*
1193 * atsym = __param_ops_*
1194 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001195 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001196 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001197 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001198 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001199 * tosec = init or exit section
1200 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001201 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1202 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001203 *
1204 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001205 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001206 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001207 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001208 * Some symbols belong to init section but still it is ok to reference
1209 * these from non-init sections as these symbols don't have any memory
1210 * allocated for them and symbol address and value are same. So even
1211 * if init section is freed, its ok to reference those symbols.
1212 * For ex. symbols marking the init section boundaries.
1213 * This pattern is identified by
1214 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001215 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301216 * Pattern 5:
1217 * GCC may optimize static inlines when fed constant arg(s) resulting
1218 * in functions like cpumask_empty() -- generating an associated symbol
1219 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1220 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1221 * meaningless section warning. May need to add isra symbols too...
1222 * This pattern is identified by
1223 * tosec = init section
1224 * fromsec = text section
1225 * refsymname = *.constprop.*
1226 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001227 * Pattern 6:
1228 * Hide section mismatch warnings for ELF local symbols. The goal
1229 * is to eliminate false positive modpost warnings caused by
1230 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1231 * Autogenerated symbol names bypass modpost's "Pattern 2"
1232 * whitelisting, which relies on pattern-matching against symbol
1233 * names to work. (One situation where gcc can autogenerate ELF
1234 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001235 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001236static int secref_whitelist(const struct sectioncheck *mismatch,
1237 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001238 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001239{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001240 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001241 if (match(tosec, init_data_sections) &&
1242 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001243 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001244 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001245
Rusty Russell6a841522010-08-11 23:04:16 -06001246 /* Check for pattern 1a */
1247 if (strcmp(tosec, ".init.text") == 0 &&
1248 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001249 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001250 return 0;
1251
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001252 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001253 if (match(tosec, init_exit_sections) &&
1254 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001255 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001256 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001257
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001258 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001259 if (match(fromsec, head_sections) &&
1260 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001261 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001262
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001263 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001264 if (match(tosym, linker_symbols))
1265 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001266
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301267 /* Check for pattern 5 */
1268 if (match(fromsec, text_sections) &&
1269 match(tosec, init_sections) &&
1270 match(fromsym, optim_symbols))
1271 return 0;
1272
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001273 /* Check for pattern 6 */
1274 if (strstarts(fromsym, ".L"))
1275 return 0;
1276
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001277 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001278}
1279
Sami Tolvanen5818c682018-10-23 15:15:35 -07001280static inline int is_arm_mapping_symbol(const char *str)
1281{
1282 return str[0] == '$' && strchr("axtd", str[1])
1283 && (str[2] == '\0' || str[2] == '.');
1284}
1285
1286/*
1287 * If there's no name there, ignore it; likewise, ignore it if it's
1288 * one of the magic symbols emitted used by current ARM tools.
1289 *
1290 * Otherwise if find_symbols_between() returns those symbols, they'll
1291 * fail the whitelist tests and cause lots of false alarms ... fixable
1292 * only by merging __exit and __init sections into __text, bloating
1293 * the kernel (which is especially evil on embedded platforms).
1294 */
1295static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1296{
1297 const char *name = elf->strtab + sym->st_name;
1298
1299 if (!name || !strlen(name))
1300 return 0;
1301 return !is_arm_mapping_symbol(name);
1302}
1303
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001304/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001305 * Find symbol based on relocation record info.
1306 * In some cases the symbol supplied is a valid symbol so
1307 * return refsym. If st_name != 0 we assume this is a valid symbol.
1308 * In other cases the symbol needs to be looked up in the symbol table
1309 * based on section and address.
1310 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001311static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001312 Elf_Sym *relsym)
1313{
1314 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001315 Elf_Sym *near = NULL;
1316 Elf64_Sword distance = 20;
1317 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001318 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001319
1320 if (relsym->st_name != 0)
1321 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001322
1323 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001324 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001325 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001326 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001327 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1328 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001329 if (!is_valid_name(elf, sym))
1330 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001331 if (sym->st_value == addr)
1332 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001333 /* Find a symbol nearby - addr are maybe negative */
1334 d = sym->st_value - addr;
1335 if (d < 0)
1336 d = addr - sym->st_value;
1337 if (d < distance) {
1338 distance = d;
1339 near = sym;
1340 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001341 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001342 /* We need a close match */
1343 if (distance < 20)
1344 return near;
1345 else
1346 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001347}
1348
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001349/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001350 * Find symbols before or equal addr and after addr - in the section sec.
1351 * If we find two symbols with equal offset prefer one with a valid name.
1352 * The ELF format may have a better way to detect what type of symbol
1353 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001354 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001355static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1356 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001357{
1358 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001359 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001360 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001361
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001362 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1363 const char *symsec;
1364
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001365 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001366 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001367 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001368 if (strcmp(symsec, sec) != 0)
1369 continue;
David Brownellda68d612007-02-20 13:58:16 -08001370 if (!is_valid_name(elf, sym))
1371 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001372 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001373 if ((addr - sym->st_value) < distance) {
1374 distance = addr - sym->st_value;
1375 near = sym;
1376 } else if ((addr - sym->st_value) == distance) {
1377 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001378 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001379 }
1380 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001381 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001382}
1383
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001384/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001385 * Convert a section name to the function/data attribute
1386 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001387 * .memexitconst => __memconst
1388 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001389 *
1390 * The memory of returned value has been allocated on a heap. The user of this
1391 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001392*/
1393static char *sec2annotation(const char *s)
1394{
1395 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001396 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001397 char *r = p;
1398
1399 *p++ = '_';
1400 *p++ = '_';
1401 if (*s == '.')
1402 s++;
1403 while (*s && *s != '.')
1404 *p++ = *s++;
1405 *p = '\0';
1406 if (*s == '.')
1407 s++;
1408 if (strstr(s, "rodata") != NULL)
1409 strcat(p, "const ");
1410 else if (strstr(s, "data") != NULL)
1411 strcat(p, "data ");
1412 else
1413 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001414 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001415 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001416 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001417 }
1418}
1419
1420static int is_function(Elf_Sym *sym)
1421{
1422 if (sym)
1423 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1424 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001425 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001426}
1427
Randy Dunlap00759c02011-03-15 14:13:47 -07001428static void print_section_list(const char * const list[20])
1429{
1430 const char *const *s = list;
1431
1432 while (*s) {
1433 fprintf(stderr, "%s", *s);
1434 s++;
1435 if (*s)
1436 fprintf(stderr, ", ");
1437 }
1438 fprintf(stderr, "\n");
1439}
1440
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301441static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1442{
1443 switch (is_func) {
1444 case 0: *name = "variable"; *name_p = ""; break;
1445 case 1: *name = "function"; *name_p = "()"; break;
1446 default: *name = "(unknown reference)"; *name_p = ""; break;
1447 }
1448}
1449
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001450/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001451 * Print a warning about a section mismatch.
1452 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001453 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001454 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001455static void report_sec_mismatch(const char *modname,
1456 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001457 const char *fromsec,
1458 unsigned long long fromaddr,
1459 const char *fromsym,
1460 int from_is_func,
1461 const char *tosec, const char *tosym,
1462 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001463{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001464 const char *from, *from_p;
1465 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001466 char *prl_from;
1467 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001468
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001469 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001470
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301471 get_pretty_name(from_is_func, &from, &from_p);
1472 get_pretty_name(to_is_func, &to, &to_p);
1473
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001474 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1475 "to the %s %s:%s%s\n",
1476 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1477 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001478
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001479 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001480 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001481 prl_from = sec2annotation(fromsec);
1482 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001483 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001484 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001485 "the %s %s%s%s.\n"
1486 "This is often because %s lacks a %s\n"
1487 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001488 prl_from, fromsym,
1489 to, prl_to, tosym, to_p,
1490 fromsym, prl_to, tosym);
1491 free(prl_from);
1492 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001493 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001494 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001495 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001496 fprintf(stderr,
1497 "The variable %s references\n"
1498 "the %s %s%s%s\n"
1499 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001500 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001501 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001502 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001503 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001504 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001505 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001506 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001507 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001508 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001509 fprintf(stderr,
1510 "The function %s() references a %s in an exit section.\n"
1511 "Often the %s %s%s has valid usage outside the exit section\n"
1512 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001513 fromsym, to, to, tosym, to_p, prl_to, tosym);
1514 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001515 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001516 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001517 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001518 fprintf(stderr,
1519 "The variable %s references\n"
1520 "the %s %s%s%s\n"
1521 "If the reference is valid then annotate the\n"
1522 "variable with __exit* (see linux/init.h) or "
1523 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001524 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001525 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001526 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001527 break;
1528 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001529 case XXXINIT_TO_SOME_INIT:
1530 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001531 prl_from = sec2annotation(fromsec);
1532 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001533 fprintf(stderr,
1534 "The %s %s%s%s references\n"
1535 "a %s %s%s%s.\n"
1536 "If %s is only used by %s then\n"
1537 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001538 from, prl_from, fromsym, from_p,
1539 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001540 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001541 free(prl_from);
1542 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001543 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001544 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001545 prl_from = sec2annotation(fromsec);
1546 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001547 fprintf(stderr,
1548 "The %s %s%s%s references\n"
1549 "a %s %s%s%s.\n"
1550 "This is often seen when error handling "
1551 "in the init function\n"
1552 "uses functionality in the exit path.\n"
1553 "The fix is often to remove the %sannotation of\n"
1554 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001555 from, prl_from, fromsym, from_p,
1556 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001557 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001558 free(prl_from);
1559 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001560 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001561 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001562 prl_from = sec2annotation(fromsec);
1563 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001564 fprintf(stderr,
1565 "The %s %s%s%s references\n"
1566 "a %s %s%s%s.\n"
1567 "This is often seen when error handling "
1568 "in the exit function\n"
1569 "uses functionality in the init path.\n"
1570 "The fix is often to remove the %sannotation of\n"
1571 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001572 from, prl_from, fromsym, from_p,
1573 to, prl_to, tosym, to_p,
1574 prl_to, tosym, to_p);
1575 free(prl_from);
1576 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001577 break;
1578 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001579 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001580 fprintf(stderr,
1581 "The symbol %s is exported and annotated %s\n"
1582 "Fix this by removing the %sannotation of %s "
1583 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001584 tosym, prl_to, prl_to, tosym);
1585 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001586 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301587 case EXTABLE_TO_NON_TEXT:
1588 fatal("There's a special handler for this mismatch type, "
1589 "we should never get here.");
1590 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001591 }
1592 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001593}
1594
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301595static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1596 const struct sectioncheck* const mismatch,
1597 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1598{
1599 const char *tosec;
1600 Elf_Sym *to;
1601 Elf_Sym *from;
1602 const char *tosym;
1603 const char *fromsym;
1604
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301605 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1606 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301607
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001608 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301609 return;
1610
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301611 tosec = sec_name(elf, get_secindex(elf, sym));
1612 to = find_elf_symbol(elf, r->r_addend, sym);
1613 tosym = sym_name(elf, to);
1614
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301615 /* check whitelist - we may ignore it */
1616 if (secref_whitelist(mismatch,
1617 fromsec, fromsym, tosec, tosym)) {
1618 report_sec_mismatch(modname, mismatch,
1619 fromsec, r->r_offset, fromsym,
1620 is_function(from), tosec, tosym,
1621 is_function(to));
1622 }
1623}
1624
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301625static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1626{
1627 if (section_index > elf->num_sections)
1628 fatal("section_index is outside elf->num_sections!\n");
1629
1630 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1631}
1632
1633/*
1634 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1635 * to know the sizeof(struct exception_table_entry) for the target architecture.
1636 */
1637static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301638static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301639{
1640 /*
1641 * If we're currently checking the second relocation within __ex_table,
1642 * that relocation offset tells us the offsetof(struct
1643 * exception_table_entry, fixup) which is equal to sizeof(struct
1644 * exception_table_entry) divided by two. We use that to our advantage
1645 * since there's no portable way to get that size as every architecture
1646 * seems to go with different sized types. Not pretty but better than
1647 * hard-coding the size for every architecture..
1648 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301649 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301650 extable_entry_size = r->r_offset * 2;
1651}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301652
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301653static inline bool is_extable_fault_address(Elf_Rela *r)
1654{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301655 /*
1656 * extable_entry_size is only discovered after we've handled the
1657 * _second_ relocation in __ex_table, so only abort when we're not
1658 * handling the first reloc and extable_entry_size is zero.
1659 */
1660 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301661 fatal("extable_entry size hasn't been discovered!\n");
1662
1663 return ((r->r_offset == 0) ||
1664 (r->r_offset % extable_entry_size == 0));
1665}
1666
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301667#define is_second_extable_reloc(Start, Cur, Sec) \
1668 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1669
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301670static void report_extable_warnings(const char* modname, struct elf_info* elf,
1671 const struct sectioncheck* const mismatch,
1672 Elf_Rela* r, Elf_Sym* sym,
1673 const char* fromsec, const char* tosec)
1674{
1675 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1676 const char* fromsym_name = sym_name(elf, fromsym);
1677 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1678 const char* tosym_name = sym_name(elf, tosym);
1679 const char* from_pretty_name;
1680 const char* from_pretty_name_p;
1681 const char* to_pretty_name;
1682 const char* to_pretty_name_p;
1683
1684 get_pretty_name(is_function(fromsym),
1685 &from_pretty_name, &from_pretty_name_p);
1686 get_pretty_name(is_function(tosym),
1687 &to_pretty_name, &to_pretty_name_p);
1688
1689 warn("%s(%s+0x%lx): Section mismatch in reference"
1690 " from the %s %s%s to the %s %s:%s%s\n",
1691 modname, fromsec, (long)r->r_offset, from_pretty_name,
1692 fromsym_name, from_pretty_name_p,
1693 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1694
1695 if (!match(tosec, mismatch->bad_tosec) &&
1696 is_executable_section(elf, get_secindex(elf, sym)))
1697 fprintf(stderr,
1698 "The relocation at %s+0x%lx references\n"
1699 "section \"%s\" which is not in the list of\n"
1700 "authorized sections. If you're adding a new section\n"
1701 "and/or if this reference is valid, add \"%s\" to the\n"
1702 "list of authorized sections to jump to on fault.\n"
1703 "This can be achieved by adding \"%s\" to \n"
1704 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1705 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1706}
1707
1708static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1709 const struct sectioncheck* const mismatch,
1710 Elf_Rela* r, Elf_Sym* sym,
1711 const char *fromsec)
1712{
1713 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1714
1715 sec_mismatch_count++;
1716
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001717 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301718
1719 if (match(tosec, mismatch->bad_tosec))
1720 fatal("The relocation at %s+0x%lx references\n"
1721 "section \"%s\" which is black-listed.\n"
1722 "Something is seriously wrong and should be fixed.\n"
1723 "You might get more information about where this is\n"
1724 "coming from by using scripts/check_extable.sh %s\n",
1725 fromsec, (long)r->r_offset, tosec, modname);
1726 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1727 if (is_extable_fault_address(r))
1728 fatal("The relocation at %s+0x%lx references\n"
1729 "section \"%s\" which is not executable, IOW\n"
1730 "it is not possible for the kernel to fault\n"
1731 "at that address. Something is seriously wrong\n"
1732 "and should be fixed.\n",
1733 fromsec, (long)r->r_offset, tosec);
1734 else
1735 fatal("The relocation at %s+0x%lx references\n"
1736 "section \"%s\" which is not executable, IOW\n"
1737 "the kernel will fault if it ever tries to\n"
1738 "jump to it. Something is seriously wrong\n"
1739 "and should be fixed.\n",
1740 fromsec, (long)r->r_offset, tosec);
1741 }
1742}
1743
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001744static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001745 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001746{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001747 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301748 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001749
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001750 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301751 if (mismatch->handler)
1752 mismatch->handler(modname, elf, mismatch,
1753 r, sym, fromsec);
1754 else
1755 default_mismatch_handler(modname, elf, mismatch,
1756 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001757 }
1758}
1759
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001760static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001761 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001762{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001763 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001764}
1765
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001766static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001767{
1768 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001769 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001770
1771 switch (r_typ) {
1772 case R_386_32:
1773 r->r_addend = TO_NATIVE(*location);
1774 break;
1775 case R_386_PC32:
1776 r->r_addend = TO_NATIVE(*location) + 4;
1777 /* For CONFIG_RELOCATABLE=y */
1778 if (elf->hdr->e_type == ET_EXEC)
1779 r->r_addend += r->r_offset;
1780 break;
1781 }
1782 return 0;
1783}
1784
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001785#ifndef R_ARM_CALL
1786#define R_ARM_CALL 28
1787#endif
1788#ifndef R_ARM_JUMP24
1789#define R_ARM_JUMP24 29
1790#endif
1791
David A. Longc9698e52014-02-14 22:41:18 +01001792#ifndef R_ARM_THM_CALL
1793#define R_ARM_THM_CALL 10
1794#endif
1795#ifndef R_ARM_THM_JUMP24
1796#define R_ARM_THM_JUMP24 30
1797#endif
1798#ifndef R_ARM_THM_JUMP19
1799#define R_ARM_THM_JUMP19 51
1800#endif
1801
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001802static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001803{
1804 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1805
1806 switch (r_typ) {
1807 case R_ARM_ABS32:
1808 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001809 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001810 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001811 break;
1812 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001813 case R_ARM_CALL:
1814 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001815 case R_ARM_THM_CALL:
1816 case R_ARM_THM_JUMP24:
1817 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001818 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001819 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001820 sechdr->sh_offset +
1821 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001822 break;
1823 default:
1824 return 1;
1825 }
1826 return 0;
1827}
1828
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001829static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001830{
1831 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001832 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001833 unsigned int inst;
1834
1835 if (r_typ == R_MIPS_HI16)
1836 return 1; /* skip this */
1837 inst = TO_NATIVE(*location);
1838 switch (r_typ) {
1839 case R_MIPS_LO16:
1840 r->r_addend = inst & 0xffff;
1841 break;
1842 case R_MIPS_26:
1843 r->r_addend = (inst & 0x03ffffff) << 2;
1844 break;
1845 case R_MIPS_32:
1846 r->r_addend = inst;
1847 break;
1848 }
1849 return 0;
1850}
1851
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001852static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001853 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001854{
1855 Elf_Sym *sym;
1856 Elf_Rela *rela;
1857 Elf_Rela r;
1858 unsigned int r_sym;
1859 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001860
Sam Ravnborgff13f922008-01-23 19:54:27 +01001861 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001862 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1863
Sam Ravnborgff13f922008-01-23 19:54:27 +01001864 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001865 fromsec += strlen(".rela");
1866 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001867 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001868 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001869
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001870 for (rela = start; rela < stop; rela++) {
1871 r.r_offset = TO_NATIVE(rela->r_offset);
1872#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001873 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001874 unsigned int r_typ;
1875 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1876 r_sym = TO_NATIVE(r_sym);
1877 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1878 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1879 } else {
1880 r.r_info = TO_NATIVE(rela->r_info);
1881 r_sym = ELF_R_SYM(r.r_info);
1882 }
1883#else
1884 r.r_info = TO_NATIVE(rela->r_info);
1885 r_sym = ELF_R_SYM(r.r_info);
1886#endif
1887 r.r_addend = TO_NATIVE(rela->r_addend);
1888 sym = elf->symtab_start + r_sym;
1889 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001890 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001891 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301892 if (is_second_extable_reloc(start, rela, fromsec))
1893 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001894 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001895 }
1896}
1897
1898static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001899 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001900{
1901 Elf_Sym *sym;
1902 Elf_Rel *rel;
1903 Elf_Rela r;
1904 unsigned int r_sym;
1905 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001906
Sam Ravnborgff13f922008-01-23 19:54:27 +01001907 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001908 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1909
Sam Ravnborgff13f922008-01-23 19:54:27 +01001910 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001911 fromsec += strlen(".rel");
1912 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001913 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001914 return;
1915
1916 for (rel = start; rel < stop; rel++) {
1917 r.r_offset = TO_NATIVE(rel->r_offset);
1918#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001919 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001920 unsigned int r_typ;
1921 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1922 r_sym = TO_NATIVE(r_sym);
1923 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1924 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1925 } else {
1926 r.r_info = TO_NATIVE(rel->r_info);
1927 r_sym = ELF_R_SYM(r.r_info);
1928 }
1929#else
1930 r.r_info = TO_NATIVE(rel->r_info);
1931 r_sym = ELF_R_SYM(r.r_info);
1932#endif
1933 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001934 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001935 case EM_386:
1936 if (addend_386_rel(elf, sechdr, &r))
1937 continue;
1938 break;
1939 case EM_ARM:
1940 if (addend_arm_rel(elf, sechdr, &r))
1941 continue;
1942 break;
1943 case EM_MIPS:
1944 if (addend_mips_rel(elf, sechdr, &r))
1945 continue;
1946 break;
1947 }
1948 sym = elf->symtab_start + r_sym;
1949 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001950 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001951 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301952 if (is_second_extable_reloc(start, rel, fromsec))
1953 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001954 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001955 }
1956}
1957
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001958/**
1959 * A module includes a number of sections that are discarded
1960 * either when loaded or when used as built-in.
1961 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001962 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001963 * Likewise for modules used built-in the sections marked __exit
1964 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001965 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001966 * The check_sec_ref() function traverses all relocation records
1967 * to find all references to a section that reference a section that will
1968 * be discarded and warns about it.
1969 **/
1970static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001971 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001972{
1973 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001974 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001975
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001976 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001977 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001978 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001979 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001980 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001981 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001982 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001983 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001984 }
1985}
1986
Andi Kleen7d02b492014-02-08 09:01:12 +01001987static char *remove_dot(char *s)
1988{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301989 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001990
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301991 if (n && s[n]) {
1992 size_t m = strspn(s + n + 1, "0123456789");
1993 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001994 s[n] = 0;
1995 }
1996 return s;
1997}
1998
Masahiro Yamada8b185742018-05-09 18:50:40 +09001999static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 const char *symname;
2002 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002003 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002004 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct module *mod;
2006 struct elf_info info = { };
2007 Elf_Sym *sym;
2008
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002009 if (!parse_elf(&info, modname))
2010 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 mod = new_module(modname);
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 mod->skip = 1;
2017 }
2018
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002019 if (!is_vmlinux(modname)) {
2020 license = get_modinfo(&info, "license");
2021 if (!license)
2022 warn("missing MODULE_LICENSE() in %s\n", modname);
2023 while (license) {
2024 if (license_is_gpl_compatible(license))
2025 mod->gpl_compatible = 1;
2026 else {
2027 mod->gpl_compatible = 0;
2028 break;
2029 }
2030 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002031 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002032
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002033 namespace = get_modinfo(&info, "import_ns");
2034 while (namespace) {
2035 add_namespace(&mod->imported_namespaces, namespace);
2036 namespace = get_next_modinfo(&info, "import_ns",
2037 namespace);
2038 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002039 }
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002042 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002044 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 handle_moddevtable(mod, &info, sym, symname);
2046 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002047
Matthias Maennich69923202019-10-18 10:31:42 +01002048 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2049 symname = remove_dot(info.strtab + sym->st_name);
2050
Masahiro Yamada17436942019-11-15 02:42:24 +09002051 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002052 if (strstarts(symname, "__kstrtabns_"))
2053 sym_update_namespace(symname + strlen("__kstrtabns_"),
2054 namespace_from_kstrtabns(&info,
2055 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002056
2057 if (strstarts(symname, "__crc_"))
2058 handle_modversion(mod, &info, sym,
2059 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002060 }
2061
Denis Efremov15bfc232019-08-01 09:06:57 +03002062 // check for static EXPORT_SYMBOL_* functions && global vars
2063 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2064 unsigned char bind = ELF_ST_BIND(sym->st_info);
2065
2066 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2067 struct symbol *s =
2068 find_symbol(remove_dot(info.strtab +
2069 sym->st_name));
2070
2071 if (s)
2072 s->is_static = 0;
2073 }
2074 }
2075
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002076 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002078 if (!is_vmlinux(modname)) {
2079 version = get_modinfo(&info, "version");
2080 if (version || all_versions)
2081 get_src_version(modname, mod->srcversion,
2082 sizeof(mod->srcversion) - 1);
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 parse_elf_finish(&info);
2086
Rusty Russell8c8ef422009-03-31 13:05:34 -06002087 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 * never passed as an argument to an exported function, so
2089 * the automatic versioning doesn't pick it up, but it's really
2090 * important anyhow */
2091 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002092 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Rusty Russell712f9b42013-04-04 17:37:38 +10302095static void read_symbols_from_files(const char *filename)
2096{
2097 FILE *in = stdin;
2098 char fname[PATH_MAX];
2099
2100 if (strcmp(filename, "-") != 0) {
2101 in = fopen(filename, "r");
2102 if (!in)
2103 fatal("Can't open filenames file %s: %m", filename);
2104 }
2105
2106 while (fgets(fname, PATH_MAX, in) != NULL) {
2107 if (strends(fname, "\n"))
2108 fname[strlen(fname)-1] = '\0';
2109 read_symbols(fname);
2110 }
2111
2112 if (in != stdin)
2113 fclose(in);
2114}
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#define SZ 500
2117
2118/* We first write the generated file into memory using the
2119 * following helper, then compare to the file on disk and
2120 * only update the later if anything changed */
2121
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002122void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2123 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 char tmp[SZ];
2126 int len;
2127 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 va_start(ap, fmt);
2130 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08002131 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 va_end(ap);
2133}
2134
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002135void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08002138 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002139 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 strncpy(buf->p + buf->pos, s, len);
2142 buf->pos += len;
2143}
2144
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002145static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2146{
2147 const char *e = is_vmlinux(m) ?"":".ko";
2148
2149 switch (exp) {
2150 case export_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002151 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002152 "uses GPL-only symbol '%s'\n", m, e, s);
2153 break;
2154 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002155 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002156 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2157 break;
2158 case export_gpl_future:
Jessica Yu93c95e52020-03-06 17:02:05 +01002159 warn("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002160 "uses future GPL-only symbol '%s'\n", m, e, s);
2161 break;
2162 case export_plain:
2163 case export_unused:
2164 case export_unknown:
2165 /* ignore */
2166 break;
2167 }
2168}
2169
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002170static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002171{
2172 const char *e = is_vmlinux(m) ?"":".ko";
2173
2174 switch (exp) {
2175 case export_unused:
2176 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002177 warn("module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002178 "uses symbol '%s' marked UNUSED\n", m, e, s);
2179 break;
2180 default:
2181 /* ignore */
2182 break;
2183 }
2184}
2185
Masahiro Yamada3b415282018-11-23 16:57:23 +09002186static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002187{
2188 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002189 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002190
2191 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002192 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002193 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002194 if (!exp || exp->module == mod) {
2195 if (have_vmlinux && !s->weak) {
Jessica Yu93c95e52020-03-06 17:02:05 +01002196 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2197 "\"%s\" [%s.ko] undefined!\n",
2198 s->name, mod->name);
2199 if (!warn_unresolved)
Masahiro Yamada3b415282018-11-23 16:57:23 +09002200 err = 1;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002201 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002202 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002203 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002204 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002205 if (basename)
2206 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002207 else
2208 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002209
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002210 if (exp->namespace &&
2211 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002212 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2213 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2214 basename, exp->name, exp->namespace);
2215 if (!allow_missing_ns_imports)
2216 err = 1;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002217 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002218 }
2219
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002220 if (!mod->gpl_compatible)
2221 check_for_gpl_usage(exp->export, basename, exp->name);
2222 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002223 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002224
2225 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002226}
2227
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002228static int check_modname_len(struct module *mod)
2229{
2230 const char *mod_name;
2231
2232 mod_name = strrchr(mod->name, '/');
2233 if (mod_name == NULL)
2234 mod_name = mod->name;
2235 else
2236 mod_name++;
2237 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2238 merror("module name is too long [%s.ko]\n", mod->name);
2239 return 1;
2240 }
2241
2242 return 0;
2243}
2244
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002245/**
2246 * Header for the generated file
2247 **/
2248static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002251 /*
2252 * Include build-salt.h after module.h in order to
2253 * inherit the definitions.
2254 */
2255 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 buf_printf(b, "#include <linux/vermagic.h>\n");
2257 buf_printf(b, "#include <linux/compiler.h>\n");
2258 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002259 buf_printf(b, "BUILD_SALT;\n");
2260 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002262 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302264 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002265 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002266 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002268 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (mod->has_cleanup)
2270 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002271 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002273 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 buf_printf(b, "};\n");
2275}
2276
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002277static void add_intree_flag(struct buffer *b, int is_intree)
2278{
2279 if (is_intree)
2280 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2281}
2282
Andi Kleencaf75012018-01-25 15:50:28 -08002283/* Cannot check for assembler */
2284static void add_retpoline(struct buffer *b)
2285{
WANG Chaoe4f35892018-12-11 00:37:25 +08002286 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002287 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2288 buf_printf(b, "#endif\n");
2289}
2290
Trevor Keith5c725132009-09-22 16:43:38 -07002291static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002292{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002293 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002294 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2295}
2296
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002297/**
2298 * Record CRCs for unresolved symbols
2299 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002300static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
2302 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002303 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 for (s = mod->unres; s; s = s->next) {
2306 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002307 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 s->module = exp->module;
2310 s->crc_valid = exp->crc_valid;
2311 s->crc = exp->crc;
2312 }
2313
2314 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002315 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 buf_printf(b, "\n");
2318 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002319 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002322 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002325 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 s->name, mod->name);
2327 continue;
2328 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302329 if (strlen(s->name) >= MODULE_NAME_LEN) {
2330 merror("too long symbol \"%s\" [%s.ko]\n",
2331 s->name, mod->name);
2332 err = 1;
2333 break;
2334 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002335 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302336 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338
2339 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002340
2341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002344static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 int first = 1;
2348
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002349 /* Clear ->seen flag of modules that own symbols needed by this. */
2350 for (s = mod->unres; s; s = s->next)
2351 if (s->module)
2352 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002355 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002357 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (!s->module)
2359 continue;
2360
2361 if (s->module->seen)
2362 continue;
2363
2364 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002365 p = strrchr(s->module->name, '/');
2366 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002367 p++;
2368 else
2369 p = s->module->name;
2370 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 first = 0;
2372 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002373 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002376static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
2378 if (mod->srcversion[0]) {
2379 buf_printf(b, "\n");
2380 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2381 mod->srcversion);
2382 }
2383}
2384
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002385static void write_buf(struct buffer *b, const char *fname)
2386{
2387 FILE *file;
2388
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 if (fclose(file) != 0) {
2399 perror(fname);
2400 exit(1);
2401 }
2402}
2403
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002404static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 char *tmp;
2407 FILE *file;
2408 struct stat st;
2409
2410 file = fopen(fname, "r");
2411 if (!file)
2412 goto write;
2413
2414 if (fstat(fileno(file), &st) < 0)
2415 goto close_write;
2416
2417 if (st.st_size != b->pos)
2418 goto close_write;
2419
2420 tmp = NOFAIL(malloc(b->pos));
2421 if (fread(tmp, 1, b->pos, file) != b->pos)
2422 goto free_write;
2423
2424 if (memcmp(tmp, b->p, b->pos) != 0)
2425 goto free_write;
2426
2427 free(tmp);
2428 fclose(file);
2429 return;
2430
2431 free_write:
2432 free(tmp);
2433 close_write:
2434 fclose(file);
2435 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002436 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
Ram Paibd5cbce2006-06-08 22:12:53 -07002439/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002440 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002441 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002442static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002444 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002446 buf = read_text_file(fname);
2447 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* No symbol versions, silently ignore */
2449 return;
2450
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002451 pos = buf;
2452
2453 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002454 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 unsigned int crc;
2456 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002457 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 if (!(symname = strchr(line, '\t')))
2460 goto fail;
2461 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002462 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 goto fail;
2464 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002465 if (!(export = strchr(modname, '\t')))
2466 goto fail;
2467 *export++ = '\0';
2468 if (!(namespace = strchr(export, '\t')))
2469 goto fail;
2470 *namespace++ = '\0';
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 crc = strtoul(line, &d, 16);
2473 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2474 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002475 mod = find_module(modname);
2476 if (!mod) {
2477 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002479 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 mod->skip = 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002481 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002483 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002484 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002485 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002486 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002488 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return;
2490fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002491 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 fatal("parse error in symbol dump file\n");
2493}
2494
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002495/* For normal builds always dump all symbols.
2496 * For external modules only dump symbols
2497 * that are not read from kernel Module.symvers.
2498 **/
2499static int dump_sym(struct symbol *sym)
2500{
2501 if (!external_module)
2502 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002503 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002504 return 0;
2505 return 1;
2506}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002507
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002508static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 struct buffer buf = { };
2511 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002512 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 int n;
2514
2515 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2516 symbol = symbolhash[n];
2517 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002518 if (dump_sym(symbol)) {
2519 namespace = symbol->namespace;
2520 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2521 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002522 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002523 export_str(symbol->export),
2524 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 symbol = symbol->next;
2527 }
2528 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002529 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002530 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002533static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002534{
2535 struct module *mod;
2536 struct namespace_list *ns;
2537 struct buffer ns_deps_buf = {};
2538
2539 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002540
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002541 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002542 continue;
2543
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002544 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002545
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002546 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2547 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002548
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002549 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002550 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002551
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002552 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002553 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002554}
2555
Masahiro Yamada79247992020-06-01 14:57:07 +09002556struct dump_list {
2557 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002558 const char *file;
2559};
2560
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002561int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
2563 struct module *mod;
2564 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002565 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302566 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002568 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002569 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002570 struct dump_list *dump_read_start = NULL;
2571 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002573 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002574 switch (opt) {
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002575 case 'e':
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002576 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002577 break;
2578 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002579 *dump_read_iter =
2580 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2581 (*dump_read_iter)->file = optarg;
2582 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002583 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002584 case 'm':
2585 modversions = 1;
2586 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302587 case 'n':
2588 ignore_missing_files = 1;
2589 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002590 case 'o':
2591 dump_write = optarg;
2592 break;
2593 case 'a':
2594 all_versions = 1;
2595 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302596 case 'T':
2597 files_source = optarg;
2598 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002599 case 'w':
2600 warn_unresolved = 1;
2601 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302602 case 'E':
2603 sec_mismatch_fatal = 1;
2604 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002605 case 'N':
2606 allow_missing_ns_imports = 1;
2607 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002608 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002609 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002610 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002611 default:
2612 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614 }
2615
Masahiro Yamada79247992020-06-01 14:57:07 +09002616 while (dump_read_start) {
2617 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002618
Masahiro Yamada79247992020-06-01 14:57:07 +09002619 read_dump(dump_read_start->file);
2620 tmp = dump_read_start->next;
2621 free(dump_read_start);
2622 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002625 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Rusty Russell712f9b42013-04-04 17:37:38 +10302628 if (files_source)
2629 read_symbols_from_files(files_source);
2630
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002631 /*
2632 * When there's no vmlinux, don't print warnings about
2633 * unresolved symbols (since there'll be too many ;)
2634 */
2635 if (!have_vmlinux)
2636 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2637
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002638 err = 0;
2639
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002640 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302641 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002642
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002643 if (mod->skip)
2644 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 buf.pos = 0;
2647
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002648 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002649 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002652 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002653 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002654 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002655 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002656 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 add_moddevtable(&buf, mod);
2658 add_srcversion(&buf, mod);
2659
2660 sprintf(fname, "%s.mod.c", mod->name);
2661 write_if_changed(&buf, fname);
2662 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002663
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002664 if (missing_namespace_deps)
2665 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 if (dump_write)
2668 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002669 if (sec_mismatch_count && sec_mismatch_fatal)
Jessica Yu93c95e52020-03-06 17:02:05 +01002670 fatal("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002671 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002672 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002673 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002674
Masahiro Yamada47346e92019-09-24 21:07:40 +09002675 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002676 if (s->is_static)
2677 warn("\"%s\" [%s] is a static %s\n",
2678 s->name, s->module->name,
2679 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002680 }
2681 }
2682
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002683 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002685 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}