blob: 0a844902998e05fb6718e65fc5d8a93eafe8b867 [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;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020033/* Warn about section mismatch in vmlinux if set to 1 */
34static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070035/* Only warn about unresolved symbols */
36static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070037/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010038static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103039static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093040/* ignore missing files */
41static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010042/* If set to 1, only warn (instead of error) about missing ns imports */
43static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010044
Sam Ravnborgc96fca22006-07-01 11:44:23 +020045enum export {
46 export_plain, export_unused, export_gpl,
47 export_unused_gpl, export_gpl_future, export_unknown
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080050/* In kernel, this size is defined in linux/module.h;
51 * here we use Elf_Addr instead of long for covering cross-compile
52 */
53
54#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
55
Jessica Yu93c95e52020-03-06 17:02:05 +010056void __attribute__((format(printf, 2, 3)))
57modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 va_list arglist;
60
Jessica Yu93c95e52020-03-06 17:02:05 +010061 switch (loglevel) {
62 case LOG_WARN:
63 fprintf(stderr, "WARNING: ");
64 break;
65 case LOG_ERROR:
66 fprintf(stderr, "ERROR: ");
67 break;
68 case LOG_FATAL:
69 fprintf(stderr, "FATAL: ");
70 break;
71 default: /* invalid loglevel, ignore */
72 break;
73 }
74
75 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 va_start(arglist, fmt);
78 vfprintf(stderr, fmt, arglist);
79 va_end(arglist);
80
Jessica Yu93c95e52020-03-06 17:02:05 +010081 if (loglevel == LOG_FATAL)
82 exit(1);
Matthew Wilcox2a116652006-10-07 05:35:32 -060083}
84
Rusty Russelld4ef1c32013-04-04 17:37:32 +103085static inline bool strends(const char *str, const char *postfix)
86{
87 if (strlen(str) < strlen(postfix))
88 return false;
89
90 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
91}
92
Sam Ravnborg040fcc82006-01-28 22:15:55 +010093static int is_vmlinux(const char *modname)
94{
95 const char *myname;
96
Sam Ravnborgdf578e72008-01-11 19:17:15 +010097 myname = strrchr(modname, '/');
98 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010099 myname++;
100 else
101 myname = modname;
102
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200103 return (strcmp(myname, "vmlinux") == 0) ||
104 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107void *do_nofail(void *ptr, const char *expr)
108{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100109 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100110 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return ptr;
113}
114
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900115char *read_text_file(const char *filename)
116{
117 struct stat st;
118 size_t nbytes;
119 int fd;
120 char *buf;
121
122 fd = open(filename, O_RDONLY);
123 if (fd < 0) {
124 perror(filename);
125 exit(1);
126 }
127
128 if (fstat(fd, &st) < 0) {
129 perror(filename);
130 exit(1);
131 }
132
133 buf = NOFAIL(malloc(st.st_size + 1));
134
135 nbytes = st.st_size;
136
137 while (nbytes) {
138 ssize_t bytes_read;
139
140 bytes_read = read(fd, buf, nbytes);
141 if (bytes_read < 0) {
142 perror(filename);
143 exit(1);
144 }
145
146 nbytes -= bytes_read;
147 }
148 buf[st.st_size] = '\0';
149
150 close(fd);
151
152 return buf;
153}
154
155char *get_line(char **stringp)
156{
157 /* do not return the unwanted extra line at EOF */
158 if (*stringp && **stringp == '\0')
159 return NULL;
160
161 return strsep(stringp, "\n");
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static struct module *modules;
166
Masahiro Yamada8b185742018-05-09 18:50:40 +0900167static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct module *mod;
170
171 for (mod = modules; mod; mod = mod->next)
172 if (strcmp(mod->name, modname) == 0)
173 break;
174 return mod;
175}
176
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030177static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030180 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 mod = NOFAIL(malloc(sizeof(*mod)));
183 memset(mod, 0, sizeof(*mod));
184 p = NOFAIL(strdup(modname));
185
186 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030187 if (strends(p, ".o")) {
188 p[strlen(p) - 2] = '\0';
189 mod->is_dot_o = 1;
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /* add to list */
193 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200194 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 mod->next = modules;
196 modules = mod;
197
198 return mod;
199}
200
201/* A hash of all exported symbols,
202 * struct symbol is also used for lists of unresolved symbols */
203
204#define SYMBOL_HASH_SIZE 1024
205
206struct symbol {
207 struct symbol *next;
208 struct module *module;
209 unsigned int crc;
210 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900211 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300213 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700214 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500215 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
219
220/* This is based on the hash agorithm from gdbm, via tdb */
221static inline unsigned int tdb_hash(const char *name)
222{
223 unsigned value; /* Used to compute the hash value. */
224 unsigned i; /* Used to cycle through random values. */
225
226 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100227 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
229
230 return (1103515243 * value + 12345);
231}
232
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100233/**
234 * Allocate a new symbols for use in the hash of exported symbols or
235 * the list of unresolved symbols per module
236 **/
237static struct symbol *alloc_symbol(const char *name, unsigned int weak,
238 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
241
242 memset(s, 0, sizeof(*s));
243 strcpy(s->name, name);
244 s->weak = weak;
245 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300246 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return s;
248}
249
250/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700251static struct symbol *new_symbol(const char *name, struct module *module,
252 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900257 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
258
259 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100262static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct symbol *s;
265
266 /* For our purposes, .foo matches foo. PPC64 needs this. */
267 if (name[0] == '.')
268 name++;
269
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100270 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (strcmp(s->name, name) == 0)
272 return s;
273 }
274 return NULL;
275}
276
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100277static bool contains_namespace(struct namespace_list *list,
278 const char *namespace)
279{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900280 for (; list; list = list->next)
281 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100282 return true;
283
284 return false;
285}
286
287static void add_namespace(struct namespace_list **list, const char *namespace)
288{
289 struct namespace_list *ns_entry;
290
291 if (!contains_namespace(*list, namespace)) {
292 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
293 strlen(namespace) + 1));
294 strcpy(ns_entry->namespace, namespace);
295 ns_entry->next = *list;
296 *list = ns_entry;
297 }
298}
299
300static bool module_imports_namespace(struct module *module,
301 const char *namespace)
302{
303 return contains_namespace(module->imported_namespaces, namespace);
304}
305
Mathias Krause7a3ee752014-08-27 20:28:53 +0930306static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700307 const char *str;
308 enum export export;
309} export_list[] = {
310 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200311 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700312 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200313 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700314 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
315 { .str = "(unknown)", .export = export_unknown },
316};
317
318
319static const char *export_str(enum export ex)
320{
321 return export_list[ex].str;
322}
323
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100324static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700325{
326 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100327
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200328 if (!s)
329 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700330 for (i = 0; export_list[i].export != export_unknown; i++) {
331 if (strcmp(export_list[i].str, s) == 0)
332 return export_list[i].export;
333 }
334 return export_unknown;
335}
336
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900337static void *sym_get_data_by_offset(const struct elf_info *info,
338 unsigned int secindex, unsigned long offset)
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900339{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800340 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900341
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900342 if (info->hdr->e_type != ET_REL)
343 offset -= sechdr->sh_addr;
344
345 return (void *)info->hdr + sechdr->sh_offset + offset;
346}
347
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900348static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
349{
350 return sym_get_data_by_offset(info, get_secindex(info, sym),
351 sym->st_value);
352}
353
Masahiro Yamada565587d2020-05-25 14:47:05 +0900354static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
355{
356 return sym_get_data_by_offset(info, info->secindex_strings,
357 sechdr->sh_name);
358}
359
360static const char *sec_name(const struct elf_info *info, int secindex)
361{
362 return sech_name(info, &info->sechdrs[secindex]);
363}
364
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200365#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
366
367static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
368{
369 const char *secname = sec_name(elf, sec);
370
371 if (strstarts(secname, "___ksymtab+"))
372 return export_plain;
373 else if (strstarts(secname, "___ksymtab_unused+"))
374 return export_unused;
375 else if (strstarts(secname, "___ksymtab_gpl+"))
376 return export_gpl;
377 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
378 return export_unused_gpl;
379 else if (strstarts(secname, "___ksymtab_gpl_future+"))
380 return export_gpl_future;
381 else
382 return export_unknown;
383}
384
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200385static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700386{
387 if (sec == elf->export_sec)
388 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200389 else if (sec == elf->export_unused_sec)
390 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700391 else if (sec == elf->export_gpl_sec)
392 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200393 else if (sec == elf->export_unused_gpl_sec)
394 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700395 else if (sec == elf->export_gpl_future_sec)
396 return export_gpl_future;
397 else
398 return export_unknown;
399}
400
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900401static const char *namespace_from_kstrtabns(const struct elf_info *info,
402 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100403{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900404 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100405 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100406}
407
Matthias Maennicha2b11182019-10-18 10:31:40 +0100408static void sym_update_namespace(const char *symname, const char *namespace)
409{
410 struct symbol *s = find_symbol(symname);
411
412 /*
413 * That symbol should have been created earlier and thus this is
414 * actually an assertion.
415 */
416 if (!s) {
417 merror("Could not update namespace(%s) for symbol %s\n",
418 namespace, symname);
419 return;
420 }
421
422 free(s->namespace);
423 s->namespace =
424 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
425}
426
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100427/**
428 * Add an exported symbol - it may have already been added without a
429 * CRC, in this case just update the CRC
430 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100431static struct symbol *sym_add_exported(const char *name, struct module *mod,
432 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct symbol *s = find_symbol(name);
435
436 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700437 s = new_symbol(name, mod, export);
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900438 } else if (!external_module || is_vmlinux(s->module->name) ||
439 s->module == mod) {
440 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
441 mod->name, name, s->module->name,
442 is_vmlinux(s->module->name) ? "" : ".ko");
443 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900445
446 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700447 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100448 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Masahiro Yamada17436942019-11-15 02:42:24 +0900451static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100452{
453 struct symbol *s = find_symbol(name);
454
Masahiro Yamada17436942019-11-15 02:42:24 +0900455 /*
456 * Ignore stand-alone __crc_*, which might be auto-generated symbols
457 * such as __*_veneer in ARM ELF.
458 */
459 if (!s)
460 return;
461
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100462 s->crc = crc;
463 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100466void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930469 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int fd;
471
472 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930473 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930475 if (fstat(fd, &st))
476 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 *size = st.st_size;
479 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930481failed:
482 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (map == MAP_FAILED)
484 return NULL;
485 return map;
486}
487
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100488/**
489 * Return a copy of the next line in a mmap'ed file.
490 * spaces in the beginning of the line is trimmed away.
491 * Return a pointer to a static buffer.
492 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100493char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 static char line[4096];
496 int skip = 1;
497 size_t len = 0;
498 signed char *p = (signed char *)file + *pos;
499 char *s = line;
500
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100501 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (skip && isspace(*p)) {
503 p++;
504 continue;
505 }
506 skip = 0;
507 if (*p != '\n' && (*pos < size)) {
508 len++;
509 *s++ = *p++;
510 if (len > 4095)
511 break; /* Too long, stop */
512 } else {
513 /* End of string */
514 *s = '\0';
515 return line;
516 }
517 }
518 /* End of buffer */
519 return NULL;
520}
521
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100522void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 munmap(file, size);
525}
526
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100527static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100530 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 Elf_Shdr *sechdrs;
532 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200533 const char *secstrings;
534 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 hdr = grab_file(filename, &info->size);
537 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930538 if (ignore_missing_files) {
539 fprintf(stderr, "%s: %s (ignored)\n", filename,
540 strerror(errno));
541 return 0;
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200544 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100547 if (info->size < sizeof(*hdr)) {
548 /* file too small, assume this is an empty .o file */
549 return 0;
550 }
551 /* Is this a valid ELF file? */
552 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
553 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
554 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
555 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
556 /* Not an ELF file - silently ignore it */
557 return 0;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200560 hdr->e_type = TO_NATIVE(hdr->e_type);
561 hdr->e_machine = TO_NATIVE(hdr->e_machine);
562 hdr->e_version = TO_NATIVE(hdr->e_version);
563 hdr->e_entry = TO_NATIVE(hdr->e_entry);
564 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
565 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
566 hdr->e_flags = TO_NATIVE(hdr->e_flags);
567 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
568 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
569 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
570 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
571 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
572 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 sechdrs = (void *)hdr + hdr->e_shoff;
574 info->sechdrs = sechdrs;
575
Petr Stetiara83710e2007-08-27 12:15:07 +0200576 /* Check if file offset is correct */
577 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100578 fatal("section header offset=%lu in file '%s' is bigger than "
579 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
580 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200581 return 0;
582 }
583
Anders Kaseorg68457562011-05-19 16:55:27 -0600584 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200585 /*
586 * There are more than 64k sections,
587 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200588 */
589 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
590 }
591 else {
592 info->num_sections = hdr->e_shnum;
593 }
594 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600595 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200596 }
597 else {
598 info->secindex_strings = hdr->e_shstrndx;
599 }
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200602 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200603 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
604 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
605 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
606 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
607 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
608 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
609 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
610 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
611 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
612 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200615 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
616 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700617 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900618 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Tejun Heo56fc82c2009-02-06 00:48:02 +0900620 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100621 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
622 "sizeof(*hrd)=%zu\n", filename,
623 (unsigned long)sechdrs[i].sh_offset,
624 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100625 return 0;
626 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700627 secname = secstrings + sechdrs[i].sh_name;
628 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900629 if (nobits)
630 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
632 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700633 } else if (strcmp(secname, "__ksymtab") == 0)
634 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200635 else if (strcmp(secname, "__ksymtab_unused") == 0)
636 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700637 else if (strcmp(secname, "__ksymtab_gpl") == 0)
638 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200639 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
640 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700641 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
642 info->export_gpl_future_sec = i;
643
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200644 if (sechdrs[i].sh_type == SHT_SYMTAB) {
645 unsigned int sh_link_idx;
646 symtab_idx = i;
647 info->symtab_start = (void *)hdr +
648 sechdrs[i].sh_offset;
649 info->symtab_stop = (void *)hdr +
650 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600651 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200652 info->strtab = (void *)hdr +
653 sechdrs[sh_link_idx].sh_offset;
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200656 /* 32bit section no. table? ("more than 64k sections") */
657 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
658 symtab_shndx_idx = i;
659 info->symtab_shndx_start = (void *)hdr +
660 sechdrs[i].sh_offset;
661 info->symtab_shndx_stop = (void *)hdr +
662 sechdrs[i].sh_offset + sechdrs[i].sh_size;
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100665 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100666 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* Fix endianness in symbols */
669 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
670 sym->st_shndx = TO_NATIVE(sym->st_shndx);
671 sym->st_name = TO_NATIVE(sym->st_name);
672 sym->st_value = TO_NATIVE(sym->st_value);
673 sym->st_size = TO_NATIVE(sym->st_size);
674 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200675
676 if (symtab_shndx_idx != ~0U) {
677 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600678 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200679 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600680 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200681 symtab_idx);
682 /* Fix endianness */
683 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
684 p++)
685 *p = TO_NATIVE(*p);
686 }
687
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100688 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100691static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 release_file(info->hdr, info->size);
694}
695
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200696static int ignore_undef_symbol(struct elf_info *info, const char *symname)
697{
698 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900699 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200700 return 1;
701 /* ignore global offset table */
702 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
703 return 1;
704 if (info->hdr->e_machine == EM_PPC)
705 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900706 if (strstarts(symname, "_restgpr_") ||
707 strstarts(symname, "_savegpr_") ||
708 strstarts(symname, "_rest32gpr_") ||
709 strstarts(symname, "_save32gpr_") ||
710 strstarts(symname, "_restvr_") ||
711 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200712 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000713 if (info->hdr->e_machine == EM_PPC64)
714 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900715 if (strstarts(symname, "_restgpr0_") ||
716 strstarts(symname, "_savegpr0_") ||
717 strstarts(symname, "_restvr_") ||
718 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100719 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000720 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200721 /* Do not ignore this symbol */
722 return 0;
723}
724
Masahiro Yamada17436942019-11-15 02:42:24 +0900725static void handle_modversion(const struct module *mod,
726 const struct elf_info *info,
727 const Elf_Sym *sym, const char *symname)
728{
729 unsigned int crc;
730
731 if (sym->st_shndx == SHN_UNDEF) {
732 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
733 symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
734 return;
735 }
736
737 if (sym->st_shndx == SHN_ABS) {
738 crc = sym->st_value;
739 } else {
740 unsigned int *crcp;
741
742 /* symbol points to the CRC in the ELF object */
743 crcp = sym_get_data(info, sym);
744 crc = TO_NATIVE(*crcp);
745 }
746 sym_set_crc(symname, crc);
747}
748
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900749static void handle_symbol(struct module *mod, struct elf_info *info,
750 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200752 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900753 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200754
Frank Rowand258f7422012-04-09 17:59:03 -0700755 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900756 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200757 export = export_from_secname(info, get_secindex(info, sym));
758 else
759 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 switch (sym->st_shndx) {
762 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900763 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100764 /* Should warn here, but modpost runs before the linker */
765 } else
766 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 case SHN_UNDEF:
769 /* undefined symbol */
770 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
771 ELF_ST_BIND(sym->st_info) != STB_WEAK)
772 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200773 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (info->hdr->e_machine == EM_SPARC ||
776 info->hdr->e_machine == EM_SPARCV9) {
777 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700778 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100780 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700781 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100782 munged[0] = '_';
783 munged[1] = toupper(munged[1]);
784 symname = munged;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100787
Rusty Russellb92021b2013-03-15 15:04:17 +1030788 mod->unres = alloc_symbol(symname,
789 ELF_ST_BIND(sym->st_info) == STB_WEAK,
790 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 break;
792 default:
793 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900794 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100795 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100796 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900798 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900800 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 mod->has_cleanup = 1;
802 break;
803 }
804}
805
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100806/**
807 * Parse tag=value strings from .modinfo section
808 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809static char *next_string(char *string, unsigned long *secsize)
810{
811 /* Skip non-zero chars */
812 while (string[0]) {
813 string++;
814 if ((*secsize)-- <= 1)
815 return NULL;
816 }
817
818 /* Skip any zero padding. */
819 while (!string[0]) {
820 string++;
821 if ((*secsize)-- <= 1)
822 return NULL;
823 }
824 return string;
825}
826
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900827static char *get_next_modinfo(struct elf_info *info, const char *tag,
828 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 char *p;
831 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900832 char *modinfo = info->modinfo;
833 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900835 if (prev) {
836 size -= prev - modinfo;
837 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200838 }
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 for (p = modinfo; p; p = next_string(p, &size)) {
841 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
842 return p + taglen + 1;
843 }
844 return NULL;
845}
846
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900847static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200848
849{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900850 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200851}
852
Sam Ravnborg93684d32006-02-19 11:53:35 +0100853/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100854 * Test if string s ends in string sub
855 * return 0 if match
856 **/
857static int strrcmp(const char *s, const char *sub)
858{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100859 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100860
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100861 if (!s || !sub)
862 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100863
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100864 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100865 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100866
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100867 if ((slen == 0) || (sublen == 0))
868 return 1;
869
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100870 if (sublen > slen)
871 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100872
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100873 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100874}
875
Sam Ravnborgff13f922008-01-23 19:54:27 +0100876static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
877{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100878 if (sym)
879 return elf->strtab + sym->st_name;
880 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100881 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100882}
883
Sam Ravnborg10668222008-01-13 22:21:31 +0100884/* The pattern is an array of simple patterns.
885 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100886 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100887 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930888 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100889 */
Trevor Keith5c725132009-09-22 16:43:38 -0700890static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100891{
892 const char *p;
893 while (*pat) {
894 p = *pat++;
895 const char *endp = p + strlen(p) - 1;
896
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930897 /* "*foo*" */
898 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300899 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
900 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930901
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930902 free(bare);
903 if (here != NULL)
904 return 1;
905 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100906 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930907 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100908 if (strrcmp(sym, p + 1) == 0)
909 return 1;
910 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100911 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100912 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100913 if (strncmp(sym, p, strlen(p) - 1) == 0)
914 return 1;
915 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100916 /* no wildcards */
917 else {
918 if (strcmp(p, sym) == 0)
919 return 1;
920 }
921 }
922 /* no match */
923 return 0;
924}
925
Sam Ravnborg10668222008-01-13 22:21:31 +0100926/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930927static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200928{
929 ".comment*",
930 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930931 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800932 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000933 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200934 ".mdebug*", /* alpha, score, mips etc. */
935 ".pdr", /* alpha, score, mips etc. */
936 ".stab*",
937 ".note*",
938 ".got*",
939 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400940 ".xt.prop", /* xtensa */
941 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030942 ".arcextmap*", /* arc */
943 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030944 ".cmem*", /* EZchip */
945 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100946 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600947 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200948 NULL
949};
Sam Ravnborg10668222008-01-13 22:21:31 +0100950
Sam Ravnborge241a632008-01-28 20:13:13 +0100951/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400952 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100953 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400954 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100955 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400956static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900957 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100958{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400959 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100960
Anders Kaseorgb614a692009-04-23 16:49:33 -0400961 if (sechdr->sh_type == SHT_PROGBITS &&
962 !(sechdr->sh_flags & SHF_ALLOC) &&
963 !match(sec, section_white_list)) {
964 warn("%s (%s): unexpected non-allocatable section.\n"
965 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
966 "Note that for example <linux/init.h> contains\n"
967 "section definitions for use in .S files.\n\n",
968 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100969 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100970}
971
972
973
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100974#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930975 ".init.setup", ".init.rodata", ".meminit.rodata", \
976 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100977#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930978 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100979
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100980#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930981 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100982#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930983 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100984
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200985#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930986 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
987 ".pci_fixup_enable", ".pci_fixup_resume", \
988 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200989
Paul Gortmakere24f6622013-06-19 19:30:48 -0400990#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
991#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100992
993#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
994#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100995
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930996#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930997#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700998 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930999#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -04001000 ".fixup", ".entry.text", ".exception.text", ".text.*", \
1001 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +01001002
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001003#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001004#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001005
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001006#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +00001007#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001008
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301009#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
1010 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
1011
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001012/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +09301013static const char *const init_data_sections[] =
1014 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001015
1016/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +09301017static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001018
1019/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +09301020static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001021 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001022
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301023/* all text sections */
1024static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
1025
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001026/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +09301027static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001028
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001029
1030/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001031#define DEFAULT_SYMBOL_WHITE_LIST \
1032 "*driver", \
1033 "*_template", /* scsi uses *_template a lot */ \
1034 "*_timer", /* arm uses ops structures named _timer a lot */ \
1035 "*_sht", /* scsi also used *_sht to some extent */ \
1036 "*_ops", \
1037 "*_probe", \
1038 "*_probe_one", \
1039 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001040
Mathias Krause7a3ee752014-08-27 20:28:53 +09301041static const char *const head_sections[] = { ".head.text*", NULL };
1042static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001043 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301044static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001045
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001046enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001047 TEXT_TO_ANY_INIT,
1048 DATA_TO_ANY_INIT,
1049 TEXT_TO_ANY_EXIT,
1050 DATA_TO_ANY_EXIT,
1051 XXXINIT_TO_SOME_INIT,
1052 XXXEXIT_TO_SOME_EXIT,
1053 ANY_INIT_TO_ANY_EXIT,
1054 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001055 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301056 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001057};
1058
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301059/**
1060 * Describe how to match sections on different criterias:
1061 *
1062 * @fromsec: Array of sections to be matched.
1063 *
1064 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1065 * this array is forbidden (black-list). Can be empty.
1066 *
1067 * @good_tosec: Relocations applied to a section in @fromsec must be
1068 * targetting sections in this array (white-list). Can be empty.
1069 *
1070 * @mismatch: Type of mismatch.
1071 *
1072 * @symbol_white_list: Do not match a relocation to a symbol in this list
1073 * even if it is targetting a section in @bad_to_sec.
1074 *
1075 * @handler: Specific handler to call when a match is found. If NULL,
1076 * default_mismatch_handler() will be called.
1077 *
1078 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001079struct sectioncheck {
1080 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301081 const char *bad_tosec[20];
1082 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001083 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001084 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301085 void (*handler)(const char *modname, struct elf_info *elf,
1086 const struct sectioncheck* const mismatch,
1087 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1088
Sam Ravnborg10668222008-01-13 22:21:31 +01001089};
1090
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301091static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1092 const struct sectioncheck* const mismatch,
1093 Elf_Rela *r, Elf_Sym *sym,
1094 const char *fromsec);
1095
Mathias Krause7a3ee752014-08-27 20:28:53 +09301096static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001097/* Do not reference init/exit code/data from
1098 * normal code and data
1099 */
1100{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001101 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301102 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001103 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001104 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001105},
1106{
1107 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301108 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001109 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001110 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001111},
1112{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001113 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301114 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001115 .mismatch = DATA_TO_ANY_INIT,
1116 .symbol_white_list = {
1117 "*_template", "*_timer", "*_sht", "*_ops",
1118 "*_probe", "*_probe_one", "*_console", NULL
1119 },
1120},
1121{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001122 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301123 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001124 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001125 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001126},
1127{
1128 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301129 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001130 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001131 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001132},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001133/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001134{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001135 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301136 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001137 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001138 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001139},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001140/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001141{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001142 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301143 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001144 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001145 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001146},
1147/* Do not use exit code/data from init code */
1148{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001149 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301150 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001151 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001152 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001153},
1154/* Do not use init code/data from exit code */
1155{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001156 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301157 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001158 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001159 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001160},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001161{
1162 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301163 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001164 .mismatch = ANY_INIT_TO_ANY_EXIT,
1165 .symbol_white_list = { NULL },
1166},
Sam Ravnborg10668222008-01-13 22:21:31 +01001167/* Do not export init/exit functions or data */
1168{
1169 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301170 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001171 .mismatch = EXPORT_TO_INIT_EXIT,
1172 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301173},
1174{
1175 .fromsec = { "__ex_table", NULL },
1176 /* If you're adding any new black-listed sections in here, consider
1177 * adding a special 'printer' for them in scripts/check_extable.
1178 */
1179 .bad_tosec = { ".altinstr_replacement", NULL },
1180 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1181 .mismatch = EXTABLE_TO_NON_TEXT,
1182 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001183}
1184};
1185
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001186static const struct sectioncheck *section_mismatch(
1187 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001188{
1189 int i;
1190 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1191 const struct sectioncheck *check = &sectioncheck[0];
1192
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301193 /*
1194 * The target section could be the SHT_NUL section when we're
1195 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001196 * doesn't make much sense and causes build failures on parisc
1197 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301198 */
1199 if (*tosec == '\0')
1200 return NULL;
1201
Sam Ravnborg10668222008-01-13 22:21:31 +01001202 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301203 if (match(fromsec, check->fromsec)) {
1204 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1205 return check;
1206 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1207 return check;
1208 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001209 check++;
1210 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001211 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001212}
1213
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001214/**
1215 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001216 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001217 * Pattern 1:
1218 * If a module parameter is declared __initdata and permissions=0
1219 * then this is legal despite the warning generated.
1220 * We cannot see value of permissions here, so just ignore
1221 * this pattern.
1222 * The pattern is identified by:
1223 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001224 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001225 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001226 *
Rusty Russell6a841522010-08-11 23:04:16 -06001227 * Pattern 1a:
1228 * module_param_call() ops can refer to __init set function if permissions=0
1229 * The pattern is identified by:
1230 * tosec = .init.text
1231 * fromsec = .data*
1232 * atsym = __param_ops_*
1233 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001234 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001235 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001236 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001237 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001238 * tosec = init or exit section
1239 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001240 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1241 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001242 *
1243 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001244 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001245 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001246 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001247 * Some symbols belong to init section but still it is ok to reference
1248 * these from non-init sections as these symbols don't have any memory
1249 * allocated for them and symbol address and value are same. So even
1250 * if init section is freed, its ok to reference those symbols.
1251 * For ex. symbols marking the init section boundaries.
1252 * This pattern is identified by
1253 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001254 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301255 * Pattern 5:
1256 * GCC may optimize static inlines when fed constant arg(s) resulting
1257 * in functions like cpumask_empty() -- generating an associated symbol
1258 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1259 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1260 * meaningless section warning. May need to add isra symbols too...
1261 * This pattern is identified by
1262 * tosec = init section
1263 * fromsec = text section
1264 * refsymname = *.constprop.*
1265 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001266 * Pattern 6:
1267 * Hide section mismatch warnings for ELF local symbols. The goal
1268 * is to eliminate false positive modpost warnings caused by
1269 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1270 * Autogenerated symbol names bypass modpost's "Pattern 2"
1271 * whitelisting, which relies on pattern-matching against symbol
1272 * names to work. (One situation where gcc can autogenerate ELF
1273 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001274 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001275static int secref_whitelist(const struct sectioncheck *mismatch,
1276 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001277 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001278{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001279 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001280 if (match(tosec, init_data_sections) &&
1281 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001282 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001283 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001284
Rusty Russell6a841522010-08-11 23:04:16 -06001285 /* Check for pattern 1a */
1286 if (strcmp(tosec, ".init.text") == 0 &&
1287 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001288 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001289 return 0;
1290
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001291 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001292 if (match(tosec, init_exit_sections) &&
1293 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001294 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001295 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001296
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001297 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001298 if (match(fromsec, head_sections) &&
1299 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001300 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001301
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001302 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001303 if (match(tosym, linker_symbols))
1304 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001305
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301306 /* Check for pattern 5 */
1307 if (match(fromsec, text_sections) &&
1308 match(tosec, init_sections) &&
1309 match(fromsym, optim_symbols))
1310 return 0;
1311
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001312 /* Check for pattern 6 */
1313 if (strstarts(fromsym, ".L"))
1314 return 0;
1315
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001316 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001317}
1318
Sami Tolvanen5818c682018-10-23 15:15:35 -07001319static inline int is_arm_mapping_symbol(const char *str)
1320{
1321 return str[0] == '$' && strchr("axtd", str[1])
1322 && (str[2] == '\0' || str[2] == '.');
1323}
1324
1325/*
1326 * If there's no name there, ignore it; likewise, ignore it if it's
1327 * one of the magic symbols emitted used by current ARM tools.
1328 *
1329 * Otherwise if find_symbols_between() returns those symbols, they'll
1330 * fail the whitelist tests and cause lots of false alarms ... fixable
1331 * only by merging __exit and __init sections into __text, bloating
1332 * the kernel (which is especially evil on embedded platforms).
1333 */
1334static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1335{
1336 const char *name = elf->strtab + sym->st_name;
1337
1338 if (!name || !strlen(name))
1339 return 0;
1340 return !is_arm_mapping_symbol(name);
1341}
1342
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001343/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001344 * Find symbol based on relocation record info.
1345 * In some cases the symbol supplied is a valid symbol so
1346 * return refsym. If st_name != 0 we assume this is a valid symbol.
1347 * In other cases the symbol needs to be looked up in the symbol table
1348 * based on section and address.
1349 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001350static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001351 Elf_Sym *relsym)
1352{
1353 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001354 Elf_Sym *near = NULL;
1355 Elf64_Sword distance = 20;
1356 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001357 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001358
1359 if (relsym->st_name != 0)
1360 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001361
1362 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001363 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001364 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001365 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001366 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1367 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001368 if (!is_valid_name(elf, sym))
1369 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001370 if (sym->st_value == addr)
1371 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001372 /* Find a symbol nearby - addr are maybe negative */
1373 d = sym->st_value - addr;
1374 if (d < 0)
1375 d = addr - sym->st_value;
1376 if (d < distance) {
1377 distance = d;
1378 near = sym;
1379 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001380 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001381 /* We need a close match */
1382 if (distance < 20)
1383 return near;
1384 else
1385 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001386}
1387
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001388/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001389 * Find symbols before or equal addr and after addr - in the section sec.
1390 * If we find two symbols with equal offset prefer one with a valid name.
1391 * The ELF format may have a better way to detect what type of symbol
1392 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001393 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001394static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1395 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001396{
1397 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001398 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001399 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001400
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001401 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1402 const char *symsec;
1403
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001404 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001405 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001406 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001407 if (strcmp(symsec, sec) != 0)
1408 continue;
David Brownellda68d612007-02-20 13:58:16 -08001409 if (!is_valid_name(elf, sym))
1410 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001411 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001412 if ((addr - sym->st_value) < distance) {
1413 distance = addr - sym->st_value;
1414 near = sym;
1415 } else if ((addr - sym->st_value) == distance) {
1416 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001417 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001418 }
1419 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001420 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001421}
1422
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001423/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001424 * Convert a section name to the function/data attribute
1425 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001426 * .memexitconst => __memconst
1427 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001428 *
1429 * The memory of returned value has been allocated on a heap. The user of this
1430 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001431*/
1432static char *sec2annotation(const char *s)
1433{
1434 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001435 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001436 char *r = p;
1437
1438 *p++ = '_';
1439 *p++ = '_';
1440 if (*s == '.')
1441 s++;
1442 while (*s && *s != '.')
1443 *p++ = *s++;
1444 *p = '\0';
1445 if (*s == '.')
1446 s++;
1447 if (strstr(s, "rodata") != NULL)
1448 strcat(p, "const ");
1449 else if (strstr(s, "data") != NULL)
1450 strcat(p, "data ");
1451 else
1452 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001453 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001454 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001455 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001456 }
1457}
1458
1459static int is_function(Elf_Sym *sym)
1460{
1461 if (sym)
1462 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1463 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001464 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001465}
1466
Randy Dunlap00759c02011-03-15 14:13:47 -07001467static void print_section_list(const char * const list[20])
1468{
1469 const char *const *s = list;
1470
1471 while (*s) {
1472 fprintf(stderr, "%s", *s);
1473 s++;
1474 if (*s)
1475 fprintf(stderr, ", ");
1476 }
1477 fprintf(stderr, "\n");
1478}
1479
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301480static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1481{
1482 switch (is_func) {
1483 case 0: *name = "variable"; *name_p = ""; break;
1484 case 1: *name = "function"; *name_p = "()"; break;
1485 default: *name = "(unknown reference)"; *name_p = ""; break;
1486 }
1487}
1488
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001489/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001490 * Print a warning about a section mismatch.
1491 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001492 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001493 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001494static void report_sec_mismatch(const char *modname,
1495 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001496 const char *fromsec,
1497 unsigned long long fromaddr,
1498 const char *fromsym,
1499 int from_is_func,
1500 const char *tosec, const char *tosym,
1501 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001502{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001503 const char *from, *from_p;
1504 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001505 char *prl_from;
1506 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001507
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001508 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001509
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301510 get_pretty_name(from_is_func, &from, &from_p);
1511 get_pretty_name(to_is_func, &to, &to_p);
1512
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001513 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1514 "to the %s %s:%s%s\n",
1515 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1516 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001517
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001518 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001519 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001520 prl_from = sec2annotation(fromsec);
1521 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001522 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001523 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001524 "the %s %s%s%s.\n"
1525 "This is often because %s lacks a %s\n"
1526 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001527 prl_from, fromsym,
1528 to, prl_to, tosym, to_p,
1529 fromsym, prl_to, tosym);
1530 free(prl_from);
1531 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001532 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001533 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001534 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001535 fprintf(stderr,
1536 "The variable %s references\n"
1537 "the %s %s%s%s\n"
1538 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001539 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001540 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001541 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001542 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001543 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001544 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001545 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001546 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001547 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001548 fprintf(stderr,
1549 "The function %s() references a %s in an exit section.\n"
1550 "Often the %s %s%s has valid usage outside the exit section\n"
1551 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001552 fromsym, to, to, tosym, to_p, prl_to, tosym);
1553 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001554 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001555 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001556 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001557 fprintf(stderr,
1558 "The variable %s references\n"
1559 "the %s %s%s%s\n"
1560 "If the reference is valid then annotate the\n"
1561 "variable with __exit* (see linux/init.h) or "
1562 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001563 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001564 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001565 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001566 break;
1567 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001568 case XXXINIT_TO_SOME_INIT:
1569 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001570 prl_from = sec2annotation(fromsec);
1571 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001572 fprintf(stderr,
1573 "The %s %s%s%s references\n"
1574 "a %s %s%s%s.\n"
1575 "If %s is only used by %s then\n"
1576 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001577 from, prl_from, fromsym, from_p,
1578 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001579 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001580 free(prl_from);
1581 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001582 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001583 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001584 prl_from = sec2annotation(fromsec);
1585 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001586 fprintf(stderr,
1587 "The %s %s%s%s references\n"
1588 "a %s %s%s%s.\n"
1589 "This is often seen when error handling "
1590 "in the init function\n"
1591 "uses functionality in the exit path.\n"
1592 "The fix is often to remove the %sannotation of\n"
1593 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001594 from, prl_from, fromsym, from_p,
1595 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001596 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001597 free(prl_from);
1598 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001599 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001600 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001601 prl_from = sec2annotation(fromsec);
1602 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001603 fprintf(stderr,
1604 "The %s %s%s%s references\n"
1605 "a %s %s%s%s.\n"
1606 "This is often seen when error handling "
1607 "in the exit function\n"
1608 "uses functionality in the init path.\n"
1609 "The fix is often to remove the %sannotation of\n"
1610 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001611 from, prl_from, fromsym, from_p,
1612 to, prl_to, tosym, to_p,
1613 prl_to, tosym, to_p);
1614 free(prl_from);
1615 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001616 break;
1617 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001618 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001619 fprintf(stderr,
1620 "The symbol %s is exported and annotated %s\n"
1621 "Fix this by removing the %sannotation of %s "
1622 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001623 tosym, prl_to, prl_to, tosym);
1624 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001625 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301626 case EXTABLE_TO_NON_TEXT:
1627 fatal("There's a special handler for this mismatch type, "
1628 "we should never get here.");
1629 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001630 }
1631 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001632}
1633
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301634static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1635 const struct sectioncheck* const mismatch,
1636 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1637{
1638 const char *tosec;
1639 Elf_Sym *to;
1640 Elf_Sym *from;
1641 const char *tosym;
1642 const char *fromsym;
1643
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301644 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1645 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301646
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001647 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301648 return;
1649
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301650 tosec = sec_name(elf, get_secindex(elf, sym));
1651 to = find_elf_symbol(elf, r->r_addend, sym);
1652 tosym = sym_name(elf, to);
1653
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301654 /* check whitelist - we may ignore it */
1655 if (secref_whitelist(mismatch,
1656 fromsec, fromsym, tosec, tosym)) {
1657 report_sec_mismatch(modname, mismatch,
1658 fromsec, r->r_offset, fromsym,
1659 is_function(from), tosec, tosym,
1660 is_function(to));
1661 }
1662}
1663
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301664static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1665{
1666 if (section_index > elf->num_sections)
1667 fatal("section_index is outside elf->num_sections!\n");
1668
1669 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1670}
1671
1672/*
1673 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1674 * to know the sizeof(struct exception_table_entry) for the target architecture.
1675 */
1676static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301677static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301678{
1679 /*
1680 * If we're currently checking the second relocation within __ex_table,
1681 * that relocation offset tells us the offsetof(struct
1682 * exception_table_entry, fixup) which is equal to sizeof(struct
1683 * exception_table_entry) divided by two. We use that to our advantage
1684 * since there's no portable way to get that size as every architecture
1685 * seems to go with different sized types. Not pretty but better than
1686 * hard-coding the size for every architecture..
1687 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301688 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301689 extable_entry_size = r->r_offset * 2;
1690}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301691
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301692static inline bool is_extable_fault_address(Elf_Rela *r)
1693{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301694 /*
1695 * extable_entry_size is only discovered after we've handled the
1696 * _second_ relocation in __ex_table, so only abort when we're not
1697 * handling the first reloc and extable_entry_size is zero.
1698 */
1699 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301700 fatal("extable_entry size hasn't been discovered!\n");
1701
1702 return ((r->r_offset == 0) ||
1703 (r->r_offset % extable_entry_size == 0));
1704}
1705
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301706#define is_second_extable_reloc(Start, Cur, Sec) \
1707 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1708
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301709static void report_extable_warnings(const char* modname, struct elf_info* elf,
1710 const struct sectioncheck* const mismatch,
1711 Elf_Rela* r, Elf_Sym* sym,
1712 const char* fromsec, const char* tosec)
1713{
1714 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1715 const char* fromsym_name = sym_name(elf, fromsym);
1716 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1717 const char* tosym_name = sym_name(elf, tosym);
1718 const char* from_pretty_name;
1719 const char* from_pretty_name_p;
1720 const char* to_pretty_name;
1721 const char* to_pretty_name_p;
1722
1723 get_pretty_name(is_function(fromsym),
1724 &from_pretty_name, &from_pretty_name_p);
1725 get_pretty_name(is_function(tosym),
1726 &to_pretty_name, &to_pretty_name_p);
1727
1728 warn("%s(%s+0x%lx): Section mismatch in reference"
1729 " from the %s %s%s to the %s %s:%s%s\n",
1730 modname, fromsec, (long)r->r_offset, from_pretty_name,
1731 fromsym_name, from_pretty_name_p,
1732 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1733
1734 if (!match(tosec, mismatch->bad_tosec) &&
1735 is_executable_section(elf, get_secindex(elf, sym)))
1736 fprintf(stderr,
1737 "The relocation at %s+0x%lx references\n"
1738 "section \"%s\" which is not in the list of\n"
1739 "authorized sections. If you're adding a new section\n"
1740 "and/or if this reference is valid, add \"%s\" to the\n"
1741 "list of authorized sections to jump to on fault.\n"
1742 "This can be achieved by adding \"%s\" to \n"
1743 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1744 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1745}
1746
1747static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1748 const struct sectioncheck* const mismatch,
1749 Elf_Rela* r, Elf_Sym* sym,
1750 const char *fromsec)
1751{
1752 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1753
1754 sec_mismatch_count++;
1755
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001756 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301757
1758 if (match(tosec, mismatch->bad_tosec))
1759 fatal("The relocation at %s+0x%lx references\n"
1760 "section \"%s\" which is black-listed.\n"
1761 "Something is seriously wrong and should be fixed.\n"
1762 "You might get more information about where this is\n"
1763 "coming from by using scripts/check_extable.sh %s\n",
1764 fromsec, (long)r->r_offset, tosec, modname);
1765 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1766 if (is_extable_fault_address(r))
1767 fatal("The relocation at %s+0x%lx references\n"
1768 "section \"%s\" which is not executable, IOW\n"
1769 "it is not possible for the kernel to fault\n"
1770 "at that address. Something is seriously wrong\n"
1771 "and should be fixed.\n",
1772 fromsec, (long)r->r_offset, tosec);
1773 else
1774 fatal("The relocation at %s+0x%lx references\n"
1775 "section \"%s\" which is not executable, IOW\n"
1776 "the kernel will fault if it ever tries to\n"
1777 "jump to it. Something is seriously wrong\n"
1778 "and should be fixed.\n",
1779 fromsec, (long)r->r_offset, tosec);
1780 }
1781}
1782
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001783static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001784 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001785{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001786 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301787 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001788
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001789 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301790 if (mismatch->handler)
1791 mismatch->handler(modname, elf, mismatch,
1792 r, sym, fromsec);
1793 else
1794 default_mismatch_handler(modname, elf, mismatch,
1795 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001796 }
1797}
1798
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001799static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001800 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001801{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001802 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001803}
1804
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001805static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001806{
1807 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001808 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001809
1810 switch (r_typ) {
1811 case R_386_32:
1812 r->r_addend = TO_NATIVE(*location);
1813 break;
1814 case R_386_PC32:
1815 r->r_addend = TO_NATIVE(*location) + 4;
1816 /* For CONFIG_RELOCATABLE=y */
1817 if (elf->hdr->e_type == ET_EXEC)
1818 r->r_addend += r->r_offset;
1819 break;
1820 }
1821 return 0;
1822}
1823
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001824#ifndef R_ARM_CALL
1825#define R_ARM_CALL 28
1826#endif
1827#ifndef R_ARM_JUMP24
1828#define R_ARM_JUMP24 29
1829#endif
1830
David A. Longc9698e52014-02-14 22:41:18 +01001831#ifndef R_ARM_THM_CALL
1832#define R_ARM_THM_CALL 10
1833#endif
1834#ifndef R_ARM_THM_JUMP24
1835#define R_ARM_THM_JUMP24 30
1836#endif
1837#ifndef R_ARM_THM_JUMP19
1838#define R_ARM_THM_JUMP19 51
1839#endif
1840
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001841static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001842{
1843 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1844
1845 switch (r_typ) {
1846 case R_ARM_ABS32:
1847 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001848 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001849 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001850 break;
1851 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001852 case R_ARM_CALL:
1853 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001854 case R_ARM_THM_CALL:
1855 case R_ARM_THM_JUMP24:
1856 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001857 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001858 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001859 sechdr->sh_offset +
1860 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001861 break;
1862 default:
1863 return 1;
1864 }
1865 return 0;
1866}
1867
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001868static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001869{
1870 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001871 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001872 unsigned int inst;
1873
1874 if (r_typ == R_MIPS_HI16)
1875 return 1; /* skip this */
1876 inst = TO_NATIVE(*location);
1877 switch (r_typ) {
1878 case R_MIPS_LO16:
1879 r->r_addend = inst & 0xffff;
1880 break;
1881 case R_MIPS_26:
1882 r->r_addend = (inst & 0x03ffffff) << 2;
1883 break;
1884 case R_MIPS_32:
1885 r->r_addend = inst;
1886 break;
1887 }
1888 return 0;
1889}
1890
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001891static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001892 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001893{
1894 Elf_Sym *sym;
1895 Elf_Rela *rela;
1896 Elf_Rela r;
1897 unsigned int r_sym;
1898 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001899
Sam Ravnborgff13f922008-01-23 19:54:27 +01001900 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001901 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1902
Sam Ravnborgff13f922008-01-23 19:54:27 +01001903 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001904 fromsec += strlen(".rela");
1905 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001906 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001907 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001908
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001909 for (rela = start; rela < stop; rela++) {
1910 r.r_offset = TO_NATIVE(rela->r_offset);
1911#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001912 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001913 unsigned int r_typ;
1914 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1915 r_sym = TO_NATIVE(r_sym);
1916 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1917 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1918 } else {
1919 r.r_info = TO_NATIVE(rela->r_info);
1920 r_sym = ELF_R_SYM(r.r_info);
1921 }
1922#else
1923 r.r_info = TO_NATIVE(rela->r_info);
1924 r_sym = ELF_R_SYM(r.r_info);
1925#endif
1926 r.r_addend = TO_NATIVE(rela->r_addend);
1927 sym = elf->symtab_start + r_sym;
1928 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001929 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001930 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301931 if (is_second_extable_reloc(start, rela, fromsec))
1932 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001933 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001934 }
1935}
1936
1937static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001938 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001939{
1940 Elf_Sym *sym;
1941 Elf_Rel *rel;
1942 Elf_Rela r;
1943 unsigned int r_sym;
1944 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001945
Sam Ravnborgff13f922008-01-23 19:54:27 +01001946 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001947 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1948
Sam Ravnborgff13f922008-01-23 19:54:27 +01001949 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001950 fromsec += strlen(".rel");
1951 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001952 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001953 return;
1954
1955 for (rel = start; rel < stop; rel++) {
1956 r.r_offset = TO_NATIVE(rel->r_offset);
1957#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001958 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001959 unsigned int r_typ;
1960 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1961 r_sym = TO_NATIVE(r_sym);
1962 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1963 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1964 } else {
1965 r.r_info = TO_NATIVE(rel->r_info);
1966 r_sym = ELF_R_SYM(r.r_info);
1967 }
1968#else
1969 r.r_info = TO_NATIVE(rel->r_info);
1970 r_sym = ELF_R_SYM(r.r_info);
1971#endif
1972 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001973 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001974 case EM_386:
1975 if (addend_386_rel(elf, sechdr, &r))
1976 continue;
1977 break;
1978 case EM_ARM:
1979 if (addend_arm_rel(elf, sechdr, &r))
1980 continue;
1981 break;
1982 case EM_MIPS:
1983 if (addend_mips_rel(elf, sechdr, &r))
1984 continue;
1985 break;
1986 }
1987 sym = elf->symtab_start + r_sym;
1988 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001989 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001990 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301991 if (is_second_extable_reloc(start, rel, fromsec))
1992 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001993 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001994 }
1995}
1996
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001997/**
1998 * A module includes a number of sections that are discarded
1999 * either when loaded or when used as built-in.
2000 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002001 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01002002 * Likewise for modules used built-in the sections marked __exit
2003 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07002004 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01002005 * The check_sec_ref() function traverses all relocation records
2006 * to find all references to a section that reference a section that will
2007 * be discarded and warns about it.
2008 **/
2009static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09002010 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01002011{
2012 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01002013 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002014
Sam Ravnborgb39927c2006-02-17 22:42:02 +01002015 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02002016 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04002017 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01002018 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01002019 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01002020 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01002021 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01002022 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01002023 }
2024}
2025
Andi Kleen7d02b492014-02-08 09:01:12 +01002026static char *remove_dot(char *s)
2027{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09302028 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01002029
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09302030 if (n && s[n]) {
2031 size_t m = strspn(s + n + 1, "0123456789");
2032 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01002033 s[n] = 0;
2034 }
2035 return s;
2036}
2037
Masahiro Yamada8b185742018-05-09 18:50:40 +09002038static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 const char *symname;
2041 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002042 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002043 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 struct module *mod;
2045 struct elf_info info = { };
2046 Elf_Sym *sym;
2047
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002048 if (!parse_elf(&info, modname))
2049 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 mod = new_module(modname);
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 mod->skip = 1;
2056 }
2057
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002058 if (!is_vmlinux(modname)) {
2059 license = get_modinfo(&info, "license");
2060 if (!license)
2061 warn("missing MODULE_LICENSE() in %s\n", modname);
2062 while (license) {
2063 if (license_is_gpl_compatible(license))
2064 mod->gpl_compatible = 1;
2065 else {
2066 mod->gpl_compatible = 0;
2067 break;
2068 }
2069 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002070 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002071
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002072 namespace = get_modinfo(&info, "import_ns");
2073 while (namespace) {
2074 add_namespace(&mod->imported_namespaces, namespace);
2075 namespace = get_next_modinfo(&info, "import_ns",
2076 namespace);
2077 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002078 }
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002081 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002083 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 handle_moddevtable(mod, &info, sym, symname);
2085 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002086
Matthias Maennich69923202019-10-18 10:31:42 +01002087 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2088 symname = remove_dot(info.strtab + sym->st_name);
2089
Masahiro Yamada17436942019-11-15 02:42:24 +09002090 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002091 if (strstarts(symname, "__kstrtabns_"))
2092 sym_update_namespace(symname + strlen("__kstrtabns_"),
2093 namespace_from_kstrtabns(&info,
2094 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002095
2096 if (strstarts(symname, "__crc_"))
2097 handle_modversion(mod, &info, sym,
2098 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002099 }
2100
Denis Efremov15bfc232019-08-01 09:06:57 +03002101 // check for static EXPORT_SYMBOL_* functions && global vars
2102 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2103 unsigned char bind = ELF_ST_BIND(sym->st_info);
2104
2105 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2106 struct symbol *s =
2107 find_symbol(remove_dot(info.strtab +
2108 sym->st_name));
2109
2110 if (s)
2111 s->is_static = 0;
2112 }
2113 }
2114
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002115 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002116 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002118 if (!is_vmlinux(modname)) {
2119 version = get_modinfo(&info, "version");
2120 if (version || all_versions)
2121 get_src_version(modname, mod->srcversion,
2122 sizeof(mod->srcversion) - 1);
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 parse_elf_finish(&info);
2126
Rusty Russell8c8ef422009-03-31 13:05:34 -06002127 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 * never passed as an argument to an exported function, so
2129 * the automatic versioning doesn't pick it up, but it's really
2130 * important anyhow */
2131 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002132 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Rusty Russell712f9b42013-04-04 17:37:38 +10302135static void read_symbols_from_files(const char *filename)
2136{
2137 FILE *in = stdin;
2138 char fname[PATH_MAX];
2139
2140 if (strcmp(filename, "-") != 0) {
2141 in = fopen(filename, "r");
2142 if (!in)
2143 fatal("Can't open filenames file %s: %m", filename);
2144 }
2145
2146 while (fgets(fname, PATH_MAX, in) != NULL) {
2147 if (strends(fname, "\n"))
2148 fname[strlen(fname)-1] = '\0';
2149 read_symbols(fname);
2150 }
2151
2152 if (in != stdin)
2153 fclose(in);
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#define SZ 500
2157
2158/* We first write the generated file into memory using the
2159 * following helper, then compare to the file on disk and
2160 * only update the later if anything changed */
2161
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002162void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2163 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 char tmp[SZ];
2166 int len;
2167 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 va_start(ap, fmt);
2170 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002171 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 va_end(ap);
2173}
2174
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002175void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002178 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002179 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181 strncpy(buf->p + buf->pos, s, len);
2182 buf->pos += len;
2183}
2184
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002185static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2186{
2187 const char *e = is_vmlinux(m) ?"":".ko";
2188
2189 switch (exp) {
2190 case export_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002191 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002192 "uses GPL-only symbol '%s'\n", m, e, s);
2193 break;
2194 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002195 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002196 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2197 break;
2198 case export_gpl_future:
Jessica Yu93c95e52020-03-06 17:02:05 +01002199 warn("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002200 "uses future GPL-only symbol '%s'\n", m, e, s);
2201 break;
2202 case export_plain:
2203 case export_unused:
2204 case export_unknown:
2205 /* ignore */
2206 break;
2207 }
2208}
2209
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002210static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002211{
2212 const char *e = is_vmlinux(m) ?"":".ko";
2213
2214 switch (exp) {
2215 case export_unused:
2216 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002217 warn("module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002218 "uses symbol '%s' marked UNUSED\n", m, e, s);
2219 break;
2220 default:
2221 /* ignore */
2222 break;
2223 }
2224}
2225
Masahiro Yamada3b415282018-11-23 16:57:23 +09002226static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002227{
2228 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002229 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002230
2231 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002232 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002233 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002234 if (!exp || exp->module == mod) {
2235 if (have_vmlinux && !s->weak) {
Jessica Yu93c95e52020-03-06 17:02:05 +01002236 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2237 "\"%s\" [%s.ko] undefined!\n",
2238 s->name, mod->name);
2239 if (!warn_unresolved)
Masahiro Yamada3b415282018-11-23 16:57:23 +09002240 err = 1;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002241 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002242 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002243 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002244 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002245 if (basename)
2246 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002247 else
2248 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002249
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002250 if (exp->namespace &&
2251 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002252 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2253 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2254 basename, exp->name, exp->namespace);
2255 if (!allow_missing_ns_imports)
2256 err = 1;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002257 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002258 }
2259
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002260 if (!mod->gpl_compatible)
2261 check_for_gpl_usage(exp->export, basename, exp->name);
2262 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002263 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002264
2265 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002266}
2267
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002268static int check_modname_len(struct module *mod)
2269{
2270 const char *mod_name;
2271
2272 mod_name = strrchr(mod->name, '/');
2273 if (mod_name == NULL)
2274 mod_name = mod->name;
2275 else
2276 mod_name++;
2277 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2278 merror("module name is too long [%s.ko]\n", mod->name);
2279 return 1;
2280 }
2281
2282 return 0;
2283}
2284
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002285/**
2286 * Header for the generated file
2287 **/
2288static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002291 /*
2292 * Include build-salt.h after module.h in order to
2293 * inherit the definitions.
2294 */
2295 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 buf_printf(b, "#include <linux/vermagic.h>\n");
2297 buf_printf(b, "#include <linux/compiler.h>\n");
2298 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002299 buf_printf(b, "BUILD_SALT;\n");
2300 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002302 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302304 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002305 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002306 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002308 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (mod->has_cleanup)
2310 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002311 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002313 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 buf_printf(b, "};\n");
2315}
2316
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002317static void add_intree_flag(struct buffer *b, int is_intree)
2318{
2319 if (is_intree)
2320 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2321}
2322
Andi Kleencaf75012018-01-25 15:50:28 -08002323/* Cannot check for assembler */
2324static void add_retpoline(struct buffer *b)
2325{
WANG Chaoe4f35892018-12-11 00:37:25 +08002326 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002327 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2328 buf_printf(b, "#endif\n");
2329}
2330
Trevor Keith5c725132009-09-22 16:43:38 -07002331static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002332{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002333 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002334 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2335}
2336
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002337/**
2338 * Record CRCs for unresolved symbols
2339 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002340static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002343 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 for (s = mod->unres; s; s = s->next) {
2346 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002347 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 s->module = exp->module;
2350 s->crc_valid = exp->crc_valid;
2351 s->crc = exp->crc;
2352 }
2353
2354 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002355 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 buf_printf(b, "\n");
2358 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002359 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002362 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002365 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 s->name, mod->name);
2367 continue;
2368 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302369 if (strlen(s->name) >= MODULE_NAME_LEN) {
2370 merror("too long symbol \"%s\" [%s.ko]\n",
2371 s->name, mod->name);
2372 err = 1;
2373 break;
2374 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002375 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302376 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
2378
2379 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002380
2381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002384static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 int first = 1;
2388
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002389 /* Clear ->seen flag of modules that own symbols needed by this. */
2390 for (s = mod->unres; s; s = s->next)
2391 if (s->module)
2392 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002395 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002397 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (!s->module)
2399 continue;
2400
2401 if (s->module->seen)
2402 continue;
2403
2404 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002405 p = strrchr(s->module->name, '/');
2406 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002407 p++;
2408 else
2409 p = s->module->name;
2410 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 first = 0;
2412 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002413 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002416static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 if (mod->srcversion[0]) {
2419 buf_printf(b, "\n");
2420 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2421 mod->srcversion);
2422 }
2423}
2424
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002425static void write_buf(struct buffer *b, const char *fname)
2426{
2427 FILE *file;
2428
2429 file = fopen(fname, "w");
2430 if (!file) {
2431 perror(fname);
2432 exit(1);
2433 }
2434 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2435 perror(fname);
2436 exit(1);
2437 }
2438 if (fclose(file) != 0) {
2439 perror(fname);
2440 exit(1);
2441 }
2442}
2443
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002444static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
2446 char *tmp;
2447 FILE *file;
2448 struct stat st;
2449
2450 file = fopen(fname, "r");
2451 if (!file)
2452 goto write;
2453
2454 if (fstat(fileno(file), &st) < 0)
2455 goto close_write;
2456
2457 if (st.st_size != b->pos)
2458 goto close_write;
2459
2460 tmp = NOFAIL(malloc(b->pos));
2461 if (fread(tmp, 1, b->pos, file) != b->pos)
2462 goto free_write;
2463
2464 if (memcmp(tmp, b->p, b->pos) != 0)
2465 goto free_write;
2466
2467 free(tmp);
2468 fclose(file);
2469 return;
2470
2471 free_write:
2472 free(tmp);
2473 close_write:
2474 fclose(file);
2475 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002476 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
Ram Paibd5cbce2006-06-08 22:12:53 -07002479/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002480 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002481 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002482static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
2484 unsigned long size, pos = 0;
2485 void *file = grab_file(fname, &size);
2486 char *line;
2487
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002488 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 /* No symbol versions, silently ignore */
2490 return;
2491
2492 while ((line = get_next_line(&pos, file, size))) {
Jessica Yu51900442020-03-11 18:01:20 +01002493 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 unsigned int crc;
2495 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002496 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 if (!(symname = strchr(line, '\t')))
2499 goto fail;
2500 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002501 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 goto fail;
2503 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002504 if (!(export = strchr(modname, '\t')))
2505 goto fail;
2506 *export++ = '\0';
2507 if (!(namespace = strchr(export, '\t')))
2508 goto fail;
2509 *namespace++ = '\0';
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 crc = strtoul(line, &d, 16);
2512 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2513 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002514 mod = find_module(modname);
2515 if (!mod) {
2516 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002518 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 mod->skip = 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002520 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002522 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002523 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002524 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002525 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302527 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return;
2529fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302530 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 fatal("parse error in symbol dump file\n");
2532}
2533
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002534/* For normal builds always dump all symbols.
2535 * For external modules only dump symbols
2536 * that are not read from kernel Module.symvers.
2537 **/
2538static int dump_sym(struct symbol *sym)
2539{
2540 if (!external_module)
2541 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002542 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002543 return 0;
2544 return 1;
2545}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002546
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002547static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 struct buffer buf = { };
2550 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002551 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 int n;
2553
2554 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2555 symbol = symbolhash[n];
2556 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002557 if (dump_sym(symbol)) {
2558 namespace = symbol->namespace;
2559 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2560 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002561 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002562 export_str(symbol->export),
2563 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 symbol = symbol->next;
2566 }
2567 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002568 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002569 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002572static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002573{
2574 struct module *mod;
2575 struct namespace_list *ns;
2576 struct buffer ns_deps_buf = {};
2577
2578 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002579
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002580 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002581 continue;
2582
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002583 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002584
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002585 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2586 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002587
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002588 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002589 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002590
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002591 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002592 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002593}
2594
Masahiro Yamada79247992020-06-01 14:57:07 +09002595struct dump_list {
2596 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002597 const char *file;
2598};
2599
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002600int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
2602 struct module *mod;
2603 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002604 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302605 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002607 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002608 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002609 struct dump_list *dump_read_start = NULL;
2610 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002612 while ((opt = getopt(argc, argv, "ei:mnsT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002613 switch (opt) {
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002614 case 'e':
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002615 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002616 break;
2617 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002618 *dump_read_iter =
2619 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2620 (*dump_read_iter)->file = optarg;
2621 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002622 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002623 case 'm':
2624 modversions = 1;
2625 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302626 case 'n':
2627 ignore_missing_files = 1;
2628 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002629 case 'o':
2630 dump_write = optarg;
2631 break;
2632 case 'a':
2633 all_versions = 1;
2634 break;
2635 case 's':
2636 vmlinux_section_warnings = 0;
2637 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302638 case 'T':
2639 files_source = optarg;
2640 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002641 case 'w':
2642 warn_unresolved = 1;
2643 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302644 case 'E':
2645 sec_mismatch_fatal = 1;
2646 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002647 case 'N':
2648 allow_missing_ns_imports = 1;
2649 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002650 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002651 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002652 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002653 default:
2654 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
2656 }
2657
Masahiro Yamada79247992020-06-01 14:57:07 +09002658 while (dump_read_start) {
2659 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002660
Masahiro Yamada79247992020-06-01 14:57:07 +09002661 read_dump(dump_read_start->file);
2662 tmp = dump_read_start->next;
2663 free(dump_read_start);
2664 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002667 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Rusty Russell712f9b42013-04-04 17:37:38 +10302670 if (files_source)
2671 read_symbols_from_files(files_source);
2672
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002673 /*
2674 * When there's no vmlinux, don't print warnings about
2675 * unresolved symbols (since there'll be too many ;)
2676 */
2677 if (!have_vmlinux)
2678 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2679
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002680 err = 0;
2681
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002682 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302683 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002684
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002685 if (mod->skip)
2686 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 buf.pos = 0;
2689
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002690 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002691 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002694 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002695 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002696 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002697 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002698 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 add_moddevtable(&buf, mod);
2700 add_srcversion(&buf, mod);
2701
2702 sprintf(fname, "%s.mod.c", mod->name);
2703 write_if_changed(&buf, fname);
2704 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002705
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002706 if (missing_namespace_deps)
2707 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (dump_write)
2710 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002711 if (sec_mismatch_count && sec_mismatch_fatal)
Jessica Yu93c95e52020-03-06 17:02:05 +01002712 fatal("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002713 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002714 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002715 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002716
Masahiro Yamada47346e92019-09-24 21:07:40 +09002717 for (s = symbolhash[n]; s; s = s->next) {
2718 /*
2719 * Do not check "vmlinux". This avoids the same warnings
2720 * shown twice, and false-positives for ARCH=um.
2721 */
2722 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2723 continue;
2724
Denis Efremov15bfc232019-08-01 09:06:57 +03002725 if (s->is_static)
2726 warn("\"%s\" [%s] is a static %s\n",
2727 s->name, s->module->name,
2728 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002729 }
2730 }
2731
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002732 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002734 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}