blob: a702ce3c1a77a9fa8e4008e42100e8f168d73fc8 [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;
Will McVicker4b9c11a2020-11-19 13:46:37 -080033#define MODULE_SCMVERSION_SIZE 64
34static char module_scmversion[MODULE_SCMVERSION_SIZE];
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070035/* Only warn about unresolved symbols */
36static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070037/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010038static int sec_mismatch_count = 0;
Masahiro Yamadac7299d92020-12-01 19:34:18 +090039static int sec_mismatch_warn_only = true;
Guenter Roeckeed380f2013-09-23 15:23:54 +093040/* ignore missing files */
41static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010042/* If set to 1, only warn (instead of error) about missing ns imports */
43static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010044
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090045static bool error_occurred;
46
Sam Ravnborgc96fca22006-07-01 11:44:23 +020047enum export {
48 export_plain, export_unused, export_gpl,
49 export_unused_gpl, export_gpl_future, export_unknown
50};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080052/* In kernel, this size is defined in linux/module.h;
53 * here we use Elf_Addr instead of long for covering cross-compile
54 */
55
56#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
57
Jessica Yu93c95e52020-03-06 17:02:05 +010058void __attribute__((format(printf, 2, 3)))
59modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 va_list arglist;
62
Jessica Yu93c95e52020-03-06 17:02:05 +010063 switch (loglevel) {
64 case LOG_WARN:
65 fprintf(stderr, "WARNING: ");
66 break;
67 case LOG_ERROR:
68 fprintf(stderr, "ERROR: ");
69 break;
70 case LOG_FATAL:
71 fprintf(stderr, "FATAL: ");
72 break;
73 default: /* invalid loglevel, ignore */
74 break;
75 }
76
77 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 va_start(arglist, fmt);
80 vfprintf(stderr, fmt, arglist);
81 va_end(arglist);
82
Jessica Yu93c95e52020-03-06 17:02:05 +010083 if (loglevel == LOG_FATAL)
84 exit(1);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090085 if (loglevel == LOG_ERROR)
86 error_occurred = true;
Matthew Wilcox2a116652006-10-07 05:35:32 -060087}
88
Rusty Russelld4ef1c32013-04-04 17:37:32 +103089static inline bool strends(const char *str, const char *postfix)
90{
91 if (strlen(str) < strlen(postfix))
92 return false;
93
94 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097void *do_nofail(void *ptr, const char *expr)
98{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010099 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100100 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return ptr;
103}
104
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900105char *read_text_file(const char *filename)
106{
107 struct stat st;
108 size_t nbytes;
109 int fd;
110 char *buf;
111
112 fd = open(filename, O_RDONLY);
113 if (fd < 0) {
114 perror(filename);
115 exit(1);
116 }
117
118 if (fstat(fd, &st) < 0) {
119 perror(filename);
120 exit(1);
121 }
122
123 buf = NOFAIL(malloc(st.st_size + 1));
124
125 nbytes = st.st_size;
126
127 while (nbytes) {
128 ssize_t bytes_read;
129
130 bytes_read = read(fd, buf, nbytes);
131 if (bytes_read < 0) {
132 perror(filename);
133 exit(1);
134 }
135
136 nbytes -= bytes_read;
137 }
138 buf[st.st_size] = '\0';
139
140 close(fd);
141
142 return buf;
143}
144
145char *get_line(char **stringp)
146{
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200147 char *orig = *stringp, *next;
148
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900149 /* do not return the unwanted extra line at EOF */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200150 if (!orig || *orig == '\0')
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900151 return NULL;
152
Wolfram Sang6020db52020-07-26 23:44:19 +0200153 /* don't use strsep here, it is not available everywhere */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200154 next = strchr(orig, '\n');
155 if (next)
156 *next++ = '\0';
157
158 *stringp = next;
159
160 return orig;
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static struct module *modules;
165
Masahiro Yamada8b185742018-05-09 18:50:40 +0900166static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct module *mod;
169
170 for (mod = modules; mod; mod = mod->next)
171 if (strcmp(mod->name, modname) == 0)
172 break;
173 return mod;
174}
175
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030176static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct module *mod;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100179
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900180 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 memset(mod, 0, sizeof(*mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* add to list */
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900184 strcpy(mod->name, modname);
Masahiro Yamada4de7b622020-06-01 14:57:30 +0900185 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200186 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 mod->next = modules;
188 modules = mod;
189
Masahiro Yamada858b9372020-06-01 14:57:28 +0900190 if (mod->is_vmlinux)
191 have_vmlinux = 1;
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return mod;
194}
195
196/* A hash of all exported symbols,
197 * struct symbol is also used for lists of unresolved symbols */
198
199#define SYMBOL_HASH_SIZE 1024
200
201struct symbol {
202 struct symbol *next;
203 struct module *module;
204 unsigned int crc;
205 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900206 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300208 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700209 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500210 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
213static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
214
215/* This is based on the hash agorithm from gdbm, via tdb */
216static inline unsigned int tdb_hash(const char *name)
217{
218 unsigned value; /* Used to compute the hash value. */
219 unsigned i; /* Used to cycle through random values. */
220
221 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100222 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
224
225 return (1103515243 * value + 12345);
226}
227
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100228/**
229 * Allocate a new symbols for use in the hash of exported symbols or
230 * the list of unresolved symbols per module
231 **/
232static struct symbol *alloc_symbol(const char *name, unsigned int weak,
233 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
236
237 memset(s, 0, sizeof(*s));
238 strcpy(s->name, name);
239 s->weak = weak;
240 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300241 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return s;
243}
244
245/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700246static struct symbol *new_symbol(const char *name, struct module *module,
247 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900252 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
253
254 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100257static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct symbol *s;
260
261 /* For our purposes, .foo matches foo. PPC64 needs this. */
262 if (name[0] == '.')
263 name++;
264
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100265 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (strcmp(s->name, name) == 0)
267 return s;
268 }
269 return NULL;
270}
271
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100272static bool contains_namespace(struct namespace_list *list,
273 const char *namespace)
274{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900275 for (; list; list = list->next)
276 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100277 return true;
278
279 return false;
280}
281
282static void add_namespace(struct namespace_list **list, const char *namespace)
283{
284 struct namespace_list *ns_entry;
285
286 if (!contains_namespace(*list, namespace)) {
287 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
288 strlen(namespace) + 1));
289 strcpy(ns_entry->namespace, namespace);
290 ns_entry->next = *list;
291 *list = ns_entry;
292 }
293}
294
295static bool module_imports_namespace(struct module *module,
296 const char *namespace)
297{
298 return contains_namespace(module->imported_namespaces, namespace);
299}
300
Mathias Krause7a3ee752014-08-27 20:28:53 +0930301static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700302 const char *str;
303 enum export export;
304} export_list[] = {
305 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200306 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700307 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200308 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700309 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
310 { .str = "(unknown)", .export = export_unknown },
311};
312
313
314static const char *export_str(enum export ex)
315{
316 return export_list[ex].str;
317}
318
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100319static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700320{
321 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100322
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200323 if (!s)
324 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700325 for (i = 0; export_list[i].export != export_unknown; i++) {
326 if (strcmp(export_list[i].str, s) == 0)
327 return export_list[i].export;
328 }
329 return export_unknown;
330}
331
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900332static void *sym_get_data_by_offset(const struct elf_info *info,
333 unsigned int secindex, unsigned long offset)
Masahiro Yamada6124c042017-09-06 16:19:05 -0700334{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800335 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900336
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900337 if (info->hdr->e_type != ET_REL)
338 offset -= sechdr->sh_addr;
339
340 return (void *)info->hdr + sechdr->sh_offset + offset;
341}
342
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900343static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
344{
345 return sym_get_data_by_offset(info, get_secindex(info, sym),
346 sym->st_value);
347}
348
Masahiro Yamada565587d2020-05-25 14:47:05 +0900349static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
350{
351 return sym_get_data_by_offset(info, info->secindex_strings,
352 sechdr->sh_name);
353}
354
355static const char *sec_name(const struct elf_info *info, int secindex)
356{
357 return sech_name(info, &info->sechdrs[secindex]);
358}
359
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200360#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
361
362static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
363{
364 const char *secname = sec_name(elf, sec);
365
366 if (strstarts(secname, "___ksymtab+"))
367 return export_plain;
368 else if (strstarts(secname, "___ksymtab_unused+"))
369 return export_unused;
370 else if (strstarts(secname, "___ksymtab_gpl+"))
371 return export_gpl;
372 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
373 return export_unused_gpl;
374 else if (strstarts(secname, "___ksymtab_gpl_future+"))
375 return export_gpl_future;
376 else
377 return export_unknown;
378}
379
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200380static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700381{
382 if (sec == elf->export_sec)
383 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200384 else if (sec == elf->export_unused_sec)
385 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700386 else if (sec == elf->export_gpl_sec)
387 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200388 else if (sec == elf->export_unused_gpl_sec)
389 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700390 else if (sec == elf->export_gpl_future_sec)
391 return export_gpl_future;
392 else
393 return export_unknown;
394}
395
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900396static const char *namespace_from_kstrtabns(const struct elf_info *info,
397 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100398{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900399 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100400 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100401}
402
Matthias Maennicha2b11182019-10-18 10:31:40 +0100403static void sym_update_namespace(const char *symname, const char *namespace)
404{
405 struct symbol *s = find_symbol(symname);
406
407 /*
408 * That symbol should have been created earlier and thus this is
409 * actually an assertion.
410 */
411 if (!s) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +0900412 error("Could not update namespace(%s) for symbol %s\n",
413 namespace, symname);
Matthias Maennicha2b11182019-10-18 10:31:40 +0100414 return;
415 }
416
417 free(s->namespace);
418 s->namespace =
419 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
420}
421
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100422/**
423 * Add an exported symbol - it may have already been added without a
424 * CRC, in this case just update the CRC
425 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100426static struct symbol *sym_add_exported(const char *name, struct module *mod,
427 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct symbol *s = find_symbol(name);
430
431 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700432 s = new_symbol(name, mod, export);
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900433 } else if (!external_module || s->module->is_vmlinux ||
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900434 s->module == mod) {
Quentin Perret479488f2020-11-24 14:40:13 +0000435 fatal("%s: '%s' exported twice. Previous export was in %s%s\n",
436 mod->name, name, s->module->name,
437 s->module->is_vmlinux ? "" : ".ko");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900439
440 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700441 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100442 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Masahiro Yamada17436942019-11-15 02:42:24 +0900445static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100446{
447 struct symbol *s = find_symbol(name);
448
Masahiro Yamada17436942019-11-15 02:42:24 +0900449 /*
450 * Ignore stand-alone __crc_*, which might be auto-generated symbols
451 * such as __*_veneer in ARM ELF.
452 */
453 if (!s)
454 return;
455
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100456 s->crc = crc;
457 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900460static void *grab_file(const char *filename, size_t *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930463 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int fd;
465
466 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930467 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930469 if (fstat(fd, &st))
470 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 *size = st.st_size;
473 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930475failed:
476 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (map == MAP_FAILED)
478 return NULL;
479 return map;
480}
481
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900482static void release_file(void *file, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 munmap(file, size);
485}
486
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100487static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100490 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 Elf_Shdr *sechdrs;
492 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200493 const char *secstrings;
494 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 hdr = grab_file(filename, &info->size);
497 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930498 if (ignore_missing_files) {
499 fprintf(stderr, "%s: %s (ignored)\n", filename,
500 strerror(errno));
501 return 0;
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200504 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100507 if (info->size < sizeof(*hdr)) {
508 /* file too small, assume this is an empty .o file */
509 return 0;
510 }
511 /* Is this a valid ELF file? */
512 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
513 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
514 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
515 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
516 /* Not an ELF file - silently ignore it */
517 return 0;
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200520 hdr->e_type = TO_NATIVE(hdr->e_type);
521 hdr->e_machine = TO_NATIVE(hdr->e_machine);
522 hdr->e_version = TO_NATIVE(hdr->e_version);
523 hdr->e_entry = TO_NATIVE(hdr->e_entry);
524 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
525 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
526 hdr->e_flags = TO_NATIVE(hdr->e_flags);
527 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
528 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
529 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
530 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
531 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
532 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 sechdrs = (void *)hdr + hdr->e_shoff;
534 info->sechdrs = sechdrs;
535
Petr Stetiara83710e2007-08-27 12:15:07 +0200536 /* Check if file offset is correct */
537 if (hdr->e_shoff > info->size) {
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900538 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
539 (unsigned long)hdr->e_shoff, filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200540 return 0;
541 }
542
Anders Kaseorg68457562011-05-19 16:55:27 -0600543 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200544 /*
545 * There are more than 64k sections,
546 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200547 */
548 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
549 }
550 else {
551 info->num_sections = hdr->e_shnum;
552 }
553 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600554 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200555 }
556 else {
557 info->secindex_strings = hdr->e_shstrndx;
558 }
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200561 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200562 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
563 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
564 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
565 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
566 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
567 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
568 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
569 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
570 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
571 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200574 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
575 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700576 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900577 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Tejun Heo56fc82c2009-02-06 00:48:02 +0900579 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100580 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
581 "sizeof(*hrd)=%zu\n", filename,
582 (unsigned long)sechdrs[i].sh_offset,
583 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100584 return 0;
585 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700586 secname = secstrings + sechdrs[i].sh_name;
587 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900588 if (nobits)
589 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
591 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700592 } else if (strcmp(secname, "__ksymtab") == 0)
593 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200594 else if (strcmp(secname, "__ksymtab_unused") == 0)
595 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700596 else if (strcmp(secname, "__ksymtab_gpl") == 0)
597 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200598 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
599 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700600 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
601 info->export_gpl_future_sec = i;
602
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200603 if (sechdrs[i].sh_type == SHT_SYMTAB) {
604 unsigned int sh_link_idx;
605 symtab_idx = i;
606 info->symtab_start = (void *)hdr +
607 sechdrs[i].sh_offset;
608 info->symtab_stop = (void *)hdr +
609 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600610 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200611 info->strtab = (void *)hdr +
612 sechdrs[sh_link_idx].sh_offset;
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200615 /* 32bit section no. table? ("more than 64k sections") */
616 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
617 symtab_shndx_idx = i;
618 info->symtab_shndx_start = (void *)hdr +
619 sechdrs[i].sh_offset;
620 info->symtab_shndx_stop = (void *)hdr +
621 sechdrs[i].sh_offset + sechdrs[i].sh_size;
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100624 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100625 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Fix endianness in symbols */
628 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
629 sym->st_shndx = TO_NATIVE(sym->st_shndx);
630 sym->st_name = TO_NATIVE(sym->st_name);
631 sym->st_value = TO_NATIVE(sym->st_value);
632 sym->st_size = TO_NATIVE(sym->st_size);
633 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200634
635 if (symtab_shndx_idx != ~0U) {
636 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600637 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200638 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600639 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200640 symtab_idx);
641 /* Fix endianness */
642 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
643 p++)
644 *p = TO_NATIVE(*p);
645 }
646
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100647 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100650static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 release_file(info->hdr, info->size);
653}
654
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200655static int ignore_undef_symbol(struct elf_info *info, const char *symname)
656{
657 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900658 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200659 return 1;
660 /* ignore global offset table */
661 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
662 return 1;
663 if (info->hdr->e_machine == EM_PPC)
664 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900665 if (strstarts(symname, "_restgpr_") ||
666 strstarts(symname, "_savegpr_") ||
667 strstarts(symname, "_rest32gpr_") ||
668 strstarts(symname, "_save32gpr_") ||
669 strstarts(symname, "_restvr_") ||
670 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200671 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000672 if (info->hdr->e_machine == EM_PPC64)
673 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900674 if (strstarts(symname, "_restgpr0_") ||
675 strstarts(symname, "_savegpr0_") ||
676 strstarts(symname, "_restvr_") ||
677 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100678 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000679 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200680 /* Do not ignore this symbol */
681 return 0;
682}
683
Masahiro Yamada17436942019-11-15 02:42:24 +0900684static void handle_modversion(const struct module *mod,
685 const struct elf_info *info,
686 const Elf_Sym *sym, const char *symname)
687{
688 unsigned int crc;
689
690 if (sym->st_shndx == SHN_UNDEF) {
691 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900692 symname, mod->name, mod->is_vmlinux ? "" : ".ko");
Masahiro Yamada17436942019-11-15 02:42:24 +0900693 return;
694 }
695
696 if (sym->st_shndx == SHN_ABS) {
697 crc = sym->st_value;
698 } else {
699 unsigned int *crcp;
700
701 /* symbol points to the CRC in the ELF object */
702 crcp = sym_get_data(info, sym);
703 crc = TO_NATIVE(*crcp);
704 }
705 sym_set_crc(symname, crc);
706}
707
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900708static void handle_symbol(struct module *mod, struct elf_info *info,
709 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200711 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900712 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200713
Masahiro Yamada33795762020-06-01 14:57:24 +0900714 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200715 export = export_from_secname(info, get_secindex(info, sym));
716 else
717 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 switch (sym->st_shndx) {
720 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900721 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100722 /* Should warn here, but modpost runs before the linker */
723 } else
724 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 case SHN_UNDEF:
727 /* undefined symbol */
728 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
729 ELF_ST_BIND(sym->st_info) != STB_WEAK)
730 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200731 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (info->hdr->e_machine == EM_SPARC ||
734 info->hdr->e_machine == EM_SPARCV9) {
735 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700736 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100738 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700739 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100740 munged[0] = '_';
741 munged[1] = toupper(munged[1]);
742 symname = munged;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100745
Rusty Russellb92021b2013-03-15 15:04:17 +1030746 mod->unres = alloc_symbol(symname,
747 ELF_ST_BIND(sym->st_info) == STB_WEAK,
748 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750 default:
751 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900752 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100753 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100754 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900756 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900758 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 mod->has_cleanup = 1;
760 break;
761 }
762}
763
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100764/**
765 * Parse tag=value strings from .modinfo section
766 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static char *next_string(char *string, unsigned long *secsize)
768{
769 /* Skip non-zero chars */
770 while (string[0]) {
771 string++;
772 if ((*secsize)-- <= 1)
773 return NULL;
774 }
775
776 /* Skip any zero padding. */
777 while (!string[0]) {
778 string++;
779 if ((*secsize)-- <= 1)
780 return NULL;
781 }
782 return string;
783}
784
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900785static char *get_next_modinfo(struct elf_info *info, const char *tag,
786 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 char *p;
789 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900790 char *modinfo = info->modinfo;
791 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900793 if (prev) {
794 size -= prev - modinfo;
795 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200796 }
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 for (p = modinfo; p; p = next_string(p, &size)) {
799 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
800 return p + taglen + 1;
801 }
802 return NULL;
803}
804
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900805static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200806
807{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900808 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200809}
810
Sam Ravnborg93684d32006-02-19 11:53:35 +0100811/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100812 * Test if string s ends in string sub
813 * return 0 if match
814 **/
815static int strrcmp(const char *s, const char *sub)
816{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100817 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100818
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100819 if (!s || !sub)
820 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100821
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100822 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100823 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100824
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100825 if ((slen == 0) || (sublen == 0))
826 return 1;
827
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100828 if (sublen > slen)
829 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100830
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100831 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100832}
833
Sam Ravnborgff13f922008-01-23 19:54:27 +0100834static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
835{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100836 if (sym)
837 return elf->strtab + sym->st_name;
838 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100839 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100840}
841
Sam Ravnborg10668222008-01-13 22:21:31 +0100842/* The pattern is an array of simple patterns.
843 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100844 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100845 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930846 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100847 */
Trevor Keith5c725132009-09-22 16:43:38 -0700848static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100849{
850 const char *p;
851 while (*pat) {
852 p = *pat++;
853 const char *endp = p + strlen(p) - 1;
854
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930855 /* "*foo*" */
856 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300857 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
858 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930859
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930860 free(bare);
861 if (here != NULL)
862 return 1;
863 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100864 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930865 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100866 if (strrcmp(sym, p + 1) == 0)
867 return 1;
868 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100869 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100870 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100871 if (strncmp(sym, p, strlen(p) - 1) == 0)
872 return 1;
873 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100874 /* no wildcards */
875 else {
876 if (strcmp(p, sym) == 0)
877 return 1;
878 }
879 }
880 /* no match */
881 return 0;
882}
883
Sam Ravnborg10668222008-01-13 22:21:31 +0100884/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930885static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200886{
887 ".comment*",
888 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930889 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800890 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000891 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200892 ".mdebug*", /* alpha, score, mips etc. */
893 ".pdr", /* alpha, score, mips etc. */
894 ".stab*",
895 ".note*",
896 ".got*",
897 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400898 ".xt.prop", /* xtensa */
899 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030900 ".arcextmap*", /* arc */
901 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030902 ".cmem*", /* EZchip */
903 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100904 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600905 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200906 NULL
907};
Sam Ravnborg10668222008-01-13 22:21:31 +0100908
Sam Ravnborge241a632008-01-28 20:13:13 +0100909/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400910 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100911 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400912 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100913 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400914static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900915 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100916{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400917 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100918
Anders Kaseorgb614a692009-04-23 16:49:33 -0400919 if (sechdr->sh_type == SHT_PROGBITS &&
920 !(sechdr->sh_flags & SHF_ALLOC) &&
921 !match(sec, section_white_list)) {
922 warn("%s (%s): unexpected non-allocatable section.\n"
923 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
924 "Note that for example <linux/init.h> contains\n"
925 "section definitions for use in .S files.\n\n",
926 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100927 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100928}
929
930
931
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100932#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930933 ".init.setup", ".init.rodata", ".meminit.rodata", \
934 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100935#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930936 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100937
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100938#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930939 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100940#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930941 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100942
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200943#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930944 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
945 ".pci_fixup_enable", ".pci_fixup_resume", \
946 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200947
Paul Gortmakere24f6622013-06-19 19:30:48 -0400948#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
949#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100950
951#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
952#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100953
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930954#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930955#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Thomas Gleixner65538962020-03-09 22:47:17 +0100956 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930957#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400958 ".fixup", ".entry.text", ".exception.text", ".text.*", \
959 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100960
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000961#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000962#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100963
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000964#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000965#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100966
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930967#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
968 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
969
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100970/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930971static const char *const init_data_sections[] =
972 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100973
974/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930975static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100976
977/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930978static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100979 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100980
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930981/* all text sections */
982static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
983
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100984/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930985static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100986
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100987
988/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100989#define DEFAULT_SYMBOL_WHITE_LIST \
990 "*driver", \
991 "*_template", /* scsi uses *_template a lot */ \
992 "*_timer", /* arm uses ops structures named _timer a lot */ \
993 "*_sht", /* scsi also used *_sht to some extent */ \
994 "*_ops", \
995 "*_probe", \
996 "*_probe_one", \
997 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100998
Mathias Krause7a3ee752014-08-27 20:28:53 +0930999static const char *const head_sections[] = { ".head.text*", NULL };
1000static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001001 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301002static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001003
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001004enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001005 TEXT_TO_ANY_INIT,
1006 DATA_TO_ANY_INIT,
1007 TEXT_TO_ANY_EXIT,
1008 DATA_TO_ANY_EXIT,
1009 XXXINIT_TO_SOME_INIT,
1010 XXXEXIT_TO_SOME_EXIT,
1011 ANY_INIT_TO_ANY_EXIT,
1012 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001013 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301014 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001015};
1016
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301017/**
1018 * Describe how to match sections on different criterias:
1019 *
1020 * @fromsec: Array of sections to be matched.
1021 *
1022 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1023 * this array is forbidden (black-list). Can be empty.
1024 *
1025 * @good_tosec: Relocations applied to a section in @fromsec must be
1026 * targetting sections in this array (white-list). Can be empty.
1027 *
1028 * @mismatch: Type of mismatch.
1029 *
1030 * @symbol_white_list: Do not match a relocation to a symbol in this list
1031 * even if it is targetting a section in @bad_to_sec.
1032 *
1033 * @handler: Specific handler to call when a match is found. If NULL,
1034 * default_mismatch_handler() will be called.
1035 *
1036 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001037struct sectioncheck {
1038 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301039 const char *bad_tosec[20];
1040 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001041 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001042 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301043 void (*handler)(const char *modname, struct elf_info *elf,
1044 const struct sectioncheck* const mismatch,
1045 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1046
Sam Ravnborg10668222008-01-13 22:21:31 +01001047};
1048
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301049static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1050 const struct sectioncheck* const mismatch,
1051 Elf_Rela *r, Elf_Sym *sym,
1052 const char *fromsec);
1053
Mathias Krause7a3ee752014-08-27 20:28:53 +09301054static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001055/* Do not reference init/exit code/data from
1056 * normal code and data
1057 */
1058{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001059 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301060 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001061 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001062 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001063},
1064{
1065 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301066 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001067 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001068 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001069},
1070{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001071 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301072 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001073 .mismatch = DATA_TO_ANY_INIT,
1074 .symbol_white_list = {
1075 "*_template", "*_timer", "*_sht", "*_ops",
1076 "*_probe", "*_probe_one", "*_console", NULL
1077 },
1078},
1079{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001080 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301081 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001082 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001083 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001084},
1085{
1086 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301087 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001088 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001089 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001090},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001091/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001092{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001093 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301094 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001095 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001096 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001097},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001098/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001099{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001100 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301101 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001102 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001103 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001104},
1105/* Do not use exit code/data from init code */
1106{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001107 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301108 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001109 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001110 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001111},
1112/* Do not use init code/data from exit code */
1113{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001114 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301115 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001116 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001117 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001118},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001119{
1120 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301121 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001122 .mismatch = ANY_INIT_TO_ANY_EXIT,
1123 .symbol_white_list = { NULL },
1124},
Sam Ravnborg10668222008-01-13 22:21:31 +01001125/* Do not export init/exit functions or data */
1126{
1127 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301128 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001129 .mismatch = EXPORT_TO_INIT_EXIT,
1130 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301131},
1132{
1133 .fromsec = { "__ex_table", NULL },
1134 /* If you're adding any new black-listed sections in here, consider
1135 * adding a special 'printer' for them in scripts/check_extable.
1136 */
1137 .bad_tosec = { ".altinstr_replacement", NULL },
1138 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1139 .mismatch = EXTABLE_TO_NON_TEXT,
1140 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001141}
1142};
1143
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001144static const struct sectioncheck *section_mismatch(
1145 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001146{
1147 int i;
1148 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1149 const struct sectioncheck *check = &sectioncheck[0];
1150
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301151 /*
1152 * The target section could be the SHT_NUL section when we're
1153 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001154 * doesn't make much sense and causes build failures on parisc
1155 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301156 */
1157 if (*tosec == '\0')
1158 return NULL;
1159
Sam Ravnborg10668222008-01-13 22:21:31 +01001160 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301161 if (match(fromsec, check->fromsec)) {
1162 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1163 return check;
1164 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1165 return check;
1166 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001167 check++;
1168 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001169 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001170}
1171
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001172/**
1173 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001174 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001175 * Pattern 1:
1176 * If a module parameter is declared __initdata and permissions=0
1177 * then this is legal despite the warning generated.
1178 * We cannot see value of permissions here, so just ignore
1179 * this pattern.
1180 * The pattern is identified by:
1181 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001182 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001183 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001184 *
Rusty Russell6a841522010-08-11 23:04:16 -06001185 * Pattern 1a:
1186 * module_param_call() ops can refer to __init set function if permissions=0
1187 * The pattern is identified by:
1188 * tosec = .init.text
1189 * fromsec = .data*
1190 * atsym = __param_ops_*
1191 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001192 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001193 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001194 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001195 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001196 * tosec = init or exit section
1197 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001198 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1199 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001200 *
1201 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001202 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001203 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001204 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001205 * Some symbols belong to init section but still it is ok to reference
1206 * these from non-init sections as these symbols don't have any memory
1207 * allocated for them and symbol address and value are same. So even
1208 * if init section is freed, its ok to reference those symbols.
1209 * For ex. symbols marking the init section boundaries.
1210 * This pattern is identified by
1211 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001212 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301213 * Pattern 5:
1214 * GCC may optimize static inlines when fed constant arg(s) resulting
1215 * in functions like cpumask_empty() -- generating an associated symbol
1216 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1217 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1218 * meaningless section warning. May need to add isra symbols too...
1219 * This pattern is identified by
1220 * tosec = init section
1221 * fromsec = text section
1222 * refsymname = *.constprop.*
1223 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001224 * Pattern 6:
1225 * Hide section mismatch warnings for ELF local symbols. The goal
1226 * is to eliminate false positive modpost warnings caused by
1227 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1228 * Autogenerated symbol names bypass modpost's "Pattern 2"
1229 * whitelisting, which relies on pattern-matching against symbol
1230 * names to work. (One situation where gcc can autogenerate ELF
1231 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001232 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001233static int secref_whitelist(const struct sectioncheck *mismatch,
1234 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001235 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001236{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001237 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001238 if (match(tosec, init_data_sections) &&
1239 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001240 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001241 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001242
Rusty Russell6a841522010-08-11 23:04:16 -06001243 /* Check for pattern 1a */
1244 if (strcmp(tosec, ".init.text") == 0 &&
1245 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001246 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001247 return 0;
1248
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001249 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001250 if (match(tosec, init_exit_sections) &&
1251 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001252 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001253 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001254
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001255 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001256 if (match(fromsec, head_sections) &&
1257 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001258 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001259
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001260 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001261 if (match(tosym, linker_symbols))
1262 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001263
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301264 /* Check for pattern 5 */
1265 if (match(fromsec, text_sections) &&
1266 match(tosec, init_sections) &&
1267 match(fromsym, optim_symbols))
1268 return 0;
1269
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001270 /* Check for pattern 6 */
1271 if (strstarts(fromsym, ".L"))
1272 return 0;
1273
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001274 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001275}
1276
Sami Tolvanen5818c682018-10-23 15:15:35 -07001277static inline int is_arm_mapping_symbol(const char *str)
1278{
1279 return str[0] == '$' && strchr("axtd", str[1])
1280 && (str[2] == '\0' || str[2] == '.');
1281}
1282
1283/*
1284 * If there's no name there, ignore it; likewise, ignore it if it's
1285 * one of the magic symbols emitted used by current ARM tools.
1286 *
1287 * Otherwise if find_symbols_between() returns those symbols, they'll
1288 * fail the whitelist tests and cause lots of false alarms ... fixable
1289 * only by merging __exit and __init sections into __text, bloating
1290 * the kernel (which is especially evil on embedded platforms).
1291 */
1292static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1293{
1294 const char *name = elf->strtab + sym->st_name;
1295
1296 if (!name || !strlen(name))
1297 return 0;
1298 return !is_arm_mapping_symbol(name);
1299}
1300
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001301/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001302 * Find symbol based on relocation record info.
1303 * In some cases the symbol supplied is a valid symbol so
1304 * return refsym. If st_name != 0 we assume this is a valid symbol.
1305 * In other cases the symbol needs to be looked up in the symbol table
1306 * based on section and address.
1307 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001308static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001309 Elf_Sym *relsym)
1310{
1311 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001312 Elf_Sym *near = NULL;
1313 Elf64_Sword distance = 20;
1314 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001315 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001316
1317 if (relsym->st_name != 0)
1318 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001319
1320 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001321 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001322 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001323 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001324 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1325 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001326 if (!is_valid_name(elf, sym))
1327 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001328 if (sym->st_value == addr)
1329 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001330 /* Find a symbol nearby - addr are maybe negative */
1331 d = sym->st_value - addr;
1332 if (d < 0)
1333 d = addr - sym->st_value;
1334 if (d < distance) {
1335 distance = d;
1336 near = sym;
1337 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001338 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001339 /* We need a close match */
1340 if (distance < 20)
1341 return near;
1342 else
1343 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001344}
1345
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001346/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001347 * Find symbols before or equal addr and after addr - in the section sec.
1348 * If we find two symbols with equal offset prefer one with a valid name.
1349 * The ELF format may have a better way to detect what type of symbol
1350 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001351 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001352static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1353 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001354{
1355 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001356 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001357 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001358
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001359 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1360 const char *symsec;
1361
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001362 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001363 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001364 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001365 if (strcmp(symsec, sec) != 0)
1366 continue;
David Brownellda68d612007-02-20 13:58:16 -08001367 if (!is_valid_name(elf, sym))
1368 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001369 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001370 if ((addr - sym->st_value) < distance) {
1371 distance = addr - sym->st_value;
1372 near = sym;
1373 } else if ((addr - sym->st_value) == distance) {
1374 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001375 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001376 }
1377 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001378 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001379}
1380
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001381/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001382 * Convert a section name to the function/data attribute
1383 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001384 * .memexitconst => __memconst
1385 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001386 *
1387 * The memory of returned value has been allocated on a heap. The user of this
1388 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001389*/
1390static char *sec2annotation(const char *s)
1391{
1392 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001393 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001394 char *r = p;
1395
1396 *p++ = '_';
1397 *p++ = '_';
1398 if (*s == '.')
1399 s++;
1400 while (*s && *s != '.')
1401 *p++ = *s++;
1402 *p = '\0';
1403 if (*s == '.')
1404 s++;
1405 if (strstr(s, "rodata") != NULL)
1406 strcat(p, "const ");
1407 else if (strstr(s, "data") != NULL)
1408 strcat(p, "data ");
1409 else
1410 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001411 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001412 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001413 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001414 }
1415}
1416
1417static int is_function(Elf_Sym *sym)
1418{
1419 if (sym)
1420 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1421 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001422 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001423}
1424
Randy Dunlap00759c02011-03-15 14:13:47 -07001425static void print_section_list(const char * const list[20])
1426{
1427 const char *const *s = list;
1428
1429 while (*s) {
1430 fprintf(stderr, "%s", *s);
1431 s++;
1432 if (*s)
1433 fprintf(stderr, ", ");
1434 }
1435 fprintf(stderr, "\n");
1436}
1437
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301438static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1439{
1440 switch (is_func) {
1441 case 0: *name = "variable"; *name_p = ""; break;
1442 case 1: *name = "function"; *name_p = "()"; break;
1443 default: *name = "(unknown reference)"; *name_p = ""; break;
1444 }
1445}
1446
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001447/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001448 * Print a warning about a section mismatch.
1449 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001450 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001451 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001452static void report_sec_mismatch(const char *modname,
1453 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001454 const char *fromsec,
1455 unsigned long long fromaddr,
1456 const char *fromsym,
1457 int from_is_func,
1458 const char *tosec, const char *tosym,
1459 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001460{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001461 const char *from, *from_p;
1462 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001463 char *prl_from;
1464 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001465
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001466 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001467
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301468 get_pretty_name(from_is_func, &from, &from_p);
1469 get_pretty_name(to_is_func, &to, &to_p);
1470
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001471 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1472 "to the %s %s:%s%s\n",
1473 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1474 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001475
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001476 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001477 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001478 prl_from = sec2annotation(fromsec);
1479 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001480 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001481 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001482 "the %s %s%s%s.\n"
1483 "This is often because %s lacks a %s\n"
1484 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001485 prl_from, fromsym,
1486 to, prl_to, tosym, to_p,
1487 fromsym, prl_to, tosym);
1488 free(prl_from);
1489 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001490 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001491 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001492 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001493 fprintf(stderr,
1494 "The variable %s references\n"
1495 "the %s %s%s%s\n"
1496 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001497 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001498 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001499 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001500 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001501 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001502 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001503 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001504 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001505 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001506 fprintf(stderr,
1507 "The function %s() references a %s in an exit section.\n"
1508 "Often the %s %s%s has valid usage outside the exit section\n"
1509 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001510 fromsym, to, to, tosym, to_p, prl_to, tosym);
1511 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001512 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001513 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001514 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001515 fprintf(stderr,
1516 "The variable %s references\n"
1517 "the %s %s%s%s\n"
1518 "If the reference is valid then annotate the\n"
1519 "variable with __exit* (see linux/init.h) or "
1520 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001521 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001522 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001523 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001524 break;
1525 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001526 case XXXINIT_TO_SOME_INIT:
1527 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001528 prl_from = sec2annotation(fromsec);
1529 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001530 fprintf(stderr,
1531 "The %s %s%s%s references\n"
1532 "a %s %s%s%s.\n"
1533 "If %s is only used by %s then\n"
1534 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001535 from, prl_from, fromsym, from_p,
1536 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001537 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001538 free(prl_from);
1539 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001540 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001541 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001542 prl_from = sec2annotation(fromsec);
1543 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001544 fprintf(stderr,
1545 "The %s %s%s%s references\n"
1546 "a %s %s%s%s.\n"
1547 "This is often seen when error handling "
1548 "in the init function\n"
1549 "uses functionality in the exit path.\n"
1550 "The fix is often to remove the %sannotation of\n"
1551 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001552 from, prl_from, fromsym, from_p,
1553 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001554 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001555 free(prl_from);
1556 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001557 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001558 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001559 prl_from = sec2annotation(fromsec);
1560 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001561 fprintf(stderr,
1562 "The %s %s%s%s references\n"
1563 "a %s %s%s%s.\n"
1564 "This is often seen when error handling "
1565 "in the exit function\n"
1566 "uses functionality in the init path.\n"
1567 "The fix is often to remove the %sannotation of\n"
1568 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001569 from, prl_from, fromsym, from_p,
1570 to, prl_to, tosym, to_p,
1571 prl_to, tosym, to_p);
1572 free(prl_from);
1573 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001574 break;
1575 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001576 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001577 fprintf(stderr,
1578 "The symbol %s is exported and annotated %s\n"
1579 "Fix this by removing the %sannotation of %s "
1580 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001581 tosym, prl_to, prl_to, tosym);
1582 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001583 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301584 case EXTABLE_TO_NON_TEXT:
1585 fatal("There's a special handler for this mismatch type, "
1586 "we should never get here.");
1587 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001588 }
1589 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001590}
1591
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301592static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1593 const struct sectioncheck* const mismatch,
1594 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1595{
1596 const char *tosec;
1597 Elf_Sym *to;
1598 Elf_Sym *from;
1599 const char *tosym;
1600 const char *fromsym;
1601
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301602 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1603 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301604
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001605 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301606 return;
1607
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301608 tosec = sec_name(elf, get_secindex(elf, sym));
1609 to = find_elf_symbol(elf, r->r_addend, sym);
1610 tosym = sym_name(elf, to);
1611
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301612 /* check whitelist - we may ignore it */
1613 if (secref_whitelist(mismatch,
1614 fromsec, fromsym, tosec, tosym)) {
1615 report_sec_mismatch(modname, mismatch,
1616 fromsec, r->r_offset, fromsym,
1617 is_function(from), tosec, tosym,
1618 is_function(to));
1619 }
1620}
1621
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301622static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1623{
1624 if (section_index > elf->num_sections)
1625 fatal("section_index is outside elf->num_sections!\n");
1626
1627 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1628}
1629
1630/*
1631 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1632 * to know the sizeof(struct exception_table_entry) for the target architecture.
1633 */
1634static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301635static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301636{
1637 /*
1638 * If we're currently checking the second relocation within __ex_table,
1639 * that relocation offset tells us the offsetof(struct
1640 * exception_table_entry, fixup) which is equal to sizeof(struct
1641 * exception_table_entry) divided by two. We use that to our advantage
1642 * since there's no portable way to get that size as every architecture
1643 * seems to go with different sized types. Not pretty but better than
1644 * hard-coding the size for every architecture..
1645 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301646 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301647 extable_entry_size = r->r_offset * 2;
1648}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301649
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301650static inline bool is_extable_fault_address(Elf_Rela *r)
1651{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301652 /*
1653 * extable_entry_size is only discovered after we've handled the
1654 * _second_ relocation in __ex_table, so only abort when we're not
1655 * handling the first reloc and extable_entry_size is zero.
1656 */
1657 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301658 fatal("extable_entry size hasn't been discovered!\n");
1659
1660 return ((r->r_offset == 0) ||
1661 (r->r_offset % extable_entry_size == 0));
1662}
1663
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301664#define is_second_extable_reloc(Start, Cur, Sec) \
1665 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1666
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301667static void report_extable_warnings(const char* modname, struct elf_info* elf,
1668 const struct sectioncheck* const mismatch,
1669 Elf_Rela* r, Elf_Sym* sym,
1670 const char* fromsec, const char* tosec)
1671{
1672 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1673 const char* fromsym_name = sym_name(elf, fromsym);
1674 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1675 const char* tosym_name = sym_name(elf, tosym);
1676 const char* from_pretty_name;
1677 const char* from_pretty_name_p;
1678 const char* to_pretty_name;
1679 const char* to_pretty_name_p;
1680
1681 get_pretty_name(is_function(fromsym),
1682 &from_pretty_name, &from_pretty_name_p);
1683 get_pretty_name(is_function(tosym),
1684 &to_pretty_name, &to_pretty_name_p);
1685
1686 warn("%s(%s+0x%lx): Section mismatch in reference"
1687 " from the %s %s%s to the %s %s:%s%s\n",
1688 modname, fromsec, (long)r->r_offset, from_pretty_name,
1689 fromsym_name, from_pretty_name_p,
1690 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1691
1692 if (!match(tosec, mismatch->bad_tosec) &&
1693 is_executable_section(elf, get_secindex(elf, sym)))
1694 fprintf(stderr,
1695 "The relocation at %s+0x%lx references\n"
1696 "section \"%s\" which is not in the list of\n"
1697 "authorized sections. If you're adding a new section\n"
1698 "and/or if this reference is valid, add \"%s\" to the\n"
1699 "list of authorized sections to jump to on fault.\n"
1700 "This can be achieved by adding \"%s\" to \n"
1701 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1702 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1703}
1704
1705static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1706 const struct sectioncheck* const mismatch,
1707 Elf_Rela* r, Elf_Sym* sym,
1708 const char *fromsec)
1709{
1710 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1711
1712 sec_mismatch_count++;
1713
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001714 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301715
1716 if (match(tosec, mismatch->bad_tosec))
1717 fatal("The relocation at %s+0x%lx references\n"
1718 "section \"%s\" which is black-listed.\n"
1719 "Something is seriously wrong and should be fixed.\n"
1720 "You might get more information about where this is\n"
1721 "coming from by using scripts/check_extable.sh %s\n",
1722 fromsec, (long)r->r_offset, tosec, modname);
1723 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1724 if (is_extable_fault_address(r))
1725 fatal("The relocation at %s+0x%lx references\n"
1726 "section \"%s\" which is not executable, IOW\n"
1727 "it is not possible for the kernel to fault\n"
1728 "at that address. Something is seriously wrong\n"
1729 "and should be fixed.\n",
1730 fromsec, (long)r->r_offset, tosec);
1731 else
1732 fatal("The relocation at %s+0x%lx references\n"
1733 "section \"%s\" which is not executable, IOW\n"
1734 "the kernel will fault if it ever tries to\n"
1735 "jump to it. Something is seriously wrong\n"
1736 "and should be fixed.\n",
1737 fromsec, (long)r->r_offset, tosec);
1738 }
1739}
1740
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001741static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001742 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001743{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001744 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301745 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001746
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001747 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301748 if (mismatch->handler)
1749 mismatch->handler(modname, elf, mismatch,
1750 r, sym, fromsec);
1751 else
1752 default_mismatch_handler(modname, elf, mismatch,
1753 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001754 }
1755}
1756
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001757static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001758 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001759{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001760 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001761}
1762
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001763static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001764{
1765 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001766 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001767
1768 switch (r_typ) {
1769 case R_386_32:
1770 r->r_addend = TO_NATIVE(*location);
1771 break;
1772 case R_386_PC32:
1773 r->r_addend = TO_NATIVE(*location) + 4;
1774 /* For CONFIG_RELOCATABLE=y */
1775 if (elf->hdr->e_type == ET_EXEC)
1776 r->r_addend += r->r_offset;
1777 break;
1778 }
1779 return 0;
1780}
1781
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001782#ifndef R_ARM_CALL
1783#define R_ARM_CALL 28
1784#endif
1785#ifndef R_ARM_JUMP24
1786#define R_ARM_JUMP24 29
1787#endif
1788
David A. Longc9698e52014-02-14 22:41:18 +01001789#ifndef R_ARM_THM_CALL
1790#define R_ARM_THM_CALL 10
1791#endif
1792#ifndef R_ARM_THM_JUMP24
1793#define R_ARM_THM_JUMP24 30
1794#endif
1795#ifndef R_ARM_THM_JUMP19
1796#define R_ARM_THM_JUMP19 51
1797#endif
1798
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001799static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001800{
1801 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1802
1803 switch (r_typ) {
1804 case R_ARM_ABS32:
1805 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001806 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001807 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001808 break;
1809 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001810 case R_ARM_CALL:
1811 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001812 case R_ARM_THM_CALL:
1813 case R_ARM_THM_JUMP24:
1814 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001815 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001816 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001817 sechdr->sh_offset +
1818 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001819 break;
1820 default:
1821 return 1;
1822 }
1823 return 0;
1824}
1825
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001826static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001827{
1828 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001829 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001830 unsigned int inst;
1831
1832 if (r_typ == R_MIPS_HI16)
1833 return 1; /* skip this */
1834 inst = TO_NATIVE(*location);
1835 switch (r_typ) {
1836 case R_MIPS_LO16:
1837 r->r_addend = inst & 0xffff;
1838 break;
1839 case R_MIPS_26:
1840 r->r_addend = (inst & 0x03ffffff) << 2;
1841 break;
1842 case R_MIPS_32:
1843 r->r_addend = inst;
1844 break;
1845 }
1846 return 0;
1847}
1848
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001849static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001850 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001851{
1852 Elf_Sym *sym;
1853 Elf_Rela *rela;
1854 Elf_Rela r;
1855 unsigned int r_sym;
1856 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001857
Sam Ravnborgff13f922008-01-23 19:54:27 +01001858 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001859 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1860
Sam Ravnborgff13f922008-01-23 19:54:27 +01001861 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001862 fromsec += strlen(".rela");
1863 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001864 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001865 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001866
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001867 for (rela = start; rela < stop; rela++) {
1868 r.r_offset = TO_NATIVE(rela->r_offset);
1869#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001870 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001871 unsigned int r_typ;
1872 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1873 r_sym = TO_NATIVE(r_sym);
1874 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1875 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1876 } else {
1877 r.r_info = TO_NATIVE(rela->r_info);
1878 r_sym = ELF_R_SYM(r.r_info);
1879 }
1880#else
1881 r.r_info = TO_NATIVE(rela->r_info);
1882 r_sym = ELF_R_SYM(r.r_info);
1883#endif
1884 r.r_addend = TO_NATIVE(rela->r_addend);
1885 sym = elf->symtab_start + r_sym;
1886 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001887 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001888 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301889 if (is_second_extable_reloc(start, rela, fromsec))
1890 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001891 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001892 }
1893}
1894
1895static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001896 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001897{
1898 Elf_Sym *sym;
1899 Elf_Rel *rel;
1900 Elf_Rela r;
1901 unsigned int r_sym;
1902 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001903
Sam Ravnborgff13f922008-01-23 19:54:27 +01001904 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001905 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1906
Sam Ravnborgff13f922008-01-23 19:54:27 +01001907 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001908 fromsec += strlen(".rel");
1909 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001910 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001911 return;
1912
1913 for (rel = start; rel < stop; rel++) {
1914 r.r_offset = TO_NATIVE(rel->r_offset);
1915#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001916 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001917 unsigned int r_typ;
1918 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1919 r_sym = TO_NATIVE(r_sym);
1920 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1921 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1922 } else {
1923 r.r_info = TO_NATIVE(rel->r_info);
1924 r_sym = ELF_R_SYM(r.r_info);
1925 }
1926#else
1927 r.r_info = TO_NATIVE(rel->r_info);
1928 r_sym = ELF_R_SYM(r.r_info);
1929#endif
1930 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001931 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001932 case EM_386:
1933 if (addend_386_rel(elf, sechdr, &r))
1934 continue;
1935 break;
1936 case EM_ARM:
1937 if (addend_arm_rel(elf, sechdr, &r))
1938 continue;
1939 break;
1940 case EM_MIPS:
1941 if (addend_mips_rel(elf, sechdr, &r))
1942 continue;
1943 break;
1944 }
1945 sym = elf->symtab_start + r_sym;
1946 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001947 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001948 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301949 if (is_second_extable_reloc(start, rel, fromsec))
1950 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001951 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001952 }
1953}
1954
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001955/**
1956 * A module includes a number of sections that are discarded
1957 * either when loaded or when used as built-in.
1958 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001959 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001960 * Likewise for modules used built-in the sections marked __exit
1961 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001962 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001963 * The check_sec_ref() function traverses all relocation records
1964 * to find all references to a section that reference a section that will
1965 * be discarded and warns about it.
1966 **/
1967static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001968 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001969{
1970 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001971 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001972
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001973 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001974 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001975 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001976 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001977 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001978 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001979 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001980 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001981 }
1982}
1983
Andi Kleen7d02b492014-02-08 09:01:12 +01001984static char *remove_dot(char *s)
1985{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301986 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001987
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301988 if (n && s[n]) {
1989 size_t m = strspn(s + n + 1, "0123456789");
1990 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001991 s[n] = 0;
1992 }
1993 return s;
1994}
1995
Masahiro Yamada8b185742018-05-09 18:50:40 +09001996static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 const char *symname;
1999 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002000 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002001 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct module *mod;
2003 struct elf_info info = { };
2004 Elf_Sym *sym;
2005
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002006 if (!parse_elf(&info, modname))
2007 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002009 {
2010 char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002012 /* strip trailing .o */
2013 tmp = NOFAIL(strdup(modname));
2014 tmp[strlen(tmp) - 2] = '\0';
2015 mod = new_module(tmp);
2016 free(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002019 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002020 license = get_modinfo(&info, "license");
2021 if (!license)
Masahiro Yamada1d6cd3922020-12-01 19:34:16 +09002022 error("missing MODULE_LICENSE() in %s\n", modname);
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002023 while (license) {
2024 if (license_is_gpl_compatible(license))
2025 mod->gpl_compatible = 1;
2026 else {
2027 mod->gpl_compatible = 0;
2028 break;
2029 }
2030 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002031 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002032
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002033 namespace = get_modinfo(&info, "import_ns");
2034 while (namespace) {
2035 add_namespace(&mod->imported_namespaces, namespace);
2036 namespace = get_next_modinfo(&info, "import_ns",
2037 namespace);
2038 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002039 }
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002042 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002044 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 handle_moddevtable(mod, &info, sym, symname);
2046 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002047
Matthias Maennich69923202019-10-18 10:31:42 +01002048 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2049 symname = remove_dot(info.strtab + sym->st_name);
2050
Masahiro Yamada17436942019-11-15 02:42:24 +09002051 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002052 if (strstarts(symname, "__kstrtabns_"))
2053 sym_update_namespace(symname + strlen("__kstrtabns_"),
2054 namespace_from_kstrtabns(&info,
2055 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002056
2057 if (strstarts(symname, "__crc_"))
2058 handle_modversion(mod, &info, sym,
2059 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002060 }
2061
Denis Efremov15bfc232019-08-01 09:06:57 +03002062 // check for static EXPORT_SYMBOL_* functions && global vars
2063 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2064 unsigned char bind = ELF_ST_BIND(sym->st_info);
2065
2066 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2067 struct symbol *s =
2068 find_symbol(remove_dot(info.strtab +
2069 sym->st_name));
2070
2071 if (s)
2072 s->is_static = 0;
2073 }
2074 }
2075
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002076 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002078 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002079 version = get_modinfo(&info, "version");
2080 if (version || all_versions)
2081 get_src_version(modname, mod->srcversion,
2082 sizeof(mod->srcversion) - 1);
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 parse_elf_finish(&info);
2086
Rusty Russell8c8ef422009-03-31 13:05:34 -06002087 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 * never passed as an argument to an exported function, so
2089 * the automatic versioning doesn't pick it up, but it's really
2090 * important anyhow */
2091 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002092 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Rusty Russell712f9b42013-04-04 17:37:38 +10302095static void read_symbols_from_files(const char *filename)
2096{
2097 FILE *in = stdin;
2098 char fname[PATH_MAX];
2099
2100 if (strcmp(filename, "-") != 0) {
2101 in = fopen(filename, "r");
2102 if (!in)
2103 fatal("Can't open filenames file %s: %m", filename);
2104 }
2105
2106 while (fgets(fname, PATH_MAX, in) != NULL) {
2107 if (strends(fname, "\n"))
2108 fname[strlen(fname)-1] = '\0';
2109 read_symbols(fname);
2110 }
2111
2112 if (in != stdin)
2113 fclose(in);
2114}
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#define SZ 500
2117
2118/* We first write the generated file into memory using the
2119 * following helper, then compare to the file on disk and
2120 * only update the later if anything changed */
2121
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002122void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2123 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 char tmp[SZ];
2126 int len;
2127 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 va_start(ap, fmt);
2130 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002131 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 va_end(ap);
2133}
2134
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002135void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002138 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002139 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 strncpy(buf->p + buf->pos, s, len);
2142 buf->pos += len;
2143}
2144
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002145static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2146{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002147 switch (exp) {
2148 case export_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002149 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002150 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002151 break;
2152 case export_unused_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002153 error("GPL-incompatible module %s.ko uses GPL-only symbol marked UNUSED '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002154 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002155 break;
2156 case export_gpl_future:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002157 warn("GPL-incompatible module %s.ko uses future GPL-only symbol '%s'\n",
2158 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002159 break;
2160 case export_plain:
2161 case export_unused:
2162 case export_unknown:
2163 /* ignore */
2164 break;
2165 }
2166}
2167
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002168static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002169{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002170 switch (exp) {
2171 case export_unused:
2172 case export_unused_gpl:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002173 warn("module %s.ko uses symbol '%s' marked UNUSED\n",
2174 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002175 break;
2176 default:
2177 /* ignore */
2178 break;
2179 }
2180}
2181
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002182static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002183{
2184 struct symbol *s, *exp;
2185
2186 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002187 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002188 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002189 if (!exp || exp->module == mod) {
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002190 if (have_vmlinux && !s->weak)
Jessica Yu93c95e52020-03-06 17:02:05 +01002191 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2192 "\"%s\" [%s.ko] undefined!\n",
2193 s->name, mod->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002194 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002195 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002196 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002197 if (basename)
2198 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002199 else
2200 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002201
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002202 if (exp->namespace &&
2203 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002204 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2205 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2206 basename, exp->name, exp->namespace);
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002207 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002208 }
2209
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002210 if (!mod->gpl_compatible)
2211 check_for_gpl_usage(exp->export, basename, exp->name);
2212 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002213 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002214}
2215
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002216static void check_modname_len(struct module *mod)
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002217{
2218 const char *mod_name;
2219
2220 mod_name = strrchr(mod->name, '/');
2221 if (mod_name == NULL)
2222 mod_name = mod->name;
2223 else
2224 mod_name++;
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002225 if (strlen(mod_name) >= MODULE_NAME_LEN)
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002226 error("module name is too long [%s.ko]\n", mod->name);
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002227}
2228
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002229/**
2230 * Header for the generated file
2231 **/
2232static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
2234 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002235 /*
2236 * Include build-salt.h after module.h in order to
2237 * inherit the definitions.
2238 */
Leon Romanovsky51161bf2020-04-19 18:55:06 +03002239 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002240 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 buf_printf(b, "#include <linux/vermagic.h>\n");
2242 buf_printf(b, "#include <linux/compiler.h>\n");
2243 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002244 buf_printf(b, "BUILD_SALT;\n");
2245 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002247 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302249 buf_printf(b, "__visible struct module __this_module\n");
Joe Perches33def842020-10-21 19:36:07 -07002250 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002251 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002253 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (mod->has_cleanup)
2255 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002256 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002258 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 buf_printf(b, "};\n");
2260}
2261
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002262static void add_intree_flag(struct buffer *b, int is_intree)
2263{
2264 if (is_intree)
2265 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2266}
2267
Will McVicker4b9c11a2020-11-19 13:46:37 -08002268/**
2269 * add_scmversion() - Adds the MODULE_INFO macro for the scmversion.
2270 * @b: Buffer to append to.
2271 *
2272 * This function fills in the module attribute `scmversion` for the kernel
2273 * module. This is useful for determining a given module's SCM version on
2274 * device via /sys/modules/<module>/scmversion and/or using the modinfo tool.
2275 */
2276static void add_scmversion(struct buffer *b)
2277{
2278 if (module_scmversion[0] != '\0')
2279 buf_printf(b, "\nMODULE_INFO(scmversion, \"%s\");\n", module_scmversion);
2280}
2281
Andi Kleencaf75012018-01-25 15:50:28 -08002282/* Cannot check for assembler */
2283static void add_retpoline(struct buffer *b)
2284{
WANG Chaoe4f35892018-12-11 00:37:25 +08002285 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002286 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2287 buf_printf(b, "#endif\n");
2288}
2289
Trevor Keith5c725132009-09-22 16:43:38 -07002290static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002291{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002292 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002293 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2294}
2295
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002296/**
2297 * Record CRCs for unresolved symbols
2298 **/
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002299static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 struct symbol *s, *exp;
2302
2303 for (s = mod->unres; s; s = s->next) {
2304 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002305 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 s->module = exp->module;
2308 s->crc_valid = exp->crc_valid;
2309 s->crc = exp->crc;
2310 }
2311
2312 if (!modversions)
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002313 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 buf_printf(b, "\n");
2316 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Joe Perches33def842020-10-21 19:36:07 -07002317 buf_printf(b, "__used __section(\"__versions\") = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002320 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002323 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 s->name, mod->name);
2325 continue;
2326 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302327 if (strlen(s->name) >= MODULE_NAME_LEN) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002328 error("too long symbol \"%s\" [%s.ko]\n",
2329 s->name, mod->name);
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302330 break;
2331 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002332 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302333 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
2336 buf_printf(b, "};\n");
2337}
2338
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002339static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 int first = 1;
2343
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002344 /* Clear ->seen flag of modules that own symbols needed by this. */
2345 for (s = mod->unres; s; s = s->next)
2346 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002347 s->module->seen = s->module->is_vmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002350 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002352 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (!s->module)
2354 continue;
2355
2356 if (s->module->seen)
2357 continue;
2358
2359 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002360 p = strrchr(s->module->name, '/');
2361 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002362 p++;
2363 else
2364 p = s->module->name;
2365 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 first = 0;
2367 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002368 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002371static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 if (mod->srcversion[0]) {
2374 buf_printf(b, "\n");
2375 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2376 mod->srcversion);
2377 }
2378}
2379
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002380static void write_buf(struct buffer *b, const char *fname)
2381{
2382 FILE *file;
2383
2384 file = fopen(fname, "w");
2385 if (!file) {
2386 perror(fname);
2387 exit(1);
2388 }
2389 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2390 perror(fname);
2391 exit(1);
2392 }
2393 if (fclose(file) != 0) {
2394 perror(fname);
2395 exit(1);
2396 }
2397}
2398
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002399static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
2401 char *tmp;
2402 FILE *file;
2403 struct stat st;
2404
2405 file = fopen(fname, "r");
2406 if (!file)
2407 goto write;
2408
2409 if (fstat(fileno(file), &st) < 0)
2410 goto close_write;
2411
2412 if (st.st_size != b->pos)
2413 goto close_write;
2414
2415 tmp = NOFAIL(malloc(b->pos));
2416 if (fread(tmp, 1, b->pos, file) != b->pos)
2417 goto free_write;
2418
2419 if (memcmp(tmp, b->p, b->pos) != 0)
2420 goto free_write;
2421
2422 free(tmp);
2423 fclose(file);
2424 return;
2425
2426 free_write:
2427 free(tmp);
2428 close_write:
2429 fclose(file);
2430 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002431 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
Ram Paibd5cbce2006-06-08 22:12:53 -07002434/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002435 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002436 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002437static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002439 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002441 buf = read_text_file(fname);
2442 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 /* No symbol versions, silently ignore */
2444 return;
2445
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002446 pos = buf;
2447
2448 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002449 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 unsigned int crc;
2451 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002452 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 if (!(symname = strchr(line, '\t')))
2455 goto fail;
2456 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002457 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto fail;
2459 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002460 if (!(export = strchr(modname, '\t')))
2461 goto fail;
2462 *export++ = '\0';
2463 if (!(namespace = strchr(export, '\t')))
2464 goto fail;
2465 *namespace++ = '\0';
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 crc = strtoul(line, &d, 16);
2468 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2469 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002470 mod = find_module(modname);
2471 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002472 mod = new_module(modname);
Masahiro Yamada52c34162020-06-01 14:57:05 +09002473 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002475 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002476 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002477 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002478 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002480 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return;
2482fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002483 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 fatal("parse error in symbol dump file\n");
2485}
2486
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002487/* For normal builds always dump all symbols.
2488 * For external modules only dump symbols
2489 * that are not read from kernel Module.symvers.
2490 **/
2491static int dump_sym(struct symbol *sym)
2492{
2493 if (!external_module)
2494 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002495 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002496 return 0;
2497 return 1;
2498}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002499
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002500static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 struct buffer buf = { };
2503 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002504 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 int n;
2506
2507 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2508 symbol = symbolhash[n];
2509 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002510 if (dump_sym(symbol)) {
2511 namespace = symbol->namespace;
2512 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2513 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002514 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002515 export_str(symbol->export),
2516 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 symbol = symbol->next;
2519 }
2520 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002521 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002522 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002525static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002526{
2527 struct module *mod;
2528 struct namespace_list *ns;
2529 struct buffer ns_deps_buf = {};
2530
2531 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002532
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002533 if (mod->from_dump || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002534 continue;
2535
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002536 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002537
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002538 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2539 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002540
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002541 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002542 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002543
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002544 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002545 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002546}
2547
Masahiro Yamada79247992020-06-01 14:57:07 +09002548struct dump_list {
2549 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002550 const char *file;
2551};
2552
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002553int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 struct module *mod;
2556 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002557 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302558 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 int opt;
Denis Efremov15bfc232019-08-01 09:06:57 +03002560 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002561 struct dump_list *dump_read_start = NULL;
2562 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Will McVicker4b9c11a2020-11-19 13:46:37 -08002564 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:v:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002565 switch (opt) {
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002566 case 'e':
2567 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002568 break;
2569 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002570 *dump_read_iter =
2571 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2572 (*dump_read_iter)->file = optarg;
2573 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002574 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002575 case 'm':
2576 modversions = 1;
2577 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302578 case 'n':
2579 ignore_missing_files = 1;
2580 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002581 case 'o':
2582 dump_write = optarg;
2583 break;
2584 case 'a':
2585 all_versions = 1;
2586 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302587 case 'T':
2588 files_source = optarg;
2589 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002590 case 'w':
2591 warn_unresolved = 1;
2592 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302593 case 'E':
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002594 sec_mismatch_warn_only = false;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302595 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002596 case 'N':
2597 allow_missing_ns_imports = 1;
2598 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002599 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002600 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002601 break;
Will McVicker4b9c11a2020-11-19 13:46:37 -08002602 case 'v':
2603 strncpy(module_scmversion, optarg, sizeof(module_scmversion) - 1);
2604 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002605 default:
2606 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608 }
2609
Masahiro Yamada79247992020-06-01 14:57:07 +09002610 while (dump_read_start) {
2611 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002612
Masahiro Yamada79247992020-06-01 14:57:07 +09002613 read_dump(dump_read_start->file);
2614 tmp = dump_read_start->next;
2615 free(dump_read_start);
2616 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002619 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Rusty Russell712f9b42013-04-04 17:37:38 +10302622 if (files_source)
2623 read_symbols_from_files(files_source);
2624
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002625 /*
2626 * When there's no vmlinux, don't print warnings about
2627 * unresolved symbols (since there'll be too many ;)
2628 */
2629 if (!have_vmlinux)
2630 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2631
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002632 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302633 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002634
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002635 if (mod->is_vmlinux || mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002636 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 buf.pos = 0;
2639
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002640 check_modname_len(mod);
2641 check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002644 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002645 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002646 add_staging_flag(&buf, mod->name);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002647 add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002648 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 add_moddevtable(&buf, mod);
2650 add_srcversion(&buf, mod);
Will McVicker4b9c11a2020-11-19 13:46:37 -08002651 add_scmversion(&buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653 sprintf(fname, "%s.mod.c", mod->name);
2654 write_if_changed(&buf, fname);
2655 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002656
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002657 if (missing_namespace_deps)
2658 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 if (dump_write)
2661 write_dump(dump_write);
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002662 if (sec_mismatch_count && !sec_mismatch_warn_only)
2663 error("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002664 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002665 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002666 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002667
Masahiro Yamada47346e92019-09-24 21:07:40 +09002668 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002669 if (s->is_static)
Greg Kroah-Hartman09059c62021-02-02 11:31:33 +01002670 error("\"%s\" [%s] is a static %s\n",
2671 s->name, s->module->name,
2672 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002673 }
2674 }
2675
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002676 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002678 return error_occurred ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}