blob: 25c1446055d16bb1f23231c6f57076103824d18c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
Masahiro Yamada5370d4a2020-01-05 00:36:51 +090015#include <elf.h>
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080016#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070018#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103019#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103020#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093021#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020023#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093026static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093028static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
30static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010031/* If we are modposting external module set to 1 */
32static int external_module = 0;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070033/* Only warn about unresolved symbols */
34static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070035/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010036static int sec_mismatch_count = 0;
Masahiro Yamadac7299d92020-12-01 19:34:18 +090037static int sec_mismatch_warn_only = true;
Guenter Roeckeed380f2013-09-23 15:23:54 +093038/* ignore missing files */
39static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010040/* If set to 1, only warn (instead of error) about missing ns imports */
41static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010042
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090043static bool error_occurred;
44
Sam Ravnborgc96fca22006-07-01 11:44:23 +020045enum export {
46 export_plain, export_unused, export_gpl,
Christoph Hellwigf1c3d732021-02-02 13:13:33 +010047 export_unused_gpl, export_unknown
Sam Ravnborgc96fca22006-07-01 11:44:23 +020048};
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);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090083 if (loglevel == LOG_ERROR)
84 error_occurred = true;
Matthew Wilcox2a116652006-10-07 05:35:32 -060085}
86
Rusty Russelld4ef1c32013-04-04 17:37:32 +103087static inline bool strends(const char *str, const char *postfix)
88{
89 if (strlen(str) < strlen(postfix))
90 return false;
91
92 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095void *do_nofail(void *ptr, const char *expr)
96{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010097 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +010098 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +010099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return ptr;
101}
102
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900103char *read_text_file(const char *filename)
104{
105 struct stat st;
106 size_t nbytes;
107 int fd;
108 char *buf;
109
110 fd = open(filename, O_RDONLY);
111 if (fd < 0) {
112 perror(filename);
113 exit(1);
114 }
115
116 if (fstat(fd, &st) < 0) {
117 perror(filename);
118 exit(1);
119 }
120
121 buf = NOFAIL(malloc(st.st_size + 1));
122
123 nbytes = st.st_size;
124
125 while (nbytes) {
126 ssize_t bytes_read;
127
128 bytes_read = read(fd, buf, nbytes);
129 if (bytes_read < 0) {
130 perror(filename);
131 exit(1);
132 }
133
134 nbytes -= bytes_read;
135 }
136 buf[st.st_size] = '\0';
137
138 close(fd);
139
140 return buf;
141}
142
143char *get_line(char **stringp)
144{
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200145 char *orig = *stringp, *next;
146
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900147 /* do not return the unwanted extra line at EOF */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200148 if (!orig || *orig == '\0')
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900149 return NULL;
150
Wolfram Sang6020db52020-07-26 23:44:19 +0200151 /* don't use strsep here, it is not available everywhere */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200152 next = strchr(orig, '\n');
153 if (next)
154 *next++ = '\0';
155
156 *stringp = next;
157
158 return orig;
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static struct module *modules;
163
Masahiro Yamada8b185742018-05-09 18:50:40 +0900164static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct module *mod;
167
168 for (mod = modules; mod; mod = mod->next)
169 if (strcmp(mod->name, modname) == 0)
170 break;
171 return mod;
172}
173
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030174static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct module *mod;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100177
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900178 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 memset(mod, 0, sizeof(*mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* add to list */
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900182 strcpy(mod->name, modname);
Masahiro Yamada4de7b622020-06-01 14:57:30 +0900183 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200184 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 mod->next = modules;
186 modules = mod;
187
Masahiro Yamada858b9372020-06-01 14:57:28 +0900188 if (mod->is_vmlinux)
189 have_vmlinux = 1;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return mod;
192}
193
194/* A hash of all exported symbols,
195 * struct symbol is also used for lists of unresolved symbols */
196
197#define SYMBOL_HASH_SIZE 1024
198
199struct symbol {
200 struct symbol *next;
201 struct module *module;
202 unsigned int crc;
203 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900204 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300206 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700207 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500208 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
211static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
212
213/* This is based on the hash agorithm from gdbm, via tdb */
214static inline unsigned int tdb_hash(const char *name)
215{
216 unsigned value; /* Used to compute the hash value. */
217 unsigned i; /* Used to cycle through random values. */
218
219 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100220 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
222
223 return (1103515243 * value + 12345);
224}
225
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100226/**
227 * Allocate a new symbols for use in the hash of exported symbols or
228 * the list of unresolved symbols per module
229 **/
230static struct symbol *alloc_symbol(const char *name, unsigned int weak,
231 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
234
235 memset(s, 0, sizeof(*s));
236 strcpy(s->name, name);
237 s->weak = weak;
238 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300239 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return s;
241}
242
243/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700244static struct symbol *new_symbol(const char *name, struct module *module,
245 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900250 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
251
252 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100255static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct symbol *s;
258
259 /* For our purposes, .foo matches foo. PPC64 needs this. */
260 if (name[0] == '.')
261 name++;
262
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100263 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (strcmp(s->name, name) == 0)
265 return s;
266 }
267 return NULL;
268}
269
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100270static bool contains_namespace(struct namespace_list *list,
271 const char *namespace)
272{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900273 for (; list; list = list->next)
274 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100275 return true;
276
277 return false;
278}
279
280static void add_namespace(struct namespace_list **list, const char *namespace)
281{
282 struct namespace_list *ns_entry;
283
284 if (!contains_namespace(*list, namespace)) {
285 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
286 strlen(namespace) + 1));
287 strcpy(ns_entry->namespace, namespace);
288 ns_entry->next = *list;
289 *list = ns_entry;
290 }
291}
292
293static bool module_imports_namespace(struct module *module,
294 const char *namespace)
295{
296 return contains_namespace(module->imported_namespaces, namespace);
297}
298
Mathias Krause7a3ee752014-08-27 20:28:53 +0930299static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700300 const char *str;
301 enum export export;
302} export_list[] = {
303 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200304 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700305 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200306 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700307 { .str = "(unknown)", .export = export_unknown },
308};
309
310
311static const char *export_str(enum export ex)
312{
313 return export_list[ex].str;
314}
315
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100316static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700317{
318 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100319
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200320 if (!s)
321 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700322 for (i = 0; export_list[i].export != export_unknown; i++) {
323 if (strcmp(export_list[i].str, s) == 0)
324 return export_list[i].export;
325 }
326 return export_unknown;
327}
328
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900329static void *sym_get_data_by_offset(const struct elf_info *info,
330 unsigned int secindex, unsigned long offset)
Masahiro Yamada6124c042017-09-06 16:19:05 -0700331{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800332 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900333
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900334 if (info->hdr->e_type != ET_REL)
335 offset -= sechdr->sh_addr;
336
337 return (void *)info->hdr + sechdr->sh_offset + offset;
338}
339
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900340static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
341{
342 return sym_get_data_by_offset(info, get_secindex(info, sym),
343 sym->st_value);
344}
345
Masahiro Yamada565587d2020-05-25 14:47:05 +0900346static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
347{
348 return sym_get_data_by_offset(info, info->secindex_strings,
349 sechdr->sh_name);
350}
351
352static const char *sec_name(const struct elf_info *info, int secindex)
353{
354 return sech_name(info, &info->sechdrs[secindex]);
355}
356
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200357#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
358
359static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
360{
361 const char *secname = sec_name(elf, sec);
362
363 if (strstarts(secname, "___ksymtab+"))
364 return export_plain;
365 else if (strstarts(secname, "___ksymtab_unused+"))
366 return export_unused;
367 else if (strstarts(secname, "___ksymtab_gpl+"))
368 return export_gpl;
369 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
370 return export_unused_gpl;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200371 else
372 return export_unknown;
373}
374
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200375static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700376{
377 if (sec == elf->export_sec)
378 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200379 else if (sec == elf->export_unused_sec)
380 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700381 else if (sec == elf->export_gpl_sec)
382 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200383 else if (sec == elf->export_unused_gpl_sec)
384 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700385 else
386 return export_unknown;
387}
388
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900389static const char *namespace_from_kstrtabns(const struct elf_info *info,
390 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100391{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900392 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100393 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100394}
395
Matthias Maennicha2b11182019-10-18 10:31:40 +0100396static void sym_update_namespace(const char *symname, const char *namespace)
397{
398 struct symbol *s = find_symbol(symname);
399
400 /*
401 * That symbol should have been created earlier and thus this is
402 * actually an assertion.
403 */
404 if (!s) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +0900405 error("Could not update namespace(%s) for symbol %s\n",
406 namespace, symname);
Matthias Maennicha2b11182019-10-18 10:31:40 +0100407 return;
408 }
409
410 free(s->namespace);
411 s->namespace =
412 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
413}
414
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100415/**
416 * Add an exported symbol - it may have already been added without a
417 * CRC, in this case just update the CRC
418 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100419static struct symbol *sym_add_exported(const char *name, struct module *mod,
420 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct symbol *s = find_symbol(name);
423
424 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700425 s = new_symbol(name, mod, export);
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900426 } else if (!external_module || s->module->is_vmlinux ||
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900427 s->module == mod) {
428 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
429 mod->name, name, s->module->name,
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900430 s->module->is_vmlinux ? "" : ".ko");
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900431 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900433
434 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700435 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100436 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Masahiro Yamada17436942019-11-15 02:42:24 +0900439static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100440{
441 struct symbol *s = find_symbol(name);
442
Masahiro Yamada17436942019-11-15 02:42:24 +0900443 /*
444 * Ignore stand-alone __crc_*, which might be auto-generated symbols
445 * such as __*_veneer in ARM ELF.
446 */
447 if (!s)
448 return;
449
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100450 s->crc = crc;
451 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900454static void *grab_file(const char *filename, size_t *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930457 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int fd;
459
460 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930461 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930463 if (fstat(fd, &st))
464 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 *size = st.st_size;
467 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930469failed:
470 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (map == MAP_FAILED)
472 return NULL;
473 return map;
474}
475
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900476static void release_file(void *file, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 munmap(file, size);
479}
480
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100481static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100484 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 Elf_Shdr *sechdrs;
486 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200487 const char *secstrings;
488 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 hdr = grab_file(filename, &info->size);
491 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930492 if (ignore_missing_files) {
493 fprintf(stderr, "%s: %s (ignored)\n", filename,
494 strerror(errno));
495 return 0;
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200498 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100501 if (info->size < sizeof(*hdr)) {
502 /* file too small, assume this is an empty .o file */
503 return 0;
504 }
505 /* Is this a valid ELF file? */
506 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
507 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
508 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
509 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
510 /* Not an ELF file - silently ignore it */
511 return 0;
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200514 hdr->e_type = TO_NATIVE(hdr->e_type);
515 hdr->e_machine = TO_NATIVE(hdr->e_machine);
516 hdr->e_version = TO_NATIVE(hdr->e_version);
517 hdr->e_entry = TO_NATIVE(hdr->e_entry);
518 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
519 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
520 hdr->e_flags = TO_NATIVE(hdr->e_flags);
521 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
522 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
523 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
524 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
525 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
526 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 sechdrs = (void *)hdr + hdr->e_shoff;
528 info->sechdrs = sechdrs;
529
Petr Stetiara83710e2007-08-27 12:15:07 +0200530 /* Check if file offset is correct */
531 if (hdr->e_shoff > info->size) {
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900532 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
533 (unsigned long)hdr->e_shoff, filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200534 return 0;
535 }
536
Anders Kaseorg68457562011-05-19 16:55:27 -0600537 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200538 /*
539 * There are more than 64k sections,
540 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200541 */
542 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
543 }
544 else {
545 info->num_sections = hdr->e_shnum;
546 }
547 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600548 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200549 }
550 else {
551 info->secindex_strings = hdr->e_shstrndx;
552 }
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200555 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200556 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
557 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
558 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
559 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
560 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
561 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
562 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
563 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
564 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
565 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200568 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
569 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700570 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900571 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Tejun Heo56fc82c2009-02-06 00:48:02 +0900573 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100574 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
575 "sizeof(*hrd)=%zu\n", filename,
576 (unsigned long)sechdrs[i].sh_offset,
577 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100578 return 0;
579 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700580 secname = secstrings + sechdrs[i].sh_name;
581 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900582 if (nobits)
583 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
585 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700586 } else if (strcmp(secname, "__ksymtab") == 0)
587 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200588 else if (strcmp(secname, "__ksymtab_unused") == 0)
589 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700590 else if (strcmp(secname, "__ksymtab_gpl") == 0)
591 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200592 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
593 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700594
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200595 if (sechdrs[i].sh_type == SHT_SYMTAB) {
596 unsigned int sh_link_idx;
597 symtab_idx = i;
598 info->symtab_start = (void *)hdr +
599 sechdrs[i].sh_offset;
600 info->symtab_stop = (void *)hdr +
601 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600602 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200603 info->strtab = (void *)hdr +
604 sechdrs[sh_link_idx].sh_offset;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200607 /* 32bit section no. table? ("more than 64k sections") */
608 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
609 symtab_shndx_idx = i;
610 info->symtab_shndx_start = (void *)hdr +
611 sechdrs[i].sh_offset;
612 info->symtab_shndx_stop = (void *)hdr +
613 sechdrs[i].sh_offset + sechdrs[i].sh_size;
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100616 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100617 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Fix endianness in symbols */
620 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
621 sym->st_shndx = TO_NATIVE(sym->st_shndx);
622 sym->st_name = TO_NATIVE(sym->st_name);
623 sym->st_value = TO_NATIVE(sym->st_value);
624 sym->st_size = TO_NATIVE(sym->st_size);
625 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200626
627 if (symtab_shndx_idx != ~0U) {
628 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600629 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200630 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600631 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200632 symtab_idx);
633 /* Fix endianness */
634 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
635 p++)
636 *p = TO_NATIVE(*p);
637 }
638
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100639 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100642static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 release_file(info->hdr, info->size);
645}
646
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200647static int ignore_undef_symbol(struct elf_info *info, const char *symname)
648{
649 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900650 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200651 return 1;
652 /* ignore global offset table */
653 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
654 return 1;
655 if (info->hdr->e_machine == EM_PPC)
656 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900657 if (strstarts(symname, "_restgpr_") ||
658 strstarts(symname, "_savegpr_") ||
659 strstarts(symname, "_rest32gpr_") ||
660 strstarts(symname, "_save32gpr_") ||
661 strstarts(symname, "_restvr_") ||
662 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200663 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000664 if (info->hdr->e_machine == EM_PPC64)
665 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900666 if (strstarts(symname, "_restgpr0_") ||
667 strstarts(symname, "_savegpr0_") ||
668 strstarts(symname, "_restvr_") ||
669 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100670 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000671 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200672 /* Do not ignore this symbol */
673 return 0;
674}
675
Masahiro Yamada17436942019-11-15 02:42:24 +0900676static void handle_modversion(const struct module *mod,
677 const struct elf_info *info,
678 const Elf_Sym *sym, const char *symname)
679{
680 unsigned int crc;
681
682 if (sym->st_shndx == SHN_UNDEF) {
683 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900684 symname, mod->name, mod->is_vmlinux ? "" : ".ko");
Masahiro Yamada17436942019-11-15 02:42:24 +0900685 return;
686 }
687
688 if (sym->st_shndx == SHN_ABS) {
689 crc = sym->st_value;
690 } else {
691 unsigned int *crcp;
692
693 /* symbol points to the CRC in the ELF object */
694 crcp = sym_get_data(info, sym);
695 crc = TO_NATIVE(*crcp);
696 }
697 sym_set_crc(symname, crc);
698}
699
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900700static void handle_symbol(struct module *mod, struct elf_info *info,
701 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200703 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900704 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200705
Masahiro Yamada33795762020-06-01 14:57:24 +0900706 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200707 export = export_from_secname(info, get_secindex(info, sym));
708 else
709 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 switch (sym->st_shndx) {
712 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900713 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100714 /* Should warn here, but modpost runs before the linker */
715 } else
716 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 case SHN_UNDEF:
719 /* undefined symbol */
720 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
721 ELF_ST_BIND(sym->st_info) != STB_WEAK)
722 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200723 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (info->hdr->e_machine == EM_SPARC ||
726 info->hdr->e_machine == EM_SPARCV9) {
727 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700728 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100730 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700731 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100732 munged[0] = '_';
733 munged[1] = toupper(munged[1]);
734 symname = munged;
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100737
Rusty Russellb92021b2013-03-15 15:04:17 +1030738 mod->unres = alloc_symbol(symname,
739 ELF_ST_BIND(sym->st_info) == STB_WEAK,
740 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742 default:
743 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900744 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100745 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100746 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900748 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900750 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 mod->has_cleanup = 1;
752 break;
753 }
754}
755
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100756/**
757 * Parse tag=value strings from .modinfo section
758 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759static char *next_string(char *string, unsigned long *secsize)
760{
761 /* Skip non-zero chars */
762 while (string[0]) {
763 string++;
764 if ((*secsize)-- <= 1)
765 return NULL;
766 }
767
768 /* Skip any zero padding. */
769 while (!string[0]) {
770 string++;
771 if ((*secsize)-- <= 1)
772 return NULL;
773 }
774 return string;
775}
776
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900777static char *get_next_modinfo(struct elf_info *info, const char *tag,
778 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 char *p;
781 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900782 char *modinfo = info->modinfo;
783 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900785 if (prev) {
786 size -= prev - modinfo;
787 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 for (p = modinfo; p; p = next_string(p, &size)) {
791 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
792 return p + taglen + 1;
793 }
794 return NULL;
795}
796
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900797static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200798
799{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900800 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200801}
802
Sam Ravnborg93684d32006-02-19 11:53:35 +0100803/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100804 * Test if string s ends in string sub
805 * return 0 if match
806 **/
807static int strrcmp(const char *s, const char *sub)
808{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100809 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100810
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100811 if (!s || !sub)
812 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100813
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100814 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100815 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100816
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100817 if ((slen == 0) || (sublen == 0))
818 return 1;
819
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100820 if (sublen > slen)
821 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100822
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100823 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100824}
825
Sam Ravnborgff13f922008-01-23 19:54:27 +0100826static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
827{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100828 if (sym)
829 return elf->strtab + sym->st_name;
830 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100831 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100832}
833
Sam Ravnborg10668222008-01-13 22:21:31 +0100834/* The pattern is an array of simple patterns.
835 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100836 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100837 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930838 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100839 */
Trevor Keith5c725132009-09-22 16:43:38 -0700840static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100841{
842 const char *p;
843 while (*pat) {
844 p = *pat++;
845 const char *endp = p + strlen(p) - 1;
846
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930847 /* "*foo*" */
848 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300849 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
850 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930851
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930852 free(bare);
853 if (here != NULL)
854 return 1;
855 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100856 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930857 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100858 if (strrcmp(sym, p + 1) == 0)
859 return 1;
860 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100861 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100862 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100863 if (strncmp(sym, p, strlen(p) - 1) == 0)
864 return 1;
865 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100866 /* no wildcards */
867 else {
868 if (strcmp(p, sym) == 0)
869 return 1;
870 }
871 }
872 /* no match */
873 return 0;
874}
875
Sam Ravnborg10668222008-01-13 22:21:31 +0100876/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930877static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200878{
879 ".comment*",
880 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930881 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800882 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000883 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200884 ".mdebug*", /* alpha, score, mips etc. */
885 ".pdr", /* alpha, score, mips etc. */
886 ".stab*",
887 ".note*",
888 ".got*",
889 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400890 ".xt.prop", /* xtensa */
891 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030892 ".arcextmap*", /* arc */
893 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030894 ".cmem*", /* EZchip */
895 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100896 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600897 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200898 NULL
899};
Sam Ravnborg10668222008-01-13 22:21:31 +0100900
Sam Ravnborge241a632008-01-28 20:13:13 +0100901/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400902 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100903 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400904 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100905 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400906static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900907 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100908{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400909 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100910
Anders Kaseorgb614a692009-04-23 16:49:33 -0400911 if (sechdr->sh_type == SHT_PROGBITS &&
912 !(sechdr->sh_flags & SHF_ALLOC) &&
913 !match(sec, section_white_list)) {
914 warn("%s (%s): unexpected non-allocatable section.\n"
915 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
916 "Note that for example <linux/init.h> contains\n"
917 "section definitions for use in .S files.\n\n",
918 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100919 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100920}
921
922
923
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100924#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930925 ".init.setup", ".init.rodata", ".meminit.rodata", \
926 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100927#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930928 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100929
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100930#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930931 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100932#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930933 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100934
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200935#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930936 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
937 ".pci_fixup_enable", ".pci_fixup_resume", \
938 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200939
Paul Gortmakere24f6622013-06-19 19:30:48 -0400940#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
941#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100942
943#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
944#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100945
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930946#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930947#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Thomas Gleixner65538962020-03-09 22:47:17 +0100948 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930949#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400950 ".fixup", ".entry.text", ".exception.text", ".text.*", \
951 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100952
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000953#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000954#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100955
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000956#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000957#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100958
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930959#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
960 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
961
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100962/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930963static const char *const init_data_sections[] =
964 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100965
966/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930967static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100968
969/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930970static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100971 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100972
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930973/* all text sections */
974static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
975
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100976/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930977static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100978
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100979
980/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100981#define DEFAULT_SYMBOL_WHITE_LIST \
982 "*driver", \
983 "*_template", /* scsi uses *_template a lot */ \
984 "*_timer", /* arm uses ops structures named _timer a lot */ \
985 "*_sht", /* scsi also used *_sht to some extent */ \
986 "*_ops", \
987 "*_probe", \
988 "*_probe_one", \
989 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100990
Mathias Krause7a3ee752014-08-27 20:28:53 +0930991static const char *const head_sections[] = { ".head.text*", NULL };
992static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100993 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930994static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100995
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100996enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100997 TEXT_TO_ANY_INIT,
998 DATA_TO_ANY_INIT,
999 TEXT_TO_ANY_EXIT,
1000 DATA_TO_ANY_EXIT,
1001 XXXINIT_TO_SOME_INIT,
1002 XXXEXIT_TO_SOME_EXIT,
1003 ANY_INIT_TO_ANY_EXIT,
1004 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001005 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301006 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001007};
1008
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301009/**
1010 * Describe how to match sections on different criterias:
1011 *
1012 * @fromsec: Array of sections to be matched.
1013 *
1014 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1015 * this array is forbidden (black-list). Can be empty.
1016 *
1017 * @good_tosec: Relocations applied to a section in @fromsec must be
1018 * targetting sections in this array (white-list). Can be empty.
1019 *
1020 * @mismatch: Type of mismatch.
1021 *
1022 * @symbol_white_list: Do not match a relocation to a symbol in this list
1023 * even if it is targetting a section in @bad_to_sec.
1024 *
1025 * @handler: Specific handler to call when a match is found. If NULL,
1026 * default_mismatch_handler() will be called.
1027 *
1028 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001029struct sectioncheck {
1030 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301031 const char *bad_tosec[20];
1032 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001033 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001034 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301035 void (*handler)(const char *modname, struct elf_info *elf,
1036 const struct sectioncheck* const mismatch,
1037 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1038
Sam Ravnborg10668222008-01-13 22:21:31 +01001039};
1040
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301041static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1042 const struct sectioncheck* const mismatch,
1043 Elf_Rela *r, Elf_Sym *sym,
1044 const char *fromsec);
1045
Mathias Krause7a3ee752014-08-27 20:28:53 +09301046static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001047/* Do not reference init/exit code/data from
1048 * normal code and data
1049 */
1050{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001051 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301052 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001053 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001054 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001055},
1056{
1057 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301058 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001059 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001060 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001061},
1062{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001063 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301064 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001065 .mismatch = DATA_TO_ANY_INIT,
1066 .symbol_white_list = {
1067 "*_template", "*_timer", "*_sht", "*_ops",
1068 "*_probe", "*_probe_one", "*_console", NULL
1069 },
1070},
1071{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001072 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301073 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001074 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001075 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001076},
1077{
1078 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301079 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001080 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001081 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001082},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001083/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001084{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001085 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301086 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001087 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001088 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001089},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001090/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001091{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001092 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301093 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001094 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001095 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001096},
1097/* Do not use exit code/data from init code */
1098{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001099 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301100 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001101 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001102 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001103},
1104/* Do not use init code/data from exit code */
1105{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001106 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301107 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001108 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001109 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001110},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001111{
1112 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301113 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001114 .mismatch = ANY_INIT_TO_ANY_EXIT,
1115 .symbol_white_list = { NULL },
1116},
Sam Ravnborg10668222008-01-13 22:21:31 +01001117/* Do not export init/exit functions or data */
1118{
1119 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301120 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001121 .mismatch = EXPORT_TO_INIT_EXIT,
1122 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301123},
1124{
1125 .fromsec = { "__ex_table", NULL },
1126 /* If you're adding any new black-listed sections in here, consider
1127 * adding a special 'printer' for them in scripts/check_extable.
1128 */
1129 .bad_tosec = { ".altinstr_replacement", NULL },
1130 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1131 .mismatch = EXTABLE_TO_NON_TEXT,
1132 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001133}
1134};
1135
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001136static const struct sectioncheck *section_mismatch(
1137 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001138{
1139 int i;
1140 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1141 const struct sectioncheck *check = &sectioncheck[0];
1142
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301143 /*
1144 * The target section could be the SHT_NUL section when we're
1145 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001146 * doesn't make much sense and causes build failures on parisc
1147 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301148 */
1149 if (*tosec == '\0')
1150 return NULL;
1151
Sam Ravnborg10668222008-01-13 22:21:31 +01001152 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301153 if (match(fromsec, check->fromsec)) {
1154 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1155 return check;
1156 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1157 return check;
1158 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001159 check++;
1160 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001161 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001162}
1163
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001164/**
1165 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001166 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001167 * Pattern 1:
1168 * If a module parameter is declared __initdata and permissions=0
1169 * then this is legal despite the warning generated.
1170 * We cannot see value of permissions here, so just ignore
1171 * this pattern.
1172 * The pattern is identified by:
1173 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001174 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001175 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001176 *
Rusty Russell6a841522010-08-11 23:04:16 -06001177 * Pattern 1a:
1178 * module_param_call() ops can refer to __init set function if permissions=0
1179 * The pattern is identified by:
1180 * tosec = .init.text
1181 * fromsec = .data*
1182 * atsym = __param_ops_*
1183 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001184 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001185 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001186 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001187 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001188 * tosec = init or exit section
1189 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001190 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1191 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001192 *
1193 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001194 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001195 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001196 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001197 * Some symbols belong to init section but still it is ok to reference
1198 * these from non-init sections as these symbols don't have any memory
1199 * allocated for them and symbol address and value are same. So even
1200 * if init section is freed, its ok to reference those symbols.
1201 * For ex. symbols marking the init section boundaries.
1202 * This pattern is identified by
1203 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001204 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301205 * Pattern 5:
1206 * GCC may optimize static inlines when fed constant arg(s) resulting
1207 * in functions like cpumask_empty() -- generating an associated symbol
1208 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1209 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1210 * meaningless section warning. May need to add isra symbols too...
1211 * This pattern is identified by
1212 * tosec = init section
1213 * fromsec = text section
1214 * refsymname = *.constprop.*
1215 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001216 * Pattern 6:
1217 * Hide section mismatch warnings for ELF local symbols. The goal
1218 * is to eliminate false positive modpost warnings caused by
1219 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1220 * Autogenerated symbol names bypass modpost's "Pattern 2"
1221 * whitelisting, which relies on pattern-matching against symbol
1222 * names to work. (One situation where gcc can autogenerate ELF
1223 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001224 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001225static int secref_whitelist(const struct sectioncheck *mismatch,
1226 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001227 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001228{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001229 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001230 if (match(tosec, init_data_sections) &&
1231 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001232 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001233 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001234
Rusty Russell6a841522010-08-11 23:04:16 -06001235 /* Check for pattern 1a */
1236 if (strcmp(tosec, ".init.text") == 0 &&
1237 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001238 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001239 return 0;
1240
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001241 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001242 if (match(tosec, init_exit_sections) &&
1243 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001244 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001245 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001246
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001247 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001248 if (match(fromsec, head_sections) &&
1249 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001250 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001251
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001252 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001253 if (match(tosym, linker_symbols))
1254 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001255
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301256 /* Check for pattern 5 */
1257 if (match(fromsec, text_sections) &&
1258 match(tosec, init_sections) &&
1259 match(fromsym, optim_symbols))
1260 return 0;
1261
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001262 /* Check for pattern 6 */
1263 if (strstarts(fromsym, ".L"))
1264 return 0;
1265
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001266 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001267}
1268
Sami Tolvanen5818c682018-10-23 15:15:35 -07001269static inline int is_arm_mapping_symbol(const char *str)
1270{
1271 return str[0] == '$' && strchr("axtd", str[1])
1272 && (str[2] == '\0' || str[2] == '.');
1273}
1274
1275/*
1276 * If there's no name there, ignore it; likewise, ignore it if it's
1277 * one of the magic symbols emitted used by current ARM tools.
1278 *
1279 * Otherwise if find_symbols_between() returns those symbols, they'll
1280 * fail the whitelist tests and cause lots of false alarms ... fixable
1281 * only by merging __exit and __init sections into __text, bloating
1282 * the kernel (which is especially evil on embedded platforms).
1283 */
1284static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1285{
1286 const char *name = elf->strtab + sym->st_name;
1287
1288 if (!name || !strlen(name))
1289 return 0;
1290 return !is_arm_mapping_symbol(name);
1291}
1292
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001293/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001294 * Find symbol based on relocation record info.
1295 * In some cases the symbol supplied is a valid symbol so
1296 * return refsym. If st_name != 0 we assume this is a valid symbol.
1297 * In other cases the symbol needs to be looked up in the symbol table
1298 * based on section and address.
1299 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001300static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001301 Elf_Sym *relsym)
1302{
1303 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001304 Elf_Sym *near = NULL;
1305 Elf64_Sword distance = 20;
1306 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001307 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001308
1309 if (relsym->st_name != 0)
1310 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001311
1312 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001313 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001314 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001315 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001316 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1317 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001318 if (!is_valid_name(elf, sym))
1319 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001320 if (sym->st_value == addr)
1321 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001322 /* Find a symbol nearby - addr are maybe negative */
1323 d = sym->st_value - addr;
1324 if (d < 0)
1325 d = addr - sym->st_value;
1326 if (d < distance) {
1327 distance = d;
1328 near = sym;
1329 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001330 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001331 /* We need a close match */
1332 if (distance < 20)
1333 return near;
1334 else
1335 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001336}
1337
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001338/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001339 * Find symbols before or equal addr and after addr - in the section sec.
1340 * If we find two symbols with equal offset prefer one with a valid name.
1341 * The ELF format may have a better way to detect what type of symbol
1342 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001343 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001344static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1345 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001346{
1347 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001348 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001349 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001350
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001351 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1352 const char *symsec;
1353
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001354 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001355 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001356 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001357 if (strcmp(symsec, sec) != 0)
1358 continue;
David Brownellda68d612007-02-20 13:58:16 -08001359 if (!is_valid_name(elf, sym))
1360 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001361 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001362 if ((addr - sym->st_value) < distance) {
1363 distance = addr - sym->st_value;
1364 near = sym;
1365 } else if ((addr - sym->st_value) == distance) {
1366 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001367 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001368 }
1369 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001370 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001371}
1372
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001373/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001374 * Convert a section name to the function/data attribute
1375 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001376 * .memexitconst => __memconst
1377 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001378 *
1379 * The memory of returned value has been allocated on a heap. The user of this
1380 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001381*/
1382static char *sec2annotation(const char *s)
1383{
1384 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001385 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001386 char *r = p;
1387
1388 *p++ = '_';
1389 *p++ = '_';
1390 if (*s == '.')
1391 s++;
1392 while (*s && *s != '.')
1393 *p++ = *s++;
1394 *p = '\0';
1395 if (*s == '.')
1396 s++;
1397 if (strstr(s, "rodata") != NULL)
1398 strcat(p, "const ");
1399 else if (strstr(s, "data") != NULL)
1400 strcat(p, "data ");
1401 else
1402 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001403 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001404 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001405 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001406 }
1407}
1408
1409static int is_function(Elf_Sym *sym)
1410{
1411 if (sym)
1412 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1413 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001414 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001415}
1416
Randy Dunlap00759c02011-03-15 14:13:47 -07001417static void print_section_list(const char * const list[20])
1418{
1419 const char *const *s = list;
1420
1421 while (*s) {
1422 fprintf(stderr, "%s", *s);
1423 s++;
1424 if (*s)
1425 fprintf(stderr, ", ");
1426 }
1427 fprintf(stderr, "\n");
1428}
1429
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301430static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1431{
1432 switch (is_func) {
1433 case 0: *name = "variable"; *name_p = ""; break;
1434 case 1: *name = "function"; *name_p = "()"; break;
1435 default: *name = "(unknown reference)"; *name_p = ""; break;
1436 }
1437}
1438
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001439/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001440 * Print a warning about a section mismatch.
1441 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001442 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001443 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001444static void report_sec_mismatch(const char *modname,
1445 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001446 const char *fromsec,
1447 unsigned long long fromaddr,
1448 const char *fromsym,
1449 int from_is_func,
1450 const char *tosec, const char *tosym,
1451 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001452{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001453 const char *from, *from_p;
1454 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001455 char *prl_from;
1456 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001457
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001458 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001459
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301460 get_pretty_name(from_is_func, &from, &from_p);
1461 get_pretty_name(to_is_func, &to, &to_p);
1462
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001463 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1464 "to the %s %s:%s%s\n",
1465 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1466 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001467
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001468 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001469 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001470 prl_from = sec2annotation(fromsec);
1471 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001472 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001473 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001474 "the %s %s%s%s.\n"
1475 "This is often because %s lacks a %s\n"
1476 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001477 prl_from, fromsym,
1478 to, prl_to, tosym, to_p,
1479 fromsym, prl_to, tosym);
1480 free(prl_from);
1481 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001482 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001483 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001484 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001485 fprintf(stderr,
1486 "The variable %s references\n"
1487 "the %s %s%s%s\n"
1488 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001489 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001490 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001491 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001492 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001493 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001494 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001495 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001496 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001497 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001498 fprintf(stderr,
1499 "The function %s() references a %s in an exit section.\n"
1500 "Often the %s %s%s has valid usage outside the exit section\n"
1501 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001502 fromsym, to, to, tosym, to_p, prl_to, tosym);
1503 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001504 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001505 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001506 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001507 fprintf(stderr,
1508 "The variable %s references\n"
1509 "the %s %s%s%s\n"
1510 "If the reference is valid then annotate the\n"
1511 "variable with __exit* (see linux/init.h) or "
1512 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001513 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001514 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001515 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001516 break;
1517 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001518 case XXXINIT_TO_SOME_INIT:
1519 case XXXEXIT_TO_SOME_EXIT:
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,
1523 "The %s %s%s%s references\n"
1524 "a %s %s%s%s.\n"
1525 "If %s is only used by %s then\n"
1526 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001527 from, prl_from, fromsym, from_p,
1528 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001529 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001530 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 ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001534 prl_from = sec2annotation(fromsec);
1535 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001536 fprintf(stderr,
1537 "The %s %s%s%s references\n"
1538 "a %s %s%s%s.\n"
1539 "This is often seen when error handling "
1540 "in the init function\n"
1541 "uses functionality in the exit path.\n"
1542 "The fix is often to remove the %sannotation of\n"
1543 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001544 from, prl_from, fromsym, from_p,
1545 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001546 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001547 free(prl_from);
1548 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001549 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001550 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001551 prl_from = sec2annotation(fromsec);
1552 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001553 fprintf(stderr,
1554 "The %s %s%s%s references\n"
1555 "a %s %s%s%s.\n"
1556 "This is often seen when error handling "
1557 "in the exit function\n"
1558 "uses functionality in the init path.\n"
1559 "The fix is often to remove the %sannotation of\n"
1560 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001561 from, prl_from, fromsym, from_p,
1562 to, prl_to, tosym, to_p,
1563 prl_to, tosym, to_p);
1564 free(prl_from);
1565 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001566 break;
1567 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001568 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001569 fprintf(stderr,
1570 "The symbol %s is exported and annotated %s\n"
1571 "Fix this by removing the %sannotation of %s "
1572 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001573 tosym, prl_to, prl_to, tosym);
1574 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001575 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301576 case EXTABLE_TO_NON_TEXT:
1577 fatal("There's a special handler for this mismatch type, "
1578 "we should never get here.");
1579 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001580 }
1581 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001582}
1583
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301584static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1585 const struct sectioncheck* const mismatch,
1586 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1587{
1588 const char *tosec;
1589 Elf_Sym *to;
1590 Elf_Sym *from;
1591 const char *tosym;
1592 const char *fromsym;
1593
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301594 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1595 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301596
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001597 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301598 return;
1599
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301600 tosec = sec_name(elf, get_secindex(elf, sym));
1601 to = find_elf_symbol(elf, r->r_addend, sym);
1602 tosym = sym_name(elf, to);
1603
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301604 /* check whitelist - we may ignore it */
1605 if (secref_whitelist(mismatch,
1606 fromsec, fromsym, tosec, tosym)) {
1607 report_sec_mismatch(modname, mismatch,
1608 fromsec, r->r_offset, fromsym,
1609 is_function(from), tosec, tosym,
1610 is_function(to));
1611 }
1612}
1613
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301614static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1615{
1616 if (section_index > elf->num_sections)
1617 fatal("section_index is outside elf->num_sections!\n");
1618
1619 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1620}
1621
1622/*
1623 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1624 * to know the sizeof(struct exception_table_entry) for the target architecture.
1625 */
1626static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301627static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301628{
1629 /*
1630 * If we're currently checking the second relocation within __ex_table,
1631 * that relocation offset tells us the offsetof(struct
1632 * exception_table_entry, fixup) which is equal to sizeof(struct
1633 * exception_table_entry) divided by two. We use that to our advantage
1634 * since there's no portable way to get that size as every architecture
1635 * seems to go with different sized types. Not pretty but better than
1636 * hard-coding the size for every architecture..
1637 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301638 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301639 extable_entry_size = r->r_offset * 2;
1640}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301641
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301642static inline bool is_extable_fault_address(Elf_Rela *r)
1643{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301644 /*
1645 * extable_entry_size is only discovered after we've handled the
1646 * _second_ relocation in __ex_table, so only abort when we're not
1647 * handling the first reloc and extable_entry_size is zero.
1648 */
1649 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301650 fatal("extable_entry size hasn't been discovered!\n");
1651
1652 return ((r->r_offset == 0) ||
1653 (r->r_offset % extable_entry_size == 0));
1654}
1655
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301656#define is_second_extable_reloc(Start, Cur, Sec) \
1657 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1658
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301659static void report_extable_warnings(const char* modname, struct elf_info* elf,
1660 const struct sectioncheck* const mismatch,
1661 Elf_Rela* r, Elf_Sym* sym,
1662 const char* fromsec, const char* tosec)
1663{
1664 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1665 const char* fromsym_name = sym_name(elf, fromsym);
1666 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1667 const char* tosym_name = sym_name(elf, tosym);
1668 const char* from_pretty_name;
1669 const char* from_pretty_name_p;
1670 const char* to_pretty_name;
1671 const char* to_pretty_name_p;
1672
1673 get_pretty_name(is_function(fromsym),
1674 &from_pretty_name, &from_pretty_name_p);
1675 get_pretty_name(is_function(tosym),
1676 &to_pretty_name, &to_pretty_name_p);
1677
1678 warn("%s(%s+0x%lx): Section mismatch in reference"
1679 " from the %s %s%s to the %s %s:%s%s\n",
1680 modname, fromsec, (long)r->r_offset, from_pretty_name,
1681 fromsym_name, from_pretty_name_p,
1682 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1683
1684 if (!match(tosec, mismatch->bad_tosec) &&
1685 is_executable_section(elf, get_secindex(elf, sym)))
1686 fprintf(stderr,
1687 "The relocation at %s+0x%lx references\n"
1688 "section \"%s\" which is not in the list of\n"
1689 "authorized sections. If you're adding a new section\n"
1690 "and/or if this reference is valid, add \"%s\" to the\n"
1691 "list of authorized sections to jump to on fault.\n"
1692 "This can be achieved by adding \"%s\" to \n"
1693 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1694 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1695}
1696
1697static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1698 const struct sectioncheck* const mismatch,
1699 Elf_Rela* r, Elf_Sym* sym,
1700 const char *fromsec)
1701{
1702 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1703
1704 sec_mismatch_count++;
1705
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001706 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301707
1708 if (match(tosec, mismatch->bad_tosec))
1709 fatal("The relocation at %s+0x%lx references\n"
1710 "section \"%s\" which is black-listed.\n"
1711 "Something is seriously wrong and should be fixed.\n"
1712 "You might get more information about where this is\n"
1713 "coming from by using scripts/check_extable.sh %s\n",
1714 fromsec, (long)r->r_offset, tosec, modname);
1715 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1716 if (is_extable_fault_address(r))
1717 fatal("The relocation at %s+0x%lx references\n"
1718 "section \"%s\" which is not executable, IOW\n"
1719 "it is not possible for the kernel to fault\n"
1720 "at that address. Something is seriously wrong\n"
1721 "and should be fixed.\n",
1722 fromsec, (long)r->r_offset, tosec);
1723 else
1724 fatal("The relocation at %s+0x%lx references\n"
1725 "section \"%s\" which is not executable, IOW\n"
1726 "the kernel will fault if it ever tries to\n"
1727 "jump to it. Something is seriously wrong\n"
1728 "and should be fixed.\n",
1729 fromsec, (long)r->r_offset, tosec);
1730 }
1731}
1732
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001733static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001734 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001735{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001736 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301737 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001738
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001739 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301740 if (mismatch->handler)
1741 mismatch->handler(modname, elf, mismatch,
1742 r, sym, fromsec);
1743 else
1744 default_mismatch_handler(modname, elf, mismatch,
1745 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001746 }
1747}
1748
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001749static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001750 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001751{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001752 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001753}
1754
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001755static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001756{
1757 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001758 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001759
1760 switch (r_typ) {
1761 case R_386_32:
1762 r->r_addend = TO_NATIVE(*location);
1763 break;
1764 case R_386_PC32:
1765 r->r_addend = TO_NATIVE(*location) + 4;
1766 /* For CONFIG_RELOCATABLE=y */
1767 if (elf->hdr->e_type == ET_EXEC)
1768 r->r_addend += r->r_offset;
1769 break;
1770 }
1771 return 0;
1772}
1773
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001774#ifndef R_ARM_CALL
1775#define R_ARM_CALL 28
1776#endif
1777#ifndef R_ARM_JUMP24
1778#define R_ARM_JUMP24 29
1779#endif
1780
David A. Longc9698e52014-02-14 22:41:18 +01001781#ifndef R_ARM_THM_CALL
1782#define R_ARM_THM_CALL 10
1783#endif
1784#ifndef R_ARM_THM_JUMP24
1785#define R_ARM_THM_JUMP24 30
1786#endif
1787#ifndef R_ARM_THM_JUMP19
1788#define R_ARM_THM_JUMP19 51
1789#endif
1790
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001791static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001792{
1793 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1794
1795 switch (r_typ) {
1796 case R_ARM_ABS32:
1797 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001798 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001799 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001800 break;
1801 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001802 case R_ARM_CALL:
1803 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001804 case R_ARM_THM_CALL:
1805 case R_ARM_THM_JUMP24:
1806 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001807 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001808 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001809 sechdr->sh_offset +
1810 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001811 break;
1812 default:
1813 return 1;
1814 }
1815 return 0;
1816}
1817
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001818static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001819{
1820 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001821 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001822 unsigned int inst;
1823
1824 if (r_typ == R_MIPS_HI16)
1825 return 1; /* skip this */
1826 inst = TO_NATIVE(*location);
1827 switch (r_typ) {
1828 case R_MIPS_LO16:
1829 r->r_addend = inst & 0xffff;
1830 break;
1831 case R_MIPS_26:
1832 r->r_addend = (inst & 0x03ffffff) << 2;
1833 break;
1834 case R_MIPS_32:
1835 r->r_addend = inst;
1836 break;
1837 }
1838 return 0;
1839}
1840
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001841static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001842 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001843{
1844 Elf_Sym *sym;
1845 Elf_Rela *rela;
1846 Elf_Rela r;
1847 unsigned int r_sym;
1848 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001849
Sam Ravnborgff13f922008-01-23 19:54:27 +01001850 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001851 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1852
Sam Ravnborgff13f922008-01-23 19:54:27 +01001853 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001854 fromsec += strlen(".rela");
1855 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001856 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001857 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001858
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001859 for (rela = start; rela < stop; rela++) {
1860 r.r_offset = TO_NATIVE(rela->r_offset);
1861#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001862 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001863 unsigned int r_typ;
1864 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1865 r_sym = TO_NATIVE(r_sym);
1866 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1867 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1868 } else {
1869 r.r_info = TO_NATIVE(rela->r_info);
1870 r_sym = ELF_R_SYM(r.r_info);
1871 }
1872#else
1873 r.r_info = TO_NATIVE(rela->r_info);
1874 r_sym = ELF_R_SYM(r.r_info);
1875#endif
1876 r.r_addend = TO_NATIVE(rela->r_addend);
1877 sym = elf->symtab_start + r_sym;
1878 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001879 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001880 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301881 if (is_second_extable_reloc(start, rela, fromsec))
1882 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001883 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001884 }
1885}
1886
1887static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001888 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001889{
1890 Elf_Sym *sym;
1891 Elf_Rel *rel;
1892 Elf_Rela r;
1893 unsigned int r_sym;
1894 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001895
Sam Ravnborgff13f922008-01-23 19:54:27 +01001896 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001897 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1898
Sam Ravnborgff13f922008-01-23 19:54:27 +01001899 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001900 fromsec += strlen(".rel");
1901 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001902 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001903 return;
1904
1905 for (rel = start; rel < stop; rel++) {
1906 r.r_offset = TO_NATIVE(rel->r_offset);
1907#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001908 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001909 unsigned int r_typ;
1910 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1911 r_sym = TO_NATIVE(r_sym);
1912 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1913 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1914 } else {
1915 r.r_info = TO_NATIVE(rel->r_info);
1916 r_sym = ELF_R_SYM(r.r_info);
1917 }
1918#else
1919 r.r_info = TO_NATIVE(rel->r_info);
1920 r_sym = ELF_R_SYM(r.r_info);
1921#endif
1922 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001923 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001924 case EM_386:
1925 if (addend_386_rel(elf, sechdr, &r))
1926 continue;
1927 break;
1928 case EM_ARM:
1929 if (addend_arm_rel(elf, sechdr, &r))
1930 continue;
1931 break;
1932 case EM_MIPS:
1933 if (addend_mips_rel(elf, sechdr, &r))
1934 continue;
1935 break;
1936 }
1937 sym = elf->symtab_start + r_sym;
1938 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001939 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001940 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301941 if (is_second_extable_reloc(start, rel, fromsec))
1942 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001943 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001944 }
1945}
1946
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001947/**
1948 * A module includes a number of sections that are discarded
1949 * either when loaded or when used as built-in.
1950 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001951 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001952 * Likewise for modules used built-in the sections marked __exit
1953 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001954 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001955 * The check_sec_ref() function traverses all relocation records
1956 * to find all references to a section that reference a section that will
1957 * be discarded and warns about it.
1958 **/
1959static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001960 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001961{
1962 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001963 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001964
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001965 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001966 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001967 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001968 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001969 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001970 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001971 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001972 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001973 }
1974}
1975
Andi Kleen7d02b492014-02-08 09:01:12 +01001976static char *remove_dot(char *s)
1977{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301978 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001979
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301980 if (n && s[n]) {
1981 size_t m = strspn(s + n + 1, "0123456789");
1982 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001983 s[n] = 0;
1984 }
1985 return s;
1986}
1987
Masahiro Yamada8b185742018-05-09 18:50:40 +09001988static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 const char *symname;
1991 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001992 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001993 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct module *mod;
1995 struct elf_info info = { };
1996 Elf_Sym *sym;
1997
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001998 if (!parse_elf(&info, modname))
1999 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002001 {
2002 char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002004 /* strip trailing .o */
2005 tmp = NOFAIL(strdup(modname));
2006 tmp[strlen(tmp) - 2] = '\0';
2007 mod = new_module(tmp);
2008 free(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002011 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002012 license = get_modinfo(&info, "license");
2013 if (!license)
Masahiro Yamada1d6cd3922020-12-01 19:34:16 +09002014 error("missing MODULE_LICENSE() in %s\n", modname);
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002015 while (license) {
2016 if (license_is_gpl_compatible(license))
2017 mod->gpl_compatible = 1;
2018 else {
2019 mod->gpl_compatible = 0;
2020 break;
2021 }
2022 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002023 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002024
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002025 namespace = get_modinfo(&info, "import_ns");
2026 while (namespace) {
2027 add_namespace(&mod->imported_namespaces, namespace);
2028 namespace = get_next_modinfo(&info, "import_ns",
2029 namespace);
2030 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002031 }
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002034 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002036 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 handle_moddevtable(mod, &info, sym, symname);
2038 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002039
Matthias Maennich69923202019-10-18 10:31:42 +01002040 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2041 symname = remove_dot(info.strtab + sym->st_name);
2042
Masahiro Yamada17436942019-11-15 02:42:24 +09002043 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002044 if (strstarts(symname, "__kstrtabns_"))
2045 sym_update_namespace(symname + strlen("__kstrtabns_"),
2046 namespace_from_kstrtabns(&info,
2047 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002048
2049 if (strstarts(symname, "__crc_"))
2050 handle_modversion(mod, &info, sym,
2051 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002052 }
2053
Denis Efremov15bfc232019-08-01 09:06:57 +03002054 // check for static EXPORT_SYMBOL_* functions && global vars
2055 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2056 unsigned char bind = ELF_ST_BIND(sym->st_info);
2057
2058 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2059 struct symbol *s =
2060 find_symbol(remove_dot(info.strtab +
2061 sym->st_name));
2062
2063 if (s)
2064 s->is_static = 0;
2065 }
2066 }
2067
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002068 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002070 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002071 version = get_modinfo(&info, "version");
2072 if (version || all_versions)
2073 get_src_version(modname, mod->srcversion,
2074 sizeof(mod->srcversion) - 1);
2075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 parse_elf_finish(&info);
2078
Rusty Russell8c8ef422009-03-31 13:05:34 -06002079 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 * never passed as an argument to an exported function, so
2081 * the automatic versioning doesn't pick it up, but it's really
2082 * important anyhow */
2083 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002084 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Rusty Russell712f9b42013-04-04 17:37:38 +10302087static void read_symbols_from_files(const char *filename)
2088{
2089 FILE *in = stdin;
2090 char fname[PATH_MAX];
2091
2092 if (strcmp(filename, "-") != 0) {
2093 in = fopen(filename, "r");
2094 if (!in)
2095 fatal("Can't open filenames file %s: %m", filename);
2096 }
2097
2098 while (fgets(fname, PATH_MAX, in) != NULL) {
2099 if (strends(fname, "\n"))
2100 fname[strlen(fname)-1] = '\0';
2101 read_symbols(fname);
2102 }
2103
2104 if (in != stdin)
2105 fclose(in);
2106}
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108#define SZ 500
2109
2110/* We first write the generated file into memory using the
2111 * following helper, then compare to the file on disk and
2112 * only update the later if anything changed */
2113
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002114void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2115 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
2117 char tmp[SZ];
2118 int len;
2119 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 va_start(ap, fmt);
2122 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002123 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 va_end(ap);
2125}
2126
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002127void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002130 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002131 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133 strncpy(buf->p + buf->pos, s, len);
2134 buf->pos += len;
2135}
2136
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002137static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2138{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002139 switch (exp) {
2140 case export_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002141 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002142 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002143 break;
2144 case export_unused_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002145 error("GPL-incompatible module %s.ko uses GPL-only symbol marked UNUSED '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002146 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002147 break;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002148 case export_plain:
2149 case export_unused:
2150 case export_unknown:
2151 /* ignore */
2152 break;
2153 }
2154}
2155
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002156static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002157{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002158 switch (exp) {
2159 case export_unused:
2160 case export_unused_gpl:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002161 warn("module %s.ko uses symbol '%s' marked UNUSED\n",
2162 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002163 break;
2164 default:
2165 /* ignore */
2166 break;
2167 }
2168}
2169
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002170static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002171{
2172 struct symbol *s, *exp;
2173
2174 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002175 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002176 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002177 if (!exp || exp->module == mod) {
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002178 if (have_vmlinux && !s->weak)
Jessica Yu93c95e52020-03-06 17:02:05 +01002179 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2180 "\"%s\" [%s.ko] undefined!\n",
2181 s->name, mod->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002182 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002183 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002184 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002185 if (basename)
2186 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002187 else
2188 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002189
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002190 if (exp->namespace &&
2191 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002192 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2193 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2194 basename, exp->name, exp->namespace);
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002195 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002196 }
2197
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002198 if (!mod->gpl_compatible)
2199 check_for_gpl_usage(exp->export, basename, exp->name);
2200 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002201 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002202}
2203
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002204static void check_modname_len(struct module *mod)
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002205{
2206 const char *mod_name;
2207
2208 mod_name = strrchr(mod->name, '/');
2209 if (mod_name == NULL)
2210 mod_name = mod->name;
2211 else
2212 mod_name++;
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002213 if (strlen(mod_name) >= MODULE_NAME_LEN)
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002214 error("module name is too long [%s.ko]\n", mod->name);
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002215}
2216
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002217/**
2218 * Header for the generated file
2219 **/
2220static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002223 /*
2224 * Include build-salt.h after module.h in order to
2225 * inherit the definitions.
2226 */
Leon Romanovsky51161bf2020-04-19 18:55:06 +03002227 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002228 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 buf_printf(b, "#include <linux/vermagic.h>\n");
2230 buf_printf(b, "#include <linux/compiler.h>\n");
2231 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002232 buf_printf(b, "BUILD_SALT;\n");
2233 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002235 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302237 buf_printf(b, "__visible struct module __this_module\n");
Joe Perches33def842020-10-21 19:36:07 -07002238 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002239 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002241 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (mod->has_cleanup)
2243 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002244 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002246 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 buf_printf(b, "};\n");
2248}
2249
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002250static void add_intree_flag(struct buffer *b, int is_intree)
2251{
2252 if (is_intree)
2253 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2254}
2255
Andi Kleencaf75012018-01-25 15:50:28 -08002256/* Cannot check for assembler */
2257static void add_retpoline(struct buffer *b)
2258{
WANG Chaoe4f35892018-12-11 00:37:25 +08002259 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002260 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2261 buf_printf(b, "#endif\n");
2262}
2263
Trevor Keith5c725132009-09-22 16:43:38 -07002264static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002265{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002266 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002267 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2268}
2269
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002270/**
2271 * Record CRCs for unresolved symbols
2272 **/
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002273static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 struct symbol *s, *exp;
2276
2277 for (s = mod->unres; s; s = s->next) {
2278 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002279 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 s->module = exp->module;
2282 s->crc_valid = exp->crc_valid;
2283 s->crc = exp->crc;
2284 }
2285
2286 if (!modversions)
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002287 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 buf_printf(b, "\n");
2290 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Joe Perches33def842020-10-21 19:36:07 -07002291 buf_printf(b, "__used __section(\"__versions\") = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002294 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002297 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 s->name, mod->name);
2299 continue;
2300 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302301 if (strlen(s->name) >= MODULE_NAME_LEN) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002302 error("too long symbol \"%s\" [%s.ko]\n",
2303 s->name, mod->name);
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302304 break;
2305 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002306 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302307 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309
2310 buf_printf(b, "};\n");
2311}
2312
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002313static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 int first = 1;
2317
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002318 /* Clear ->seen flag of modules that own symbols needed by this. */
2319 for (s = mod->unres; s; s = s->next)
2320 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002321 s->module->seen = s->module->is_vmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002324 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002326 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (!s->module)
2328 continue;
2329
2330 if (s->module->seen)
2331 continue;
2332
2333 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002334 p = strrchr(s->module->name, '/');
2335 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002336 p++;
2337 else
2338 p = s->module->name;
2339 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 first = 0;
2341 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002342 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002345static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 if (mod->srcversion[0]) {
2348 buf_printf(b, "\n");
2349 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2350 mod->srcversion);
2351 }
2352}
2353
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002354static void write_buf(struct buffer *b, const char *fname)
2355{
2356 FILE *file;
2357
2358 file = fopen(fname, "w");
2359 if (!file) {
2360 perror(fname);
2361 exit(1);
2362 }
2363 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2364 perror(fname);
2365 exit(1);
2366 }
2367 if (fclose(file) != 0) {
2368 perror(fname);
2369 exit(1);
2370 }
2371}
2372
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002373static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
2375 char *tmp;
2376 FILE *file;
2377 struct stat st;
2378
2379 file = fopen(fname, "r");
2380 if (!file)
2381 goto write;
2382
2383 if (fstat(fileno(file), &st) < 0)
2384 goto close_write;
2385
2386 if (st.st_size != b->pos)
2387 goto close_write;
2388
2389 tmp = NOFAIL(malloc(b->pos));
2390 if (fread(tmp, 1, b->pos, file) != b->pos)
2391 goto free_write;
2392
2393 if (memcmp(tmp, b->p, b->pos) != 0)
2394 goto free_write;
2395
2396 free(tmp);
2397 fclose(file);
2398 return;
2399
2400 free_write:
2401 free(tmp);
2402 close_write:
2403 fclose(file);
2404 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002405 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
Ram Paibd5cbce2006-06-08 22:12:53 -07002408/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002409 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002410 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002411static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002413 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002415 buf = read_text_file(fname);
2416 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 /* No symbol versions, silently ignore */
2418 return;
2419
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002420 pos = buf;
2421
2422 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002423 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 unsigned int crc;
2425 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002426 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 if (!(symname = strchr(line, '\t')))
2429 goto fail;
2430 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002431 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 goto fail;
2433 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002434 if (!(export = strchr(modname, '\t')))
2435 goto fail;
2436 *export++ = '\0';
2437 if (!(namespace = strchr(export, '\t')))
2438 goto fail;
2439 *namespace++ = '\0';
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 crc = strtoul(line, &d, 16);
2442 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2443 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002444 mod = find_module(modname);
2445 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002446 mod = new_module(modname);
Masahiro Yamada52c34162020-06-01 14:57:05 +09002447 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002449 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002450 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002451 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002452 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002454 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return;
2456fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002457 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 fatal("parse error in symbol dump file\n");
2459}
2460
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002461/* For normal builds always dump all symbols.
2462 * For external modules only dump symbols
2463 * that are not read from kernel Module.symvers.
2464 **/
2465static int dump_sym(struct symbol *sym)
2466{
2467 if (!external_module)
2468 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002469 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002470 return 0;
2471 return 1;
2472}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002473
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002474static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 struct buffer buf = { };
2477 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002478 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 int n;
2480
2481 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2482 symbol = symbolhash[n];
2483 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002484 if (dump_sym(symbol)) {
2485 namespace = symbol->namespace;
2486 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2487 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002488 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002489 export_str(symbol->export),
2490 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 symbol = symbol->next;
2493 }
2494 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002495 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002496 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002499static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002500{
2501 struct module *mod;
2502 struct namespace_list *ns;
2503 struct buffer ns_deps_buf = {};
2504
2505 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002506
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002507 if (mod->from_dump || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002508 continue;
2509
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002510 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002511
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002512 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2513 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002514
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002515 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002516 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002517
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002518 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002519 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002520}
2521
Masahiro Yamada79247992020-06-01 14:57:07 +09002522struct dump_list {
2523 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002524 const char *file;
2525};
2526
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002527int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
2529 struct module *mod;
2530 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002531 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302532 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 int opt;
Denis Efremov15bfc232019-08-01 09:06:57 +03002534 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002535 struct dump_list *dump_read_start = NULL;
2536 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002538 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002539 switch (opt) {
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002540 case 'e':
2541 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002542 break;
2543 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002544 *dump_read_iter =
2545 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2546 (*dump_read_iter)->file = optarg;
2547 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002548 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002549 case 'm':
2550 modversions = 1;
2551 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302552 case 'n':
2553 ignore_missing_files = 1;
2554 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002555 case 'o':
2556 dump_write = optarg;
2557 break;
2558 case 'a':
2559 all_versions = 1;
2560 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302561 case 'T':
2562 files_source = optarg;
2563 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002564 case 'w':
2565 warn_unresolved = 1;
2566 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302567 case 'E':
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002568 sec_mismatch_warn_only = false;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302569 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002570 case 'N':
2571 allow_missing_ns_imports = 1;
2572 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002573 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002574 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002575 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002576 default:
2577 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
2579 }
2580
Masahiro Yamada79247992020-06-01 14:57:07 +09002581 while (dump_read_start) {
2582 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002583
Masahiro Yamada79247992020-06-01 14:57:07 +09002584 read_dump(dump_read_start->file);
2585 tmp = dump_read_start->next;
2586 free(dump_read_start);
2587 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002590 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Rusty Russell712f9b42013-04-04 17:37:38 +10302593 if (files_source)
2594 read_symbols_from_files(files_source);
2595
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002596 /*
2597 * When there's no vmlinux, don't print warnings about
2598 * unresolved symbols (since there'll be too many ;)
2599 */
2600 if (!have_vmlinux)
2601 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2602
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002603 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302604 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002605
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002606 if (mod->is_vmlinux || mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002607 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 buf.pos = 0;
2610
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002611 check_modname_len(mod);
2612 check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002615 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002616 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002617 add_staging_flag(&buf, mod->name);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002618 add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002619 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 add_moddevtable(&buf, mod);
2621 add_srcversion(&buf, mod);
2622
2623 sprintf(fname, "%s.mod.c", mod->name);
2624 write_if_changed(&buf, fname);
2625 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002626
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002627 if (missing_namespace_deps)
2628 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (dump_write)
2631 write_dump(dump_write);
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002632 if (sec_mismatch_count && !sec_mismatch_warn_only)
2633 error("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002634 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002635 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002636 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002637
Masahiro Yamada47346e92019-09-24 21:07:40 +09002638 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002639 if (s->is_static)
Quentin Perretb9ed8472020-12-01 16:52:22 +00002640 error("\"%s\" [%s] is a static %s\n",
2641 s->name, s->module->name,
2642 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002643 }
2644 }
2645
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002646 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002648 return error_occurred ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649}