blob: b667f531a6453a4fa5fa041f2be7e85583325e3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
Masahiro Yamada5370d4a2020-01-05 00:36:51 +090015#include <elf.h>
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080016#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070018#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103019#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103020#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093021#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020023#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093026static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093028static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
30static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010031/* If we are modposting external module set to 1 */
32static int external_module = 0;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070033/* Only warn about unresolved symbols */
34static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070035/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010036static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103037static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093038/* ignore missing files */
39static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010040/* If set to 1, only warn (instead of error) about missing ns imports */
41static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010042
Sam Ravnborgc96fca22006-07-01 11:44:23 +020043enum export {
44 export_plain, export_unused, export_gpl,
45 export_unused_gpl, export_gpl_future, export_unknown
46};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080048/* In kernel, this size is defined in linux/module.h;
49 * here we use Elf_Addr instead of long for covering cross-compile
50 */
51
52#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
53
Jessica Yu93c95e52020-03-06 17:02:05 +010054void __attribute__((format(printf, 2, 3)))
55modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
Jessica Yu93c95e52020-03-06 17:02:05 +010059 switch (loglevel) {
60 case LOG_WARN:
61 fprintf(stderr, "WARNING: ");
62 break;
63 case LOG_ERROR:
64 fprintf(stderr, "ERROR: ");
65 break;
66 case LOG_FATAL:
67 fprintf(stderr, "FATAL: ");
68 break;
69 default: /* invalid loglevel, ignore */
70 break;
71 }
72
73 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 va_start(arglist, fmt);
76 vfprintf(stderr, fmt, arglist);
77 va_end(arglist);
78
Jessica Yu93c95e52020-03-06 17:02:05 +010079 if (loglevel == LOG_FATAL)
80 exit(1);
Matthew Wilcox2a116652006-10-07 05:35:32 -060081}
82
Rusty Russelld4ef1c32013-04-04 17:37:32 +103083static inline bool strends(const char *str, const char *postfix)
84{
85 if (strlen(str) < strlen(postfix))
86 return false;
87
88 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
89}
90
Sam Ravnborg040fcc82006-01-28 22:15:55 +010091static int is_vmlinux(const char *modname)
92{
93 const char *myname;
94
Sam Ravnborgdf578e72008-01-11 19:17:15 +010095 myname = strrchr(modname, '/');
96 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010097 myname++;
98 else
99 myname = modname;
100
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200101 return (strcmp(myname, "vmlinux") == 0) ||
102 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void *do_nofail(void *ptr, const char *expr)
106{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100107 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100108 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return ptr;
111}
112
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900113char *read_text_file(const char *filename)
114{
115 struct stat st;
116 size_t nbytes;
117 int fd;
118 char *buf;
119
120 fd = open(filename, O_RDONLY);
121 if (fd < 0) {
122 perror(filename);
123 exit(1);
124 }
125
126 if (fstat(fd, &st) < 0) {
127 perror(filename);
128 exit(1);
129 }
130
131 buf = NOFAIL(malloc(st.st_size + 1));
132
133 nbytes = st.st_size;
134
135 while (nbytes) {
136 ssize_t bytes_read;
137
138 bytes_read = read(fd, buf, nbytes);
139 if (bytes_read < 0) {
140 perror(filename);
141 exit(1);
142 }
143
144 nbytes -= bytes_read;
145 }
146 buf[st.st_size] = '\0';
147
148 close(fd);
149
150 return buf;
151}
152
153char *get_line(char **stringp)
154{
155 /* do not return the unwanted extra line at EOF */
156 if (*stringp && **stringp == '\0')
157 return NULL;
158
159 return strsep(stringp, "\n");
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static struct module *modules;
164
Masahiro Yamada8b185742018-05-09 18:50:40 +0900165static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct module *mod;
168
169 for (mod = modules; mod; mod = mod->next)
170 if (strcmp(mod->name, modname) == 0)
171 break;
172 return mod;
173}
174
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030175static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030178 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 mod = NOFAIL(malloc(sizeof(*mod)));
181 memset(mod, 0, sizeof(*mod));
182 p = NOFAIL(strdup(modname));
183
184 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030185 if (strends(p, ".o")) {
186 p[strlen(p) - 2] = '\0';
187 mod->is_dot_o = 1;
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* add to list */
191 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200192 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 mod->next = modules;
194 modules = mod;
195
196 return mod;
197}
198
199/* A hash of all exported symbols,
200 * struct symbol is also used for lists of unresolved symbols */
201
202#define SYMBOL_HASH_SIZE 1024
203
204struct symbol {
205 struct symbol *next;
206 struct module *module;
207 unsigned int crc;
208 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900209 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300211 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700212 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500213 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
216static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
217
218/* This is based on the hash agorithm from gdbm, via tdb */
219static inline unsigned int tdb_hash(const char *name)
220{
221 unsigned value; /* Used to compute the hash value. */
222 unsigned i; /* Used to cycle through random values. */
223
224 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100225 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
227
228 return (1103515243 * value + 12345);
229}
230
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100231/**
232 * Allocate a new symbols for use in the hash of exported symbols or
233 * the list of unresolved symbols per module
234 **/
235static struct symbol *alloc_symbol(const char *name, unsigned int weak,
236 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
239
240 memset(s, 0, sizeof(*s));
241 strcpy(s->name, name);
242 s->weak = weak;
243 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300244 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return s;
246}
247
248/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700249static struct symbol *new_symbol(const char *name, struct module *module,
250 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900255 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
256
257 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100260static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct symbol *s;
263
264 /* For our purposes, .foo matches foo. PPC64 needs this. */
265 if (name[0] == '.')
266 name++;
267
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100268 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (strcmp(s->name, name) == 0)
270 return s;
271 }
272 return NULL;
273}
274
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100275static bool contains_namespace(struct namespace_list *list,
276 const char *namespace)
277{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900278 for (; list; list = list->next)
279 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100280 return true;
281
282 return false;
283}
284
285static void add_namespace(struct namespace_list **list, const char *namespace)
286{
287 struct namespace_list *ns_entry;
288
289 if (!contains_namespace(*list, namespace)) {
290 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
291 strlen(namespace) + 1));
292 strcpy(ns_entry->namespace, namespace);
293 ns_entry->next = *list;
294 *list = ns_entry;
295 }
296}
297
298static bool module_imports_namespace(struct module *module,
299 const char *namespace)
300{
301 return contains_namespace(module->imported_namespaces, namespace);
302}
303
Mathias Krause7a3ee752014-08-27 20:28:53 +0930304static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700305 const char *str;
306 enum export export;
307} export_list[] = {
308 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200309 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700310 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200311 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700312 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
313 { .str = "(unknown)", .export = export_unknown },
314};
315
316
317static const char *export_str(enum export ex)
318{
319 return export_list[ex].str;
320}
321
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100322static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700323{
324 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100325
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200326 if (!s)
327 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700328 for (i = 0; export_list[i].export != export_unknown; i++) {
329 if (strcmp(export_list[i].str, s) == 0)
330 return export_list[i].export;
331 }
332 return export_unknown;
333}
334
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900335static void *sym_get_data_by_offset(const struct elf_info *info,
336 unsigned int secindex, unsigned long offset)
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900337{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800338 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900339
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900340 if (info->hdr->e_type != ET_REL)
341 offset -= sechdr->sh_addr;
342
343 return (void *)info->hdr + sechdr->sh_offset + offset;
344}
345
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900346static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
347{
348 return sym_get_data_by_offset(info, get_secindex(info, sym),
349 sym->st_value);
350}
351
Masahiro Yamada565587d2020-05-25 14:47:05 +0900352static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
353{
354 return sym_get_data_by_offset(info, info->secindex_strings,
355 sechdr->sh_name);
356}
357
358static const char *sec_name(const struct elf_info *info, int secindex)
359{
360 return sech_name(info, &info->sechdrs[secindex]);
361}
362
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200363#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
364
365static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
366{
367 const char *secname = sec_name(elf, sec);
368
369 if (strstarts(secname, "___ksymtab+"))
370 return export_plain;
371 else if (strstarts(secname, "___ksymtab_unused+"))
372 return export_unused;
373 else if (strstarts(secname, "___ksymtab_gpl+"))
374 return export_gpl;
375 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
376 return export_unused_gpl;
377 else if (strstarts(secname, "___ksymtab_gpl_future+"))
378 return export_gpl_future;
379 else
380 return export_unknown;
381}
382
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200383static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700384{
385 if (sec == elf->export_sec)
386 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200387 else if (sec == elf->export_unused_sec)
388 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700389 else if (sec == elf->export_gpl_sec)
390 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200391 else if (sec == elf->export_unused_gpl_sec)
392 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700393 else if (sec == elf->export_gpl_future_sec)
394 return export_gpl_future;
395 else
396 return export_unknown;
397}
398
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900399static const char *namespace_from_kstrtabns(const struct elf_info *info,
400 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100401{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900402 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100403 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100404}
405
Matthias Maennicha2b11182019-10-18 10:31:40 +0100406static void sym_update_namespace(const char *symname, const char *namespace)
407{
408 struct symbol *s = find_symbol(symname);
409
410 /*
411 * That symbol should have been created earlier and thus this is
412 * actually an assertion.
413 */
414 if (!s) {
415 merror("Could not update namespace(%s) for symbol %s\n",
416 namespace, symname);
417 return;
418 }
419
420 free(s->namespace);
421 s->namespace =
422 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
423}
424
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100425/**
426 * Add an exported symbol - it may have already been added without a
427 * CRC, in this case just update the CRC
428 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100429static struct symbol *sym_add_exported(const char *name, struct module *mod,
430 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct symbol *s = find_symbol(name);
433
434 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700435 s = new_symbol(name, mod, export);
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900436 } else if (!external_module || is_vmlinux(s->module->name) ||
437 s->module == mod) {
438 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
439 mod->name, name, s->module->name,
440 is_vmlinux(s->module->name) ? "" : ".ko");
441 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900443
444 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700445 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100446 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Masahiro Yamada17436942019-11-15 02:42:24 +0900449static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100450{
451 struct symbol *s = find_symbol(name);
452
Masahiro Yamada17436942019-11-15 02:42:24 +0900453 /*
454 * Ignore stand-alone __crc_*, which might be auto-generated symbols
455 * such as __*_veneer in ARM ELF.
456 */
457 if (!s)
458 return;
459
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100460 s->crc = crc;
461 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Masahiro Yamada75893572020-06-01 14:57:21 +0900464static void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930467 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int fd;
469
470 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930471 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930473 if (fstat(fd, &st))
474 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 *size = st.st_size;
477 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930479failed:
480 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (map == MAP_FAILED)
482 return NULL;
483 return map;
484}
485
Masahiro Yamada75893572020-06-01 14:57:21 +0900486static void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 munmap(file, size);
489}
490
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100491static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100494 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 Elf_Shdr *sechdrs;
496 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200497 const char *secstrings;
498 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 hdr = grab_file(filename, &info->size);
501 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930502 if (ignore_missing_files) {
503 fprintf(stderr, "%s: %s (ignored)\n", filename,
504 strerror(errno));
505 return 0;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200508 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100511 if (info->size < sizeof(*hdr)) {
512 /* file too small, assume this is an empty .o file */
513 return 0;
514 }
515 /* Is this a valid ELF file? */
516 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
517 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
518 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
519 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
520 /* Not an ELF file - silently ignore it */
521 return 0;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200524 hdr->e_type = TO_NATIVE(hdr->e_type);
525 hdr->e_machine = TO_NATIVE(hdr->e_machine);
526 hdr->e_version = TO_NATIVE(hdr->e_version);
527 hdr->e_entry = TO_NATIVE(hdr->e_entry);
528 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
529 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
530 hdr->e_flags = TO_NATIVE(hdr->e_flags);
531 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
532 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
533 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
534 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
535 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
536 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 sechdrs = (void *)hdr + hdr->e_shoff;
538 info->sechdrs = sechdrs;
539
Petr Stetiara83710e2007-08-27 12:15:07 +0200540 /* Check if file offset is correct */
541 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100542 fatal("section header offset=%lu in file '%s' is bigger than "
543 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
544 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200545 return 0;
546 }
547
Anders Kaseorg68457562011-05-19 16:55:27 -0600548 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200549 /*
550 * There are more than 64k sections,
551 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200552 */
553 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
554 }
555 else {
556 info->num_sections = hdr->e_shnum;
557 }
558 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600559 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200560 }
561 else {
562 info->secindex_strings = hdr->e_shstrndx;
563 }
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200566 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200567 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
568 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
569 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
570 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
571 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
572 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
573 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
574 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
575 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
576 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200579 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
580 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700581 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900582 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Tejun Heo56fc82c2009-02-06 00:48:02 +0900584 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100585 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
586 "sizeof(*hrd)=%zu\n", filename,
587 (unsigned long)sechdrs[i].sh_offset,
588 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100589 return 0;
590 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700591 secname = secstrings + sechdrs[i].sh_name;
592 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900593 if (nobits)
594 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
596 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700597 } else if (strcmp(secname, "__ksymtab") == 0)
598 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200599 else if (strcmp(secname, "__ksymtab_unused") == 0)
600 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700601 else if (strcmp(secname, "__ksymtab_gpl") == 0)
602 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200603 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
604 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700605 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
606 info->export_gpl_future_sec = i;
607
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200608 if (sechdrs[i].sh_type == SHT_SYMTAB) {
609 unsigned int sh_link_idx;
610 symtab_idx = i;
611 info->symtab_start = (void *)hdr +
612 sechdrs[i].sh_offset;
613 info->symtab_stop = (void *)hdr +
614 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600615 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200616 info->strtab = (void *)hdr +
617 sechdrs[sh_link_idx].sh_offset;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200620 /* 32bit section no. table? ("more than 64k sections") */
621 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
622 symtab_shndx_idx = i;
623 info->symtab_shndx_start = (void *)hdr +
624 sechdrs[i].sh_offset;
625 info->symtab_shndx_stop = (void *)hdr +
626 sechdrs[i].sh_offset + sechdrs[i].sh_size;
627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100629 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100630 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Fix endianness in symbols */
633 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
634 sym->st_shndx = TO_NATIVE(sym->st_shndx);
635 sym->st_name = TO_NATIVE(sym->st_name);
636 sym->st_value = TO_NATIVE(sym->st_value);
637 sym->st_size = TO_NATIVE(sym->st_size);
638 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200639
640 if (symtab_shndx_idx != ~0U) {
641 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600642 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200643 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600644 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200645 symtab_idx);
646 /* Fix endianness */
647 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
648 p++)
649 *p = TO_NATIVE(*p);
650 }
651
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100652 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100655static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 release_file(info->hdr, info->size);
658}
659
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200660static int ignore_undef_symbol(struct elf_info *info, const char *symname)
661{
662 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900663 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200664 return 1;
665 /* ignore global offset table */
666 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
667 return 1;
668 if (info->hdr->e_machine == EM_PPC)
669 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900670 if (strstarts(symname, "_restgpr_") ||
671 strstarts(symname, "_savegpr_") ||
672 strstarts(symname, "_rest32gpr_") ||
673 strstarts(symname, "_save32gpr_") ||
674 strstarts(symname, "_restvr_") ||
675 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200676 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000677 if (info->hdr->e_machine == EM_PPC64)
678 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900679 if (strstarts(symname, "_restgpr0_") ||
680 strstarts(symname, "_savegpr0_") ||
681 strstarts(symname, "_restvr_") ||
682 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100683 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000684 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200685 /* Do not ignore this symbol */
686 return 0;
687}
688
Masahiro Yamada17436942019-11-15 02:42:24 +0900689static void handle_modversion(const struct module *mod,
690 const struct elf_info *info,
691 const Elf_Sym *sym, const char *symname)
692{
693 unsigned int crc;
694
695 if (sym->st_shndx == SHN_UNDEF) {
696 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
697 symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
698 return;
699 }
700
701 if (sym->st_shndx == SHN_ABS) {
702 crc = sym->st_value;
703 } else {
704 unsigned int *crcp;
705
706 /* symbol points to the CRC in the ELF object */
707 crcp = sym_get_data(info, sym);
708 crc = TO_NATIVE(*crcp);
709 }
710 sym_set_crc(symname, crc);
711}
712
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900713static void handle_symbol(struct module *mod, struct elf_info *info,
714 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200716 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900717 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200718
Frank Rowand258f7422012-04-09 17:59:03 -0700719 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900720 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200721 export = export_from_secname(info, get_secindex(info, sym));
722 else
723 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 switch (sym->st_shndx) {
726 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900727 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100728 /* Should warn here, but modpost runs before the linker */
729 } else
730 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 case SHN_UNDEF:
733 /* undefined symbol */
734 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
735 ELF_ST_BIND(sym->st_info) != STB_WEAK)
736 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200737 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (info->hdr->e_machine == EM_SPARC ||
740 info->hdr->e_machine == EM_SPARCV9) {
741 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700742 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100744 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700745 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100746 munged[0] = '_';
747 munged[1] = toupper(munged[1]);
748 symname = munged;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100751
Rusty Russellb92021b2013-03-15 15:04:17 +1030752 mod->unres = alloc_symbol(symname,
753 ELF_ST_BIND(sym->st_info) == STB_WEAK,
754 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756 default:
757 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900758 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100759 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100760 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900762 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900764 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 mod->has_cleanup = 1;
766 break;
767 }
768}
769
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100770/**
771 * Parse tag=value strings from .modinfo section
772 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static char *next_string(char *string, unsigned long *secsize)
774{
775 /* Skip non-zero chars */
776 while (string[0]) {
777 string++;
778 if ((*secsize)-- <= 1)
779 return NULL;
780 }
781
782 /* Skip any zero padding. */
783 while (!string[0]) {
784 string++;
785 if ((*secsize)-- <= 1)
786 return NULL;
787 }
788 return string;
789}
790
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900791static char *get_next_modinfo(struct elf_info *info, const char *tag,
792 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 char *p;
795 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900796 char *modinfo = info->modinfo;
797 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900799 if (prev) {
800 size -= prev - modinfo;
801 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 for (p = modinfo; p; p = next_string(p, &size)) {
805 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
806 return p + taglen + 1;
807 }
808 return NULL;
809}
810
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900811static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200812
813{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900814 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200815}
816
Sam Ravnborg93684d32006-02-19 11:53:35 +0100817/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100818 * Test if string s ends in string sub
819 * return 0 if match
820 **/
821static int strrcmp(const char *s, const char *sub)
822{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100823 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100824
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100825 if (!s || !sub)
826 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100827
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100828 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100829 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100830
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100831 if ((slen == 0) || (sublen == 0))
832 return 1;
833
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100834 if (sublen > slen)
835 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100836
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100837 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100838}
839
Sam Ravnborgff13f922008-01-23 19:54:27 +0100840static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
841{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100842 if (sym)
843 return elf->strtab + sym->st_name;
844 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100845 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100846}
847
Sam Ravnborg10668222008-01-13 22:21:31 +0100848/* The pattern is an array of simple patterns.
849 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100850 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100851 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930852 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100853 */
Trevor Keith5c725132009-09-22 16:43:38 -0700854static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100855{
856 const char *p;
857 while (*pat) {
858 p = *pat++;
859 const char *endp = p + strlen(p) - 1;
860
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930861 /* "*foo*" */
862 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300863 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
864 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930865
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930866 free(bare);
867 if (here != NULL)
868 return 1;
869 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100870 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930871 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100872 if (strrcmp(sym, p + 1) == 0)
873 return 1;
874 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100875 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100876 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100877 if (strncmp(sym, p, strlen(p) - 1) == 0)
878 return 1;
879 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100880 /* no wildcards */
881 else {
882 if (strcmp(p, sym) == 0)
883 return 1;
884 }
885 }
886 /* no match */
887 return 0;
888}
889
Sam Ravnborg10668222008-01-13 22:21:31 +0100890/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930891static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200892{
893 ".comment*",
894 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930895 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800896 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000897 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200898 ".mdebug*", /* alpha, score, mips etc. */
899 ".pdr", /* alpha, score, mips etc. */
900 ".stab*",
901 ".note*",
902 ".got*",
903 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400904 ".xt.prop", /* xtensa */
905 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030906 ".arcextmap*", /* arc */
907 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030908 ".cmem*", /* EZchip */
909 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100910 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600911 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200912 NULL
913};
Sam Ravnborg10668222008-01-13 22:21:31 +0100914
Sam Ravnborge241a632008-01-28 20:13:13 +0100915/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400916 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100917 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400918 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100919 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400920static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900921 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100922{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400923 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100924
Anders Kaseorgb614a692009-04-23 16:49:33 -0400925 if (sechdr->sh_type == SHT_PROGBITS &&
926 !(sechdr->sh_flags & SHF_ALLOC) &&
927 !match(sec, section_white_list)) {
928 warn("%s (%s): unexpected non-allocatable section.\n"
929 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
930 "Note that for example <linux/init.h> contains\n"
931 "section definitions for use in .S files.\n\n",
932 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100933 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100934}
935
936
937
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100938#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930939 ".init.setup", ".init.rodata", ".meminit.rodata", \
940 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100941#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930942 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100943
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100944#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930945 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100946#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930947 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100948
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200949#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930950 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
951 ".pci_fixup_enable", ".pci_fixup_resume", \
952 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200953
Paul Gortmakere24f6622013-06-19 19:30:48 -0400954#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
955#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100956
957#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
958#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100959
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930960#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930961#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700962 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930963#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400964 ".fixup", ".entry.text", ".exception.text", ".text.*", \
965 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100966
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000967#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000968#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100969
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000970#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000971#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100972
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930973#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
974 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
975
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100976/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930977static const char *const init_data_sections[] =
978 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100979
980/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930981static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100982
983/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930984static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100985 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100986
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930987/* all text sections */
988static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
989
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100990/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930991static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100992
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100993
994/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100995#define DEFAULT_SYMBOL_WHITE_LIST \
996 "*driver", \
997 "*_template", /* scsi uses *_template a lot */ \
998 "*_timer", /* arm uses ops structures named _timer a lot */ \
999 "*_sht", /* scsi also used *_sht to some extent */ \
1000 "*_ops", \
1001 "*_probe", \
1002 "*_probe_one", \
1003 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001004
Mathias Krause7a3ee752014-08-27 20:28:53 +09301005static const char *const head_sections[] = { ".head.text*", NULL };
1006static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001007 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301008static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001009
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001010enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001011 TEXT_TO_ANY_INIT,
1012 DATA_TO_ANY_INIT,
1013 TEXT_TO_ANY_EXIT,
1014 DATA_TO_ANY_EXIT,
1015 XXXINIT_TO_SOME_INIT,
1016 XXXEXIT_TO_SOME_EXIT,
1017 ANY_INIT_TO_ANY_EXIT,
1018 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001019 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301020 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001021};
1022
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301023/**
1024 * Describe how to match sections on different criterias:
1025 *
1026 * @fromsec: Array of sections to be matched.
1027 *
1028 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1029 * this array is forbidden (black-list). Can be empty.
1030 *
1031 * @good_tosec: Relocations applied to a section in @fromsec must be
1032 * targetting sections in this array (white-list). Can be empty.
1033 *
1034 * @mismatch: Type of mismatch.
1035 *
1036 * @symbol_white_list: Do not match a relocation to a symbol in this list
1037 * even if it is targetting a section in @bad_to_sec.
1038 *
1039 * @handler: Specific handler to call when a match is found. If NULL,
1040 * default_mismatch_handler() will be called.
1041 *
1042 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001043struct sectioncheck {
1044 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301045 const char *bad_tosec[20];
1046 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001047 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001048 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301049 void (*handler)(const char *modname, struct elf_info *elf,
1050 const struct sectioncheck* const mismatch,
1051 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1052
Sam Ravnborg10668222008-01-13 22:21:31 +01001053};
1054
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301055static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1056 const struct sectioncheck* const mismatch,
1057 Elf_Rela *r, Elf_Sym *sym,
1058 const char *fromsec);
1059
Mathias Krause7a3ee752014-08-27 20:28:53 +09301060static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001061/* Do not reference init/exit code/data from
1062 * normal code and data
1063 */
1064{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001065 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301066 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001067 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001068 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001069},
1070{
1071 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301072 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001073 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001074 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001075},
1076{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001077 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301078 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001079 .mismatch = DATA_TO_ANY_INIT,
1080 .symbol_white_list = {
1081 "*_template", "*_timer", "*_sht", "*_ops",
1082 "*_probe", "*_probe_one", "*_console", NULL
1083 },
1084},
1085{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001086 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301087 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001088 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001089 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001090},
1091{
1092 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301093 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001094 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001095 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001096},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001097/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001098{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001099 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301100 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001101 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001102 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001103},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001104/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001105{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001106 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301107 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001108 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001109 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001110},
1111/* Do not use exit code/data from init code */
1112{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001113 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301114 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001115 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001116 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001117},
1118/* Do not use init code/data from exit code */
1119{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001120 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301121 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001122 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001123 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001124},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001125{
1126 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301127 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001128 .mismatch = ANY_INIT_TO_ANY_EXIT,
1129 .symbol_white_list = { NULL },
1130},
Sam Ravnborg10668222008-01-13 22:21:31 +01001131/* Do not export init/exit functions or data */
1132{
1133 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301134 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001135 .mismatch = EXPORT_TO_INIT_EXIT,
1136 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301137},
1138{
1139 .fromsec = { "__ex_table", NULL },
1140 /* If you're adding any new black-listed sections in here, consider
1141 * adding a special 'printer' for them in scripts/check_extable.
1142 */
1143 .bad_tosec = { ".altinstr_replacement", NULL },
1144 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1145 .mismatch = EXTABLE_TO_NON_TEXT,
1146 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001147}
1148};
1149
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001150static const struct sectioncheck *section_mismatch(
1151 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001152{
1153 int i;
1154 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1155 const struct sectioncheck *check = &sectioncheck[0];
1156
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301157 /*
1158 * The target section could be the SHT_NUL section when we're
1159 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001160 * doesn't make much sense and causes build failures on parisc
1161 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301162 */
1163 if (*tosec == '\0')
1164 return NULL;
1165
Sam Ravnborg10668222008-01-13 22:21:31 +01001166 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301167 if (match(fromsec, check->fromsec)) {
1168 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1169 return check;
1170 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1171 return check;
1172 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001173 check++;
1174 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001175 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001176}
1177
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001178/**
1179 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001180 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001181 * Pattern 1:
1182 * If a module parameter is declared __initdata and permissions=0
1183 * then this is legal despite the warning generated.
1184 * We cannot see value of permissions here, so just ignore
1185 * this pattern.
1186 * The pattern is identified by:
1187 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001188 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001189 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001190 *
Rusty Russell6a841522010-08-11 23:04:16 -06001191 * Pattern 1a:
1192 * module_param_call() ops can refer to __init set function if permissions=0
1193 * The pattern is identified by:
1194 * tosec = .init.text
1195 * fromsec = .data*
1196 * atsym = __param_ops_*
1197 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001198 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001199 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001200 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001201 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001202 * tosec = init or exit section
1203 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001204 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1205 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001206 *
1207 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001208 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001209 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001210 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001211 * Some symbols belong to init section but still it is ok to reference
1212 * these from non-init sections as these symbols don't have any memory
1213 * allocated for them and symbol address and value are same. So even
1214 * if init section is freed, its ok to reference those symbols.
1215 * For ex. symbols marking the init section boundaries.
1216 * This pattern is identified by
1217 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001218 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301219 * Pattern 5:
1220 * GCC may optimize static inlines when fed constant arg(s) resulting
1221 * in functions like cpumask_empty() -- generating an associated symbol
1222 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1223 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1224 * meaningless section warning. May need to add isra symbols too...
1225 * This pattern is identified by
1226 * tosec = init section
1227 * fromsec = text section
1228 * refsymname = *.constprop.*
1229 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001230 * Pattern 6:
1231 * Hide section mismatch warnings for ELF local symbols. The goal
1232 * is to eliminate false positive modpost warnings caused by
1233 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1234 * Autogenerated symbol names bypass modpost's "Pattern 2"
1235 * whitelisting, which relies on pattern-matching against symbol
1236 * names to work. (One situation where gcc can autogenerate ELF
1237 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001238 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001239static int secref_whitelist(const struct sectioncheck *mismatch,
1240 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001241 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001242{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001243 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001244 if (match(tosec, init_data_sections) &&
1245 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001246 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001247 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001248
Rusty Russell6a841522010-08-11 23:04:16 -06001249 /* Check for pattern 1a */
1250 if (strcmp(tosec, ".init.text") == 0 &&
1251 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001252 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001253 return 0;
1254
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001255 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001256 if (match(tosec, init_exit_sections) &&
1257 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001258 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001259 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001260
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001261 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001262 if (match(fromsec, head_sections) &&
1263 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001264 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001265
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001266 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001267 if (match(tosym, linker_symbols))
1268 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001269
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301270 /* Check for pattern 5 */
1271 if (match(fromsec, text_sections) &&
1272 match(tosec, init_sections) &&
1273 match(fromsym, optim_symbols))
1274 return 0;
1275
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001276 /* Check for pattern 6 */
1277 if (strstarts(fromsym, ".L"))
1278 return 0;
1279
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001280 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001281}
1282
Sami Tolvanen5818c682018-10-23 15:15:35 -07001283static inline int is_arm_mapping_symbol(const char *str)
1284{
1285 return str[0] == '$' && strchr("axtd", str[1])
1286 && (str[2] == '\0' || str[2] == '.');
1287}
1288
1289/*
1290 * If there's no name there, ignore it; likewise, ignore it if it's
1291 * one of the magic symbols emitted used by current ARM tools.
1292 *
1293 * Otherwise if find_symbols_between() returns those symbols, they'll
1294 * fail the whitelist tests and cause lots of false alarms ... fixable
1295 * only by merging __exit and __init sections into __text, bloating
1296 * the kernel (which is especially evil on embedded platforms).
1297 */
1298static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1299{
1300 const char *name = elf->strtab + sym->st_name;
1301
1302 if (!name || !strlen(name))
1303 return 0;
1304 return !is_arm_mapping_symbol(name);
1305}
1306
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001307/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001308 * Find symbol based on relocation record info.
1309 * In some cases the symbol supplied is a valid symbol so
1310 * return refsym. If st_name != 0 we assume this is a valid symbol.
1311 * In other cases the symbol needs to be looked up in the symbol table
1312 * based on section and address.
1313 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001314static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001315 Elf_Sym *relsym)
1316{
1317 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001318 Elf_Sym *near = NULL;
1319 Elf64_Sword distance = 20;
1320 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001321 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001322
1323 if (relsym->st_name != 0)
1324 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001325
1326 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001327 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001328 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001329 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001330 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1331 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001332 if (!is_valid_name(elf, sym))
1333 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001334 if (sym->st_value == addr)
1335 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001336 /* Find a symbol nearby - addr are maybe negative */
1337 d = sym->st_value - addr;
1338 if (d < 0)
1339 d = addr - sym->st_value;
1340 if (d < distance) {
1341 distance = d;
1342 near = sym;
1343 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001344 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001345 /* We need a close match */
1346 if (distance < 20)
1347 return near;
1348 else
1349 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001350}
1351
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001352/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001353 * Find symbols before or equal addr and after addr - in the section sec.
1354 * If we find two symbols with equal offset prefer one with a valid name.
1355 * The ELF format may have a better way to detect what type of symbol
1356 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001357 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001358static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1359 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001360{
1361 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001362 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001363 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001364
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001365 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1366 const char *symsec;
1367
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001368 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001369 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001370 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001371 if (strcmp(symsec, sec) != 0)
1372 continue;
David Brownellda68d612007-02-20 13:58:16 -08001373 if (!is_valid_name(elf, sym))
1374 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001375 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001376 if ((addr - sym->st_value) < distance) {
1377 distance = addr - sym->st_value;
1378 near = sym;
1379 } else if ((addr - sym->st_value) == distance) {
1380 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001381 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001382 }
1383 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001384 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001385}
1386
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001387/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001388 * Convert a section name to the function/data attribute
1389 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001390 * .memexitconst => __memconst
1391 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001392 *
1393 * The memory of returned value has been allocated on a heap. The user of this
1394 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001395*/
1396static char *sec2annotation(const char *s)
1397{
1398 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001399 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001400 char *r = p;
1401
1402 *p++ = '_';
1403 *p++ = '_';
1404 if (*s == '.')
1405 s++;
1406 while (*s && *s != '.')
1407 *p++ = *s++;
1408 *p = '\0';
1409 if (*s == '.')
1410 s++;
1411 if (strstr(s, "rodata") != NULL)
1412 strcat(p, "const ");
1413 else if (strstr(s, "data") != NULL)
1414 strcat(p, "data ");
1415 else
1416 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001417 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001418 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001419 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001420 }
1421}
1422
1423static int is_function(Elf_Sym *sym)
1424{
1425 if (sym)
1426 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1427 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001428 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001429}
1430
Randy Dunlap00759c02011-03-15 14:13:47 -07001431static void print_section_list(const char * const list[20])
1432{
1433 const char *const *s = list;
1434
1435 while (*s) {
1436 fprintf(stderr, "%s", *s);
1437 s++;
1438 if (*s)
1439 fprintf(stderr, ", ");
1440 }
1441 fprintf(stderr, "\n");
1442}
1443
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301444static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1445{
1446 switch (is_func) {
1447 case 0: *name = "variable"; *name_p = ""; break;
1448 case 1: *name = "function"; *name_p = "()"; break;
1449 default: *name = "(unknown reference)"; *name_p = ""; break;
1450 }
1451}
1452
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001453/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001454 * Print a warning about a section mismatch.
1455 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001456 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001457 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001458static void report_sec_mismatch(const char *modname,
1459 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001460 const char *fromsec,
1461 unsigned long long fromaddr,
1462 const char *fromsym,
1463 int from_is_func,
1464 const char *tosec, const char *tosym,
1465 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001466{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001467 const char *from, *from_p;
1468 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001469 char *prl_from;
1470 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001471
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001472 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001473
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301474 get_pretty_name(from_is_func, &from, &from_p);
1475 get_pretty_name(to_is_func, &to, &to_p);
1476
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001477 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1478 "to the %s %s:%s%s\n",
1479 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1480 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001481
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001482 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001483 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001484 prl_from = sec2annotation(fromsec);
1485 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001486 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001487 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001488 "the %s %s%s%s.\n"
1489 "This is often because %s lacks a %s\n"
1490 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001491 prl_from, fromsym,
1492 to, prl_to, tosym, to_p,
1493 fromsym, prl_to, tosym);
1494 free(prl_from);
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_INIT: {
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"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001503 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001504 "or 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;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001509 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001510 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001511 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001512 fprintf(stderr,
1513 "The function %s() references a %s in an exit section.\n"
1514 "Often the %s %s%s has valid usage outside the exit section\n"
1515 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001516 fromsym, to, to, tosym, to_p, prl_to, tosym);
1517 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001518 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001519 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001520 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001521 fprintf(stderr,
1522 "The variable %s references\n"
1523 "the %s %s%s%s\n"
1524 "If the reference is valid then annotate the\n"
1525 "variable with __exit* (see linux/init.h) or "
1526 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001527 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001528 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001529 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001530 break;
1531 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001532 case XXXINIT_TO_SOME_INIT:
1533 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001534 prl_from = sec2annotation(fromsec);
1535 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001536 fprintf(stderr,
1537 "The %s %s%s%s references\n"
1538 "a %s %s%s%s.\n"
1539 "If %s is only used by %s then\n"
1540 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001541 from, prl_from, fromsym, from_p,
1542 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001543 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001544 free(prl_from);
1545 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001546 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001547 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001548 prl_from = sec2annotation(fromsec);
1549 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001550 fprintf(stderr,
1551 "The %s %s%s%s references\n"
1552 "a %s %s%s%s.\n"
1553 "This is often seen when error handling "
1554 "in the init function\n"
1555 "uses functionality in the exit path.\n"
1556 "The fix is often to remove the %sannotation of\n"
1557 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001558 from, prl_from, fromsym, from_p,
1559 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001560 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001561 free(prl_from);
1562 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001563 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001564 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001565 prl_from = sec2annotation(fromsec);
1566 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001567 fprintf(stderr,
1568 "The %s %s%s%s references\n"
1569 "a %s %s%s%s.\n"
1570 "This is often seen when error handling "
1571 "in the exit function\n"
1572 "uses functionality in the init path.\n"
1573 "The fix is often to remove the %sannotation of\n"
1574 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001575 from, prl_from, fromsym, from_p,
1576 to, prl_to, tosym, to_p,
1577 prl_to, tosym, to_p);
1578 free(prl_from);
1579 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001580 break;
1581 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001582 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001583 fprintf(stderr,
1584 "The symbol %s is exported and annotated %s\n"
1585 "Fix this by removing the %sannotation of %s "
1586 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001587 tosym, prl_to, prl_to, tosym);
1588 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001589 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301590 case EXTABLE_TO_NON_TEXT:
1591 fatal("There's a special handler for this mismatch type, "
1592 "we should never get here.");
1593 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001594 }
1595 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001596}
1597
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301598static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1599 const struct sectioncheck* const mismatch,
1600 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1601{
1602 const char *tosec;
1603 Elf_Sym *to;
1604 Elf_Sym *from;
1605 const char *tosym;
1606 const char *fromsym;
1607
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301608 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1609 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301610
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001611 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301612 return;
1613
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301614 tosec = sec_name(elf, get_secindex(elf, sym));
1615 to = find_elf_symbol(elf, r->r_addend, sym);
1616 tosym = sym_name(elf, to);
1617
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301618 /* check whitelist - we may ignore it */
1619 if (secref_whitelist(mismatch,
1620 fromsec, fromsym, tosec, tosym)) {
1621 report_sec_mismatch(modname, mismatch,
1622 fromsec, r->r_offset, fromsym,
1623 is_function(from), tosec, tosym,
1624 is_function(to));
1625 }
1626}
1627
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301628static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1629{
1630 if (section_index > elf->num_sections)
1631 fatal("section_index is outside elf->num_sections!\n");
1632
1633 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1634}
1635
1636/*
1637 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1638 * to know the sizeof(struct exception_table_entry) for the target architecture.
1639 */
1640static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301641static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301642{
1643 /*
1644 * If we're currently checking the second relocation within __ex_table,
1645 * that relocation offset tells us the offsetof(struct
1646 * exception_table_entry, fixup) which is equal to sizeof(struct
1647 * exception_table_entry) divided by two. We use that to our advantage
1648 * since there's no portable way to get that size as every architecture
1649 * seems to go with different sized types. Not pretty but better than
1650 * hard-coding the size for every architecture..
1651 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301652 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301653 extable_entry_size = r->r_offset * 2;
1654}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301655
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301656static inline bool is_extable_fault_address(Elf_Rela *r)
1657{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301658 /*
1659 * extable_entry_size is only discovered after we've handled the
1660 * _second_ relocation in __ex_table, so only abort when we're not
1661 * handling the first reloc and extable_entry_size is zero.
1662 */
1663 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301664 fatal("extable_entry size hasn't been discovered!\n");
1665
1666 return ((r->r_offset == 0) ||
1667 (r->r_offset % extable_entry_size == 0));
1668}
1669
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301670#define is_second_extable_reloc(Start, Cur, Sec) \
1671 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1672
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301673static void report_extable_warnings(const char* modname, struct elf_info* elf,
1674 const struct sectioncheck* const mismatch,
1675 Elf_Rela* r, Elf_Sym* sym,
1676 const char* fromsec, const char* tosec)
1677{
1678 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1679 const char* fromsym_name = sym_name(elf, fromsym);
1680 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1681 const char* tosym_name = sym_name(elf, tosym);
1682 const char* from_pretty_name;
1683 const char* from_pretty_name_p;
1684 const char* to_pretty_name;
1685 const char* to_pretty_name_p;
1686
1687 get_pretty_name(is_function(fromsym),
1688 &from_pretty_name, &from_pretty_name_p);
1689 get_pretty_name(is_function(tosym),
1690 &to_pretty_name, &to_pretty_name_p);
1691
1692 warn("%s(%s+0x%lx): Section mismatch in reference"
1693 " from the %s %s%s to the %s %s:%s%s\n",
1694 modname, fromsec, (long)r->r_offset, from_pretty_name,
1695 fromsym_name, from_pretty_name_p,
1696 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1697
1698 if (!match(tosec, mismatch->bad_tosec) &&
1699 is_executable_section(elf, get_secindex(elf, sym)))
1700 fprintf(stderr,
1701 "The relocation at %s+0x%lx references\n"
1702 "section \"%s\" which is not in the list of\n"
1703 "authorized sections. If you're adding a new section\n"
1704 "and/or if this reference is valid, add \"%s\" to the\n"
1705 "list of authorized sections to jump to on fault.\n"
1706 "This can be achieved by adding \"%s\" to \n"
1707 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1708 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1709}
1710
1711static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1712 const struct sectioncheck* const mismatch,
1713 Elf_Rela* r, Elf_Sym* sym,
1714 const char *fromsec)
1715{
1716 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1717
1718 sec_mismatch_count++;
1719
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001720 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301721
1722 if (match(tosec, mismatch->bad_tosec))
1723 fatal("The relocation at %s+0x%lx references\n"
1724 "section \"%s\" which is black-listed.\n"
1725 "Something is seriously wrong and should be fixed.\n"
1726 "You might get more information about where this is\n"
1727 "coming from by using scripts/check_extable.sh %s\n",
1728 fromsec, (long)r->r_offset, tosec, modname);
1729 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1730 if (is_extable_fault_address(r))
1731 fatal("The relocation at %s+0x%lx references\n"
1732 "section \"%s\" which is not executable, IOW\n"
1733 "it is not possible for the kernel to fault\n"
1734 "at that address. Something is seriously wrong\n"
1735 "and should be fixed.\n",
1736 fromsec, (long)r->r_offset, tosec);
1737 else
1738 fatal("The relocation at %s+0x%lx references\n"
1739 "section \"%s\" which is not executable, IOW\n"
1740 "the kernel will fault if it ever tries to\n"
1741 "jump to it. Something is seriously wrong\n"
1742 "and should be fixed.\n",
1743 fromsec, (long)r->r_offset, tosec);
1744 }
1745}
1746
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001747static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001748 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001749{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001750 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301751 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001752
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001753 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301754 if (mismatch->handler)
1755 mismatch->handler(modname, elf, mismatch,
1756 r, sym, fromsec);
1757 else
1758 default_mismatch_handler(modname, elf, mismatch,
1759 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001760 }
1761}
1762
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001763static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001764 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001765{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001766 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001767}
1768
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001769static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001770{
1771 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001772 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001773
1774 switch (r_typ) {
1775 case R_386_32:
1776 r->r_addend = TO_NATIVE(*location);
1777 break;
1778 case R_386_PC32:
1779 r->r_addend = TO_NATIVE(*location) + 4;
1780 /* For CONFIG_RELOCATABLE=y */
1781 if (elf->hdr->e_type == ET_EXEC)
1782 r->r_addend += r->r_offset;
1783 break;
1784 }
1785 return 0;
1786}
1787
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001788#ifndef R_ARM_CALL
1789#define R_ARM_CALL 28
1790#endif
1791#ifndef R_ARM_JUMP24
1792#define R_ARM_JUMP24 29
1793#endif
1794
David A. Longc9698e52014-02-14 22:41:18 +01001795#ifndef R_ARM_THM_CALL
1796#define R_ARM_THM_CALL 10
1797#endif
1798#ifndef R_ARM_THM_JUMP24
1799#define R_ARM_THM_JUMP24 30
1800#endif
1801#ifndef R_ARM_THM_JUMP19
1802#define R_ARM_THM_JUMP19 51
1803#endif
1804
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001805static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001806{
1807 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1808
1809 switch (r_typ) {
1810 case R_ARM_ABS32:
1811 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001812 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001813 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001814 break;
1815 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001816 case R_ARM_CALL:
1817 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001818 case R_ARM_THM_CALL:
1819 case R_ARM_THM_JUMP24:
1820 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001821 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001822 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001823 sechdr->sh_offset +
1824 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001825 break;
1826 default:
1827 return 1;
1828 }
1829 return 0;
1830}
1831
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001832static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001833{
1834 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001835 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001836 unsigned int inst;
1837
1838 if (r_typ == R_MIPS_HI16)
1839 return 1; /* skip this */
1840 inst = TO_NATIVE(*location);
1841 switch (r_typ) {
1842 case R_MIPS_LO16:
1843 r->r_addend = inst & 0xffff;
1844 break;
1845 case R_MIPS_26:
1846 r->r_addend = (inst & 0x03ffffff) << 2;
1847 break;
1848 case R_MIPS_32:
1849 r->r_addend = inst;
1850 break;
1851 }
1852 return 0;
1853}
1854
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001855static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001856 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001857{
1858 Elf_Sym *sym;
1859 Elf_Rela *rela;
1860 Elf_Rela r;
1861 unsigned int r_sym;
1862 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001863
Sam Ravnborgff13f922008-01-23 19:54:27 +01001864 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001865 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1866
Sam Ravnborgff13f922008-01-23 19:54:27 +01001867 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001868 fromsec += strlen(".rela");
1869 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001870 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001871 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001872
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001873 for (rela = start; rela < stop; rela++) {
1874 r.r_offset = TO_NATIVE(rela->r_offset);
1875#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001876 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001877 unsigned int r_typ;
1878 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1879 r_sym = TO_NATIVE(r_sym);
1880 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1881 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1882 } else {
1883 r.r_info = TO_NATIVE(rela->r_info);
1884 r_sym = ELF_R_SYM(r.r_info);
1885 }
1886#else
1887 r.r_info = TO_NATIVE(rela->r_info);
1888 r_sym = ELF_R_SYM(r.r_info);
1889#endif
1890 r.r_addend = TO_NATIVE(rela->r_addend);
1891 sym = elf->symtab_start + r_sym;
1892 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001893 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001894 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301895 if (is_second_extable_reloc(start, rela, fromsec))
1896 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001897 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001898 }
1899}
1900
1901static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001902 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001903{
1904 Elf_Sym *sym;
1905 Elf_Rel *rel;
1906 Elf_Rela r;
1907 unsigned int r_sym;
1908 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001909
Sam Ravnborgff13f922008-01-23 19:54:27 +01001910 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001911 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1912
Sam Ravnborgff13f922008-01-23 19:54:27 +01001913 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001914 fromsec += strlen(".rel");
1915 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001916 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001917 return;
1918
1919 for (rel = start; rel < stop; rel++) {
1920 r.r_offset = TO_NATIVE(rel->r_offset);
1921#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001922 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001923 unsigned int r_typ;
1924 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1925 r_sym = TO_NATIVE(r_sym);
1926 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1927 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1928 } else {
1929 r.r_info = TO_NATIVE(rel->r_info);
1930 r_sym = ELF_R_SYM(r.r_info);
1931 }
1932#else
1933 r.r_info = TO_NATIVE(rel->r_info);
1934 r_sym = ELF_R_SYM(r.r_info);
1935#endif
1936 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001937 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001938 case EM_386:
1939 if (addend_386_rel(elf, sechdr, &r))
1940 continue;
1941 break;
1942 case EM_ARM:
1943 if (addend_arm_rel(elf, sechdr, &r))
1944 continue;
1945 break;
1946 case EM_MIPS:
1947 if (addend_mips_rel(elf, sechdr, &r))
1948 continue;
1949 break;
1950 }
1951 sym = elf->symtab_start + r_sym;
1952 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001953 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001954 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301955 if (is_second_extable_reloc(start, rel, fromsec))
1956 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001957 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001958 }
1959}
1960
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001961/**
1962 * A module includes a number of sections that are discarded
1963 * either when loaded or when used as built-in.
1964 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001965 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001966 * Likewise for modules used built-in the sections marked __exit
1967 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001968 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001969 * The check_sec_ref() function traverses all relocation records
1970 * to find all references to a section that reference a section that will
1971 * be discarded and warns about it.
1972 **/
1973static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001974 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001975{
1976 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001977 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001978
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001979 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001980 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001981 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001982 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001983 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001984 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001985 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001986 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001987 }
1988}
1989
Andi Kleen7d02b492014-02-08 09:01:12 +01001990static char *remove_dot(char *s)
1991{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301992 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001993
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301994 if (n && s[n]) {
1995 size_t m = strspn(s + n + 1, "0123456789");
1996 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001997 s[n] = 0;
1998 }
1999 return s;
2000}
2001
Masahiro Yamada8b185742018-05-09 18:50:40 +09002002static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 const char *symname;
2005 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002006 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002007 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 struct module *mod;
2009 struct elf_info info = { };
2010 Elf_Sym *sym;
2011
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002012 if (!parse_elf(&info, modname))
2013 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 mod = new_module(modname);
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 mod->skip = 1;
2020 }
2021
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002022 if (!is_vmlinux(modname)) {
2023 license = get_modinfo(&info, "license");
2024 if (!license)
2025 warn("missing MODULE_LICENSE() in %s\n", modname);
2026 while (license) {
2027 if (license_is_gpl_compatible(license))
2028 mod->gpl_compatible = 1;
2029 else {
2030 mod->gpl_compatible = 0;
2031 break;
2032 }
2033 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002034 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002035
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002036 namespace = get_modinfo(&info, "import_ns");
2037 while (namespace) {
2038 add_namespace(&mod->imported_namespaces, namespace);
2039 namespace = get_next_modinfo(&info, "import_ns",
2040 namespace);
2041 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002042 }
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002045 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002047 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 handle_moddevtable(mod, &info, sym, symname);
2049 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002050
Matthias Maennich69923202019-10-18 10:31:42 +01002051 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2052 symname = remove_dot(info.strtab + sym->st_name);
2053
Masahiro Yamada17436942019-11-15 02:42:24 +09002054 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002055 if (strstarts(symname, "__kstrtabns_"))
2056 sym_update_namespace(symname + strlen("__kstrtabns_"),
2057 namespace_from_kstrtabns(&info,
2058 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002059
2060 if (strstarts(symname, "__crc_"))
2061 handle_modversion(mod, &info, sym,
2062 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002063 }
2064
Denis Efremov15bfc232019-08-01 09:06:57 +03002065 // check for static EXPORT_SYMBOL_* functions && global vars
2066 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2067 unsigned char bind = ELF_ST_BIND(sym->st_info);
2068
2069 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2070 struct symbol *s =
2071 find_symbol(remove_dot(info.strtab +
2072 sym->st_name));
2073
2074 if (s)
2075 s->is_static = 0;
2076 }
2077 }
2078
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002079 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002081 if (!is_vmlinux(modname)) {
2082 version = get_modinfo(&info, "version");
2083 if (version || all_versions)
2084 get_src_version(modname, mod->srcversion,
2085 sizeof(mod->srcversion) - 1);
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 parse_elf_finish(&info);
2089
Rusty Russell8c8ef422009-03-31 13:05:34 -06002090 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 * never passed as an argument to an exported function, so
2092 * the automatic versioning doesn't pick it up, but it's really
2093 * important anyhow */
2094 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002095 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Rusty Russell712f9b42013-04-04 17:37:38 +10302098static void read_symbols_from_files(const char *filename)
2099{
2100 FILE *in = stdin;
2101 char fname[PATH_MAX];
2102
2103 if (strcmp(filename, "-") != 0) {
2104 in = fopen(filename, "r");
2105 if (!in)
2106 fatal("Can't open filenames file %s: %m", filename);
2107 }
2108
2109 while (fgets(fname, PATH_MAX, in) != NULL) {
2110 if (strends(fname, "\n"))
2111 fname[strlen(fname)-1] = '\0';
2112 read_symbols(fname);
2113 }
2114
2115 if (in != stdin)
2116 fclose(in);
2117}
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119#define SZ 500
2120
2121/* We first write the generated file into memory using the
2122 * following helper, then compare to the file on disk and
2123 * only update the later if anything changed */
2124
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002125void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2126 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 char tmp[SZ];
2129 int len;
2130 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 va_start(ap, fmt);
2133 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002134 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 va_end(ap);
2136}
2137
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002138void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002141 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002142 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144 strncpy(buf->p + buf->pos, s, len);
2145 buf->pos += len;
2146}
2147
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002148static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2149{
2150 const char *e = is_vmlinux(m) ?"":".ko";
2151
2152 switch (exp) {
2153 case export_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002154 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002155 "uses GPL-only symbol '%s'\n", m, e, s);
2156 break;
2157 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002158 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002159 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2160 break;
2161 case export_gpl_future:
Jessica Yu93c95e52020-03-06 17:02:05 +01002162 warn("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002163 "uses future GPL-only symbol '%s'\n", m, e, s);
2164 break;
2165 case export_plain:
2166 case export_unused:
2167 case export_unknown:
2168 /* ignore */
2169 break;
2170 }
2171}
2172
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002173static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002174{
2175 const char *e = is_vmlinux(m) ?"":".ko";
2176
2177 switch (exp) {
2178 case export_unused:
2179 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002180 warn("module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002181 "uses symbol '%s' marked UNUSED\n", m, e, s);
2182 break;
2183 default:
2184 /* ignore */
2185 break;
2186 }
2187}
2188
Masahiro Yamada3b415282018-11-23 16:57:23 +09002189static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002190{
2191 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002192 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002193
2194 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002195 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002196 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002197 if (!exp || exp->module == mod) {
2198 if (have_vmlinux && !s->weak) {
Jessica Yu93c95e52020-03-06 17:02:05 +01002199 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2200 "\"%s\" [%s.ko] undefined!\n",
2201 s->name, mod->name);
2202 if (!warn_unresolved)
Masahiro Yamada3b415282018-11-23 16:57:23 +09002203 err = 1;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002204 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002205 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002206 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002207 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002208 if (basename)
2209 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002210 else
2211 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002212
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002213 if (exp->namespace &&
2214 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002215 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2216 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2217 basename, exp->name, exp->namespace);
2218 if (!allow_missing_ns_imports)
2219 err = 1;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002220 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002221 }
2222
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002223 if (!mod->gpl_compatible)
2224 check_for_gpl_usage(exp->export, basename, exp->name);
2225 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002226 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002227
2228 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002229}
2230
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002231static int check_modname_len(struct module *mod)
2232{
2233 const char *mod_name;
2234
2235 mod_name = strrchr(mod->name, '/');
2236 if (mod_name == NULL)
2237 mod_name = mod->name;
2238 else
2239 mod_name++;
2240 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2241 merror("module name is too long [%s.ko]\n", mod->name);
2242 return 1;
2243 }
2244
2245 return 0;
2246}
2247
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002248/**
2249 * Header for the generated file
2250 **/
2251static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002254 /*
2255 * Include build-salt.h after module.h in order to
2256 * inherit the definitions.
2257 */
2258 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 buf_printf(b, "#include <linux/vermagic.h>\n");
2260 buf_printf(b, "#include <linux/compiler.h>\n");
2261 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002262 buf_printf(b, "BUILD_SALT;\n");
2263 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002265 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302267 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002268 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002269 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002271 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (mod->has_cleanup)
2273 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002274 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002276 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 buf_printf(b, "};\n");
2278}
2279
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002280static void add_intree_flag(struct buffer *b, int is_intree)
2281{
2282 if (is_intree)
2283 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2284}
2285
Andi Kleencaf75012018-01-25 15:50:28 -08002286/* Cannot check for assembler */
2287static void add_retpoline(struct buffer *b)
2288{
WANG Chaoe4f35892018-12-11 00:37:25 +08002289 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002290 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2291 buf_printf(b, "#endif\n");
2292}
2293
Trevor Keith5c725132009-09-22 16:43:38 -07002294static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002295{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002296 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002297 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2298}
2299
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002300/**
2301 * Record CRCs for unresolved symbols
2302 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002303static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002306 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 for (s = mod->unres; s; s = s->next) {
2309 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002310 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 s->module = exp->module;
2313 s->crc_valid = exp->crc_valid;
2314 s->crc = exp->crc;
2315 }
2316
2317 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002318 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 buf_printf(b, "\n");
2321 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002322 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002325 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002328 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 s->name, mod->name);
2330 continue;
2331 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302332 if (strlen(s->name) >= MODULE_NAME_LEN) {
2333 merror("too long symbol \"%s\" [%s.ko]\n",
2334 s->name, mod->name);
2335 err = 1;
2336 break;
2337 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002338 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302339 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341
2342 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002343
2344 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002347static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 int first = 1;
2351
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002352 /* Clear ->seen flag of modules that own symbols needed by this. */
2353 for (s = mod->unres; s; s = s->next)
2354 if (s->module)
2355 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002358 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002360 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (!s->module)
2362 continue;
2363
2364 if (s->module->seen)
2365 continue;
2366
2367 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002368 p = strrchr(s->module->name, '/');
2369 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002370 p++;
2371 else
2372 p = s->module->name;
2373 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 first = 0;
2375 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002376 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002379static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
2381 if (mod->srcversion[0]) {
2382 buf_printf(b, "\n");
2383 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2384 mod->srcversion);
2385 }
2386}
2387
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002388static void write_buf(struct buffer *b, const char *fname)
2389{
2390 FILE *file;
2391
2392 file = fopen(fname, "w");
2393 if (!file) {
2394 perror(fname);
2395 exit(1);
2396 }
2397 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2398 perror(fname);
2399 exit(1);
2400 }
2401 if (fclose(file) != 0) {
2402 perror(fname);
2403 exit(1);
2404 }
2405}
2406
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002407static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 char *tmp;
2410 FILE *file;
2411 struct stat st;
2412
2413 file = fopen(fname, "r");
2414 if (!file)
2415 goto write;
2416
2417 if (fstat(fileno(file), &st) < 0)
2418 goto close_write;
2419
2420 if (st.st_size != b->pos)
2421 goto close_write;
2422
2423 tmp = NOFAIL(malloc(b->pos));
2424 if (fread(tmp, 1, b->pos, file) != b->pos)
2425 goto free_write;
2426
2427 if (memcmp(tmp, b->p, b->pos) != 0)
2428 goto free_write;
2429
2430 free(tmp);
2431 fclose(file);
2432 return;
2433
2434 free_write:
2435 free(tmp);
2436 close_write:
2437 fclose(file);
2438 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002439 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Ram Paibd5cbce2006-06-08 22:12:53 -07002442/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002443 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002444 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002445static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002447 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002449 buf = read_text_file(fname);
2450 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* No symbol versions, silently ignore */
2452 return;
2453
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002454 pos = buf;
2455
2456 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002457 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 unsigned int crc;
2459 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002460 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 if (!(symname = strchr(line, '\t')))
2463 goto fail;
2464 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002465 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 goto fail;
2467 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002468 if (!(export = strchr(modname, '\t')))
2469 goto fail;
2470 *export++ = '\0';
2471 if (!(namespace = strchr(export, '\t')))
2472 goto fail;
2473 *namespace++ = '\0';
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 crc = strtoul(line, &d, 16);
2476 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2477 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002478 mod = find_module(modname);
2479 if (!mod) {
2480 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002482 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 mod->skip = 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002484 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002486 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002487 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002488 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002489 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002491 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return;
2493fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002494 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 fatal("parse error in symbol dump file\n");
2496}
2497
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002498/* For normal builds always dump all symbols.
2499 * For external modules only dump symbols
2500 * that are not read from kernel Module.symvers.
2501 **/
2502static int dump_sym(struct symbol *sym)
2503{
2504 if (!external_module)
2505 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002506 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002507 return 0;
2508 return 1;
2509}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002510
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002511static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 struct buffer buf = { };
2514 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002515 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 int n;
2517
2518 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2519 symbol = symbolhash[n];
2520 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002521 if (dump_sym(symbol)) {
2522 namespace = symbol->namespace;
2523 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2524 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002525 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002526 export_str(symbol->export),
2527 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 symbol = symbol->next;
2530 }
2531 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002532 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002533 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002536static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002537{
2538 struct module *mod;
2539 struct namespace_list *ns;
2540 struct buffer ns_deps_buf = {};
2541
2542 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002543
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002544 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002545 continue;
2546
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002547 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002548
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002549 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2550 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002551
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002552 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002553 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002554
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002555 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002556 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002557}
2558
Masahiro Yamada79247992020-06-01 14:57:07 +09002559struct dump_list {
2560 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002561 const char *file;
2562};
2563
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002564int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
2566 struct module *mod;
2567 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002568 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302569 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002571 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002572 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002573 struct dump_list *dump_read_start = NULL;
2574 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002576 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002577 switch (opt) {
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002578 case 'e':
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002579 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002580 break;
2581 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002582 *dump_read_iter =
2583 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2584 (*dump_read_iter)->file = optarg;
2585 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002586 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002587 case 'm':
2588 modversions = 1;
2589 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302590 case 'n':
2591 ignore_missing_files = 1;
2592 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002593 case 'o':
2594 dump_write = optarg;
2595 break;
2596 case 'a':
2597 all_versions = 1;
2598 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302599 case 'T':
2600 files_source = optarg;
2601 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002602 case 'w':
2603 warn_unresolved = 1;
2604 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302605 case 'E':
2606 sec_mismatch_fatal = 1;
2607 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002608 case 'N':
2609 allow_missing_ns_imports = 1;
2610 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002611 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002612 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002613 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002614 default:
2615 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617 }
2618
Masahiro Yamada79247992020-06-01 14:57:07 +09002619 while (dump_read_start) {
2620 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002621
Masahiro Yamada79247992020-06-01 14:57:07 +09002622 read_dump(dump_read_start->file);
2623 tmp = dump_read_start->next;
2624 free(dump_read_start);
2625 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002628 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Rusty Russell712f9b42013-04-04 17:37:38 +10302631 if (files_source)
2632 read_symbols_from_files(files_source);
2633
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002634 /*
2635 * When there's no vmlinux, don't print warnings about
2636 * unresolved symbols (since there'll be too many ;)
2637 */
2638 if (!have_vmlinux)
2639 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2640
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002641 err = 0;
2642
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002643 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302644 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002645
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002646 if (mod->skip)
2647 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 buf.pos = 0;
2650
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002651 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002652 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002655 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002656 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002657 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002658 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002659 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 add_moddevtable(&buf, mod);
2661 add_srcversion(&buf, mod);
2662
2663 sprintf(fname, "%s.mod.c", mod->name);
2664 write_if_changed(&buf, fname);
2665 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002666
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002667 if (missing_namespace_deps)
2668 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (dump_write)
2671 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002672 if (sec_mismatch_count && sec_mismatch_fatal)
Jessica Yu93c95e52020-03-06 17:02:05 +01002673 fatal("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002674 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002675 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002676 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002677
Masahiro Yamada47346e92019-09-24 21:07:40 +09002678 for (s = symbolhash[n]; s; s = s->next) {
2679 /*
2680 * Do not check "vmlinux". This avoids the same warnings
2681 * shown twice, and false-positives for ARCH=um.
2682 */
2683 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2684 continue;
2685
Denis Efremov15bfc232019-08-01 09:06:57 +03002686 if (s->is_static)
2687 warn("\"%s\" [%s] is a static %s\n",
2688 s->name, s->module->name,
2689 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002690 }
2691 }
2692
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002693 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002695 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}