blob: 10c3fba26f03a34a34cf140d90f8f7f0623a1eb3 [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 {
Christoph Hellwig36794822021-02-02 13:13:34 +010045 export_plain,
46 export_gpl,
47 export_unknown
Sam Ravnborgc96fca22006-07-01 11:44:23 +020048};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080050/* In kernel, this size is defined in linux/module.h;
51 * here we use Elf_Addr instead of long for covering cross-compile
52 */
53
54#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
55
Jessica Yu93c95e52020-03-06 17:02:05 +010056void __attribute__((format(printf, 2, 3)))
57modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 va_list arglist;
60
Jessica Yu93c95e52020-03-06 17:02:05 +010061 switch (loglevel) {
62 case LOG_WARN:
63 fprintf(stderr, "WARNING: ");
64 break;
65 case LOG_ERROR:
66 fprintf(stderr, "ERROR: ");
67 break;
68 case LOG_FATAL:
69 fprintf(stderr, "FATAL: ");
70 break;
71 default: /* invalid loglevel, ignore */
72 break;
73 }
74
75 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 va_start(arglist, fmt);
78 vfprintf(stderr, fmt, arglist);
79 va_end(arglist);
80
Jessica Yu93c95e52020-03-06 17:02:05 +010081 if (loglevel == LOG_FATAL)
82 exit(1);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090083 if (loglevel == LOG_ERROR)
84 error_occurred = true;
Matthew Wilcox2a116652006-10-07 05:35:32 -060085}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087void *do_nofail(void *ptr, const char *expr)
88{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010089 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +010090 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +010091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return ptr;
93}
94
Masahiro Yamadaac5100f2020-06-01 14:57:17 +090095char *read_text_file(const char *filename)
96{
97 struct stat st;
98 size_t nbytes;
99 int fd;
100 char *buf;
101
102 fd = open(filename, O_RDONLY);
103 if (fd < 0) {
104 perror(filename);
105 exit(1);
106 }
107
108 if (fstat(fd, &st) < 0) {
109 perror(filename);
110 exit(1);
111 }
112
113 buf = NOFAIL(malloc(st.st_size + 1));
114
115 nbytes = st.st_size;
116
117 while (nbytes) {
118 ssize_t bytes_read;
119
120 bytes_read = read(fd, buf, nbytes);
121 if (bytes_read < 0) {
122 perror(filename);
123 exit(1);
124 }
125
126 nbytes -= bytes_read;
127 }
128 buf[st.st_size] = '\0';
129
130 close(fd);
131
132 return buf;
133}
134
135char *get_line(char **stringp)
136{
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200137 char *orig = *stringp, *next;
138
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900139 /* do not return the unwanted extra line at EOF */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200140 if (!orig || *orig == '\0')
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900141 return NULL;
142
Wolfram Sang6020db52020-07-26 23:44:19 +0200143 /* don't use strsep here, it is not available everywhere */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200144 next = strchr(orig, '\n');
145 if (next)
146 *next++ = '\0';
147
148 *stringp = next;
149
150 return orig;
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static struct module *modules;
155
Masahiro Yamada8b185742018-05-09 18:50:40 +0900156static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct module *mod;
159
160 for (mod = modules; mod; mod = mod->next)
161 if (strcmp(mod->name, modname) == 0)
162 break;
163 return mod;
164}
165
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030166static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct module *mod;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100169
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900170 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 memset(mod, 0, sizeof(*mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* add to list */
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900174 strcpy(mod->name, modname);
Masahiro Yamada4de7b622020-06-01 14:57:30 +0900175 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200176 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mod->next = modules;
178 modules = mod;
179
Masahiro Yamada858b9372020-06-01 14:57:28 +0900180 if (mod->is_vmlinux)
181 have_vmlinux = 1;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return mod;
184}
185
186/* A hash of all exported symbols,
187 * struct symbol is also used for lists of unresolved symbols */
188
189#define SYMBOL_HASH_SIZE 1024
190
191struct symbol {
192 struct symbol *next;
193 struct module *module;
194 unsigned int crc;
195 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900196 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300198 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700199 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500200 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
203static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
204
205/* This is based on the hash agorithm from gdbm, via tdb */
206static inline unsigned int tdb_hash(const char *name)
207{
208 unsigned value; /* Used to compute the hash value. */
209 unsigned i; /* Used to cycle through random values. */
210
211 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100212 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
214
215 return (1103515243 * value + 12345);
216}
217
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100218/**
219 * Allocate a new symbols for use in the hash of exported symbols or
220 * the list of unresolved symbols per module
221 **/
222static struct symbol *alloc_symbol(const char *name, unsigned int weak,
223 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
226
227 memset(s, 0, sizeof(*s));
228 strcpy(s->name, name);
229 s->weak = weak;
230 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300231 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return s;
233}
234
235/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700236static struct symbol *new_symbol(const char *name, struct module *module,
237 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900242 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
243
244 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100247static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct symbol *s;
250
251 /* For our purposes, .foo matches foo. PPC64 needs this. */
252 if (name[0] == '.')
253 name++;
254
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100255 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (strcmp(s->name, name) == 0)
257 return s;
258 }
259 return NULL;
260}
261
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100262static bool contains_namespace(struct namespace_list *list,
263 const char *namespace)
264{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900265 for (; list; list = list->next)
266 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100267 return true;
268
269 return false;
270}
271
272static void add_namespace(struct namespace_list **list, const char *namespace)
273{
274 struct namespace_list *ns_entry;
275
276 if (!contains_namespace(*list, namespace)) {
277 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
278 strlen(namespace) + 1));
279 strcpy(ns_entry->namespace, namespace);
280 ns_entry->next = *list;
281 *list = ns_entry;
282 }
283}
284
285static bool module_imports_namespace(struct module *module,
286 const char *namespace)
287{
288 return contains_namespace(module->imported_namespaces, namespace);
289}
290
Mathias Krause7a3ee752014-08-27 20:28:53 +0930291static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700292 const char *str;
293 enum export export;
294} export_list[] = {
295 { .str = "EXPORT_SYMBOL", .export = export_plain },
296 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700297 { .str = "(unknown)", .export = export_unknown },
298};
299
300
301static const char *export_str(enum export ex)
302{
303 return export_list[ex].str;
304}
305
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100306static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700307{
308 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100309
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200310 if (!s)
311 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700312 for (i = 0; export_list[i].export != export_unknown; i++) {
313 if (strcmp(export_list[i].str, s) == 0)
314 return export_list[i].export;
315 }
316 return export_unknown;
317}
318
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900319static void *sym_get_data_by_offset(const struct elf_info *info,
320 unsigned int secindex, unsigned long offset)
Masahiro Yamada6124c042017-09-06 16:19:05 -0700321{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800322 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900323
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900324 if (info->hdr->e_type != ET_REL)
325 offset -= sechdr->sh_addr;
326
327 return (void *)info->hdr + sechdr->sh_offset + offset;
328}
329
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900330static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
331{
332 return sym_get_data_by_offset(info, get_secindex(info, sym),
333 sym->st_value);
334}
335
Masahiro Yamada565587d2020-05-25 14:47:05 +0900336static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
337{
338 return sym_get_data_by_offset(info, info->secindex_strings,
339 sechdr->sh_name);
340}
341
342static const char *sec_name(const struct elf_info *info, int secindex)
343{
344 return sech_name(info, &info->sechdrs[secindex]);
345}
346
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200347#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
348
349static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
350{
351 const char *secname = sec_name(elf, sec);
352
353 if (strstarts(secname, "___ksymtab+"))
354 return export_plain;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200355 else if (strstarts(secname, "___ksymtab_gpl+"))
356 return export_gpl;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200357 else
358 return export_unknown;
359}
360
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200361static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700362{
363 if (sec == elf->export_sec)
364 return export_plain;
365 else if (sec == elf->export_gpl_sec)
366 return export_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700367 else
368 return export_unknown;
369}
370
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900371static const char *namespace_from_kstrtabns(const struct elf_info *info,
372 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100373{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900374 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100375 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100376}
377
Matthias Maennicha2b11182019-10-18 10:31:40 +0100378static void sym_update_namespace(const char *symname, const char *namespace)
379{
380 struct symbol *s = find_symbol(symname);
381
382 /*
383 * That symbol should have been created earlier and thus this is
384 * actually an assertion.
385 */
386 if (!s) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +0900387 error("Could not update namespace(%s) for symbol %s\n",
388 namespace, symname);
Matthias Maennicha2b11182019-10-18 10:31:40 +0100389 return;
390 }
391
392 free(s->namespace);
393 s->namespace =
394 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
395}
396
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100397/**
398 * Add an exported symbol - it may have already been added without a
399 * CRC, in this case just update the CRC
400 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100401static struct symbol *sym_add_exported(const char *name, struct module *mod,
402 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct symbol *s = find_symbol(name);
405
406 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700407 s = new_symbol(name, mod, export);
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900408 } else if (!external_module || s->module->is_vmlinux ||
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900409 s->module == mod) {
410 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
411 mod->name, name, s->module->name,
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900412 s->module->is_vmlinux ? "" : ".ko");
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900413 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900415
416 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700417 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100418 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Masahiro Yamada17436942019-11-15 02:42:24 +0900421static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100422{
423 struct symbol *s = find_symbol(name);
424
Masahiro Yamada17436942019-11-15 02:42:24 +0900425 /*
426 * Ignore stand-alone __crc_*, which might be auto-generated symbols
427 * such as __*_veneer in ARM ELF.
428 */
429 if (!s)
430 return;
431
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100432 s->crc = crc;
433 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900436static void *grab_file(const char *filename, size_t *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930439 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int fd;
441
442 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930443 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930445 if (fstat(fd, &st))
446 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 *size = st.st_size;
449 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930451failed:
452 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (map == MAP_FAILED)
454 return NULL;
455 return map;
456}
457
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900458static void release_file(void *file, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 munmap(file, size);
461}
462
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100463static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100466 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 Elf_Shdr *sechdrs;
468 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200469 const char *secstrings;
470 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 hdr = grab_file(filename, &info->size);
473 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930474 if (ignore_missing_files) {
475 fprintf(stderr, "%s: %s (ignored)\n", filename,
476 strerror(errno));
477 return 0;
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200480 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100483 if (info->size < sizeof(*hdr)) {
484 /* file too small, assume this is an empty .o file */
485 return 0;
486 }
487 /* Is this a valid ELF file? */
488 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
489 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
490 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
491 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
492 /* Not an ELF file - silently ignore it */
493 return 0;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200496 hdr->e_type = TO_NATIVE(hdr->e_type);
497 hdr->e_machine = TO_NATIVE(hdr->e_machine);
498 hdr->e_version = TO_NATIVE(hdr->e_version);
499 hdr->e_entry = TO_NATIVE(hdr->e_entry);
500 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
501 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
502 hdr->e_flags = TO_NATIVE(hdr->e_flags);
503 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
504 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
505 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
506 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
507 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
508 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 sechdrs = (void *)hdr + hdr->e_shoff;
510 info->sechdrs = sechdrs;
511
Petr Stetiara83710e2007-08-27 12:15:07 +0200512 /* Check if file offset is correct */
513 if (hdr->e_shoff > info->size) {
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900514 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
515 (unsigned long)hdr->e_shoff, filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200516 return 0;
517 }
518
Anders Kaseorg68457562011-05-19 16:55:27 -0600519 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200520 /*
521 * There are more than 64k sections,
522 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200523 */
524 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
525 }
526 else {
527 info->num_sections = hdr->e_shnum;
528 }
529 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600530 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200531 }
532 else {
533 info->secindex_strings = hdr->e_shstrndx;
534 }
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200537 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200538 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
539 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
540 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
541 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
542 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
543 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
544 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
545 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
546 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
547 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200550 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
551 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700552 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900553 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Tejun Heo56fc82c2009-02-06 00:48:02 +0900555 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100556 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
557 "sizeof(*hrd)=%zu\n", filename,
558 (unsigned long)sechdrs[i].sh_offset,
559 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100560 return 0;
561 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700562 secname = secstrings + sechdrs[i].sh_name;
563 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900564 if (nobits)
565 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
567 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700568 } else if (strcmp(secname, "__ksymtab") == 0)
569 info->export_sec = i;
570 else if (strcmp(secname, "__ksymtab_gpl") == 0)
571 info->export_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700572
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200573 if (sechdrs[i].sh_type == SHT_SYMTAB) {
574 unsigned int sh_link_idx;
575 symtab_idx = i;
576 info->symtab_start = (void *)hdr +
577 sechdrs[i].sh_offset;
578 info->symtab_stop = (void *)hdr +
579 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600580 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200581 info->strtab = (void *)hdr +
582 sechdrs[sh_link_idx].sh_offset;
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200585 /* 32bit section no. table? ("more than 64k sections") */
586 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
587 symtab_shndx_idx = i;
588 info->symtab_shndx_start = (void *)hdr +
589 sechdrs[i].sh_offset;
590 info->symtab_shndx_stop = (void *)hdr +
591 sechdrs[i].sh_offset + sechdrs[i].sh_size;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100594 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100595 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /* Fix endianness in symbols */
598 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
599 sym->st_shndx = TO_NATIVE(sym->st_shndx);
600 sym->st_name = TO_NATIVE(sym->st_name);
601 sym->st_value = TO_NATIVE(sym->st_value);
602 sym->st_size = TO_NATIVE(sym->st_size);
603 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200604
605 if (symtab_shndx_idx != ~0U) {
606 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600607 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200608 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600609 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200610 symtab_idx);
611 /* Fix endianness */
612 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
613 p++)
614 *p = TO_NATIVE(*p);
615 }
616
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100617 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100620static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 release_file(info->hdr, info->size);
623}
624
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200625static int ignore_undef_symbol(struct elf_info *info, const char *symname)
626{
627 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900628 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200629 return 1;
630 /* ignore global offset table */
631 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
632 return 1;
633 if (info->hdr->e_machine == EM_PPC)
634 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900635 if (strstarts(symname, "_restgpr_") ||
636 strstarts(symname, "_savegpr_") ||
637 strstarts(symname, "_rest32gpr_") ||
638 strstarts(symname, "_save32gpr_") ||
639 strstarts(symname, "_restvr_") ||
640 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200641 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000642 if (info->hdr->e_machine == EM_PPC64)
643 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900644 if (strstarts(symname, "_restgpr0_") ||
645 strstarts(symname, "_savegpr0_") ||
646 strstarts(symname, "_restvr_") ||
647 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100648 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000649 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200650 /* Do not ignore this symbol */
651 return 0;
652}
653
Masahiro Yamada17436942019-11-15 02:42:24 +0900654static void handle_modversion(const struct module *mod,
655 const struct elf_info *info,
656 const Elf_Sym *sym, const char *symname)
657{
658 unsigned int crc;
659
660 if (sym->st_shndx == SHN_UNDEF) {
661 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900662 symname, mod->name, mod->is_vmlinux ? "" : ".ko");
Masahiro Yamada17436942019-11-15 02:42:24 +0900663 return;
664 }
665
666 if (sym->st_shndx == SHN_ABS) {
667 crc = sym->st_value;
668 } else {
669 unsigned int *crcp;
670
671 /* symbol points to the CRC in the ELF object */
672 crcp = sym_get_data(info, sym);
673 crc = TO_NATIVE(*crcp);
674 }
675 sym_set_crc(symname, crc);
676}
677
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900678static void handle_symbol(struct module *mod, struct elf_info *info,
679 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200681 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900682 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200683
Masahiro Yamada33795762020-06-01 14:57:24 +0900684 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200685 export = export_from_secname(info, get_secindex(info, sym));
686 else
687 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 switch (sym->st_shndx) {
690 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900691 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100692 /* Should warn here, but modpost runs before the linker */
693 } else
694 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 case SHN_UNDEF:
697 /* undefined symbol */
698 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
699 ELF_ST_BIND(sym->st_info) != STB_WEAK)
700 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200701 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (info->hdr->e_machine == EM_SPARC ||
704 info->hdr->e_machine == EM_SPARCV9) {
705 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700706 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100708 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700709 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100710 munged[0] = '_';
711 munged[1] = toupper(munged[1]);
712 symname = munged;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100715
Rusty Russellb92021b2013-03-15 15:04:17 +1030716 mod->unres = alloc_symbol(symname,
717 ELF_ST_BIND(sym->st_info) == STB_WEAK,
718 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 break;
720 default:
721 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900722 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100723 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100724 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900726 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900728 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 mod->has_cleanup = 1;
730 break;
731 }
732}
733
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100734/**
735 * Parse tag=value strings from .modinfo section
736 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static char *next_string(char *string, unsigned long *secsize)
738{
739 /* Skip non-zero chars */
740 while (string[0]) {
741 string++;
742 if ((*secsize)-- <= 1)
743 return NULL;
744 }
745
746 /* Skip any zero padding. */
747 while (!string[0]) {
748 string++;
749 if ((*secsize)-- <= 1)
750 return NULL;
751 }
752 return string;
753}
754
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900755static char *get_next_modinfo(struct elf_info *info, const char *tag,
756 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 char *p;
759 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900760 char *modinfo = info->modinfo;
761 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900763 if (prev) {
764 size -= prev - modinfo;
765 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200766 }
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 for (p = modinfo; p; p = next_string(p, &size)) {
769 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
770 return p + taglen + 1;
771 }
772 return NULL;
773}
774
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900775static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200776
777{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900778 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200779}
780
Sam Ravnborg93684d32006-02-19 11:53:35 +0100781/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100782 * Test if string s ends in string sub
783 * return 0 if match
784 **/
785static int strrcmp(const char *s, const char *sub)
786{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100787 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100788
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100789 if (!s || !sub)
790 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100791
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100792 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100793 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100794
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100795 if ((slen == 0) || (sublen == 0))
796 return 1;
797
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100798 if (sublen > slen)
799 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100800
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100801 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100802}
803
Sam Ravnborgff13f922008-01-23 19:54:27 +0100804static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
805{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100806 if (sym)
807 return elf->strtab + sym->st_name;
808 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100809 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100810}
811
Sam Ravnborg10668222008-01-13 22:21:31 +0100812/* The pattern is an array of simple patterns.
813 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100814 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100815 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930816 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100817 */
Trevor Keith5c725132009-09-22 16:43:38 -0700818static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100819{
820 const char *p;
821 while (*pat) {
822 p = *pat++;
823 const char *endp = p + strlen(p) - 1;
824
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930825 /* "*foo*" */
826 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300827 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
828 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930829
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930830 free(bare);
831 if (here != NULL)
832 return 1;
833 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100834 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930835 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100836 if (strrcmp(sym, p + 1) == 0)
837 return 1;
838 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100839 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100840 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100841 if (strncmp(sym, p, strlen(p) - 1) == 0)
842 return 1;
843 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100844 /* no wildcards */
845 else {
846 if (strcmp(p, sym) == 0)
847 return 1;
848 }
849 }
850 /* no match */
851 return 0;
852}
853
Sam Ravnborg10668222008-01-13 22:21:31 +0100854/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930855static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200856{
857 ".comment*",
858 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930859 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800860 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000861 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200862 ".mdebug*", /* alpha, score, mips etc. */
863 ".pdr", /* alpha, score, mips etc. */
864 ".stab*",
865 ".note*",
866 ".got*",
867 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400868 ".xt.prop", /* xtensa */
869 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030870 ".arcextmap*", /* arc */
871 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030872 ".cmem*", /* EZchip */
873 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100874 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600875 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200876 NULL
877};
Sam Ravnborg10668222008-01-13 22:21:31 +0100878
Sam Ravnborge241a632008-01-28 20:13:13 +0100879/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400880 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100881 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400882 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100883 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400884static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900885 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100886{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400887 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100888
Anders Kaseorgb614a692009-04-23 16:49:33 -0400889 if (sechdr->sh_type == SHT_PROGBITS &&
890 !(sechdr->sh_flags & SHF_ALLOC) &&
891 !match(sec, section_white_list)) {
892 warn("%s (%s): unexpected non-allocatable section.\n"
893 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
894 "Note that for example <linux/init.h> contains\n"
895 "section definitions for use in .S files.\n\n",
896 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100897 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100898}
899
900
901
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100902#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930903 ".init.setup", ".init.rodata", ".meminit.rodata", \
904 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100905#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930906 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100907
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100908#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930909 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100910#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930911 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100912
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200913#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930914 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
915 ".pci_fixup_enable", ".pci_fixup_resume", \
916 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200917
Paul Gortmakere24f6622013-06-19 19:30:48 -0400918#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
919#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100920
921#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
922#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100923
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930924#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930925#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Thomas Gleixner65538962020-03-09 22:47:17 +0100926 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930927#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400928 ".fixup", ".entry.text", ".exception.text", ".text.*", \
929 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100930
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000931#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000932#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100933
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000934#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000935#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100936
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930937#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
938 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
939
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100940/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930941static const char *const init_data_sections[] =
942 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100943
944/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930945static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100946
947/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930948static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100949 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100950
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930951/* all text sections */
952static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
953
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100954/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930955static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100956
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100957
958/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100959#define DEFAULT_SYMBOL_WHITE_LIST \
960 "*driver", \
961 "*_template", /* scsi uses *_template a lot */ \
962 "*_timer", /* arm uses ops structures named _timer a lot */ \
963 "*_sht", /* scsi also used *_sht to some extent */ \
964 "*_ops", \
965 "*_probe", \
966 "*_probe_one", \
967 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100968
Mathias Krause7a3ee752014-08-27 20:28:53 +0930969static const char *const head_sections[] = { ".head.text*", NULL };
970static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100971 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930972static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100973
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100974enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100975 TEXT_TO_ANY_INIT,
976 DATA_TO_ANY_INIT,
977 TEXT_TO_ANY_EXIT,
978 DATA_TO_ANY_EXIT,
979 XXXINIT_TO_SOME_INIT,
980 XXXEXIT_TO_SOME_EXIT,
981 ANY_INIT_TO_ANY_EXIT,
982 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100983 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930984 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100985};
986
Quentin Casasnovase5d8f592015-04-13 20:55:15 +0930987/**
988 * Describe how to match sections on different criterias:
989 *
990 * @fromsec: Array of sections to be matched.
991 *
992 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
993 * this array is forbidden (black-list). Can be empty.
994 *
995 * @good_tosec: Relocations applied to a section in @fromsec must be
996 * targetting sections in this array (white-list). Can be empty.
997 *
998 * @mismatch: Type of mismatch.
999 *
1000 * @symbol_white_list: Do not match a relocation to a symbol in this list
1001 * even if it is targetting a section in @bad_to_sec.
1002 *
1003 * @handler: Specific handler to call when a match is found. If NULL,
1004 * default_mismatch_handler() will be called.
1005 *
1006 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001007struct sectioncheck {
1008 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301009 const char *bad_tosec[20];
1010 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001011 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001012 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301013 void (*handler)(const char *modname, struct elf_info *elf,
1014 const struct sectioncheck* const mismatch,
1015 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1016
Sam Ravnborg10668222008-01-13 22:21:31 +01001017};
1018
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301019static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1020 const struct sectioncheck* const mismatch,
1021 Elf_Rela *r, Elf_Sym *sym,
1022 const char *fromsec);
1023
Mathias Krause7a3ee752014-08-27 20:28:53 +09301024static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001025/* Do not reference init/exit code/data from
1026 * normal code and data
1027 */
1028{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001029 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301030 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001031 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001032 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001033},
1034{
1035 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301036 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001037 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001038 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001039},
1040{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001041 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301042 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001043 .mismatch = DATA_TO_ANY_INIT,
1044 .symbol_white_list = {
1045 "*_template", "*_timer", "*_sht", "*_ops",
1046 "*_probe", "*_probe_one", "*_console", NULL
1047 },
1048},
1049{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001050 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301051 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001052 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001053 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001054},
1055{
1056 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301057 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001058 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001059 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001060},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001061/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001062{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001063 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301064 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001065 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001066 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001067},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001068/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001069{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001070 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301071 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001072 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001073 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001074},
1075/* Do not use exit code/data from init code */
1076{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001077 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301078 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001079 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001080 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001081},
1082/* Do not use init code/data from exit code */
1083{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001084 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301085 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001086 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001087 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001088},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001089{
1090 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301091 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001092 .mismatch = ANY_INIT_TO_ANY_EXIT,
1093 .symbol_white_list = { NULL },
1094},
Sam Ravnborg10668222008-01-13 22:21:31 +01001095/* Do not export init/exit functions or data */
1096{
1097 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301098 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001099 .mismatch = EXPORT_TO_INIT_EXIT,
1100 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301101},
1102{
1103 .fromsec = { "__ex_table", NULL },
1104 /* If you're adding any new black-listed sections in here, consider
1105 * adding a special 'printer' for them in scripts/check_extable.
1106 */
1107 .bad_tosec = { ".altinstr_replacement", NULL },
1108 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1109 .mismatch = EXTABLE_TO_NON_TEXT,
1110 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001111}
1112};
1113
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001114static const struct sectioncheck *section_mismatch(
1115 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001116{
1117 int i;
1118 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1119 const struct sectioncheck *check = &sectioncheck[0];
1120
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301121 /*
1122 * The target section could be the SHT_NUL section when we're
1123 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001124 * doesn't make much sense and causes build failures on parisc
1125 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301126 */
1127 if (*tosec == '\0')
1128 return NULL;
1129
Sam Ravnborg10668222008-01-13 22:21:31 +01001130 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301131 if (match(fromsec, check->fromsec)) {
1132 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1133 return check;
1134 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1135 return check;
1136 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001137 check++;
1138 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001139 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001140}
1141
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001142/**
1143 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001144 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001145 * Pattern 1:
1146 * If a module parameter is declared __initdata and permissions=0
1147 * then this is legal despite the warning generated.
1148 * We cannot see value of permissions here, so just ignore
1149 * this pattern.
1150 * The pattern is identified by:
1151 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001152 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001153 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001154 *
Rusty Russell6a841522010-08-11 23:04:16 -06001155 * Pattern 1a:
1156 * module_param_call() ops can refer to __init set function if permissions=0
1157 * The pattern is identified by:
1158 * tosec = .init.text
1159 * fromsec = .data*
1160 * atsym = __param_ops_*
1161 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001162 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001163 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001164 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001165 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001166 * tosec = init or exit section
1167 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001168 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1169 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001170 *
1171 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001172 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001173 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001174 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001175 * Some symbols belong to init section but still it is ok to reference
1176 * these from non-init sections as these symbols don't have any memory
1177 * allocated for them and symbol address and value are same. So even
1178 * if init section is freed, its ok to reference those symbols.
1179 * For ex. symbols marking the init section boundaries.
1180 * This pattern is identified by
1181 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001182 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301183 * Pattern 5:
1184 * GCC may optimize static inlines when fed constant arg(s) resulting
1185 * in functions like cpumask_empty() -- generating an associated symbol
1186 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1187 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1188 * meaningless section warning. May need to add isra symbols too...
1189 * This pattern is identified by
1190 * tosec = init section
1191 * fromsec = text section
1192 * refsymname = *.constprop.*
1193 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001194 * Pattern 6:
1195 * Hide section mismatch warnings for ELF local symbols. The goal
1196 * is to eliminate false positive modpost warnings caused by
1197 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1198 * Autogenerated symbol names bypass modpost's "Pattern 2"
1199 * whitelisting, which relies on pattern-matching against symbol
1200 * names to work. (One situation where gcc can autogenerate ELF
1201 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001202 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001203static int secref_whitelist(const struct sectioncheck *mismatch,
1204 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001205 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001206{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001207 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001208 if (match(tosec, init_data_sections) &&
1209 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001210 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001211 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001212
Rusty Russell6a841522010-08-11 23:04:16 -06001213 /* Check for pattern 1a */
1214 if (strcmp(tosec, ".init.text") == 0 &&
1215 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001216 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001217 return 0;
1218
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001219 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001220 if (match(tosec, init_exit_sections) &&
1221 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001222 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001223 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001224
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001225 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001226 if (match(fromsec, head_sections) &&
1227 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001228 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001229
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001230 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001231 if (match(tosym, linker_symbols))
1232 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001233
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301234 /* Check for pattern 5 */
1235 if (match(fromsec, text_sections) &&
1236 match(tosec, init_sections) &&
1237 match(fromsym, optim_symbols))
1238 return 0;
1239
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001240 /* Check for pattern 6 */
1241 if (strstarts(fromsym, ".L"))
1242 return 0;
1243
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001244 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001245}
1246
Sami Tolvanen5818c682018-10-23 15:15:35 -07001247static inline int is_arm_mapping_symbol(const char *str)
1248{
1249 return str[0] == '$' && strchr("axtd", str[1])
1250 && (str[2] == '\0' || str[2] == '.');
1251}
1252
1253/*
1254 * If there's no name there, ignore it; likewise, ignore it if it's
1255 * one of the magic symbols emitted used by current ARM tools.
1256 *
1257 * Otherwise if find_symbols_between() returns those symbols, they'll
1258 * fail the whitelist tests and cause lots of false alarms ... fixable
1259 * only by merging __exit and __init sections into __text, bloating
1260 * the kernel (which is especially evil on embedded platforms).
1261 */
1262static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1263{
1264 const char *name = elf->strtab + sym->st_name;
1265
1266 if (!name || !strlen(name))
1267 return 0;
1268 return !is_arm_mapping_symbol(name);
1269}
1270
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001271/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001272 * Find symbol based on relocation record info.
1273 * In some cases the symbol supplied is a valid symbol so
1274 * return refsym. If st_name != 0 we assume this is a valid symbol.
1275 * In other cases the symbol needs to be looked up in the symbol table
1276 * based on section and address.
1277 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001278static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001279 Elf_Sym *relsym)
1280{
1281 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001282 Elf_Sym *near = NULL;
1283 Elf64_Sword distance = 20;
1284 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001285 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001286
1287 if (relsym->st_name != 0)
1288 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001289
1290 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001291 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001292 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001293 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001294 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1295 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001296 if (!is_valid_name(elf, sym))
1297 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001298 if (sym->st_value == addr)
1299 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001300 /* Find a symbol nearby - addr are maybe negative */
1301 d = sym->st_value - addr;
1302 if (d < 0)
1303 d = addr - sym->st_value;
1304 if (d < distance) {
1305 distance = d;
1306 near = sym;
1307 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001308 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001309 /* We need a close match */
1310 if (distance < 20)
1311 return near;
1312 else
1313 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001314}
1315
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001316/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001317 * Find symbols before or equal addr and after addr - in the section sec.
1318 * If we find two symbols with equal offset prefer one with a valid name.
1319 * The ELF format may have a better way to detect what type of symbol
1320 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001321 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001322static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1323 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001324{
1325 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001326 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001327 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001328
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001329 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1330 const char *symsec;
1331
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001332 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001333 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001334 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001335 if (strcmp(symsec, sec) != 0)
1336 continue;
David Brownellda68d612007-02-20 13:58:16 -08001337 if (!is_valid_name(elf, sym))
1338 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001339 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001340 if ((addr - sym->st_value) < distance) {
1341 distance = addr - sym->st_value;
1342 near = sym;
1343 } else if ((addr - sym->st_value) == distance) {
1344 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001345 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001346 }
1347 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001348 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001349}
1350
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001351/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001352 * Convert a section name to the function/data attribute
1353 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001354 * .memexitconst => __memconst
1355 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001356 *
1357 * The memory of returned value has been allocated on a heap. The user of this
1358 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001359*/
1360static char *sec2annotation(const char *s)
1361{
1362 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001363 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001364 char *r = p;
1365
1366 *p++ = '_';
1367 *p++ = '_';
1368 if (*s == '.')
1369 s++;
1370 while (*s && *s != '.')
1371 *p++ = *s++;
1372 *p = '\0';
1373 if (*s == '.')
1374 s++;
1375 if (strstr(s, "rodata") != NULL)
1376 strcat(p, "const ");
1377 else if (strstr(s, "data") != NULL)
1378 strcat(p, "data ");
1379 else
1380 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001381 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001382 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001383 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001384 }
1385}
1386
1387static int is_function(Elf_Sym *sym)
1388{
1389 if (sym)
1390 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1391 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001392 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001393}
1394
Randy Dunlap00759c02011-03-15 14:13:47 -07001395static void print_section_list(const char * const list[20])
1396{
1397 const char *const *s = list;
1398
1399 while (*s) {
1400 fprintf(stderr, "%s", *s);
1401 s++;
1402 if (*s)
1403 fprintf(stderr, ", ");
1404 }
1405 fprintf(stderr, "\n");
1406}
1407
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301408static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1409{
1410 switch (is_func) {
1411 case 0: *name = "variable"; *name_p = ""; break;
1412 case 1: *name = "function"; *name_p = "()"; break;
1413 default: *name = "(unknown reference)"; *name_p = ""; break;
1414 }
1415}
1416
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001417/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001418 * Print a warning about a section mismatch.
1419 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001420 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001421 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001422static void report_sec_mismatch(const char *modname,
1423 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001424 const char *fromsec,
1425 unsigned long long fromaddr,
1426 const char *fromsym,
1427 int from_is_func,
1428 const char *tosec, const char *tosym,
1429 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001430{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001431 const char *from, *from_p;
1432 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001433 char *prl_from;
1434 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001435
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001436 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001437
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301438 get_pretty_name(from_is_func, &from, &from_p);
1439 get_pretty_name(to_is_func, &to, &to_p);
1440
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001441 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1442 "to the %s %s:%s%s\n",
1443 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1444 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001445
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001446 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001447 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001448 prl_from = sec2annotation(fromsec);
1449 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001450 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001451 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001452 "the %s %s%s%s.\n"
1453 "This is often because %s lacks a %s\n"
1454 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001455 prl_from, fromsym,
1456 to, prl_to, tosym, to_p,
1457 fromsym, prl_to, tosym);
1458 free(prl_from);
1459 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001460 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001461 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001462 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001463 fprintf(stderr,
1464 "The variable %s references\n"
1465 "the %s %s%s%s\n"
1466 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001467 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001468 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001469 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001470 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001471 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001472 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001473 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001474 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001475 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001476 fprintf(stderr,
1477 "The function %s() references a %s in an exit section.\n"
1478 "Often the %s %s%s has valid usage outside the exit section\n"
1479 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001480 fromsym, to, to, tosym, to_p, prl_to, tosym);
1481 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001482 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001483 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001484 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001485 fprintf(stderr,
1486 "The variable %s references\n"
1487 "the %s %s%s%s\n"
1488 "If the reference is valid then annotate the\n"
1489 "variable with __exit* (see linux/init.h) or "
1490 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001491 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001492 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001493 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001494 break;
1495 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001496 case XXXINIT_TO_SOME_INIT:
1497 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001498 prl_from = sec2annotation(fromsec);
1499 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001500 fprintf(stderr,
1501 "The %s %s%s%s references\n"
1502 "a %s %s%s%s.\n"
1503 "If %s is only used by %s then\n"
1504 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001505 from, prl_from, fromsym, from_p,
1506 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001507 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001508 free(prl_from);
1509 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001510 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001511 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001512 prl_from = sec2annotation(fromsec);
1513 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001514 fprintf(stderr,
1515 "The %s %s%s%s references\n"
1516 "a %s %s%s%s.\n"
1517 "This is often seen when error handling "
1518 "in the init function\n"
1519 "uses functionality in the exit path.\n"
1520 "The fix is often to remove the %sannotation of\n"
1521 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001522 from, prl_from, fromsym, from_p,
1523 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001524 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001525 free(prl_from);
1526 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001527 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001528 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001529 prl_from = sec2annotation(fromsec);
1530 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001531 fprintf(stderr,
1532 "The %s %s%s%s references\n"
1533 "a %s %s%s%s.\n"
1534 "This is often seen when error handling "
1535 "in the exit function\n"
1536 "uses functionality in the init path.\n"
1537 "The fix is often to remove the %sannotation of\n"
1538 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001539 from, prl_from, fromsym, from_p,
1540 to, prl_to, tosym, to_p,
1541 prl_to, tosym, to_p);
1542 free(prl_from);
1543 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001544 break;
1545 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001546 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001547 fprintf(stderr,
1548 "The symbol %s is exported and annotated %s\n"
1549 "Fix this by removing the %sannotation of %s "
1550 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001551 tosym, prl_to, prl_to, tosym);
1552 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001553 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301554 case EXTABLE_TO_NON_TEXT:
1555 fatal("There's a special handler for this mismatch type, "
1556 "we should never get here.");
1557 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001558 }
1559 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001560}
1561
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301562static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1563 const struct sectioncheck* const mismatch,
1564 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1565{
1566 const char *tosec;
1567 Elf_Sym *to;
1568 Elf_Sym *from;
1569 const char *tosym;
1570 const char *fromsym;
1571
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301572 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1573 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301574
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001575 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301576 return;
1577
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301578 tosec = sec_name(elf, get_secindex(elf, sym));
1579 to = find_elf_symbol(elf, r->r_addend, sym);
1580 tosym = sym_name(elf, to);
1581
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301582 /* check whitelist - we may ignore it */
1583 if (secref_whitelist(mismatch,
1584 fromsec, fromsym, tosec, tosym)) {
1585 report_sec_mismatch(modname, mismatch,
1586 fromsec, r->r_offset, fromsym,
1587 is_function(from), tosec, tosym,
1588 is_function(to));
1589 }
1590}
1591
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301592static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1593{
1594 if (section_index > elf->num_sections)
1595 fatal("section_index is outside elf->num_sections!\n");
1596
1597 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1598}
1599
1600/*
1601 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1602 * to know the sizeof(struct exception_table_entry) for the target architecture.
1603 */
1604static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301605static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301606{
1607 /*
1608 * If we're currently checking the second relocation within __ex_table,
1609 * that relocation offset tells us the offsetof(struct
1610 * exception_table_entry, fixup) which is equal to sizeof(struct
1611 * exception_table_entry) divided by two. We use that to our advantage
1612 * since there's no portable way to get that size as every architecture
1613 * seems to go with different sized types. Not pretty but better than
1614 * hard-coding the size for every architecture..
1615 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301616 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301617 extable_entry_size = r->r_offset * 2;
1618}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301619
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301620static inline bool is_extable_fault_address(Elf_Rela *r)
1621{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301622 /*
1623 * extable_entry_size is only discovered after we've handled the
1624 * _second_ relocation in __ex_table, so only abort when we're not
1625 * handling the first reloc and extable_entry_size is zero.
1626 */
1627 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301628 fatal("extable_entry size hasn't been discovered!\n");
1629
1630 return ((r->r_offset == 0) ||
1631 (r->r_offset % extable_entry_size == 0));
1632}
1633
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301634#define is_second_extable_reloc(Start, Cur, Sec) \
1635 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1636
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301637static void report_extable_warnings(const char* modname, struct elf_info* elf,
1638 const struct sectioncheck* const mismatch,
1639 Elf_Rela* r, Elf_Sym* sym,
1640 const char* fromsec, const char* tosec)
1641{
1642 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1643 const char* fromsym_name = sym_name(elf, fromsym);
1644 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1645 const char* tosym_name = sym_name(elf, tosym);
1646 const char* from_pretty_name;
1647 const char* from_pretty_name_p;
1648 const char* to_pretty_name;
1649 const char* to_pretty_name_p;
1650
1651 get_pretty_name(is_function(fromsym),
1652 &from_pretty_name, &from_pretty_name_p);
1653 get_pretty_name(is_function(tosym),
1654 &to_pretty_name, &to_pretty_name_p);
1655
1656 warn("%s(%s+0x%lx): Section mismatch in reference"
1657 " from the %s %s%s to the %s %s:%s%s\n",
1658 modname, fromsec, (long)r->r_offset, from_pretty_name,
1659 fromsym_name, from_pretty_name_p,
1660 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1661
1662 if (!match(tosec, mismatch->bad_tosec) &&
1663 is_executable_section(elf, get_secindex(elf, sym)))
1664 fprintf(stderr,
1665 "The relocation at %s+0x%lx references\n"
1666 "section \"%s\" which is not in the list of\n"
1667 "authorized sections. If you're adding a new section\n"
1668 "and/or if this reference is valid, add \"%s\" to the\n"
1669 "list of authorized sections to jump to on fault.\n"
1670 "This can be achieved by adding \"%s\" to \n"
1671 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1672 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1673}
1674
1675static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1676 const struct sectioncheck* const mismatch,
1677 Elf_Rela* r, Elf_Sym* sym,
1678 const char *fromsec)
1679{
1680 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1681
1682 sec_mismatch_count++;
1683
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001684 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301685
1686 if (match(tosec, mismatch->bad_tosec))
1687 fatal("The relocation at %s+0x%lx references\n"
1688 "section \"%s\" which is black-listed.\n"
1689 "Something is seriously wrong and should be fixed.\n"
1690 "You might get more information about where this is\n"
1691 "coming from by using scripts/check_extable.sh %s\n",
1692 fromsec, (long)r->r_offset, tosec, modname);
1693 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1694 if (is_extable_fault_address(r))
1695 fatal("The relocation at %s+0x%lx references\n"
1696 "section \"%s\" which is not executable, IOW\n"
1697 "it is not possible for the kernel to fault\n"
1698 "at that address. Something is seriously wrong\n"
1699 "and should be fixed.\n",
1700 fromsec, (long)r->r_offset, tosec);
1701 else
1702 fatal("The relocation at %s+0x%lx references\n"
1703 "section \"%s\" which is not executable, IOW\n"
1704 "the kernel will fault if it ever tries to\n"
1705 "jump to it. Something is seriously wrong\n"
1706 "and should be fixed.\n",
1707 fromsec, (long)r->r_offset, tosec);
1708 }
1709}
1710
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001711static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001712 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001713{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001714 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301715 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001716
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001717 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301718 if (mismatch->handler)
1719 mismatch->handler(modname, elf, mismatch,
1720 r, sym, fromsec);
1721 else
1722 default_mismatch_handler(modname, elf, mismatch,
1723 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001724 }
1725}
1726
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001727static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001728 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001729{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001730 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001731}
1732
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001733static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001734{
1735 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001736 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001737
1738 switch (r_typ) {
1739 case R_386_32:
1740 r->r_addend = TO_NATIVE(*location);
1741 break;
1742 case R_386_PC32:
1743 r->r_addend = TO_NATIVE(*location) + 4;
1744 /* For CONFIG_RELOCATABLE=y */
1745 if (elf->hdr->e_type == ET_EXEC)
1746 r->r_addend += r->r_offset;
1747 break;
1748 }
1749 return 0;
1750}
1751
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001752#ifndef R_ARM_CALL
1753#define R_ARM_CALL 28
1754#endif
1755#ifndef R_ARM_JUMP24
1756#define R_ARM_JUMP24 29
1757#endif
1758
David A. Longc9698e52014-02-14 22:41:18 +01001759#ifndef R_ARM_THM_CALL
1760#define R_ARM_THM_CALL 10
1761#endif
1762#ifndef R_ARM_THM_JUMP24
1763#define R_ARM_THM_JUMP24 30
1764#endif
1765#ifndef R_ARM_THM_JUMP19
1766#define R_ARM_THM_JUMP19 51
1767#endif
1768
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001769static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001770{
1771 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1772
1773 switch (r_typ) {
1774 case R_ARM_ABS32:
1775 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001776 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001777 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001778 break;
1779 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001780 case R_ARM_CALL:
1781 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001782 case R_ARM_THM_CALL:
1783 case R_ARM_THM_JUMP24:
1784 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001785 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001786 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001787 sechdr->sh_offset +
1788 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001789 break;
1790 default:
1791 return 1;
1792 }
1793 return 0;
1794}
1795
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001796static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001797{
1798 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001799 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001800 unsigned int inst;
1801
1802 if (r_typ == R_MIPS_HI16)
1803 return 1; /* skip this */
1804 inst = TO_NATIVE(*location);
1805 switch (r_typ) {
1806 case R_MIPS_LO16:
1807 r->r_addend = inst & 0xffff;
1808 break;
1809 case R_MIPS_26:
1810 r->r_addend = (inst & 0x03ffffff) << 2;
1811 break;
1812 case R_MIPS_32:
1813 r->r_addend = inst;
1814 break;
1815 }
1816 return 0;
1817}
1818
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001819static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001820 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001821{
1822 Elf_Sym *sym;
1823 Elf_Rela *rela;
1824 Elf_Rela r;
1825 unsigned int r_sym;
1826 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001827
Sam Ravnborgff13f922008-01-23 19:54:27 +01001828 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001829 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1830
Sam Ravnborgff13f922008-01-23 19:54:27 +01001831 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001832 fromsec += strlen(".rela");
1833 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001834 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001835 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001836
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001837 for (rela = start; rela < stop; rela++) {
1838 r.r_offset = TO_NATIVE(rela->r_offset);
1839#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001840 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001841 unsigned int r_typ;
1842 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1843 r_sym = TO_NATIVE(r_sym);
1844 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1845 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1846 } else {
1847 r.r_info = TO_NATIVE(rela->r_info);
1848 r_sym = ELF_R_SYM(r.r_info);
1849 }
1850#else
1851 r.r_info = TO_NATIVE(rela->r_info);
1852 r_sym = ELF_R_SYM(r.r_info);
1853#endif
1854 r.r_addend = TO_NATIVE(rela->r_addend);
1855 sym = elf->symtab_start + r_sym;
1856 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001857 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301859 if (is_second_extable_reloc(start, rela, fromsec))
1860 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001861 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001862 }
1863}
1864
1865static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001866 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001867{
1868 Elf_Sym *sym;
1869 Elf_Rel *rel;
1870 Elf_Rela r;
1871 unsigned int r_sym;
1872 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001873
Sam Ravnborgff13f922008-01-23 19:54:27 +01001874 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001875 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1876
Sam Ravnborgff13f922008-01-23 19:54:27 +01001877 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001878 fromsec += strlen(".rel");
1879 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001880 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001881 return;
1882
1883 for (rel = start; rel < stop; rel++) {
1884 r.r_offset = TO_NATIVE(rel->r_offset);
1885#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001886 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001887 unsigned int r_typ;
1888 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1889 r_sym = TO_NATIVE(r_sym);
1890 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1891 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1892 } else {
1893 r.r_info = TO_NATIVE(rel->r_info);
1894 r_sym = ELF_R_SYM(r.r_info);
1895 }
1896#else
1897 r.r_info = TO_NATIVE(rel->r_info);
1898 r_sym = ELF_R_SYM(r.r_info);
1899#endif
1900 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001901 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001902 case EM_386:
1903 if (addend_386_rel(elf, sechdr, &r))
1904 continue;
1905 break;
1906 case EM_ARM:
1907 if (addend_arm_rel(elf, sechdr, &r))
1908 continue;
1909 break;
1910 case EM_MIPS:
1911 if (addend_mips_rel(elf, sechdr, &r))
1912 continue;
1913 break;
1914 }
1915 sym = elf->symtab_start + r_sym;
1916 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001917 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001918 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301919 if (is_second_extable_reloc(start, rel, fromsec))
1920 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001921 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001922 }
1923}
1924
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001925/**
1926 * A module includes a number of sections that are discarded
1927 * either when loaded or when used as built-in.
1928 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001929 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001930 * Likewise for modules used built-in the sections marked __exit
1931 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001932 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001933 * The check_sec_ref() function traverses all relocation records
1934 * to find all references to a section that reference a section that will
1935 * be discarded and warns about it.
1936 **/
1937static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001938 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001939{
1940 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001941 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001942
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001943 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001944 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001945 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001946 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001947 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001948 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001949 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001950 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001951 }
1952}
1953
Andi Kleen7d02b492014-02-08 09:01:12 +01001954static char *remove_dot(char *s)
1955{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301956 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001957
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301958 if (n && s[n]) {
1959 size_t m = strspn(s + n + 1, "0123456789");
1960 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001961 s[n] = 0;
Sami Tolvanen7ac204b2020-12-11 10:46:27 -08001962
1963 /* strip trailing .lto */
1964 if (strends(s, ".lto"))
1965 s[strlen(s) - 4] = '\0';
Andi Kleen7d02b492014-02-08 09:01:12 +01001966 }
1967 return s;
1968}
1969
Masahiro Yamada8b185742018-05-09 18:50:40 +09001970static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 const char *symname;
1973 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001974 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001975 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 struct module *mod;
1977 struct elf_info info = { };
1978 Elf_Sym *sym;
1979
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001980 if (!parse_elf(&info, modname))
1981 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09001983 {
1984 char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09001986 /* strip trailing .o */
1987 tmp = NOFAIL(strdup(modname));
1988 tmp[strlen(tmp) - 2] = '\0';
Sami Tolvanen7ac204b2020-12-11 10:46:27 -08001989 /* strip trailing .lto */
1990 if (strends(tmp, ".lto"))
1991 tmp[strlen(tmp) - 4] = '\0';
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09001992 mod = new_module(tmp);
1993 free(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995
Masahiro Yamada5a438af2020-06-01 14:57:26 +09001996 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001997 license = get_modinfo(&info, "license");
1998 if (!license)
Masahiro Yamada1d6cd3922020-12-01 19:34:16 +09001999 error("missing MODULE_LICENSE() in %s\n", modname);
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002000 while (license) {
2001 if (license_is_gpl_compatible(license))
2002 mod->gpl_compatible = 1;
2003 else {
2004 mod->gpl_compatible = 0;
2005 break;
2006 }
2007 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002008 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002009
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002010 namespace = get_modinfo(&info, "import_ns");
2011 while (namespace) {
2012 add_namespace(&mod->imported_namespaces, namespace);
2013 namespace = get_next_modinfo(&info, "import_ns",
2014 namespace);
2015 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002016 }
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002019 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002021 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 handle_moddevtable(mod, &info, sym, symname);
2023 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002024
Matthias Maennich69923202019-10-18 10:31:42 +01002025 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2026 symname = remove_dot(info.strtab + sym->st_name);
2027
Masahiro Yamada17436942019-11-15 02:42:24 +09002028 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002029 if (strstarts(symname, "__kstrtabns_"))
2030 sym_update_namespace(symname + strlen("__kstrtabns_"),
2031 namespace_from_kstrtabns(&info,
2032 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002033
2034 if (strstarts(symname, "__crc_"))
2035 handle_modversion(mod, &info, sym,
2036 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002037 }
2038
Denis Efremov15bfc232019-08-01 09:06:57 +03002039 // check for static EXPORT_SYMBOL_* functions && global vars
2040 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2041 unsigned char bind = ELF_ST_BIND(sym->st_info);
2042
2043 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2044 struct symbol *s =
2045 find_symbol(remove_dot(info.strtab +
2046 sym->st_name));
2047
2048 if (s)
2049 s->is_static = 0;
2050 }
2051 }
2052
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002053 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002055 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002056 version = get_modinfo(&info, "version");
2057 if (version || all_versions)
2058 get_src_version(modname, mod->srcversion,
2059 sizeof(mod->srcversion) - 1);
2060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 parse_elf_finish(&info);
2063
Rusty Russell8c8ef422009-03-31 13:05:34 -06002064 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 * never passed as an argument to an exported function, so
2066 * the automatic versioning doesn't pick it up, but it's really
2067 * important anyhow */
2068 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002069 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Rusty Russell712f9b42013-04-04 17:37:38 +10302072static void read_symbols_from_files(const char *filename)
2073{
2074 FILE *in = stdin;
2075 char fname[PATH_MAX];
2076
2077 if (strcmp(filename, "-") != 0) {
2078 in = fopen(filename, "r");
2079 if (!in)
2080 fatal("Can't open filenames file %s: %m", filename);
2081 }
2082
2083 while (fgets(fname, PATH_MAX, in) != NULL) {
2084 if (strends(fname, "\n"))
2085 fname[strlen(fname)-1] = '\0';
2086 read_symbols(fname);
2087 }
2088
2089 if (in != stdin)
2090 fclose(in);
2091}
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093#define SZ 500
2094
2095/* We first write the generated file into memory using the
2096 * following helper, then compare to the file on disk and
2097 * only update the later if anything changed */
2098
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002099void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2100 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 char tmp[SZ];
2103 int len;
2104 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 va_start(ap, fmt);
2107 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002108 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 va_end(ap);
2110}
2111
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002112void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002115 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002116 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118 strncpy(buf->p + buf->pos, s, len);
2119 buf->pos += len;
2120}
2121
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002122static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2123{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002124 switch (exp) {
2125 case export_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002126 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002127 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002128 break;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002129 case export_plain:
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002130 case export_unknown:
2131 /* ignore */
2132 break;
2133 }
2134}
2135
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002136static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002137{
2138 struct symbol *s, *exp;
2139
2140 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002141 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002142 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002143 if (!exp || exp->module == mod) {
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002144 if (have_vmlinux && !s->weak)
Jessica Yu93c95e52020-03-06 17:02:05 +01002145 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2146 "\"%s\" [%s.ko] undefined!\n",
2147 s->name, mod->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002148 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002149 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002150 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002151 if (basename)
2152 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002153 else
2154 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002155
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002156 if (exp->namespace &&
2157 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002158 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2159 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2160 basename, exp->name, exp->namespace);
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002161 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002162 }
2163
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002164 if (!mod->gpl_compatible)
2165 check_for_gpl_usage(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002166 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002167}
2168
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002169static void check_modname_len(struct module *mod)
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002170{
2171 const char *mod_name;
2172
2173 mod_name = strrchr(mod->name, '/');
2174 if (mod_name == NULL)
2175 mod_name = mod->name;
2176 else
2177 mod_name++;
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002178 if (strlen(mod_name) >= MODULE_NAME_LEN)
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002179 error("module name is too long [%s.ko]\n", mod->name);
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002180}
2181
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002182/**
2183 * Header for the generated file
2184 **/
2185static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002188 /*
2189 * Include build-salt.h after module.h in order to
2190 * inherit the definitions.
2191 */
Leon Romanovsky51161bf2020-04-19 18:55:06 +03002192 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002193 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 buf_printf(b, "#include <linux/vermagic.h>\n");
2195 buf_printf(b, "#include <linux/compiler.h>\n");
2196 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002197 buf_printf(b, "BUILD_SALT;\n");
2198 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002200 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302202 buf_printf(b, "__visible struct module __this_module\n");
Joe Perches33def842020-10-21 19:36:07 -07002203 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002204 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002206 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (mod->has_cleanup)
2208 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002209 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002211 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 buf_printf(b, "};\n");
2213}
2214
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002215static void add_intree_flag(struct buffer *b, int is_intree)
2216{
2217 if (is_intree)
2218 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2219}
2220
Andi Kleencaf75012018-01-25 15:50:28 -08002221/* Cannot check for assembler */
2222static void add_retpoline(struct buffer *b)
2223{
WANG Chaoe4f35892018-12-11 00:37:25 +08002224 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002225 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2226 buf_printf(b, "#endif\n");
2227}
2228
Trevor Keith5c725132009-09-22 16:43:38 -07002229static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002230{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002231 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002232 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2233}
2234
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002235/**
2236 * Record CRCs for unresolved symbols
2237 **/
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002238static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
2240 struct symbol *s, *exp;
2241
2242 for (s = mod->unres; s; s = s->next) {
2243 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002244 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 s->module = exp->module;
2247 s->crc_valid = exp->crc_valid;
2248 s->crc = exp->crc;
2249 }
2250
2251 if (!modversions)
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002252 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 buf_printf(b, "\n");
2255 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Joe Perches33def842020-10-21 19:36:07 -07002256 buf_printf(b, "__used __section(\"__versions\") = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002259 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002262 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 s->name, mod->name);
2264 continue;
2265 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302266 if (strlen(s->name) >= MODULE_NAME_LEN) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002267 error("too long symbol \"%s\" [%s.ko]\n",
2268 s->name, mod->name);
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302269 break;
2270 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002271 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302272 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274
2275 buf_printf(b, "};\n");
2276}
2277
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002278static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 int first = 1;
2282
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002283 /* Clear ->seen flag of modules that own symbols needed by this. */
2284 for (s = mod->unres; s; s = s->next)
2285 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002286 s->module->seen = s->module->is_vmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002289 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002291 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (!s->module)
2293 continue;
2294
2295 if (s->module->seen)
2296 continue;
2297
2298 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002299 p = strrchr(s->module->name, '/');
2300 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002301 p++;
2302 else
2303 p = s->module->name;
2304 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 first = 0;
2306 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002307 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002310static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
2312 if (mod->srcversion[0]) {
2313 buf_printf(b, "\n");
2314 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2315 mod->srcversion);
2316 }
2317}
2318
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002319static void write_buf(struct buffer *b, const char *fname)
2320{
2321 FILE *file;
2322
2323 file = fopen(fname, "w");
2324 if (!file) {
2325 perror(fname);
2326 exit(1);
2327 }
2328 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2329 perror(fname);
2330 exit(1);
2331 }
2332 if (fclose(file) != 0) {
2333 perror(fname);
2334 exit(1);
2335 }
2336}
2337
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002338static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
2340 char *tmp;
2341 FILE *file;
2342 struct stat st;
2343
2344 file = fopen(fname, "r");
2345 if (!file)
2346 goto write;
2347
2348 if (fstat(fileno(file), &st) < 0)
2349 goto close_write;
2350
2351 if (st.st_size != b->pos)
2352 goto close_write;
2353
2354 tmp = NOFAIL(malloc(b->pos));
2355 if (fread(tmp, 1, b->pos, file) != b->pos)
2356 goto free_write;
2357
2358 if (memcmp(tmp, b->p, b->pos) != 0)
2359 goto free_write;
2360
2361 free(tmp);
2362 fclose(file);
2363 return;
2364
2365 free_write:
2366 free(tmp);
2367 close_write:
2368 fclose(file);
2369 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002370 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
Ram Paibd5cbce2006-06-08 22:12:53 -07002373/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002374 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002375 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002376static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002378 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002380 buf = read_text_file(fname);
2381 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 /* No symbol versions, silently ignore */
2383 return;
2384
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002385 pos = buf;
2386
2387 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002388 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 unsigned int crc;
2390 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002391 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 if (!(symname = strchr(line, '\t')))
2394 goto fail;
2395 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002396 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 goto fail;
2398 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002399 if (!(export = strchr(modname, '\t')))
2400 goto fail;
2401 *export++ = '\0';
2402 if (!(namespace = strchr(export, '\t')))
2403 goto fail;
2404 *namespace++ = '\0';
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 crc = strtoul(line, &d, 16);
2407 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2408 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002409 mod = find_module(modname);
2410 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002411 mod = new_module(modname);
Masahiro Yamada52c34162020-06-01 14:57:05 +09002412 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002414 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002415 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002416 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002417 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002419 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return;
2421fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002422 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 fatal("parse error in symbol dump file\n");
2424}
2425
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002426static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 struct buffer buf = { };
2429 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002430 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 int n;
2432
2433 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2434 symbol = symbolhash[n];
2435 while (symbol) {
Masahiro Yamada69bc8d32021-03-26 03:54:09 +09002436 if (!symbol->module->from_dump) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002437 namespace = symbol->namespace;
2438 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2439 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002440 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002441 export_str(symbol->export),
2442 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 symbol = symbol->next;
2445 }
2446 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002447 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002448 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002451static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002452{
2453 struct module *mod;
2454 struct namespace_list *ns;
2455 struct buffer ns_deps_buf = {};
2456
2457 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002458
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002459 if (mod->from_dump || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002460 continue;
2461
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002462 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002463
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002464 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2465 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002466
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002467 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002468 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002469
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002470 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002471 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002472}
2473
Masahiro Yamada79247992020-06-01 14:57:07 +09002474struct dump_list {
2475 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002476 const char *file;
2477};
2478
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002479int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 struct module *mod;
2482 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002483 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302484 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 int opt;
Denis Efremov15bfc232019-08-01 09:06:57 +03002486 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002487 struct dump_list *dump_read_start = NULL;
2488 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002490 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002491 switch (opt) {
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002492 case 'e':
2493 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002494 break;
2495 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002496 *dump_read_iter =
2497 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2498 (*dump_read_iter)->file = optarg;
2499 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002500 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002501 case 'm':
2502 modversions = 1;
2503 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302504 case 'n':
2505 ignore_missing_files = 1;
2506 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002507 case 'o':
2508 dump_write = optarg;
2509 break;
2510 case 'a':
2511 all_versions = 1;
2512 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302513 case 'T':
2514 files_source = optarg;
2515 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002516 case 'w':
2517 warn_unresolved = 1;
2518 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302519 case 'E':
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002520 sec_mismatch_warn_only = false;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302521 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002522 case 'N':
2523 allow_missing_ns_imports = 1;
2524 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002525 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002526 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002527 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002528 default:
2529 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531 }
2532
Masahiro Yamada79247992020-06-01 14:57:07 +09002533 while (dump_read_start) {
2534 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002535
Masahiro Yamada79247992020-06-01 14:57:07 +09002536 read_dump(dump_read_start->file);
2537 tmp = dump_read_start->next;
2538 free(dump_read_start);
2539 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002542 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Rusty Russell712f9b42013-04-04 17:37:38 +10302545 if (files_source)
2546 read_symbols_from_files(files_source);
2547
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002548 /*
2549 * When there's no vmlinux, don't print warnings about
2550 * unresolved symbols (since there'll be too many ;)
2551 */
2552 if (!have_vmlinux)
2553 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2554
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002555 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302556 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002557
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002558 if (mod->is_vmlinux || mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002559 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 buf.pos = 0;
2562
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002563 check_modname_len(mod);
2564 check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002567 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002568 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002569 add_staging_flag(&buf, mod->name);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002570 add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002571 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 add_moddevtable(&buf, mod);
2573 add_srcversion(&buf, mod);
2574
2575 sprintf(fname, "%s.mod.c", mod->name);
2576 write_if_changed(&buf, fname);
2577 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002578
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002579 if (missing_namespace_deps)
2580 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (dump_write)
2583 write_dump(dump_write);
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002584 if (sec_mismatch_count && !sec_mismatch_warn_only)
2585 error("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002586 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002587 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002588 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002589
Masahiro Yamada47346e92019-09-24 21:07:40 +09002590 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002591 if (s->is_static)
Quentin Perretb9ed8472020-12-01 16:52:22 +00002592 error("\"%s\" [%s] is a static %s\n",
2593 s->name, s->module->name,
2594 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002595 }
2596 }
2597
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002598 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002600 return error_occurred ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601}