blob: 6bfa332179140bee888179e24c9346d05736c16e [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>
Masahiro Yamadae54dd932021-08-28 18:50:59 +090020#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/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
28static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010029/* If we are modposting external module set to 1 */
30static int external_module = 0;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070031/* Only warn about unresolved symbols */
32static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070033/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010034static int sec_mismatch_count = 0;
Masahiro Yamadac7299d92020-12-01 19:34:18 +090035static int sec_mismatch_warn_only = true;
Guenter Roeckeed380f2013-09-23 15:23:54 +093036/* ignore missing files */
37static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010038/* If set to 1, only warn (instead of error) about missing ns imports */
39static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010040
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090041static bool error_occurred;
42
Masahiro Yamada4475dff2021-03-26 03:54:11 +090043/*
44 * Cut off the warnings when there are too many. This typically occurs when
45 * vmlinux is missing. ('make modules' without building vmlinux.)
46 */
47#define MAX_UNRESOLVED_REPORTS 10
48static unsigned int nr_unresolved;
49
Sam Ravnborgc96fca22006-07-01 11:44:23 +020050enum export {
Christoph Hellwig36794822021-02-02 13:13:34 +010051 export_plain,
52 export_gpl,
53 export_unknown
Sam Ravnborgc96fca22006-07-01 11:44:23 +020054};
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080056/* In kernel, this size is defined in linux/module.h;
57 * here we use Elf_Addr instead of long for covering cross-compile
58 */
59
60#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
61
Jessica Yu93c95e52020-03-06 17:02:05 +010062void __attribute__((format(printf, 2, 3)))
63modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 va_list arglist;
66
Jessica Yu93c95e52020-03-06 17:02:05 +010067 switch (loglevel) {
68 case LOG_WARN:
69 fprintf(stderr, "WARNING: ");
70 break;
71 case LOG_ERROR:
72 fprintf(stderr, "ERROR: ");
73 break;
74 case LOG_FATAL:
75 fprintf(stderr, "FATAL: ");
76 break;
77 default: /* invalid loglevel, ignore */
78 break;
79 }
80
81 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 va_start(arglist, fmt);
84 vfprintf(stderr, fmt, arglist);
85 va_end(arglist);
86
Jessica Yu93c95e52020-03-06 17:02:05 +010087 if (loglevel == LOG_FATAL)
88 exit(1);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090089 if (loglevel == LOG_ERROR)
90 error_occurred = true;
Matthew Wilcox2a116652006-10-07 05:35:32 -060091}
92
Masahiro Yamadae54dd932021-08-28 18:50:59 +090093static inline bool strends(const char *str, const char *postfix)
94{
95 if (strlen(str) < strlen(postfix))
96 return false;
97
98 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101void *do_nofail(void *ptr, const char *expr)
102{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100103 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100104 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return ptr;
107}
108
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900109char *read_text_file(const char *filename)
110{
111 struct stat st;
112 size_t nbytes;
113 int fd;
114 char *buf;
115
116 fd = open(filename, O_RDONLY);
117 if (fd < 0) {
118 perror(filename);
119 exit(1);
120 }
121
122 if (fstat(fd, &st) < 0) {
123 perror(filename);
124 exit(1);
125 }
126
127 buf = NOFAIL(malloc(st.st_size + 1));
128
129 nbytes = st.st_size;
130
131 while (nbytes) {
132 ssize_t bytes_read;
133
134 bytes_read = read(fd, buf, nbytes);
135 if (bytes_read < 0) {
136 perror(filename);
137 exit(1);
138 }
139
140 nbytes -= bytes_read;
141 }
142 buf[st.st_size] = '\0';
143
144 close(fd);
145
146 return buf;
147}
148
149char *get_line(char **stringp)
150{
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200151 char *orig = *stringp, *next;
152
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900153 /* do not return the unwanted extra line at EOF */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200154 if (!orig || *orig == '\0')
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900155 return NULL;
156
Wolfram Sang6020db52020-07-26 23:44:19 +0200157 /* don't use strsep here, it is not available everywhere */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200158 next = strchr(orig, '\n');
159 if (next)
160 *next++ = '\0';
161
162 *stringp = next;
163
164 return orig;
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static struct module *modules;
169
Masahiro Yamada8b185742018-05-09 18:50:40 +0900170static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 struct module *mod;
173
174 for (mod = modules; mod; mod = mod->next)
175 if (strcmp(mod->name, modname) == 0)
176 break;
177 return mod;
178}
179
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030180static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct module *mod;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100183
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900184 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 memset(mod, 0, sizeof(*mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* add to list */
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900188 strcpy(mod->name, modname);
Masahiro Yamada4de7b622020-06-01 14:57:30 +0900189 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
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
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +0530216/* This is based on the hash algorithm from gdbm, via tdb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static 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 },
307 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700308 { .str = "(unknown)", .export = export_unknown },
309};
310
311
312static const char *export_str(enum export ex)
313{
314 return export_list[ex].str;
315}
316
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100317static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700318{
319 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100320
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200321 if (!s)
322 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700323 for (i = 0; export_list[i].export != export_unknown; i++) {
324 if (strcmp(export_list[i].str, s) == 0)
325 return export_list[i].export;
326 }
327 return export_unknown;
328}
329
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900330static void *sym_get_data_by_offset(const struct elf_info *info,
331 unsigned int secindex, unsigned long offset)
Masahiro Yamada6124c042017-09-06 16:19:05 -0700332{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800333 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900334
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900335 if (info->hdr->e_type != ET_REL)
336 offset -= sechdr->sh_addr;
337
338 return (void *)info->hdr + sechdr->sh_offset + offset;
339}
340
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900341static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
342{
343 return sym_get_data_by_offset(info, get_secindex(info, sym),
344 sym->st_value);
345}
346
Masahiro Yamada565587d2020-05-25 14:47:05 +0900347static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
348{
349 return sym_get_data_by_offset(info, info->secindex_strings,
350 sechdr->sh_name);
351}
352
353static const char *sec_name(const struct elf_info *info, int secindex)
354{
355 return sech_name(info, &info->sechdrs[secindex]);
356}
357
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200358#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
359
360static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
361{
362 const char *secname = sec_name(elf, sec);
363
364 if (strstarts(secname, "___ksymtab+"))
365 return export_plain;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200366 else if (strstarts(secname, "___ksymtab_gpl+"))
367 return export_gpl;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200368 else
369 return export_unknown;
370}
371
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200372static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700373{
374 if (sec == elf->export_sec)
375 return export_plain;
376 else if (sec == elf->export_gpl_sec)
377 return export_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700378 else
379 return export_unknown;
380}
381
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900382static const char *namespace_from_kstrtabns(const struct elf_info *info,
383 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100384{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900385 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100386 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100387}
388
Matthias Maennicha2b11182019-10-18 10:31:40 +0100389static void sym_update_namespace(const char *symname, const char *namespace)
390{
391 struct symbol *s = find_symbol(symname);
392
393 /*
394 * That symbol should have been created earlier and thus this is
395 * actually an assertion.
396 */
397 if (!s) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +0900398 error("Could not update namespace(%s) for symbol %s\n",
399 namespace, symname);
Matthias Maennicha2b11182019-10-18 10:31:40 +0100400 return;
401 }
402
403 free(s->namespace);
404 s->namespace =
405 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
406}
407
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100408/**
409 * Add an exported symbol - it may have already been added without a
410 * CRC, in this case just update the CRC
411 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100412static struct symbol *sym_add_exported(const char *name, struct module *mod,
413 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct symbol *s = find_symbol(name);
416
417 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700418 s = new_symbol(name, mod, export);
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900419 } else if (!external_module || s->module->is_vmlinux ||
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900420 s->module == mod) {
421 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
422 mod->name, name, s->module->name,
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900423 s->module->is_vmlinux ? "" : ".ko");
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900424 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900426
427 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700428 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100429 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Masahiro Yamada17436942019-11-15 02:42:24 +0900432static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100433{
434 struct symbol *s = find_symbol(name);
435
Masahiro Yamada17436942019-11-15 02:42:24 +0900436 /*
437 * Ignore stand-alone __crc_*, which might be auto-generated symbols
438 * such as __*_veneer in ARM ELF.
439 */
440 if (!s)
441 return;
442
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100443 s->crc = crc;
444 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900447static void *grab_file(const char *filename, size_t *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930450 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int fd;
452
453 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930454 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930456 if (fstat(fd, &st))
457 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 *size = st.st_size;
460 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930462failed:
463 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (map == MAP_FAILED)
465 return NULL;
466 return map;
467}
468
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900469static void release_file(void *file, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 munmap(file, size);
472}
473
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100474static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100477 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 Elf_Shdr *sechdrs;
479 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200480 const char *secstrings;
481 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 hdr = grab_file(filename, &info->size);
484 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930485 if (ignore_missing_files) {
486 fprintf(stderr, "%s: %s (ignored)\n", filename,
487 strerror(errno));
488 return 0;
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200491 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100494 if (info->size < sizeof(*hdr)) {
495 /* file too small, assume this is an empty .o file */
496 return 0;
497 }
498 /* Is this a valid ELF file? */
499 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
500 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
501 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
502 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
503 /* Not an ELF file - silently ignore it */
504 return 0;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200507 hdr->e_type = TO_NATIVE(hdr->e_type);
508 hdr->e_machine = TO_NATIVE(hdr->e_machine);
509 hdr->e_version = TO_NATIVE(hdr->e_version);
510 hdr->e_entry = TO_NATIVE(hdr->e_entry);
511 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
512 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
513 hdr->e_flags = TO_NATIVE(hdr->e_flags);
514 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
515 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
516 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
517 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
518 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
519 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 sechdrs = (void *)hdr + hdr->e_shoff;
521 info->sechdrs = sechdrs;
522
Petr Stetiara83710e2007-08-27 12:15:07 +0200523 /* Check if file offset is correct */
524 if (hdr->e_shoff > info->size) {
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900525 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
526 (unsigned long)hdr->e_shoff, filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200527 return 0;
528 }
529
Anders Kaseorg68457562011-05-19 16:55:27 -0600530 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200531 /*
532 * There are more than 64k sections,
533 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200534 */
535 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
536 }
537 else {
538 info->num_sections = hdr->e_shnum;
539 }
540 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600541 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200542 }
543 else {
544 info->secindex_strings = hdr->e_shstrndx;
545 }
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200548 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200549 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
550 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
551 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
552 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
553 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
554 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
555 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
556 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
557 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
558 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200561 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
562 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700563 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900564 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Tejun Heo56fc82c2009-02-06 00:48:02 +0900566 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100567 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
568 "sizeof(*hrd)=%zu\n", filename,
569 (unsigned long)sechdrs[i].sh_offset,
570 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100571 return 0;
572 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700573 secname = secstrings + sechdrs[i].sh_name;
574 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900575 if (nobits)
576 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
578 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700579 } else if (strcmp(secname, "__ksymtab") == 0)
580 info->export_sec = i;
581 else if (strcmp(secname, "__ksymtab_gpl") == 0)
582 info->export_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700583
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200584 if (sechdrs[i].sh_type == SHT_SYMTAB) {
585 unsigned int sh_link_idx;
586 symtab_idx = i;
587 info->symtab_start = (void *)hdr +
588 sechdrs[i].sh_offset;
589 info->symtab_stop = (void *)hdr +
590 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600591 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200592 info->strtab = (void *)hdr +
593 sechdrs[sh_link_idx].sh_offset;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200596 /* 32bit section no. table? ("more than 64k sections") */
597 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
598 symtab_shndx_idx = i;
599 info->symtab_shndx_start = (void *)hdr +
600 sechdrs[i].sh_offset;
601 info->symtab_shndx_stop = (void *)hdr +
602 sechdrs[i].sh_offset + sechdrs[i].sh_size;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100605 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100606 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Fix endianness in symbols */
609 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
610 sym->st_shndx = TO_NATIVE(sym->st_shndx);
611 sym->st_name = TO_NATIVE(sym->st_name);
612 sym->st_value = TO_NATIVE(sym->st_value);
613 sym->st_size = TO_NATIVE(sym->st_size);
614 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200615
616 if (symtab_shndx_idx != ~0U) {
617 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600618 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200619 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600620 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200621 symtab_idx);
622 /* Fix endianness */
623 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
624 p++)
625 *p = TO_NATIVE(*p);
626 }
627
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100628 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100631static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 release_file(info->hdr, info->size);
634}
635
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200636static int ignore_undef_symbol(struct elf_info *info, const char *symname)
637{
638 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900639 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200640 return 1;
641 /* ignore global offset table */
642 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
643 return 1;
644 if (info->hdr->e_machine == EM_PPC)
645 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900646 if (strstarts(symname, "_restgpr_") ||
647 strstarts(symname, "_savegpr_") ||
648 strstarts(symname, "_rest32gpr_") ||
649 strstarts(symname, "_save32gpr_") ||
650 strstarts(symname, "_restvr_") ||
651 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200652 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000653 if (info->hdr->e_machine == EM_PPC64)
654 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900655 if (strstarts(symname, "_restgpr0_") ||
656 strstarts(symname, "_savegpr0_") ||
657 strstarts(symname, "_restvr_") ||
658 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100659 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000660 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200661 /* Do not ignore this symbol */
662 return 0;
663}
664
Masahiro Yamada17436942019-11-15 02:42:24 +0900665static void handle_modversion(const struct module *mod,
666 const struct elf_info *info,
667 const Elf_Sym *sym, const char *symname)
668{
669 unsigned int crc;
670
671 if (sym->st_shndx == SHN_UNDEF) {
Mark Brown4a679592021-06-07 15:02:06 +0100672 warn("EXPORT symbol \"%s\" [%s%s] version ...\n"
673 "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
674 symname, mod->name, mod->is_vmlinux ? "" : ".ko",
675 symname);
676
Masahiro Yamada17436942019-11-15 02:42:24 +0900677 return;
678 }
679
680 if (sym->st_shndx == SHN_ABS) {
681 crc = sym->st_value;
682 } else {
683 unsigned int *crcp;
684
685 /* symbol points to the CRC in the ELF object */
686 crcp = sym_get_data(info, sym);
687 crc = TO_NATIVE(*crcp);
688 }
689 sym_set_crc(symname, crc);
690}
691
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900692static void handle_symbol(struct module *mod, struct elf_info *info,
693 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200695 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900696 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200697
Masahiro Yamada33795762020-06-01 14:57:24 +0900698 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200699 export = export_from_secname(info, get_secindex(info, sym));
700 else
701 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 switch (sym->st_shndx) {
704 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900705 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100706 /* Should warn here, but modpost runs before the linker */
707 } else
708 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 case SHN_UNDEF:
711 /* undefined symbol */
712 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
713 ELF_ST_BIND(sym->st_info) != STB_WEAK)
714 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200715 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (info->hdr->e_machine == EM_SPARC ||
718 info->hdr->e_machine == EM_SPARCV9) {
719 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700720 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100722 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700723 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100724 munged[0] = '_';
725 munged[1] = toupper(munged[1]);
726 symname = munged;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100729
Rusty Russellb92021b2013-03-15 15:04:17 +1030730 mod->unres = alloc_symbol(symname,
731 ELF_ST_BIND(sym->st_info) == STB_WEAK,
732 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
734 default:
735 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900736 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100737 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100738 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900740 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900742 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 mod->has_cleanup = 1;
744 break;
745 }
746}
747
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100748/**
749 * Parse tag=value strings from .modinfo section
750 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751static char *next_string(char *string, unsigned long *secsize)
752{
753 /* Skip non-zero chars */
754 while (string[0]) {
755 string++;
756 if ((*secsize)-- <= 1)
757 return NULL;
758 }
759
760 /* Skip any zero padding. */
761 while (!string[0]) {
762 string++;
763 if ((*secsize)-- <= 1)
764 return NULL;
765 }
766 return string;
767}
768
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900769static char *get_next_modinfo(struct elf_info *info, const char *tag,
770 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 char *p;
773 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900774 char *modinfo = info->modinfo;
775 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900777 if (prev) {
778 size -= prev - modinfo;
779 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200780 }
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 for (p = modinfo; p; p = next_string(p, &size)) {
783 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
784 return p + taglen + 1;
785 }
786 return NULL;
787}
788
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900789static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200790
791{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900792 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200793}
794
Sam Ravnborg93684d32006-02-19 11:53:35 +0100795/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100796 * Test if string s ends in string sub
797 * return 0 if match
798 **/
799static int strrcmp(const char *s, const char *sub)
800{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100801 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100802
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100803 if (!s || !sub)
804 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100805
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100806 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100807 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100808
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100809 if ((slen == 0) || (sublen == 0))
810 return 1;
811
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100812 if (sublen > slen)
813 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100814
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100815 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100816}
817
Sam Ravnborgff13f922008-01-23 19:54:27 +0100818static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
819{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100820 if (sym)
821 return elf->strtab + sym->st_name;
822 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100823 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100824}
825
Sam Ravnborg10668222008-01-13 22:21:31 +0100826/* The pattern is an array of simple patterns.
827 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100828 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100829 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930830 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100831 */
Trevor Keith5c725132009-09-22 16:43:38 -0700832static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100833{
834 const char *p;
835 while (*pat) {
836 p = *pat++;
837 const char *endp = p + strlen(p) - 1;
838
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930839 /* "*foo*" */
840 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300841 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
842 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930843
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930844 free(bare);
845 if (here != NULL)
846 return 1;
847 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100848 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930849 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100850 if (strrcmp(sym, p + 1) == 0)
851 return 1;
852 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100853 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100854 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100855 if (strncmp(sym, p, strlen(p) - 1) == 0)
856 return 1;
857 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100858 /* no wildcards */
859 else {
860 if (strcmp(p, sym) == 0)
861 return 1;
862 }
863 }
864 /* no match */
865 return 0;
866}
867
Sam Ravnborg10668222008-01-13 22:21:31 +0100868/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930869static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200870{
871 ".comment*",
872 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930873 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800874 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000875 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200876 ".mdebug*", /* alpha, score, mips etc. */
877 ".pdr", /* alpha, score, mips etc. */
878 ".stab*",
879 ".note*",
880 ".got*",
881 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400882 ".xt.prop", /* xtensa */
883 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030884 ".arcextmap*", /* arc */
885 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030886 ".cmem*", /* EZchip */
887 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100888 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600889 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200890 NULL
891};
Sam Ravnborg10668222008-01-13 22:21:31 +0100892
Sam Ravnborge241a632008-01-28 20:13:13 +0100893/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400894 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100895 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400896 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100897 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400898static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900899 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100900{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400901 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100902
Anders Kaseorgb614a692009-04-23 16:49:33 -0400903 if (sechdr->sh_type == SHT_PROGBITS &&
904 !(sechdr->sh_flags & SHF_ALLOC) &&
905 !match(sec, section_white_list)) {
906 warn("%s (%s): unexpected non-allocatable section.\n"
907 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
908 "Note that for example <linux/init.h> contains\n"
909 "section definitions for use in .S files.\n\n",
910 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100911 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100912}
913
914
915
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100916#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930917 ".init.setup", ".init.rodata", ".meminit.rodata", \
918 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100919#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930920 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100921
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100922#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930923 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100924#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930925 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100926
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200927#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930928 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
929 ".pci_fixup_enable", ".pci_fixup_resume", \
930 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200931
Paul Gortmakere24f6622013-06-19 19:30:48 -0400932#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
933#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100934
935#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
936#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100937
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930938#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930939#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Thomas Gleixner65538962020-03-09 22:47:17 +0100940 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930941#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400942 ".fixup", ".entry.text", ".exception.text", ".text.*", \
Christophe Leroy1e688dd22021-04-13 16:38:10 +0000943 ".coldtext", ".softirqentry.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100944
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000945#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000946#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100947
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000948#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000949#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100950
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930951#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
952 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
953
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100954/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930955static const char *const init_data_sections[] =
956 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100957
958/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930959static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100960
961/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930962static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100963 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100964
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930965/* all text sections */
966static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
967
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100968/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930969static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100970
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100971
972/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100973#define DEFAULT_SYMBOL_WHITE_LIST \
974 "*driver", \
975 "*_template", /* scsi uses *_template a lot */ \
976 "*_timer", /* arm uses ops structures named _timer a lot */ \
977 "*_sht", /* scsi also used *_sht to some extent */ \
978 "*_ops", \
979 "*_probe", \
980 "*_probe_one", \
981 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100982
Mathias Krause7a3ee752014-08-27 20:28:53 +0930983static const char *const head_sections[] = { ".head.text*", NULL };
984static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100985 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930986static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100987
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100988enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100989 TEXT_TO_ANY_INIT,
990 DATA_TO_ANY_INIT,
991 TEXT_TO_ANY_EXIT,
992 DATA_TO_ANY_EXIT,
993 XXXINIT_TO_SOME_INIT,
994 XXXEXIT_TO_SOME_EXIT,
995 ANY_INIT_TO_ANY_EXIT,
996 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100997 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930998 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100999};
1000
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301001/**
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +05301002 * Describe how to match sections on different criteria:
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301003 *
1004 * @fromsec: Array of sections to be matched.
1005 *
1006 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1007 * this array is forbidden (black-list). Can be empty.
1008 *
1009 * @good_tosec: Relocations applied to a section in @fromsec must be
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +05301010 * targeting sections in this array (white-list). Can be empty.
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301011 *
1012 * @mismatch: Type of mismatch.
1013 *
1014 * @symbol_white_list: Do not match a relocation to a symbol in this list
Bhaskar Chowdhuryf3945832021-03-26 11:22:19 +05301015 * even if it is targeting a section in @bad_to_sec.
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301016 *
1017 * @handler: Specific handler to call when a match is found. If NULL,
1018 * default_mismatch_handler() will be called.
1019 *
1020 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001021struct sectioncheck {
1022 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301023 const char *bad_tosec[20];
1024 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001025 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001026 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301027 void (*handler)(const char *modname, struct elf_info *elf,
1028 const struct sectioncheck* const mismatch,
1029 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1030
Sam Ravnborg10668222008-01-13 22:21:31 +01001031};
1032
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301033static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1034 const struct sectioncheck* const mismatch,
1035 Elf_Rela *r, Elf_Sym *sym,
1036 const char *fromsec);
1037
Mathias Krause7a3ee752014-08-27 20:28:53 +09301038static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001039/* Do not reference init/exit code/data from
1040 * normal code and data
1041 */
1042{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001043 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301044 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001045 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001046 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001047},
1048{
1049 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301050 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001051 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001052 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001053},
1054{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001055 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301056 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001057 .mismatch = DATA_TO_ANY_INIT,
1058 .symbol_white_list = {
1059 "*_template", "*_timer", "*_sht", "*_ops",
1060 "*_probe", "*_probe_one", "*_console", NULL
1061 },
1062},
1063{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001064 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301065 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001066 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001067 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001068},
1069{
1070 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301071 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001072 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001073 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001074},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001075/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001076{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001077 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301078 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001079 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001080 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001081},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001082/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001083{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001084 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301085 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001086 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001087 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001088},
1089/* Do not use exit code/data from init code */
1090{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001091 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301092 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001093 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001094 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001095},
1096/* Do not use init code/data from exit code */
1097{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001098 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301099 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001100 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001101 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001102},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001103{
1104 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301105 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001106 .mismatch = ANY_INIT_TO_ANY_EXIT,
1107 .symbol_white_list = { NULL },
1108},
Sam Ravnborg10668222008-01-13 22:21:31 +01001109/* Do not export init/exit functions or data */
1110{
1111 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301112 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001113 .mismatch = EXPORT_TO_INIT_EXIT,
1114 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301115},
1116{
1117 .fromsec = { "__ex_table", NULL },
1118 /* If you're adding any new black-listed sections in here, consider
1119 * adding a special 'printer' for them in scripts/check_extable.
1120 */
1121 .bad_tosec = { ".altinstr_replacement", NULL },
1122 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1123 .mismatch = EXTABLE_TO_NON_TEXT,
1124 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001125}
1126};
1127
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001128static const struct sectioncheck *section_mismatch(
1129 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001130{
1131 int i;
1132 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1133 const struct sectioncheck *check = &sectioncheck[0];
1134
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301135 /*
1136 * The target section could be the SHT_NUL section when we're
1137 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001138 * doesn't make much sense and causes build failures on parisc
1139 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301140 */
1141 if (*tosec == '\0')
1142 return NULL;
1143
Sam Ravnborg10668222008-01-13 22:21:31 +01001144 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301145 if (match(fromsec, check->fromsec)) {
1146 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1147 return check;
1148 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1149 return check;
1150 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001151 check++;
1152 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001153 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001154}
1155
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001156/**
1157 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001158 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001159 * Pattern 1:
1160 * If a module parameter is declared __initdata and permissions=0
1161 * then this is legal despite the warning generated.
1162 * We cannot see value of permissions here, so just ignore
1163 * this pattern.
1164 * The pattern is identified by:
1165 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001166 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001167 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001168 *
Rusty Russell6a841522010-08-11 23:04:16 -06001169 * Pattern 1a:
1170 * module_param_call() ops can refer to __init set function if permissions=0
1171 * The pattern is identified by:
1172 * tosec = .init.text
1173 * fromsec = .data*
1174 * atsym = __param_ops_*
1175 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001176 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001177 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001178 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001179 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001180 * tosec = init or exit section
1181 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001182 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1183 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001184 *
1185 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001186 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001187 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001188 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001189 * Some symbols belong to init section but still it is ok to reference
1190 * these from non-init sections as these symbols don't have any memory
1191 * allocated for them and symbol address and value are same. So even
1192 * if init section is freed, its ok to reference those symbols.
1193 * For ex. symbols marking the init section boundaries.
1194 * This pattern is identified by
1195 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001196 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301197 * Pattern 5:
1198 * GCC may optimize static inlines when fed constant arg(s) resulting
1199 * in functions like cpumask_empty() -- generating an associated symbol
1200 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1201 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1202 * meaningless section warning. May need to add isra symbols too...
1203 * This pattern is identified by
1204 * tosec = init section
1205 * fromsec = text section
1206 * refsymname = *.constprop.*
1207 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001208 * Pattern 6:
1209 * Hide section mismatch warnings for ELF local symbols. The goal
1210 * is to eliminate false positive modpost warnings caused by
1211 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1212 * Autogenerated symbol names bypass modpost's "Pattern 2"
1213 * whitelisting, which relies on pattern-matching against symbol
1214 * names to work. (One situation where gcc can autogenerate ELF
1215 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001216 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001217static int secref_whitelist(const struct sectioncheck *mismatch,
1218 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001219 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001220{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001221 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001222 if (match(tosec, init_data_sections) &&
1223 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001224 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001225 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001226
Rusty Russell6a841522010-08-11 23:04:16 -06001227 /* Check for pattern 1a */
1228 if (strcmp(tosec, ".init.text") == 0 &&
1229 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001230 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001231 return 0;
1232
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001233 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001234 if (match(tosec, init_exit_sections) &&
1235 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001236 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001237 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001238
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001239 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001240 if (match(fromsec, head_sections) &&
1241 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001242 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001243
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001244 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001245 if (match(tosym, linker_symbols))
1246 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001247
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301248 /* Check for pattern 5 */
1249 if (match(fromsec, text_sections) &&
1250 match(tosec, init_sections) &&
1251 match(fromsym, optim_symbols))
1252 return 0;
1253
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001254 /* Check for pattern 6 */
1255 if (strstarts(fromsym, ".L"))
1256 return 0;
1257
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001258 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001259}
1260
Sami Tolvanen5818c682018-10-23 15:15:35 -07001261static inline int is_arm_mapping_symbol(const char *str)
1262{
1263 return str[0] == '$' && strchr("axtd", str[1])
1264 && (str[2] == '\0' || str[2] == '.');
1265}
1266
1267/*
1268 * If there's no name there, ignore it; likewise, ignore it if it's
1269 * one of the magic symbols emitted used by current ARM tools.
1270 *
1271 * Otherwise if find_symbols_between() returns those symbols, they'll
1272 * fail the whitelist tests and cause lots of false alarms ... fixable
1273 * only by merging __exit and __init sections into __text, bloating
1274 * the kernel (which is especially evil on embedded platforms).
1275 */
1276static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1277{
1278 const char *name = elf->strtab + sym->st_name;
1279
1280 if (!name || !strlen(name))
1281 return 0;
1282 return !is_arm_mapping_symbol(name);
1283}
1284
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001285/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001286 * Find symbol based on relocation record info.
1287 * In some cases the symbol supplied is a valid symbol so
1288 * return refsym. If st_name != 0 we assume this is a valid symbol.
1289 * In other cases the symbol needs to be looked up in the symbol table
1290 * based on section and address.
1291 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001292static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001293 Elf_Sym *relsym)
1294{
1295 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001296 Elf_Sym *near = NULL;
1297 Elf64_Sword distance = 20;
1298 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001299 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001300
1301 if (relsym->st_name != 0)
1302 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001303
1304 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001305 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001306 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001307 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001308 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1309 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001310 if (!is_valid_name(elf, sym))
1311 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001312 if (sym->st_value == addr)
1313 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001314 /* Find a symbol nearby - addr are maybe negative */
1315 d = sym->st_value - addr;
1316 if (d < 0)
1317 d = addr - sym->st_value;
1318 if (d < distance) {
1319 distance = d;
1320 near = sym;
1321 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001322 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001323 /* We need a close match */
1324 if (distance < 20)
1325 return near;
1326 else
1327 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001328}
1329
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001330/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001331 * Find symbols before or equal addr and after addr - in the section sec.
1332 * If we find two symbols with equal offset prefer one with a valid name.
1333 * The ELF format may have a better way to detect what type of symbol
1334 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001335 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001336static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1337 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001338{
1339 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001340 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001341 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001342
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001343 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1344 const char *symsec;
1345
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001346 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001347 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001348 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001349 if (strcmp(symsec, sec) != 0)
1350 continue;
David Brownellda68d612007-02-20 13:58:16 -08001351 if (!is_valid_name(elf, sym))
1352 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001353 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001354 if ((addr - sym->st_value) < distance) {
1355 distance = addr - sym->st_value;
1356 near = sym;
1357 } else if ((addr - sym->st_value) == distance) {
1358 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001359 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001360 }
1361 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001362 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001363}
1364
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001365/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001366 * Convert a section name to the function/data attribute
1367 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001368 * .memexitconst => __memconst
1369 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001370 *
1371 * The memory of returned value has been allocated on a heap. The user of this
1372 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001373*/
1374static char *sec2annotation(const char *s)
1375{
1376 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001377 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001378 char *r = p;
1379
1380 *p++ = '_';
1381 *p++ = '_';
1382 if (*s == '.')
1383 s++;
1384 while (*s && *s != '.')
1385 *p++ = *s++;
1386 *p = '\0';
1387 if (*s == '.')
1388 s++;
1389 if (strstr(s, "rodata") != NULL)
1390 strcat(p, "const ");
1391 else if (strstr(s, "data") != NULL)
1392 strcat(p, "data ");
1393 else
1394 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001395 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001396 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001397 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001398 }
1399}
1400
1401static int is_function(Elf_Sym *sym)
1402{
1403 if (sym)
1404 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1405 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001406 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001407}
1408
Randy Dunlap00759c02011-03-15 14:13:47 -07001409static void print_section_list(const char * const list[20])
1410{
1411 const char *const *s = list;
1412
1413 while (*s) {
1414 fprintf(stderr, "%s", *s);
1415 s++;
1416 if (*s)
1417 fprintf(stderr, ", ");
1418 }
1419 fprintf(stderr, "\n");
1420}
1421
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301422static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1423{
1424 switch (is_func) {
1425 case 0: *name = "variable"; *name_p = ""; break;
1426 case 1: *name = "function"; *name_p = "()"; break;
1427 default: *name = "(unknown reference)"; *name_p = ""; break;
1428 }
1429}
1430
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001431/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001432 * Print a warning about a section mismatch.
1433 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001434 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001435 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001436static void report_sec_mismatch(const char *modname,
1437 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001438 const char *fromsec,
1439 unsigned long long fromaddr,
1440 const char *fromsym,
1441 int from_is_func,
1442 const char *tosec, const char *tosym,
1443 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001444{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001445 const char *from, *from_p;
1446 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001447 char *prl_from;
1448 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001449
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001450 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001451
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301452 get_pretty_name(from_is_func, &from, &from_p);
1453 get_pretty_name(to_is_func, &to, &to_p);
1454
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001455 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1456 "to the %s %s:%s%s\n",
1457 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1458 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001459
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001460 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001461 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001462 prl_from = sec2annotation(fromsec);
1463 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001464 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001465 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001466 "the %s %s%s%s.\n"
1467 "This is often because %s lacks a %s\n"
1468 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001469 prl_from, fromsym,
1470 to, prl_to, tosym, to_p,
1471 fromsym, prl_to, tosym);
1472 free(prl_from);
1473 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001474 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001475 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001476 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001477 fprintf(stderr,
1478 "The variable %s references\n"
1479 "the %s %s%s%s\n"
1480 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001481 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001482 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001483 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001484 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001485 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001486 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001487 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001488 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001489 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001490 fprintf(stderr,
1491 "The function %s() references a %s in an exit section.\n"
1492 "Often the %s %s%s has valid usage outside the exit section\n"
1493 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001494 fromsym, to, to, tosym, to_p, prl_to, tosym);
1495 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001496 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001497 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001498 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001499 fprintf(stderr,
1500 "The variable %s references\n"
1501 "the %s %s%s%s\n"
1502 "If the reference is valid then annotate the\n"
1503 "variable with __exit* (see linux/init.h) or "
1504 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001505 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001506 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001507 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001508 break;
1509 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001510 case XXXINIT_TO_SOME_INIT:
1511 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001512 prl_from = sec2annotation(fromsec);
1513 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001514 fprintf(stderr,
1515 "The %s %s%s%s references\n"
1516 "a %s %s%s%s.\n"
1517 "If %s is only used by %s then\n"
1518 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001519 from, prl_from, fromsym, from_p,
1520 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001521 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001522 free(prl_from);
1523 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001524 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001525 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001526 prl_from = sec2annotation(fromsec);
1527 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001528 fprintf(stderr,
1529 "The %s %s%s%s references\n"
1530 "a %s %s%s%s.\n"
1531 "This is often seen when error handling "
1532 "in the init function\n"
1533 "uses functionality in the exit path.\n"
1534 "The fix is often to remove the %sannotation of\n"
1535 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001536 from, prl_from, fromsym, from_p,
1537 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001538 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001539 free(prl_from);
1540 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001541 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001542 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001543 prl_from = sec2annotation(fromsec);
1544 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001545 fprintf(stderr,
1546 "The %s %s%s%s references\n"
1547 "a %s %s%s%s.\n"
1548 "This is often seen when error handling "
1549 "in the exit function\n"
1550 "uses functionality in the init path.\n"
1551 "The fix is often to remove the %sannotation of\n"
1552 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001553 from, prl_from, fromsym, from_p,
1554 to, prl_to, tosym, to_p,
1555 prl_to, tosym, to_p);
1556 free(prl_from);
1557 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001558 break;
1559 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001560 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001561 fprintf(stderr,
1562 "The symbol %s is exported and annotated %s\n"
1563 "Fix this by removing the %sannotation of %s "
1564 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001565 tosym, prl_to, prl_to, tosym);
1566 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001567 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301568 case EXTABLE_TO_NON_TEXT:
1569 fatal("There's a special handler for this mismatch type, "
1570 "we should never get here.");
1571 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001572 }
1573 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001574}
1575
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301576static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1577 const struct sectioncheck* const mismatch,
1578 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1579{
1580 const char *tosec;
1581 Elf_Sym *to;
1582 Elf_Sym *from;
1583 const char *tosym;
1584 const char *fromsym;
1585
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301586 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1587 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301588
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001589 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301590 return;
1591
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301592 tosec = sec_name(elf, get_secindex(elf, sym));
1593 to = find_elf_symbol(elf, r->r_addend, sym);
1594 tosym = sym_name(elf, to);
1595
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301596 /* check whitelist - we may ignore it */
1597 if (secref_whitelist(mismatch,
1598 fromsec, fromsym, tosec, tosym)) {
1599 report_sec_mismatch(modname, mismatch,
1600 fromsec, r->r_offset, fromsym,
1601 is_function(from), tosec, tosym,
1602 is_function(to));
1603 }
1604}
1605
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301606static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1607{
1608 if (section_index > elf->num_sections)
1609 fatal("section_index is outside elf->num_sections!\n");
1610
1611 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1612}
1613
1614/*
1615 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1616 * to know the sizeof(struct exception_table_entry) for the target architecture.
1617 */
1618static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301619static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301620{
1621 /*
1622 * If we're currently checking the second relocation within __ex_table,
1623 * that relocation offset tells us the offsetof(struct
1624 * exception_table_entry, fixup) which is equal to sizeof(struct
1625 * exception_table_entry) divided by two. We use that to our advantage
1626 * since there's no portable way to get that size as every architecture
1627 * seems to go with different sized types. Not pretty but better than
1628 * hard-coding the size for every architecture..
1629 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301630 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301631 extable_entry_size = r->r_offset * 2;
1632}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301633
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301634static inline bool is_extable_fault_address(Elf_Rela *r)
1635{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301636 /*
1637 * extable_entry_size is only discovered after we've handled the
1638 * _second_ relocation in __ex_table, so only abort when we're not
1639 * handling the first reloc and extable_entry_size is zero.
1640 */
1641 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301642 fatal("extable_entry size hasn't been discovered!\n");
1643
1644 return ((r->r_offset == 0) ||
1645 (r->r_offset % extable_entry_size == 0));
1646}
1647
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301648#define is_second_extable_reloc(Start, Cur, Sec) \
1649 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1650
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301651static void report_extable_warnings(const char* modname, struct elf_info* elf,
1652 const struct sectioncheck* const mismatch,
1653 Elf_Rela* r, Elf_Sym* sym,
1654 const char* fromsec, const char* tosec)
1655{
1656 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1657 const char* fromsym_name = sym_name(elf, fromsym);
1658 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1659 const char* tosym_name = sym_name(elf, tosym);
1660 const char* from_pretty_name;
1661 const char* from_pretty_name_p;
1662 const char* to_pretty_name;
1663 const char* to_pretty_name_p;
1664
1665 get_pretty_name(is_function(fromsym),
1666 &from_pretty_name, &from_pretty_name_p);
1667 get_pretty_name(is_function(tosym),
1668 &to_pretty_name, &to_pretty_name_p);
1669
1670 warn("%s(%s+0x%lx): Section mismatch in reference"
1671 " from the %s %s%s to the %s %s:%s%s\n",
1672 modname, fromsec, (long)r->r_offset, from_pretty_name,
1673 fromsym_name, from_pretty_name_p,
1674 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1675
1676 if (!match(tosec, mismatch->bad_tosec) &&
1677 is_executable_section(elf, get_secindex(elf, sym)))
1678 fprintf(stderr,
1679 "The relocation at %s+0x%lx references\n"
1680 "section \"%s\" which is not in the list of\n"
1681 "authorized sections. If you're adding a new section\n"
1682 "and/or if this reference is valid, add \"%s\" to the\n"
1683 "list of authorized sections to jump to on fault.\n"
1684 "This can be achieved by adding \"%s\" to \n"
1685 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1686 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1687}
1688
1689static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1690 const struct sectioncheck* const mismatch,
1691 Elf_Rela* r, Elf_Sym* sym,
1692 const char *fromsec)
1693{
1694 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1695
1696 sec_mismatch_count++;
1697
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001698 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301699
1700 if (match(tosec, mismatch->bad_tosec))
1701 fatal("The relocation at %s+0x%lx references\n"
1702 "section \"%s\" which is black-listed.\n"
1703 "Something is seriously wrong and should be fixed.\n"
1704 "You might get more information about where this is\n"
1705 "coming from by using scripts/check_extable.sh %s\n",
1706 fromsec, (long)r->r_offset, tosec, modname);
1707 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1708 if (is_extable_fault_address(r))
1709 fatal("The relocation at %s+0x%lx references\n"
1710 "section \"%s\" which is not executable, IOW\n"
1711 "it is not possible for the kernel to fault\n"
1712 "at that address. Something is seriously wrong\n"
1713 "and should be fixed.\n",
1714 fromsec, (long)r->r_offset, tosec);
1715 else
1716 fatal("The relocation at %s+0x%lx references\n"
1717 "section \"%s\" which is not executable, IOW\n"
1718 "the kernel will fault if it ever tries to\n"
1719 "jump to it. Something is seriously wrong\n"
1720 "and should be fixed.\n",
1721 fromsec, (long)r->r_offset, tosec);
1722 }
1723}
1724
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001725static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001726 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001727{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001728 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301729 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001730
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001731 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301732 if (mismatch->handler)
1733 mismatch->handler(modname, elf, mismatch,
1734 r, sym, fromsec);
1735 else
1736 default_mismatch_handler(modname, elf, mismatch,
1737 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001738 }
1739}
1740
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001741static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001742 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001743{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001744 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001745}
1746
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001747static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001748{
1749 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001750 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001751
1752 switch (r_typ) {
1753 case R_386_32:
1754 r->r_addend = TO_NATIVE(*location);
1755 break;
1756 case R_386_PC32:
1757 r->r_addend = TO_NATIVE(*location) + 4;
1758 /* For CONFIG_RELOCATABLE=y */
1759 if (elf->hdr->e_type == ET_EXEC)
1760 r->r_addend += r->r_offset;
1761 break;
1762 }
1763 return 0;
1764}
1765
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001766#ifndef R_ARM_CALL
1767#define R_ARM_CALL 28
1768#endif
1769#ifndef R_ARM_JUMP24
1770#define R_ARM_JUMP24 29
1771#endif
1772
David A. Longc9698e52014-02-14 22:41:18 +01001773#ifndef R_ARM_THM_CALL
1774#define R_ARM_THM_CALL 10
1775#endif
1776#ifndef R_ARM_THM_JUMP24
1777#define R_ARM_THM_JUMP24 30
1778#endif
1779#ifndef R_ARM_THM_JUMP19
1780#define R_ARM_THM_JUMP19 51
1781#endif
1782
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001783static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001784{
1785 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1786
1787 switch (r_typ) {
1788 case R_ARM_ABS32:
1789 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001790 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001791 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001792 break;
1793 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001794 case R_ARM_CALL:
1795 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001796 case R_ARM_THM_CALL:
1797 case R_ARM_THM_JUMP24:
1798 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001799 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001800 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001801 sechdr->sh_offset +
1802 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001803 break;
1804 default:
1805 return 1;
1806 }
1807 return 0;
1808}
1809
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001810static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001811{
1812 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001813 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001814 unsigned int inst;
1815
1816 if (r_typ == R_MIPS_HI16)
1817 return 1; /* skip this */
1818 inst = TO_NATIVE(*location);
1819 switch (r_typ) {
1820 case R_MIPS_LO16:
1821 r->r_addend = inst & 0xffff;
1822 break;
1823 case R_MIPS_26:
1824 r->r_addend = (inst & 0x03ffffff) << 2;
1825 break;
1826 case R_MIPS_32:
1827 r->r_addend = inst;
1828 break;
1829 }
1830 return 0;
1831}
1832
Jisheng Zhangbb1f85d2021-11-18 19:22:51 +08001833#ifndef EM_RISCV
1834#define EM_RISCV 243
1835#endif
1836
1837#ifndef R_RISCV_SUB32
1838#define R_RISCV_SUB32 39
1839#endif
1840
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001841static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001842 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001843{
1844 Elf_Sym *sym;
1845 Elf_Rela *rela;
1846 Elf_Rela r;
1847 unsigned int r_sym;
1848 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001849
Sam Ravnborgff13f922008-01-23 19:54:27 +01001850 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001851 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1852
Sam Ravnborgff13f922008-01-23 19:54:27 +01001853 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001854 fromsec += strlen(".rela");
1855 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001856 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001857 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001858
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001859 for (rela = start; rela < stop; rela++) {
1860 r.r_offset = TO_NATIVE(rela->r_offset);
1861#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001862 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001863 unsigned int r_typ;
1864 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1865 r_sym = TO_NATIVE(r_sym);
1866 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1867 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1868 } else {
1869 r.r_info = TO_NATIVE(rela->r_info);
1870 r_sym = ELF_R_SYM(r.r_info);
1871 }
1872#else
1873 r.r_info = TO_NATIVE(rela->r_info);
1874 r_sym = ELF_R_SYM(r.r_info);
1875#endif
1876 r.r_addend = TO_NATIVE(rela->r_addend);
Jisheng Zhangbb1f85d2021-11-18 19:22:51 +08001877 switch (elf->hdr->e_machine) {
1878 case EM_RISCV:
1879 if (!strcmp("__ex_table", fromsec) &&
1880 ELF_R_TYPE(r.r_info) == R_RISCV_SUB32)
1881 continue;
1882 break;
1883 }
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001884 sym = elf->symtab_start + r_sym;
1885 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001886 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001887 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301888 if (is_second_extable_reloc(start, rela, fromsec))
1889 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001890 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001891 }
1892}
1893
1894static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001895 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001896{
1897 Elf_Sym *sym;
1898 Elf_Rel *rel;
1899 Elf_Rela r;
1900 unsigned int r_sym;
1901 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001902
Sam Ravnborgff13f922008-01-23 19:54:27 +01001903 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001904 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1905
Sam Ravnborgff13f922008-01-23 19:54:27 +01001906 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001907 fromsec += strlen(".rel");
1908 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001909 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001910 return;
1911
1912 for (rel = start; rel < stop; rel++) {
1913 r.r_offset = TO_NATIVE(rel->r_offset);
1914#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001915 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001916 unsigned int r_typ;
1917 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1918 r_sym = TO_NATIVE(r_sym);
1919 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1920 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1921 } else {
1922 r.r_info = TO_NATIVE(rel->r_info);
1923 r_sym = ELF_R_SYM(r.r_info);
1924 }
1925#else
1926 r.r_info = TO_NATIVE(rel->r_info);
1927 r_sym = ELF_R_SYM(r.r_info);
1928#endif
1929 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001930 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001931 case EM_386:
1932 if (addend_386_rel(elf, sechdr, &r))
1933 continue;
1934 break;
1935 case EM_ARM:
1936 if (addend_arm_rel(elf, sechdr, &r))
1937 continue;
1938 break;
1939 case EM_MIPS:
1940 if (addend_mips_rel(elf, sechdr, &r))
1941 continue;
1942 break;
1943 }
1944 sym = elf->symtab_start + r_sym;
1945 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001946 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001947 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301948 if (is_second_extable_reloc(start, rel, fromsec))
1949 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001950 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001951 }
1952}
1953
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001954/**
1955 * A module includes a number of sections that are discarded
1956 * either when loaded or when used as built-in.
1957 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001958 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001959 * Likewise for modules used built-in the sections marked __exit
1960 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001961 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001962 * The check_sec_ref() function traverses all relocation records
1963 * to find all references to a section that reference a section that will
1964 * be discarded and warns about it.
1965 **/
1966static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001967 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001968{
1969 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001970 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001971
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001972 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001973 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001974 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001975 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001976 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001977 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001978 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001979 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001980 }
1981}
1982
Andi Kleen7d02b492014-02-08 09:01:12 +01001983static char *remove_dot(char *s)
1984{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301985 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001986
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301987 if (n && s[n]) {
1988 size_t m = strspn(s + n + 1, "0123456789");
1989 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001990 s[n] = 0;
Sami Tolvanen7ac204b2020-12-11 10:46:27 -08001991
1992 /* strip trailing .lto */
1993 if (strends(s, ".lto"))
1994 s[strlen(s) - 4] = '\0';
Andi Kleen7d02b492014-02-08 09:01:12 +01001995 }
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
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002012 {
2013 char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002015 /* strip trailing .o */
2016 tmp = NOFAIL(strdup(modname));
2017 tmp[strlen(tmp) - 2] = '\0';
Sami Tolvanen7ac204b2020-12-11 10:46:27 -08002018 /* strip trailing .lto */
2019 if (strends(tmp, ".lto"))
2020 tmp[strlen(tmp) - 4] = '\0';
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002021 mod = new_module(tmp);
2022 free(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002025 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002026 license = get_modinfo(&info, "license");
2027 if (!license)
Masahiro Yamada1d6cd3922020-12-01 19:34:16 +09002028 error("missing MODULE_LICENSE() in %s\n", modname);
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002029 while (license) {
2030 if (license_is_gpl_compatible(license))
2031 mod->gpl_compatible = 1;
2032 else {
2033 mod->gpl_compatible = 0;
2034 break;
2035 }
2036 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002037 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002038
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002039 namespace = get_modinfo(&info, "import_ns");
2040 while (namespace) {
2041 add_namespace(&mod->imported_namespaces, namespace);
2042 namespace = get_next_modinfo(&info, "import_ns",
2043 namespace);
2044 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002045 }
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002048 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002050 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 handle_moddevtable(mod, &info, sym, symname);
2052 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002053
Matthias Maennich69923202019-10-18 10:31:42 +01002054 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2055 symname = remove_dot(info.strtab + sym->st_name);
2056
Masahiro Yamada17436942019-11-15 02:42:24 +09002057 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002058 if (strstarts(symname, "__kstrtabns_"))
2059 sym_update_namespace(symname + strlen("__kstrtabns_"),
2060 namespace_from_kstrtabns(&info,
2061 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002062
2063 if (strstarts(symname, "__crc_"))
2064 handle_modversion(mod, &info, sym,
2065 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002066 }
2067
Denis Efremov15bfc232019-08-01 09:06:57 +03002068 // check for static EXPORT_SYMBOL_* functions && global vars
2069 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2070 unsigned char bind = ELF_ST_BIND(sym->st_info);
2071
2072 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2073 struct symbol *s =
2074 find_symbol(remove_dot(info.strtab +
2075 sym->st_name));
2076
2077 if (s)
2078 s->is_static = 0;
2079 }
2080 }
2081
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002082 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002084 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002085 version = get_modinfo(&info, "version");
2086 if (version || all_versions)
Masahiro Yamadae54dd932021-08-28 18:50:59 +09002087 get_src_version(mod->name, mod->srcversion,
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002088 sizeof(mod->srcversion) - 1);
2089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 parse_elf_finish(&info);
2092
Rusty Russell8c8ef422009-03-31 13:05:34 -06002093 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 * never passed as an argument to an exported function, so
2095 * the automatic versioning doesn't pick it up, but it's really
2096 * important anyhow */
2097 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002098 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Rusty Russell712f9b42013-04-04 17:37:38 +10302101static void read_symbols_from_files(const char *filename)
2102{
2103 FILE *in = stdin;
2104 char fname[PATH_MAX];
2105
2106 if (strcmp(filename, "-") != 0) {
2107 in = fopen(filename, "r");
2108 if (!in)
2109 fatal("Can't open filenames file %s: %m", filename);
2110 }
2111
2112 while (fgets(fname, PATH_MAX, in) != NULL) {
2113 if (strends(fname, "\n"))
2114 fname[strlen(fname)-1] = '\0';
2115 read_symbols(fname);
2116 }
2117
2118 if (in != stdin)
2119 fclose(in);
2120}
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122#define SZ 500
2123
2124/* We first write the generated file into memory using the
2125 * following helper, then compare to the file on disk and
2126 * only update the later if anything changed */
2127
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002128void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2129 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 char tmp[SZ];
2132 int len;
2133 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 va_start(ap, fmt);
2136 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002137 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 va_end(ap);
2139}
2140
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002141void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002144 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002145 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147 strncpy(buf->p + buf->pos, s, len);
2148 buf->pos += len;
2149}
2150
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002151static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2152{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002153 switch (exp) {
2154 case export_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002155 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002156 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002157 break;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002158 case export_plain:
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002159 case export_unknown:
2160 /* ignore */
2161 break;
2162 }
2163}
2164
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002165static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002166{
2167 struct symbol *s, *exp;
2168
2169 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002170 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002171 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002172 if (!exp || exp->module == mod) {
Masahiro Yamada4475dff2021-03-26 03:54:11 +09002173 if (!s->weak && nr_unresolved++ < MAX_UNRESOLVED_REPORTS)
Jessica Yu93c95e52020-03-06 17:02:05 +01002174 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2175 "\"%s\" [%s.ko] undefined!\n",
2176 s->name, mod->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002177 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002178 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002179 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002180 if (basename)
2181 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002182 else
2183 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002184
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002185 if (exp->namespace &&
2186 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002187 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2188 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2189 basename, exp->name, exp->namespace);
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002190 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002191 }
2192
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002193 if (!mod->gpl_compatible)
2194 check_for_gpl_usage(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002195 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002196}
2197
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002198static void check_modname_len(struct module *mod)
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002199{
2200 const char *mod_name;
2201
2202 mod_name = strrchr(mod->name, '/');
2203 if (mod_name == NULL)
2204 mod_name = mod->name;
2205 else
2206 mod_name++;
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002207 if (strlen(mod_name) >= MODULE_NAME_LEN)
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002208 error("module name is too long [%s.ko]\n", mod->name);
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002209}
2210
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002211/**
2212 * Header for the generated file
2213 **/
2214static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002217 /*
2218 * Include build-salt.h after module.h in order to
2219 * inherit the definitions.
2220 */
Leon Romanovsky51161bf2020-04-19 18:55:06 +03002221 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002222 buf_printf(b, "#include <linux/build-salt.h>\n");
Yonghong Song1fdd7432021-04-01 16:27:23 -07002223 buf_printf(b, "#include <linux/elfnote-lto.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 buf_printf(b, "#include <linux/vermagic.h>\n");
2225 buf_printf(b, "#include <linux/compiler.h>\n");
2226 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002227 buf_printf(b, "BUILD_SALT;\n");
Yonghong Song1fdd7432021-04-01 16:27:23 -07002228 buf_printf(b, "BUILD_LTO_INFO;\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002229 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002231 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302233 buf_printf(b, "__visible struct module __this_module\n");
Joe Perches33def842020-10-21 19:36:07 -07002234 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002235 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002237 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (mod->has_cleanup)
2239 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002240 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002242 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 buf_printf(b, "};\n");
2244}
2245
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002246static void add_intree_flag(struct buffer *b, int is_intree)
2247{
2248 if (is_intree)
2249 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2250}
2251
Andi Kleencaf75012018-01-25 15:50:28 -08002252/* Cannot check for assembler */
2253static void add_retpoline(struct buffer *b)
2254{
WANG Chaoe4f35892018-12-11 00:37:25 +08002255 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002256 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2257 buf_printf(b, "#endif\n");
2258}
2259
Trevor Keith5c725132009-09-22 16:43:38 -07002260static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002261{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002262 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002263 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2264}
2265
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002266/**
2267 * Record CRCs for unresolved symbols
2268 **/
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002269static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 struct symbol *s, *exp;
2272
2273 for (s = mod->unres; s; s = s->next) {
2274 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002275 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 s->module = exp->module;
2278 s->crc_valid = exp->crc_valid;
2279 s->crc = exp->crc;
2280 }
2281
2282 if (!modversions)
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002283 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 buf_printf(b, "\n");
2286 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Joe Perches33def842020-10-21 19:36:07 -07002287 buf_printf(b, "__used __section(\"__versions\") = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002290 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002293 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 s->name, mod->name);
2295 continue;
2296 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302297 if (strlen(s->name) >= MODULE_NAME_LEN) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002298 error("too long symbol \"%s\" [%s.ko]\n",
2299 s->name, mod->name);
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302300 break;
2301 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002302 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302303 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305
2306 buf_printf(b, "};\n");
2307}
2308
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002309static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 int first = 1;
2313
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002314 /* Clear ->seen flag of modules that own symbols needed by this. */
2315 for (s = mod->unres; s; s = s->next)
2316 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002317 s->module->seen = s->module->is_vmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002320 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002322 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!s->module)
2324 continue;
2325
2326 if (s->module->seen)
2327 continue;
2328
2329 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002330 p = strrchr(s->module->name, '/');
2331 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002332 p++;
2333 else
2334 p = s->module->name;
2335 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 first = 0;
2337 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002338 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002341static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 if (mod->srcversion[0]) {
2344 buf_printf(b, "\n");
2345 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2346 mod->srcversion);
2347 }
2348}
2349
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002350static void write_buf(struct buffer *b, const char *fname)
2351{
2352 FILE *file;
2353
2354 file = fopen(fname, "w");
2355 if (!file) {
2356 perror(fname);
2357 exit(1);
2358 }
2359 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2360 perror(fname);
2361 exit(1);
2362 }
2363 if (fclose(file) != 0) {
2364 perror(fname);
2365 exit(1);
2366 }
2367}
2368
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002369static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
2371 char *tmp;
2372 FILE *file;
2373 struct stat st;
2374
2375 file = fopen(fname, "r");
2376 if (!file)
2377 goto write;
2378
2379 if (fstat(fileno(file), &st) < 0)
2380 goto close_write;
2381
2382 if (st.st_size != b->pos)
2383 goto close_write;
2384
2385 tmp = NOFAIL(malloc(b->pos));
2386 if (fread(tmp, 1, b->pos, file) != b->pos)
2387 goto free_write;
2388
2389 if (memcmp(tmp, b->p, b->pos) != 0)
2390 goto free_write;
2391
2392 free(tmp);
2393 fclose(file);
2394 return;
2395
2396 free_write:
2397 free(tmp);
2398 close_write:
2399 fclose(file);
2400 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002401 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Ram Paibd5cbce2006-06-08 22:12:53 -07002404/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002405 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002406 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002407static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002409 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002411 buf = read_text_file(fname);
2412 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 /* No symbol versions, silently ignore */
2414 return;
2415
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002416 pos = buf;
2417
2418 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002419 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 unsigned int crc;
2421 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002422 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 if (!(symname = strchr(line, '\t')))
2425 goto fail;
2426 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002427 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 goto fail;
2429 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002430 if (!(export = strchr(modname, '\t')))
2431 goto fail;
2432 *export++ = '\0';
2433 if (!(namespace = strchr(export, '\t')))
2434 goto fail;
2435 *namespace++ = '\0';
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 crc = strtoul(line, &d, 16);
2438 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2439 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002440 mod = find_module(modname);
2441 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002442 mod = new_module(modname);
Masahiro Yamada52c34162020-06-01 14:57:05 +09002443 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002445 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002446 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002447 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002448 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002450 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return;
2452fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002453 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 fatal("parse error in symbol dump file\n");
2455}
2456
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002457static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 struct buffer buf = { };
2460 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002461 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 int n;
2463
2464 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2465 symbol = symbolhash[n];
2466 while (symbol) {
Masahiro Yamada69bc8d32021-03-26 03:54:09 +09002467 if (!symbol->module->from_dump) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002468 namespace = symbol->namespace;
2469 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2470 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002471 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002472 export_str(symbol->export),
2473 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 symbol = symbol->next;
2476 }
2477 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002478 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002479 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002482static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002483{
2484 struct module *mod;
2485 struct namespace_list *ns;
2486 struct buffer ns_deps_buf = {};
2487
2488 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002489
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002490 if (mod->from_dump || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002491 continue;
2492
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002493 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002494
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002495 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2496 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002497
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002498 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002499 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002500
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002501 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002502 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002503}
2504
Masahiro Yamada79247992020-06-01 14:57:07 +09002505struct dump_list {
2506 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002507 const char *file;
2508};
2509
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002510int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 struct module *mod;
2513 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002514 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302515 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 int opt;
Denis Efremov15bfc232019-08-01 09:06:57 +03002517 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002518 struct dump_list *dump_read_start = NULL;
2519 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002521 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002522 switch (opt) {
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002523 case 'e':
2524 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002525 break;
2526 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002527 *dump_read_iter =
2528 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2529 (*dump_read_iter)->file = optarg;
2530 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002531 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002532 case 'm':
2533 modversions = 1;
2534 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302535 case 'n':
2536 ignore_missing_files = 1;
2537 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002538 case 'o':
2539 dump_write = optarg;
2540 break;
2541 case 'a':
2542 all_versions = 1;
2543 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302544 case 'T':
2545 files_source = optarg;
2546 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002547 case 'w':
2548 warn_unresolved = 1;
2549 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302550 case 'E':
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002551 sec_mismatch_warn_only = false;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302552 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002553 case 'N':
2554 allow_missing_ns_imports = 1;
2555 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002556 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002557 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002558 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002559 default:
2560 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
2562 }
2563
Masahiro Yamada79247992020-06-01 14:57:07 +09002564 while (dump_read_start) {
2565 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002566
Masahiro Yamada79247992020-06-01 14:57:07 +09002567 read_dump(dump_read_start->file);
2568 tmp = dump_read_start->next;
2569 free(dump_read_start);
2570 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002573 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Rusty Russell712f9b42013-04-04 17:37:38 +10302576 if (files_source)
2577 read_symbols_from_files(files_source);
2578
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002579 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302580 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002581
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002582 if (mod->is_vmlinux || mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002583 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 buf.pos = 0;
2586
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002587 check_modname_len(mod);
2588 check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002591 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002592 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002593 add_staging_flag(&buf, mod->name);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002594 add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002595 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 add_moddevtable(&buf, mod);
2597 add_srcversion(&buf, mod);
2598
2599 sprintf(fname, "%s.mod.c", mod->name);
2600 write_if_changed(&buf, fname);
2601 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002602
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002603 if (missing_namespace_deps)
2604 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (dump_write)
2607 write_dump(dump_write);
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002608 if (sec_mismatch_count && !sec_mismatch_warn_only)
2609 error("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002610 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002611 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002612 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002613
Masahiro Yamada47346e92019-09-24 21:07:40 +09002614 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002615 if (s->is_static)
Quentin Perretb9ed8472020-12-01 16:52:22 +00002616 error("\"%s\" [%s] is a static %s\n",
2617 s->name, s->module->name,
2618 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002619 }
2620 }
2621
Masahiro Yamada4475dff2021-03-26 03:54:11 +09002622 if (nr_unresolved > MAX_UNRESOLVED_REPORTS)
2623 warn("suppressed %u unresolved symbol warnings because there were too many)\n",
2624 nr_unresolved - MAX_UNRESOLVED_REPORTS);
2625
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002626 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002628 return error_occurred ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}