blob: be0642126f535609ad717d02faa16393242fb668 [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>
Guenter Roeckeed380f2013-09-23 15:23:54 +093020#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020022#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093025static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093027static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010030/* If we are modposting external module set to 1 */
31static int external_module = 0;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070032/* Only warn about unresolved symbols */
33static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070034/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010035static int sec_mismatch_count = 0;
Masahiro Yamadac7299d92020-12-01 19:34:18 +090036static int sec_mismatch_warn_only = true;
Guenter Roeckeed380f2013-09-23 15:23:54 +093037/* ignore missing files */
38static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010039/* If set to 1, only warn (instead of error) about missing ns imports */
40static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010041
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090042static bool error_occurred;
43
Sam Ravnborgc96fca22006-07-01 11:44:23 +020044enum export {
45 export_plain, export_unused, export_gpl,
46 export_unused_gpl, export_gpl_future, export_unknown
47};
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080049/* In kernel, this size is defined in linux/module.h;
50 * here we use Elf_Addr instead of long for covering cross-compile
51 */
52
53#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
54
Jessica Yu93c95e52020-03-06 17:02:05 +010055void __attribute__((format(printf, 2, 3)))
56modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 va_list arglist;
59
Jessica Yu93c95e52020-03-06 17:02:05 +010060 switch (loglevel) {
61 case LOG_WARN:
62 fprintf(stderr, "WARNING: ");
63 break;
64 case LOG_ERROR:
65 fprintf(stderr, "ERROR: ");
66 break;
67 case LOG_FATAL:
68 fprintf(stderr, "FATAL: ");
69 break;
70 default: /* invalid loglevel, ignore */
71 break;
72 }
73
74 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 va_start(arglist, fmt);
77 vfprintf(stderr, fmt, arglist);
78 va_end(arglist);
79
Jessica Yu93c95e52020-03-06 17:02:05 +010080 if (loglevel == LOG_FATAL)
81 exit(1);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090082 if (loglevel == LOG_ERROR)
83 error_occurred = true;
Matthew Wilcox2a116652006-10-07 05:35:32 -060084}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086void *do_nofail(void *ptr, const char *expr)
87{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010088 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +010089 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +010090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return ptr;
92}
93
Masahiro Yamadaac5100f2020-06-01 14:57:17 +090094char *read_text_file(const char *filename)
95{
96 struct stat st;
97 size_t nbytes;
98 int fd;
99 char *buf;
100
101 fd = open(filename, O_RDONLY);
102 if (fd < 0) {
103 perror(filename);
104 exit(1);
105 }
106
107 if (fstat(fd, &st) < 0) {
108 perror(filename);
109 exit(1);
110 }
111
112 buf = NOFAIL(malloc(st.st_size + 1));
113
114 nbytes = st.st_size;
115
116 while (nbytes) {
117 ssize_t bytes_read;
118
119 bytes_read = read(fd, buf, nbytes);
120 if (bytes_read < 0) {
121 perror(filename);
122 exit(1);
123 }
124
125 nbytes -= bytes_read;
126 }
127 buf[st.st_size] = '\0';
128
129 close(fd);
130
131 return buf;
132}
133
134char *get_line(char **stringp)
135{
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200136 char *orig = *stringp, *next;
137
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900138 /* do not return the unwanted extra line at EOF */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200139 if (!orig || *orig == '\0')
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900140 return NULL;
141
Wolfram Sang6020db52020-07-26 23:44:19 +0200142 /* don't use strsep here, it is not available everywhere */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200143 next = strchr(orig, '\n');
144 if (next)
145 *next++ = '\0';
146
147 *stringp = next;
148
149 return orig;
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static struct module *modules;
154
Masahiro Yamada8b185742018-05-09 18:50:40 +0900155static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct module *mod;
158
159 for (mod = modules; mod; mod = mod->next)
160 if (strcmp(mod->name, modname) == 0)
161 break;
162 return mod;
163}
164
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030165static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct module *mod;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100168
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900169 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 memset(mod, 0, sizeof(*mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* add to list */
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900173 strcpy(mod->name, modname);
Masahiro Yamada4de7b622020-06-01 14:57:30 +0900174 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200175 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 mod->next = modules;
177 modules = mod;
178
Masahiro Yamada858b9372020-06-01 14:57:28 +0900179 if (mod->is_vmlinux)
180 have_vmlinux = 1;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return mod;
183}
184
185/* A hash of all exported symbols,
186 * struct symbol is also used for lists of unresolved symbols */
187
188#define SYMBOL_HASH_SIZE 1024
189
190struct symbol {
191 struct symbol *next;
192 struct module *module;
193 unsigned int crc;
194 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900195 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300197 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700198 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500199 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
202static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
203
204/* This is based on the hash agorithm from gdbm, via tdb */
205static inline unsigned int tdb_hash(const char *name)
206{
207 unsigned value; /* Used to compute the hash value. */
208 unsigned i; /* Used to cycle through random values. */
209
210 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100211 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
213
214 return (1103515243 * value + 12345);
215}
216
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100217/**
218 * Allocate a new symbols for use in the hash of exported symbols or
219 * the list of unresolved symbols per module
220 **/
221static struct symbol *alloc_symbol(const char *name, unsigned int weak,
222 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
225
226 memset(s, 0, sizeof(*s));
227 strcpy(s->name, name);
228 s->weak = weak;
229 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300230 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return s;
232}
233
234/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700235static struct symbol *new_symbol(const char *name, struct module *module,
236 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900241 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
242
243 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100246static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct symbol *s;
249
250 /* For our purposes, .foo matches foo. PPC64 needs this. */
251 if (name[0] == '.')
252 name++;
253
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100254 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (strcmp(s->name, name) == 0)
256 return s;
257 }
258 return NULL;
259}
260
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100261static bool contains_namespace(struct namespace_list *list,
262 const char *namespace)
263{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900264 for (; list; list = list->next)
265 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100266 return true;
267
268 return false;
269}
270
271static void add_namespace(struct namespace_list **list, const char *namespace)
272{
273 struct namespace_list *ns_entry;
274
275 if (!contains_namespace(*list, namespace)) {
276 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
277 strlen(namespace) + 1));
278 strcpy(ns_entry->namespace, namespace);
279 ns_entry->next = *list;
280 *list = ns_entry;
281 }
282}
283
284static bool module_imports_namespace(struct module *module,
285 const char *namespace)
286{
287 return contains_namespace(module->imported_namespaces, namespace);
288}
289
Mathias Krause7a3ee752014-08-27 20:28:53 +0930290static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700291 const char *str;
292 enum export export;
293} export_list[] = {
294 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200295 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700296 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200297 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700298 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
299 { .str = "(unknown)", .export = export_unknown },
300};
301
302
303static const char *export_str(enum export ex)
304{
305 return export_list[ex].str;
306}
307
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100308static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700309{
310 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100311
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200312 if (!s)
313 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700314 for (i = 0; export_list[i].export != export_unknown; i++) {
315 if (strcmp(export_list[i].str, s) == 0)
316 return export_list[i].export;
317 }
318 return export_unknown;
319}
320
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900321static void *sym_get_data_by_offset(const struct elf_info *info,
322 unsigned int secindex, unsigned long offset)
Masahiro Yamada6124c042017-09-06 16:19:05 -0700323{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800324 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900325
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900326 if (info->hdr->e_type != ET_REL)
327 offset -= sechdr->sh_addr;
328
329 return (void *)info->hdr + sechdr->sh_offset + offset;
330}
331
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900332static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
333{
334 return sym_get_data_by_offset(info, get_secindex(info, sym),
335 sym->st_value);
336}
337
Masahiro Yamada565587d2020-05-25 14:47:05 +0900338static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
339{
340 return sym_get_data_by_offset(info, info->secindex_strings,
341 sechdr->sh_name);
342}
343
344static const char *sec_name(const struct elf_info *info, int secindex)
345{
346 return sech_name(info, &info->sechdrs[secindex]);
347}
348
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200349#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
350
351static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
352{
353 const char *secname = sec_name(elf, sec);
354
355 if (strstarts(secname, "___ksymtab+"))
356 return export_plain;
357 else if (strstarts(secname, "___ksymtab_unused+"))
358 return export_unused;
359 else if (strstarts(secname, "___ksymtab_gpl+"))
360 return export_gpl;
361 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
362 return export_unused_gpl;
363 else if (strstarts(secname, "___ksymtab_gpl_future+"))
364 return export_gpl_future;
365 else
366 return export_unknown;
367}
368
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200369static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700370{
371 if (sec == elf->export_sec)
372 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200373 else if (sec == elf->export_unused_sec)
374 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700375 else if (sec == elf->export_gpl_sec)
376 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200377 else if (sec == elf->export_unused_gpl_sec)
378 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700379 else if (sec == elf->export_gpl_future_sec)
380 return export_gpl_future;
381 else
382 return export_unknown;
383}
384
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900385static const char *namespace_from_kstrtabns(const struct elf_info *info,
386 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100387{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900388 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100389 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100390}
391
Matthias Maennicha2b11182019-10-18 10:31:40 +0100392static void sym_update_namespace(const char *symname, const char *namespace)
393{
394 struct symbol *s = find_symbol(symname);
395
396 /*
397 * That symbol should have been created earlier and thus this is
398 * actually an assertion.
399 */
400 if (!s) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +0900401 error("Could not update namespace(%s) for symbol %s\n",
402 namespace, symname);
Matthias Maennicha2b11182019-10-18 10:31:40 +0100403 return;
404 }
405
406 free(s->namespace);
407 s->namespace =
408 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
409}
410
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100411/**
412 * Add an exported symbol - it may have already been added without a
413 * CRC, in this case just update the CRC
414 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100415static struct symbol *sym_add_exported(const char *name, struct module *mod,
416 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct symbol *s = find_symbol(name);
419
420 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700421 s = new_symbol(name, mod, export);
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900422 } else if (!external_module || s->module->is_vmlinux ||
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900423 s->module == mod) {
424 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
425 mod->name, name, s->module->name,
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900426 s->module->is_vmlinux ? "" : ".ko");
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900427 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900429
430 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700431 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100432 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Masahiro Yamada17436942019-11-15 02:42:24 +0900435static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100436{
437 struct symbol *s = find_symbol(name);
438
Masahiro Yamada17436942019-11-15 02:42:24 +0900439 /*
440 * Ignore stand-alone __crc_*, which might be auto-generated symbols
441 * such as __*_veneer in ARM ELF.
442 */
443 if (!s)
444 return;
445
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100446 s->crc = crc;
447 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900450static void *grab_file(const char *filename, size_t *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930453 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int fd;
455
456 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930457 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930459 if (fstat(fd, &st))
460 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 *size = st.st_size;
463 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930465failed:
466 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (map == MAP_FAILED)
468 return NULL;
469 return map;
470}
471
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900472static void release_file(void *file, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 munmap(file, size);
475}
476
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100477static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100480 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 Elf_Shdr *sechdrs;
482 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200483 const char *secstrings;
484 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 hdr = grab_file(filename, &info->size);
487 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930488 if (ignore_missing_files) {
489 fprintf(stderr, "%s: %s (ignored)\n", filename,
490 strerror(errno));
491 return 0;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200494 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100497 if (info->size < sizeof(*hdr)) {
498 /* file too small, assume this is an empty .o file */
499 return 0;
500 }
501 /* Is this a valid ELF file? */
502 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
503 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
504 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
505 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
506 /* Not an ELF file - silently ignore it */
507 return 0;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200510 hdr->e_type = TO_NATIVE(hdr->e_type);
511 hdr->e_machine = TO_NATIVE(hdr->e_machine);
512 hdr->e_version = TO_NATIVE(hdr->e_version);
513 hdr->e_entry = TO_NATIVE(hdr->e_entry);
514 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
515 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
516 hdr->e_flags = TO_NATIVE(hdr->e_flags);
517 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
518 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
519 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
520 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
521 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
522 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sechdrs = (void *)hdr + hdr->e_shoff;
524 info->sechdrs = sechdrs;
525
Petr Stetiara83710e2007-08-27 12:15:07 +0200526 /* Check if file offset is correct */
527 if (hdr->e_shoff > info->size) {
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900528 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
529 (unsigned long)hdr->e_shoff, filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200530 return 0;
531 }
532
Anders Kaseorg68457562011-05-19 16:55:27 -0600533 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200534 /*
535 * There are more than 64k sections,
536 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200537 */
538 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
539 }
540 else {
541 info->num_sections = hdr->e_shnum;
542 }
543 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600544 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200545 }
546 else {
547 info->secindex_strings = hdr->e_shstrndx;
548 }
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200551 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200552 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
553 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
554 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
555 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
556 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
557 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
558 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
559 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
560 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
561 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200564 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
565 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700566 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900567 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Tejun Heo56fc82c2009-02-06 00:48:02 +0900569 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100570 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
571 "sizeof(*hrd)=%zu\n", filename,
572 (unsigned long)sechdrs[i].sh_offset,
573 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100574 return 0;
575 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700576 secname = secstrings + sechdrs[i].sh_name;
577 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900578 if (nobits)
579 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
581 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700582 } else if (strcmp(secname, "__ksymtab") == 0)
583 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200584 else if (strcmp(secname, "__ksymtab_unused") == 0)
585 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700586 else if (strcmp(secname, "__ksymtab_gpl") == 0)
587 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200588 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
589 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700590 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
591 info->export_gpl_future_sec = i;
592
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200593 if (sechdrs[i].sh_type == SHT_SYMTAB) {
594 unsigned int sh_link_idx;
595 symtab_idx = i;
596 info->symtab_start = (void *)hdr +
597 sechdrs[i].sh_offset;
598 info->symtab_stop = (void *)hdr +
599 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600600 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200601 info->strtab = (void *)hdr +
602 sechdrs[sh_link_idx].sh_offset;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200605 /* 32bit section no. table? ("more than 64k sections") */
606 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
607 symtab_shndx_idx = i;
608 info->symtab_shndx_start = (void *)hdr +
609 sechdrs[i].sh_offset;
610 info->symtab_shndx_stop = (void *)hdr +
611 sechdrs[i].sh_offset + sechdrs[i].sh_size;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100614 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100615 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Fix endianness in symbols */
618 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
619 sym->st_shndx = TO_NATIVE(sym->st_shndx);
620 sym->st_name = TO_NATIVE(sym->st_name);
621 sym->st_value = TO_NATIVE(sym->st_value);
622 sym->st_size = TO_NATIVE(sym->st_size);
623 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200624
625 if (symtab_shndx_idx != ~0U) {
626 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600627 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200628 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600629 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200630 symtab_idx);
631 /* Fix endianness */
632 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
633 p++)
634 *p = TO_NATIVE(*p);
635 }
636
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100637 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100640static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 release_file(info->hdr, info->size);
643}
644
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200645static int ignore_undef_symbol(struct elf_info *info, const char *symname)
646{
647 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900648 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200649 return 1;
650 /* ignore global offset table */
651 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
652 return 1;
653 if (info->hdr->e_machine == EM_PPC)
654 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900655 if (strstarts(symname, "_restgpr_") ||
656 strstarts(symname, "_savegpr_") ||
657 strstarts(symname, "_rest32gpr_") ||
658 strstarts(symname, "_save32gpr_") ||
659 strstarts(symname, "_restvr_") ||
660 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200661 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000662 if (info->hdr->e_machine == EM_PPC64)
663 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900664 if (strstarts(symname, "_restgpr0_") ||
665 strstarts(symname, "_savegpr0_") ||
666 strstarts(symname, "_restvr_") ||
667 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100668 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000669 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200670 /* Do not ignore this symbol */
671 return 0;
672}
673
Masahiro Yamada17436942019-11-15 02:42:24 +0900674static void handle_modversion(const struct module *mod,
675 const struct elf_info *info,
676 const Elf_Sym *sym, const char *symname)
677{
678 unsigned int crc;
679
680 if (sym->st_shndx == SHN_UNDEF) {
681 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900682 symname, mod->name, mod->is_vmlinux ? "" : ".ko");
Masahiro Yamada17436942019-11-15 02:42:24 +0900683 return;
684 }
685
686 if (sym->st_shndx == SHN_ABS) {
687 crc = sym->st_value;
688 } else {
689 unsigned int *crcp;
690
691 /* symbol points to the CRC in the ELF object */
692 crcp = sym_get_data(info, sym);
693 crc = TO_NATIVE(*crcp);
694 }
695 sym_set_crc(symname, crc);
696}
697
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900698static void handle_symbol(struct module *mod, struct elf_info *info,
699 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200701 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900702 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200703
Masahiro Yamada33795762020-06-01 14:57:24 +0900704 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200705 export = export_from_secname(info, get_secindex(info, sym));
706 else
707 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 switch (sym->st_shndx) {
710 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900711 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100712 /* Should warn here, but modpost runs before the linker */
713 } else
714 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 case SHN_UNDEF:
717 /* undefined symbol */
718 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
719 ELF_ST_BIND(sym->st_info) != STB_WEAK)
720 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200721 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (info->hdr->e_machine == EM_SPARC ||
724 info->hdr->e_machine == EM_SPARCV9) {
725 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700726 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100728 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700729 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100730 munged[0] = '_';
731 munged[1] = toupper(munged[1]);
732 symname = munged;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100735
Rusty Russellb92021b2013-03-15 15:04:17 +1030736 mod->unres = alloc_symbol(symname,
737 ELF_ST_BIND(sym->st_info) == STB_WEAK,
738 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740 default:
741 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900742 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100743 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100744 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900746 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900748 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 mod->has_cleanup = 1;
750 break;
751 }
752}
753
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100754/**
755 * Parse tag=value strings from .modinfo section
756 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757static char *next_string(char *string, unsigned long *secsize)
758{
759 /* Skip non-zero chars */
760 while (string[0]) {
761 string++;
762 if ((*secsize)-- <= 1)
763 return NULL;
764 }
765
766 /* Skip any zero padding. */
767 while (!string[0]) {
768 string++;
769 if ((*secsize)-- <= 1)
770 return NULL;
771 }
772 return string;
773}
774
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900775static char *get_next_modinfo(struct elf_info *info, const char *tag,
776 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 char *p;
779 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900780 char *modinfo = info->modinfo;
781 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900783 if (prev) {
784 size -= prev - modinfo;
785 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200786 }
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 for (p = modinfo; p; p = next_string(p, &size)) {
789 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
790 return p + taglen + 1;
791 }
792 return NULL;
793}
794
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900795static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200796
797{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900798 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200799}
800
Sam Ravnborg93684d32006-02-19 11:53:35 +0100801/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100802 * Test if string s ends in string sub
803 * return 0 if match
804 **/
805static int strrcmp(const char *s, const char *sub)
806{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100807 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100808
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100809 if (!s || !sub)
810 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100811
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100812 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100813 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100814
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100815 if ((slen == 0) || (sublen == 0))
816 return 1;
817
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100818 if (sublen > slen)
819 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100820
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100821 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100822}
823
Sam Ravnborgff13f922008-01-23 19:54:27 +0100824static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
825{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100826 if (sym)
827 return elf->strtab + sym->st_name;
828 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100829 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100830}
831
Sam Ravnborg10668222008-01-13 22:21:31 +0100832/* The pattern is an array of simple patterns.
833 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100834 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100835 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930836 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100837 */
Trevor Keith5c725132009-09-22 16:43:38 -0700838static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100839{
840 const char *p;
841 while (*pat) {
842 p = *pat++;
843 const char *endp = p + strlen(p) - 1;
844
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930845 /* "*foo*" */
846 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300847 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
848 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930849
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930850 free(bare);
851 if (here != NULL)
852 return 1;
853 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100854 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930855 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100856 if (strrcmp(sym, p + 1) == 0)
857 return 1;
858 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100859 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100860 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100861 if (strncmp(sym, p, strlen(p) - 1) == 0)
862 return 1;
863 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100864 /* no wildcards */
865 else {
866 if (strcmp(p, sym) == 0)
867 return 1;
868 }
869 }
870 /* no match */
871 return 0;
872}
873
Sam Ravnborg10668222008-01-13 22:21:31 +0100874/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930875static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200876{
877 ".comment*",
878 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930879 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800880 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000881 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200882 ".mdebug*", /* alpha, score, mips etc. */
883 ".pdr", /* alpha, score, mips etc. */
884 ".stab*",
885 ".note*",
886 ".got*",
887 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400888 ".xt.prop", /* xtensa */
889 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030890 ".arcextmap*", /* arc */
891 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030892 ".cmem*", /* EZchip */
893 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100894 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600895 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200896 NULL
897};
Sam Ravnborg10668222008-01-13 22:21:31 +0100898
Sam Ravnborge241a632008-01-28 20:13:13 +0100899/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400900 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100901 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400902 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100903 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400904static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900905 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100906{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400907 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100908
Anders Kaseorgb614a692009-04-23 16:49:33 -0400909 if (sechdr->sh_type == SHT_PROGBITS &&
910 !(sechdr->sh_flags & SHF_ALLOC) &&
911 !match(sec, section_white_list)) {
912 warn("%s (%s): unexpected non-allocatable section.\n"
913 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
914 "Note that for example <linux/init.h> contains\n"
915 "section definitions for use in .S files.\n\n",
916 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100917 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100918}
919
920
921
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100922#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930923 ".init.setup", ".init.rodata", ".meminit.rodata", \
924 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100925#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930926 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100927
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100928#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930929 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100930#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930931 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100932
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200933#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930934 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
935 ".pci_fixup_enable", ".pci_fixup_resume", \
936 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200937
Paul Gortmakere24f6622013-06-19 19:30:48 -0400938#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
939#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100940
941#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
942#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100943
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930944#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930945#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Thomas Gleixner65538962020-03-09 22:47:17 +0100946 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930947#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400948 ".fixup", ".entry.text", ".exception.text", ".text.*", \
949 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100950
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000951#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000952#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100953
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000954#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000955#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100956
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930957#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
958 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
959
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100960/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930961static const char *const init_data_sections[] =
962 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100963
964/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930965static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100966
967/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930968static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100969 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100970
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930971/* all text sections */
972static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
973
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100974/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930975static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100976
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100977
978/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100979#define DEFAULT_SYMBOL_WHITE_LIST \
980 "*driver", \
981 "*_template", /* scsi uses *_template a lot */ \
982 "*_timer", /* arm uses ops structures named _timer a lot */ \
983 "*_sht", /* scsi also used *_sht to some extent */ \
984 "*_ops", \
985 "*_probe", \
986 "*_probe_one", \
987 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100988
Mathias Krause7a3ee752014-08-27 20:28:53 +0930989static const char *const head_sections[] = { ".head.text*", NULL };
990static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100991 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930992static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100993
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100994enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100995 TEXT_TO_ANY_INIT,
996 DATA_TO_ANY_INIT,
997 TEXT_TO_ANY_EXIT,
998 DATA_TO_ANY_EXIT,
999 XXXINIT_TO_SOME_INIT,
1000 XXXEXIT_TO_SOME_EXIT,
1001 ANY_INIT_TO_ANY_EXIT,
1002 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001003 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301004 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001005};
1006
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301007/**
1008 * Describe how to match sections on different criterias:
1009 *
1010 * @fromsec: Array of sections to be matched.
1011 *
1012 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1013 * this array is forbidden (black-list). Can be empty.
1014 *
1015 * @good_tosec: Relocations applied to a section in @fromsec must be
1016 * targetting sections in this array (white-list). Can be empty.
1017 *
1018 * @mismatch: Type of mismatch.
1019 *
1020 * @symbol_white_list: Do not match a relocation to a symbol in this list
1021 * even if it is targetting a section in @bad_to_sec.
1022 *
1023 * @handler: Specific handler to call when a match is found. If NULL,
1024 * default_mismatch_handler() will be called.
1025 *
1026 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001027struct sectioncheck {
1028 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301029 const char *bad_tosec[20];
1030 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001031 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001032 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301033 void (*handler)(const char *modname, struct elf_info *elf,
1034 const struct sectioncheck* const mismatch,
1035 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1036
Sam Ravnborg10668222008-01-13 22:21:31 +01001037};
1038
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301039static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1040 const struct sectioncheck* const mismatch,
1041 Elf_Rela *r, Elf_Sym *sym,
1042 const char *fromsec);
1043
Mathias Krause7a3ee752014-08-27 20:28:53 +09301044static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001045/* Do not reference init/exit code/data from
1046 * normal code and data
1047 */
1048{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001049 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301050 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001051 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001052 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001053},
1054{
1055 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301056 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001057 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001058 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001059},
1060{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001061 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301062 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001063 .mismatch = DATA_TO_ANY_INIT,
1064 .symbol_white_list = {
1065 "*_template", "*_timer", "*_sht", "*_ops",
1066 "*_probe", "*_probe_one", "*_console", NULL
1067 },
1068},
1069{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001070 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301071 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001072 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001073 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001074},
1075{
1076 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301077 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001078 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001079 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001080},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001081/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001082{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001083 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301084 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001085 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001086 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001087},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001088/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001089{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001090 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301091 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001092 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001093 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001094},
1095/* Do not use exit code/data from init code */
1096{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001097 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301098 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001099 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001100 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001101},
1102/* Do not use init code/data from exit code */
1103{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001104 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301105 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001106 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001107 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001108},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001109{
1110 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301111 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001112 .mismatch = ANY_INIT_TO_ANY_EXIT,
1113 .symbol_white_list = { NULL },
1114},
Sam Ravnborg10668222008-01-13 22:21:31 +01001115/* Do not export init/exit functions or data */
1116{
1117 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301118 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001119 .mismatch = EXPORT_TO_INIT_EXIT,
1120 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301121},
1122{
1123 .fromsec = { "__ex_table", NULL },
1124 /* If you're adding any new black-listed sections in here, consider
1125 * adding a special 'printer' for them in scripts/check_extable.
1126 */
1127 .bad_tosec = { ".altinstr_replacement", NULL },
1128 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1129 .mismatch = EXTABLE_TO_NON_TEXT,
1130 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001131}
1132};
1133
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001134static const struct sectioncheck *section_mismatch(
1135 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001136{
1137 int i;
1138 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1139 const struct sectioncheck *check = &sectioncheck[0];
1140
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301141 /*
1142 * The target section could be the SHT_NUL section when we're
1143 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001144 * doesn't make much sense and causes build failures on parisc
1145 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301146 */
1147 if (*tosec == '\0')
1148 return NULL;
1149
Sam Ravnborg10668222008-01-13 22:21:31 +01001150 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301151 if (match(fromsec, check->fromsec)) {
1152 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1153 return check;
1154 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1155 return check;
1156 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001157 check++;
1158 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001159 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001160}
1161
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001162/**
1163 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001164 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001165 * Pattern 1:
1166 * If a module parameter is declared __initdata and permissions=0
1167 * then this is legal despite the warning generated.
1168 * We cannot see value of permissions here, so just ignore
1169 * this pattern.
1170 * The pattern is identified by:
1171 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001172 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001173 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001174 *
Rusty Russell6a841522010-08-11 23:04:16 -06001175 * Pattern 1a:
1176 * module_param_call() ops can refer to __init set function if permissions=0
1177 * The pattern is identified by:
1178 * tosec = .init.text
1179 * fromsec = .data*
1180 * atsym = __param_ops_*
1181 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001182 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001183 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001184 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001185 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001186 * tosec = init or exit section
1187 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001188 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1189 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001190 *
1191 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001192 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001193 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001194 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001195 * Some symbols belong to init section but still it is ok to reference
1196 * these from non-init sections as these symbols don't have any memory
1197 * allocated for them and symbol address and value are same. So even
1198 * if init section is freed, its ok to reference those symbols.
1199 * For ex. symbols marking the init section boundaries.
1200 * This pattern is identified by
1201 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001202 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301203 * Pattern 5:
1204 * GCC may optimize static inlines when fed constant arg(s) resulting
1205 * in functions like cpumask_empty() -- generating an associated symbol
1206 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1207 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1208 * meaningless section warning. May need to add isra symbols too...
1209 * This pattern is identified by
1210 * tosec = init section
1211 * fromsec = text section
1212 * refsymname = *.constprop.*
1213 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001214 * Pattern 6:
1215 * Hide section mismatch warnings for ELF local symbols. The goal
1216 * is to eliminate false positive modpost warnings caused by
1217 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1218 * Autogenerated symbol names bypass modpost's "Pattern 2"
1219 * whitelisting, which relies on pattern-matching against symbol
1220 * names to work. (One situation where gcc can autogenerate ELF
1221 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001222 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001223static int secref_whitelist(const struct sectioncheck *mismatch,
1224 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001225 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001226{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001227 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001228 if (match(tosec, init_data_sections) &&
1229 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001230 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001231 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001232
Rusty Russell6a841522010-08-11 23:04:16 -06001233 /* Check for pattern 1a */
1234 if (strcmp(tosec, ".init.text") == 0 &&
1235 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001236 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001237 return 0;
1238
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001239 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001240 if (match(tosec, init_exit_sections) &&
1241 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001242 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001243 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001244
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001245 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001246 if (match(fromsec, head_sections) &&
1247 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001248 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001249
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001250 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001251 if (match(tosym, linker_symbols))
1252 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001253
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301254 /* Check for pattern 5 */
1255 if (match(fromsec, text_sections) &&
1256 match(tosec, init_sections) &&
1257 match(fromsym, optim_symbols))
1258 return 0;
1259
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001260 /* Check for pattern 6 */
1261 if (strstarts(fromsym, ".L"))
1262 return 0;
1263
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001264 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001265}
1266
Sami Tolvanen5818c682018-10-23 15:15:35 -07001267static inline int is_arm_mapping_symbol(const char *str)
1268{
1269 return str[0] == '$' && strchr("axtd", str[1])
1270 && (str[2] == '\0' || str[2] == '.');
1271}
1272
1273/*
1274 * If there's no name there, ignore it; likewise, ignore it if it's
1275 * one of the magic symbols emitted used by current ARM tools.
1276 *
1277 * Otherwise if find_symbols_between() returns those symbols, they'll
1278 * fail the whitelist tests and cause lots of false alarms ... fixable
1279 * only by merging __exit and __init sections into __text, bloating
1280 * the kernel (which is especially evil on embedded platforms).
1281 */
1282static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1283{
1284 const char *name = elf->strtab + sym->st_name;
1285
1286 if (!name || !strlen(name))
1287 return 0;
1288 return !is_arm_mapping_symbol(name);
1289}
1290
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001291/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001292 * Find symbol based on relocation record info.
1293 * In some cases the symbol supplied is a valid symbol so
1294 * return refsym. If st_name != 0 we assume this is a valid symbol.
1295 * In other cases the symbol needs to be looked up in the symbol table
1296 * based on section and address.
1297 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001298static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001299 Elf_Sym *relsym)
1300{
1301 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001302 Elf_Sym *near = NULL;
1303 Elf64_Sword distance = 20;
1304 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001305 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001306
1307 if (relsym->st_name != 0)
1308 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001309
1310 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001311 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001312 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001313 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001314 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1315 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001316 if (!is_valid_name(elf, sym))
1317 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001318 if (sym->st_value == addr)
1319 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001320 /* Find a symbol nearby - addr are maybe negative */
1321 d = sym->st_value - addr;
1322 if (d < 0)
1323 d = addr - sym->st_value;
1324 if (d < distance) {
1325 distance = d;
1326 near = sym;
1327 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001328 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001329 /* We need a close match */
1330 if (distance < 20)
1331 return near;
1332 else
1333 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001334}
1335
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001336/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001337 * Find symbols before or equal addr and after addr - in the section sec.
1338 * If we find two symbols with equal offset prefer one with a valid name.
1339 * The ELF format may have a better way to detect what type of symbol
1340 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001341 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001342static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1343 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001344{
1345 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001346 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001347 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001348
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001349 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1350 const char *symsec;
1351
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001352 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001353 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001354 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001355 if (strcmp(symsec, sec) != 0)
1356 continue;
David Brownellda68d612007-02-20 13:58:16 -08001357 if (!is_valid_name(elf, sym))
1358 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001359 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001360 if ((addr - sym->st_value) < distance) {
1361 distance = addr - sym->st_value;
1362 near = sym;
1363 } else if ((addr - sym->st_value) == distance) {
1364 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001365 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001366 }
1367 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001368 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001369}
1370
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001371/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001372 * Convert a section name to the function/data attribute
1373 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001374 * .memexitconst => __memconst
1375 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001376 *
1377 * The memory of returned value has been allocated on a heap. The user of this
1378 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001379*/
1380static char *sec2annotation(const char *s)
1381{
1382 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001383 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001384 char *r = p;
1385
1386 *p++ = '_';
1387 *p++ = '_';
1388 if (*s == '.')
1389 s++;
1390 while (*s && *s != '.')
1391 *p++ = *s++;
1392 *p = '\0';
1393 if (*s == '.')
1394 s++;
1395 if (strstr(s, "rodata") != NULL)
1396 strcat(p, "const ");
1397 else if (strstr(s, "data") != NULL)
1398 strcat(p, "data ");
1399 else
1400 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001401 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001402 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001403 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001404 }
1405}
1406
1407static int is_function(Elf_Sym *sym)
1408{
1409 if (sym)
1410 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1411 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001412 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001413}
1414
Randy Dunlap00759c02011-03-15 14:13:47 -07001415static void print_section_list(const char * const list[20])
1416{
1417 const char *const *s = list;
1418
1419 while (*s) {
1420 fprintf(stderr, "%s", *s);
1421 s++;
1422 if (*s)
1423 fprintf(stderr, ", ");
1424 }
1425 fprintf(stderr, "\n");
1426}
1427
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301428static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1429{
1430 switch (is_func) {
1431 case 0: *name = "variable"; *name_p = ""; break;
1432 case 1: *name = "function"; *name_p = "()"; break;
1433 default: *name = "(unknown reference)"; *name_p = ""; break;
1434 }
1435}
1436
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001437/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001438 * Print a warning about a section mismatch.
1439 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001440 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001441 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001442static void report_sec_mismatch(const char *modname,
1443 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001444 const char *fromsec,
1445 unsigned long long fromaddr,
1446 const char *fromsym,
1447 int from_is_func,
1448 const char *tosec, const char *tosym,
1449 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001450{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001451 const char *from, *from_p;
1452 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001453 char *prl_from;
1454 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001455
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001456 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001457
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301458 get_pretty_name(from_is_func, &from, &from_p);
1459 get_pretty_name(to_is_func, &to, &to_p);
1460
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001461 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1462 "to the %s %s:%s%s\n",
1463 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1464 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001465
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001466 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001467 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001468 prl_from = sec2annotation(fromsec);
1469 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001470 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001471 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001472 "the %s %s%s%s.\n"
1473 "This is often because %s lacks a %s\n"
1474 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001475 prl_from, fromsym,
1476 to, prl_to, tosym, to_p,
1477 fromsym, prl_to, tosym);
1478 free(prl_from);
1479 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001480 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001481 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001482 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001483 fprintf(stderr,
1484 "The variable %s references\n"
1485 "the %s %s%s%s\n"
1486 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001487 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001488 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001489 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001490 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001491 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001492 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001493 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001494 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001495 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001496 fprintf(stderr,
1497 "The function %s() references a %s in an exit section.\n"
1498 "Often the %s %s%s has valid usage outside the exit section\n"
1499 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001500 fromsym, to, to, tosym, to_p, prl_to, tosym);
1501 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001502 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001503 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001504 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001505 fprintf(stderr,
1506 "The variable %s references\n"
1507 "the %s %s%s%s\n"
1508 "If the reference is valid then annotate the\n"
1509 "variable with __exit* (see linux/init.h) or "
1510 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001511 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001512 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001513 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001514 break;
1515 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001516 case XXXINIT_TO_SOME_INIT:
1517 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001518 prl_from = sec2annotation(fromsec);
1519 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001520 fprintf(stderr,
1521 "The %s %s%s%s references\n"
1522 "a %s %s%s%s.\n"
1523 "If %s is only used by %s then\n"
1524 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001525 from, prl_from, fromsym, from_p,
1526 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001527 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001528 free(prl_from);
1529 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001530 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001531 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001532 prl_from = sec2annotation(fromsec);
1533 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001534 fprintf(stderr,
1535 "The %s %s%s%s references\n"
1536 "a %s %s%s%s.\n"
1537 "This is often seen when error handling "
1538 "in the init function\n"
1539 "uses functionality in the exit path.\n"
1540 "The fix is often to remove the %sannotation of\n"
1541 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001542 from, prl_from, fromsym, from_p,
1543 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001544 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001545 free(prl_from);
1546 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001547 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001548 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001549 prl_from = sec2annotation(fromsec);
1550 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001551 fprintf(stderr,
1552 "The %s %s%s%s references\n"
1553 "a %s %s%s%s.\n"
1554 "This is often seen when error handling "
1555 "in the exit function\n"
1556 "uses functionality in the init path.\n"
1557 "The fix is often to remove the %sannotation of\n"
1558 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001559 from, prl_from, fromsym, from_p,
1560 to, prl_to, tosym, to_p,
1561 prl_to, tosym, to_p);
1562 free(prl_from);
1563 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001564 break;
1565 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001566 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001567 fprintf(stderr,
1568 "The symbol %s is exported and annotated %s\n"
1569 "Fix this by removing the %sannotation of %s "
1570 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001571 tosym, prl_to, prl_to, tosym);
1572 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001573 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301574 case EXTABLE_TO_NON_TEXT:
1575 fatal("There's a special handler for this mismatch type, "
1576 "we should never get here.");
1577 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001578 }
1579 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001580}
1581
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301582static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1583 const struct sectioncheck* const mismatch,
1584 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1585{
1586 const char *tosec;
1587 Elf_Sym *to;
1588 Elf_Sym *from;
1589 const char *tosym;
1590 const char *fromsym;
1591
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301592 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1593 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301594
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001595 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301596 return;
1597
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301598 tosec = sec_name(elf, get_secindex(elf, sym));
1599 to = find_elf_symbol(elf, r->r_addend, sym);
1600 tosym = sym_name(elf, to);
1601
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301602 /* check whitelist - we may ignore it */
1603 if (secref_whitelist(mismatch,
1604 fromsec, fromsym, tosec, tosym)) {
1605 report_sec_mismatch(modname, mismatch,
1606 fromsec, r->r_offset, fromsym,
1607 is_function(from), tosec, tosym,
1608 is_function(to));
1609 }
1610}
1611
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301612static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1613{
1614 if (section_index > elf->num_sections)
1615 fatal("section_index is outside elf->num_sections!\n");
1616
1617 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1618}
1619
1620/*
1621 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1622 * to know the sizeof(struct exception_table_entry) for the target architecture.
1623 */
1624static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301625static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301626{
1627 /*
1628 * If we're currently checking the second relocation within __ex_table,
1629 * that relocation offset tells us the offsetof(struct
1630 * exception_table_entry, fixup) which is equal to sizeof(struct
1631 * exception_table_entry) divided by two. We use that to our advantage
1632 * since there's no portable way to get that size as every architecture
1633 * seems to go with different sized types. Not pretty but better than
1634 * hard-coding the size for every architecture..
1635 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301636 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301637 extable_entry_size = r->r_offset * 2;
1638}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301639
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301640static inline bool is_extable_fault_address(Elf_Rela *r)
1641{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301642 /*
1643 * extable_entry_size is only discovered after we've handled the
1644 * _second_ relocation in __ex_table, so only abort when we're not
1645 * handling the first reloc and extable_entry_size is zero.
1646 */
1647 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301648 fatal("extable_entry size hasn't been discovered!\n");
1649
1650 return ((r->r_offset == 0) ||
1651 (r->r_offset % extable_entry_size == 0));
1652}
1653
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301654#define is_second_extable_reloc(Start, Cur, Sec) \
1655 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1656
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301657static void report_extable_warnings(const char* modname, struct elf_info* elf,
1658 const struct sectioncheck* const mismatch,
1659 Elf_Rela* r, Elf_Sym* sym,
1660 const char* fromsec, const char* tosec)
1661{
1662 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1663 const char* fromsym_name = sym_name(elf, fromsym);
1664 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1665 const char* tosym_name = sym_name(elf, tosym);
1666 const char* from_pretty_name;
1667 const char* from_pretty_name_p;
1668 const char* to_pretty_name;
1669 const char* to_pretty_name_p;
1670
1671 get_pretty_name(is_function(fromsym),
1672 &from_pretty_name, &from_pretty_name_p);
1673 get_pretty_name(is_function(tosym),
1674 &to_pretty_name, &to_pretty_name_p);
1675
1676 warn("%s(%s+0x%lx): Section mismatch in reference"
1677 " from the %s %s%s to the %s %s:%s%s\n",
1678 modname, fromsec, (long)r->r_offset, from_pretty_name,
1679 fromsym_name, from_pretty_name_p,
1680 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1681
1682 if (!match(tosec, mismatch->bad_tosec) &&
1683 is_executable_section(elf, get_secindex(elf, sym)))
1684 fprintf(stderr,
1685 "The relocation at %s+0x%lx references\n"
1686 "section \"%s\" which is not in the list of\n"
1687 "authorized sections. If you're adding a new section\n"
1688 "and/or if this reference is valid, add \"%s\" to the\n"
1689 "list of authorized sections to jump to on fault.\n"
1690 "This can be achieved by adding \"%s\" to \n"
1691 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1692 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1693}
1694
1695static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1696 const struct sectioncheck* const mismatch,
1697 Elf_Rela* r, Elf_Sym* sym,
1698 const char *fromsec)
1699{
1700 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1701
1702 sec_mismatch_count++;
1703
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001704 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301705
1706 if (match(tosec, mismatch->bad_tosec))
1707 fatal("The relocation at %s+0x%lx references\n"
1708 "section \"%s\" which is black-listed.\n"
1709 "Something is seriously wrong and should be fixed.\n"
1710 "You might get more information about where this is\n"
1711 "coming from by using scripts/check_extable.sh %s\n",
1712 fromsec, (long)r->r_offset, tosec, modname);
1713 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1714 if (is_extable_fault_address(r))
1715 fatal("The relocation at %s+0x%lx references\n"
1716 "section \"%s\" which is not executable, IOW\n"
1717 "it is not possible for the kernel to fault\n"
1718 "at that address. Something is seriously wrong\n"
1719 "and should be fixed.\n",
1720 fromsec, (long)r->r_offset, tosec);
1721 else
1722 fatal("The relocation at %s+0x%lx references\n"
1723 "section \"%s\" which is not executable, IOW\n"
1724 "the kernel will fault if it ever tries to\n"
1725 "jump to it. Something is seriously wrong\n"
1726 "and should be fixed.\n",
1727 fromsec, (long)r->r_offset, tosec);
1728 }
1729}
1730
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001731static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001732 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001733{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001734 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301735 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001736
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001737 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301738 if (mismatch->handler)
1739 mismatch->handler(modname, elf, mismatch,
1740 r, sym, fromsec);
1741 else
1742 default_mismatch_handler(modname, elf, mismatch,
1743 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001744 }
1745}
1746
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001747static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001748 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001749{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001750 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001751}
1752
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001753static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001754{
1755 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001756 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001757
1758 switch (r_typ) {
1759 case R_386_32:
1760 r->r_addend = TO_NATIVE(*location);
1761 break;
1762 case R_386_PC32:
1763 r->r_addend = TO_NATIVE(*location) + 4;
1764 /* For CONFIG_RELOCATABLE=y */
1765 if (elf->hdr->e_type == ET_EXEC)
1766 r->r_addend += r->r_offset;
1767 break;
1768 }
1769 return 0;
1770}
1771
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001772#ifndef R_ARM_CALL
1773#define R_ARM_CALL 28
1774#endif
1775#ifndef R_ARM_JUMP24
1776#define R_ARM_JUMP24 29
1777#endif
1778
David A. Longc9698e52014-02-14 22:41:18 +01001779#ifndef R_ARM_THM_CALL
1780#define R_ARM_THM_CALL 10
1781#endif
1782#ifndef R_ARM_THM_JUMP24
1783#define R_ARM_THM_JUMP24 30
1784#endif
1785#ifndef R_ARM_THM_JUMP19
1786#define R_ARM_THM_JUMP19 51
1787#endif
1788
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001789static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001790{
1791 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1792
1793 switch (r_typ) {
1794 case R_ARM_ABS32:
1795 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001796 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001797 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001798 break;
1799 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001800 case R_ARM_CALL:
1801 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001802 case R_ARM_THM_CALL:
1803 case R_ARM_THM_JUMP24:
1804 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001805 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001806 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001807 sechdr->sh_offset +
1808 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001809 break;
1810 default:
1811 return 1;
1812 }
1813 return 0;
1814}
1815
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001816static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001817{
1818 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001819 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001820 unsigned int inst;
1821
1822 if (r_typ == R_MIPS_HI16)
1823 return 1; /* skip this */
1824 inst = TO_NATIVE(*location);
1825 switch (r_typ) {
1826 case R_MIPS_LO16:
1827 r->r_addend = inst & 0xffff;
1828 break;
1829 case R_MIPS_26:
1830 r->r_addend = (inst & 0x03ffffff) << 2;
1831 break;
1832 case R_MIPS_32:
1833 r->r_addend = inst;
1834 break;
1835 }
1836 return 0;
1837}
1838
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001839static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001840 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001841{
1842 Elf_Sym *sym;
1843 Elf_Rela *rela;
1844 Elf_Rela r;
1845 unsigned int r_sym;
1846 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001847
Sam Ravnborgff13f922008-01-23 19:54:27 +01001848 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001849 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1850
Sam Ravnborgff13f922008-01-23 19:54:27 +01001851 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001852 fromsec += strlen(".rela");
1853 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001854 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001855 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001856
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001857 for (rela = start; rela < stop; rela++) {
1858 r.r_offset = TO_NATIVE(rela->r_offset);
1859#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001860 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001861 unsigned int r_typ;
1862 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1863 r_sym = TO_NATIVE(r_sym);
1864 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1865 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1866 } else {
1867 r.r_info = TO_NATIVE(rela->r_info);
1868 r_sym = ELF_R_SYM(r.r_info);
1869 }
1870#else
1871 r.r_info = TO_NATIVE(rela->r_info);
1872 r_sym = ELF_R_SYM(r.r_info);
1873#endif
1874 r.r_addend = TO_NATIVE(rela->r_addend);
1875 sym = elf->symtab_start + r_sym;
1876 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001877 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001878 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301879 if (is_second_extable_reloc(start, rela, fromsec))
1880 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001881 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001882 }
1883}
1884
1885static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001886 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001887{
1888 Elf_Sym *sym;
1889 Elf_Rel *rel;
1890 Elf_Rela r;
1891 unsigned int r_sym;
1892 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001893
Sam Ravnborgff13f922008-01-23 19:54:27 +01001894 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001895 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1896
Sam Ravnborgff13f922008-01-23 19:54:27 +01001897 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001898 fromsec += strlen(".rel");
1899 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001900 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001901 return;
1902
1903 for (rel = start; rel < stop; rel++) {
1904 r.r_offset = TO_NATIVE(rel->r_offset);
1905#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001906 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001907 unsigned int r_typ;
1908 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1909 r_sym = TO_NATIVE(r_sym);
1910 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1911 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1912 } else {
1913 r.r_info = TO_NATIVE(rel->r_info);
1914 r_sym = ELF_R_SYM(r.r_info);
1915 }
1916#else
1917 r.r_info = TO_NATIVE(rel->r_info);
1918 r_sym = ELF_R_SYM(r.r_info);
1919#endif
1920 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001921 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001922 case EM_386:
1923 if (addend_386_rel(elf, sechdr, &r))
1924 continue;
1925 break;
1926 case EM_ARM:
1927 if (addend_arm_rel(elf, sechdr, &r))
1928 continue;
1929 break;
1930 case EM_MIPS:
1931 if (addend_mips_rel(elf, sechdr, &r))
1932 continue;
1933 break;
1934 }
1935 sym = elf->symtab_start + r_sym;
1936 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001937 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001938 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301939 if (is_second_extable_reloc(start, rel, fromsec))
1940 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001941 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001942 }
1943}
1944
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001945/**
1946 * A module includes a number of sections that are discarded
1947 * either when loaded or when used as built-in.
1948 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001949 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001950 * Likewise for modules used built-in the sections marked __exit
1951 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001952 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001953 * The check_sec_ref() function traverses all relocation records
1954 * to find all references to a section that reference a section that will
1955 * be discarded and warns about it.
1956 **/
1957static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001958 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001959{
1960 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001961 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001962
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001963 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001964 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001965 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001966 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001967 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001968 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001969 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001970 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001971 }
1972}
1973
Andi Kleen7d02b492014-02-08 09:01:12 +01001974static char *remove_dot(char *s)
1975{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301976 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001977
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301978 if (n && s[n]) {
1979 size_t m = strspn(s + n + 1, "0123456789");
1980 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001981 s[n] = 0;
Sami Tolvanen7ac204b2020-12-11 10:46:27 -08001982
1983 /* strip trailing .lto */
1984 if (strends(s, ".lto"))
1985 s[strlen(s) - 4] = '\0';
Andi Kleen7d02b492014-02-08 09:01:12 +01001986 }
1987 return s;
1988}
1989
Masahiro Yamada8b185742018-05-09 18:50:40 +09001990static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 const char *symname;
1993 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001994 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001995 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 struct module *mod;
1997 struct elf_info info = { };
1998 Elf_Sym *sym;
1999
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002000 if (!parse_elf(&info, modname))
2001 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002003 {
2004 char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002006 /* strip trailing .o */
2007 tmp = NOFAIL(strdup(modname));
2008 tmp[strlen(tmp) - 2] = '\0';
Sami Tolvanen7ac204b2020-12-11 10:46:27 -08002009 /* strip trailing .lto */
2010 if (strends(tmp, ".lto"))
2011 tmp[strlen(tmp) - 4] = '\0';
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09002012 mod = new_module(tmp);
2013 free(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002016 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002017 license = get_modinfo(&info, "license");
2018 if (!license)
Masahiro Yamada1d6cd3922020-12-01 19:34:16 +09002019 error("missing MODULE_LICENSE() in %s\n", modname);
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002020 while (license) {
2021 if (license_is_gpl_compatible(license))
2022 mod->gpl_compatible = 1;
2023 else {
2024 mod->gpl_compatible = 0;
2025 break;
2026 }
2027 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002028 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002029
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002030 namespace = get_modinfo(&info, "import_ns");
2031 while (namespace) {
2032 add_namespace(&mod->imported_namespaces, namespace);
2033 namespace = get_next_modinfo(&info, "import_ns",
2034 namespace);
2035 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002036 }
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002039 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002041 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 handle_moddevtable(mod, &info, sym, symname);
2043 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002044
Matthias Maennich69923202019-10-18 10:31:42 +01002045 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2046 symname = remove_dot(info.strtab + sym->st_name);
2047
Masahiro Yamada17436942019-11-15 02:42:24 +09002048 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002049 if (strstarts(symname, "__kstrtabns_"))
2050 sym_update_namespace(symname + strlen("__kstrtabns_"),
2051 namespace_from_kstrtabns(&info,
2052 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002053
2054 if (strstarts(symname, "__crc_"))
2055 handle_modversion(mod, &info, sym,
2056 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002057 }
2058
Denis Efremov15bfc232019-08-01 09:06:57 +03002059 // check for static EXPORT_SYMBOL_* functions && global vars
2060 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2061 unsigned char bind = ELF_ST_BIND(sym->st_info);
2062
2063 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2064 struct symbol *s =
2065 find_symbol(remove_dot(info.strtab +
2066 sym->st_name));
2067
2068 if (s)
2069 s->is_static = 0;
2070 }
2071 }
2072
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002073 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002075 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002076 version = get_modinfo(&info, "version");
2077 if (version || all_versions)
2078 get_src_version(modname, mod->srcversion,
2079 sizeof(mod->srcversion) - 1);
2080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 parse_elf_finish(&info);
2083
Rusty Russell8c8ef422009-03-31 13:05:34 -06002084 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 * never passed as an argument to an exported function, so
2086 * the automatic versioning doesn't pick it up, but it's really
2087 * important anyhow */
2088 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002089 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Rusty Russell712f9b42013-04-04 17:37:38 +10302092static void read_symbols_from_files(const char *filename)
2093{
2094 FILE *in = stdin;
2095 char fname[PATH_MAX];
2096
2097 if (strcmp(filename, "-") != 0) {
2098 in = fopen(filename, "r");
2099 if (!in)
2100 fatal("Can't open filenames file %s: %m", filename);
2101 }
2102
2103 while (fgets(fname, PATH_MAX, in) != NULL) {
2104 if (strends(fname, "\n"))
2105 fname[strlen(fname)-1] = '\0';
2106 read_symbols(fname);
2107 }
2108
2109 if (in != stdin)
2110 fclose(in);
2111}
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#define SZ 500
2114
2115/* We first write the generated file into memory using the
2116 * following helper, then compare to the file on disk and
2117 * only update the later if anything changed */
2118
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002119void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2120 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 char tmp[SZ];
2123 int len;
2124 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 va_start(ap, fmt);
2127 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002128 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 va_end(ap);
2130}
2131
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002132void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002135 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002136 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 strncpy(buf->p + buf->pos, s, len);
2139 buf->pos += len;
2140}
2141
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002142static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2143{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002144 switch (exp) {
2145 case export_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002146 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002147 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002148 break;
2149 case export_unused_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002150 error("GPL-incompatible module %s.ko uses GPL-only symbol marked UNUSED '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002151 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002152 break;
2153 case export_gpl_future:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002154 warn("GPL-incompatible module %s.ko uses future GPL-only symbol '%s'\n",
2155 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002156 break;
2157 case export_plain:
2158 case export_unused:
2159 case export_unknown:
2160 /* ignore */
2161 break;
2162 }
2163}
2164
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002165static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002166{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002167 switch (exp) {
2168 case export_unused:
2169 case export_unused_gpl:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002170 warn("module %s.ko uses symbol '%s' marked UNUSED\n",
2171 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002172 break;
2173 default:
2174 /* ignore */
2175 break;
2176 }
2177}
2178
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002179static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002180{
2181 struct symbol *s, *exp;
2182
2183 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002184 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002185 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002186 if (!exp || exp->module == mod) {
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002187 if (have_vmlinux && !s->weak)
Jessica Yu93c95e52020-03-06 17:02:05 +01002188 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2189 "\"%s\" [%s.ko] undefined!\n",
2190 s->name, mod->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002191 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002192 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002193 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002194 if (basename)
2195 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002196 else
2197 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002198
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002199 if (exp->namespace &&
2200 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002201 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2202 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2203 basename, exp->name, exp->namespace);
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002204 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002205 }
2206
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002207 if (!mod->gpl_compatible)
2208 check_for_gpl_usage(exp->export, basename, exp->name);
2209 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002210 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002211}
2212
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002213static void check_modname_len(struct module *mod)
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002214{
2215 const char *mod_name;
2216
2217 mod_name = strrchr(mod->name, '/');
2218 if (mod_name == NULL)
2219 mod_name = mod->name;
2220 else
2221 mod_name++;
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002222 if (strlen(mod_name) >= MODULE_NAME_LEN)
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002223 error("module name is too long [%s.ko]\n", mod->name);
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002224}
2225
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002226/**
2227 * Header for the generated file
2228 **/
2229static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002232 /*
2233 * Include build-salt.h after module.h in order to
2234 * inherit the definitions.
2235 */
Leon Romanovsky51161bf2020-04-19 18:55:06 +03002236 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002237 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 buf_printf(b, "#include <linux/vermagic.h>\n");
2239 buf_printf(b, "#include <linux/compiler.h>\n");
2240 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002241 buf_printf(b, "BUILD_SALT;\n");
2242 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002244 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302246 buf_printf(b, "__visible struct module __this_module\n");
Joe Perches33def842020-10-21 19:36:07 -07002247 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002248 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002250 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (mod->has_cleanup)
2252 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002253 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002255 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 buf_printf(b, "};\n");
2257}
2258
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002259static void add_intree_flag(struct buffer *b, int is_intree)
2260{
2261 if (is_intree)
2262 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2263}
2264
Andi Kleencaf75012018-01-25 15:50:28 -08002265/* Cannot check for assembler */
2266static void add_retpoline(struct buffer *b)
2267{
WANG Chaoe4f35892018-12-11 00:37:25 +08002268 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002269 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2270 buf_printf(b, "#endif\n");
2271}
2272
Trevor Keith5c725132009-09-22 16:43:38 -07002273static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002274{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002275 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002276 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2277}
2278
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002279/**
2280 * Record CRCs for unresolved symbols
2281 **/
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002282static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
2284 struct symbol *s, *exp;
2285
2286 for (s = mod->unres; s; s = s->next) {
2287 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002288 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 s->module = exp->module;
2291 s->crc_valid = exp->crc_valid;
2292 s->crc = exp->crc;
2293 }
2294
2295 if (!modversions)
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002296 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 buf_printf(b, "\n");
2299 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Joe Perches33def842020-10-21 19:36:07 -07002300 buf_printf(b, "__used __section(\"__versions\") = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002303 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002306 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 s->name, mod->name);
2308 continue;
2309 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302310 if (strlen(s->name) >= MODULE_NAME_LEN) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002311 error("too long symbol \"%s\" [%s.ko]\n",
2312 s->name, mod->name);
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302313 break;
2314 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002315 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302316 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318
2319 buf_printf(b, "};\n");
2320}
2321
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002322static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
2324 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 int first = 1;
2326
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002327 /* Clear ->seen flag of modules that own symbols needed by this. */
2328 for (s = mod->unres; s; s = s->next)
2329 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002330 s->module->seen = s->module->is_vmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002333 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002335 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (!s->module)
2337 continue;
2338
2339 if (s->module->seen)
2340 continue;
2341
2342 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002343 p = strrchr(s->module->name, '/');
2344 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002345 p++;
2346 else
2347 p = s->module->name;
2348 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 first = 0;
2350 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002351 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002354static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 if (mod->srcversion[0]) {
2357 buf_printf(b, "\n");
2358 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2359 mod->srcversion);
2360 }
2361}
2362
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002363static void write_buf(struct buffer *b, const char *fname)
2364{
2365 FILE *file;
2366
2367 file = fopen(fname, "w");
2368 if (!file) {
2369 perror(fname);
2370 exit(1);
2371 }
2372 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2373 perror(fname);
2374 exit(1);
2375 }
2376 if (fclose(file) != 0) {
2377 perror(fname);
2378 exit(1);
2379 }
2380}
2381
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002382static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 char *tmp;
2385 FILE *file;
2386 struct stat st;
2387
2388 file = fopen(fname, "r");
2389 if (!file)
2390 goto write;
2391
2392 if (fstat(fileno(file), &st) < 0)
2393 goto close_write;
2394
2395 if (st.st_size != b->pos)
2396 goto close_write;
2397
2398 tmp = NOFAIL(malloc(b->pos));
2399 if (fread(tmp, 1, b->pos, file) != b->pos)
2400 goto free_write;
2401
2402 if (memcmp(tmp, b->p, b->pos) != 0)
2403 goto free_write;
2404
2405 free(tmp);
2406 fclose(file);
2407 return;
2408
2409 free_write:
2410 free(tmp);
2411 close_write:
2412 fclose(file);
2413 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002414 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Ram Paibd5cbce2006-06-08 22:12:53 -07002417/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002418 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002419 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002420static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002422 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002424 buf = read_text_file(fname);
2425 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /* No symbol versions, silently ignore */
2427 return;
2428
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002429 pos = buf;
2430
2431 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002432 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 unsigned int crc;
2434 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002435 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 if (!(symname = strchr(line, '\t')))
2438 goto fail;
2439 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002440 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto fail;
2442 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002443 if (!(export = strchr(modname, '\t')))
2444 goto fail;
2445 *export++ = '\0';
2446 if (!(namespace = strchr(export, '\t')))
2447 goto fail;
2448 *namespace++ = '\0';
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 crc = strtoul(line, &d, 16);
2451 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2452 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002453 mod = find_module(modname);
2454 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002455 mod = new_module(modname);
Masahiro Yamada52c34162020-06-01 14:57:05 +09002456 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002458 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002459 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002460 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002461 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002463 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return;
2465fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002466 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 fatal("parse error in symbol dump file\n");
2468}
2469
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002470/* For normal builds always dump all symbols.
2471 * For external modules only dump symbols
2472 * that are not read from kernel Module.symvers.
2473 **/
2474static int dump_sym(struct symbol *sym)
2475{
2476 if (!external_module)
2477 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002478 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002479 return 0;
2480 return 1;
2481}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002482
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002483static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
2485 struct buffer buf = { };
2486 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002487 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 int n;
2489
2490 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2491 symbol = symbolhash[n];
2492 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002493 if (dump_sym(symbol)) {
2494 namespace = symbol->namespace;
2495 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2496 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002497 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002498 export_str(symbol->export),
2499 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 symbol = symbol->next;
2502 }
2503 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002504 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002505 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002508static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002509{
2510 struct module *mod;
2511 struct namespace_list *ns;
2512 struct buffer ns_deps_buf = {};
2513
2514 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002515
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002516 if (mod->from_dump || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002517 continue;
2518
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002519 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002520
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002521 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2522 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002523
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002524 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002525 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002526
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002527 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002528 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002529}
2530
Masahiro Yamada79247992020-06-01 14:57:07 +09002531struct dump_list {
2532 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002533 const char *file;
2534};
2535
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002536int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
2538 struct module *mod;
2539 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002540 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302541 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 int opt;
Denis Efremov15bfc232019-08-01 09:06:57 +03002543 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002544 struct dump_list *dump_read_start = NULL;
2545 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002547 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002548 switch (opt) {
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002549 case 'e':
2550 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002551 break;
2552 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002553 *dump_read_iter =
2554 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2555 (*dump_read_iter)->file = optarg;
2556 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002557 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002558 case 'm':
2559 modversions = 1;
2560 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302561 case 'n':
2562 ignore_missing_files = 1;
2563 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002564 case 'o':
2565 dump_write = optarg;
2566 break;
2567 case 'a':
2568 all_versions = 1;
2569 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302570 case 'T':
2571 files_source = optarg;
2572 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002573 case 'w':
2574 warn_unresolved = 1;
2575 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302576 case 'E':
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002577 sec_mismatch_warn_only = false;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302578 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002579 case 'N':
2580 allow_missing_ns_imports = 1;
2581 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002582 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002583 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002584 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002585 default:
2586 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588 }
2589
Masahiro Yamada79247992020-06-01 14:57:07 +09002590 while (dump_read_start) {
2591 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002592
Masahiro Yamada79247992020-06-01 14:57:07 +09002593 read_dump(dump_read_start->file);
2594 tmp = dump_read_start->next;
2595 free(dump_read_start);
2596 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002599 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Rusty Russell712f9b42013-04-04 17:37:38 +10302602 if (files_source)
2603 read_symbols_from_files(files_source);
2604
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002605 /*
2606 * When there's no vmlinux, don't print warnings about
2607 * unresolved symbols (since there'll be too many ;)
2608 */
2609 if (!have_vmlinux)
2610 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2611
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002612 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302613 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002614
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002615 if (mod->is_vmlinux || mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002616 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 buf.pos = 0;
2619
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002620 check_modname_len(mod);
2621 check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002624 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002625 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002626 add_staging_flag(&buf, mod->name);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002627 add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002628 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 add_moddevtable(&buf, mod);
2630 add_srcversion(&buf, mod);
2631
2632 sprintf(fname, "%s.mod.c", mod->name);
2633 write_if_changed(&buf, fname);
2634 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002635
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002636 if (missing_namespace_deps)
2637 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (dump_write)
2640 write_dump(dump_write);
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002641 if (sec_mismatch_count && !sec_mismatch_warn_only)
2642 error("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002643 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002644 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002645 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002646
Masahiro Yamada47346e92019-09-24 21:07:40 +09002647 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002648 if (s->is_static)
Quentin Perretb9ed8472020-12-01 16:52:22 +00002649 error("\"%s\" [%s] is a static %s\n",
2650 s->name, s->module->name,
2651 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002652 }
2653 }
2654
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002655 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002657 return error_occurred ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}