blob: 20fc57837881abfa624e12110c521c7f251d5181 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
Masahiro Yamada5370d4a2020-01-05 00:36:51 +090015#include <elf.h>
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080016#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070018#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103019#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103020#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093021#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020023#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093026static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093028static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
30static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010031/* If we are modposting external module set to 1 */
32static int external_module = 0;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070033/* Only warn about unresolved symbols */
34static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070035/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010036static int sec_mismatch_count = 0;
Masahiro Yamadac7299d92020-12-01 19:34:18 +090037static int sec_mismatch_warn_only = true;
Guenter Roeckeed380f2013-09-23 15:23:54 +093038/* ignore missing files */
39static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010040/* If set to 1, only warn (instead of error) about missing ns imports */
41static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010042
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090043static bool error_occurred;
44
Sam Ravnborgc96fca22006-07-01 11:44:23 +020045enum export {
Christoph Hellwig36794822021-02-02 13:13:34 +010046 export_plain,
47 export_gpl,
48 export_unknown
Sam Ravnborgc96fca22006-07-01 11:44:23 +020049};
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080051/* In kernel, this size is defined in linux/module.h;
52 * here we use Elf_Addr instead of long for covering cross-compile
53 */
54
55#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
56
Jessica Yu93c95e52020-03-06 17:02:05 +010057void __attribute__((format(printf, 2, 3)))
58modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 va_list arglist;
61
Jessica Yu93c95e52020-03-06 17:02:05 +010062 switch (loglevel) {
63 case LOG_WARN:
64 fprintf(stderr, "WARNING: ");
65 break;
66 case LOG_ERROR:
67 fprintf(stderr, "ERROR: ");
68 break;
69 case LOG_FATAL:
70 fprintf(stderr, "FATAL: ");
71 break;
72 default: /* invalid loglevel, ignore */
73 break;
74 }
75
76 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 va_start(arglist, fmt);
79 vfprintf(stderr, fmt, arglist);
80 va_end(arglist);
81
Jessica Yu93c95e52020-03-06 17:02:05 +010082 if (loglevel == LOG_FATAL)
83 exit(1);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +090084 if (loglevel == LOG_ERROR)
85 error_occurred = true;
Matthew Wilcox2a116652006-10-07 05:35:32 -060086}
87
Rusty Russelld4ef1c32013-04-04 17:37:32 +103088static inline bool strends(const char *str, const char *postfix)
89{
90 if (strlen(str) < strlen(postfix))
91 return false;
92
93 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096void *do_nofail(void *ptr, const char *expr)
97{
Sam Ravnborgdf578e72008-01-11 19:17:15 +010098 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +010099 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return ptr;
102}
103
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900104char *read_text_file(const char *filename)
105{
106 struct stat st;
107 size_t nbytes;
108 int fd;
109 char *buf;
110
111 fd = open(filename, O_RDONLY);
112 if (fd < 0) {
113 perror(filename);
114 exit(1);
115 }
116
117 if (fstat(fd, &st) < 0) {
118 perror(filename);
119 exit(1);
120 }
121
122 buf = NOFAIL(malloc(st.st_size + 1));
123
124 nbytes = st.st_size;
125
126 while (nbytes) {
127 ssize_t bytes_read;
128
129 bytes_read = read(fd, buf, nbytes);
130 if (bytes_read < 0) {
131 perror(filename);
132 exit(1);
133 }
134
135 nbytes -= bytes_read;
136 }
137 buf[st.st_size] = '\0';
138
139 close(fd);
140
141 return buf;
142}
143
144char *get_line(char **stringp)
145{
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200146 char *orig = *stringp, *next;
147
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900148 /* do not return the unwanted extra line at EOF */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200149 if (!orig || *orig == '\0')
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900150 return NULL;
151
Wolfram Sang6020db52020-07-26 23:44:19 +0200152 /* don't use strsep here, it is not available everywhere */
H. Nikolaus Schaller736bb112020-07-01 08:18:27 +0200153 next = strchr(orig, '\n');
154 if (next)
155 *next++ = '\0';
156
157 *stringp = next;
158
159 return orig;
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static struct module *modules;
164
Masahiro Yamada8b185742018-05-09 18:50:40 +0900165static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct module *mod;
168
169 for (mod = modules; mod; mod = mod->next)
170 if (strcmp(mod->name, modname) == 0)
171 break;
172 return mod;
173}
174
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030175static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct module *mod;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100178
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900179 mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 memset(mod, 0, sizeof(*mod));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /* add to list */
Masahiro Yamadaa82f7942020-06-01 14:57:29 +0900183 strcpy(mod->name, modname);
Masahiro Yamada4de7b622020-06-01 14:57:30 +0900184 mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200185 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 mod->next = modules;
187 modules = mod;
188
Masahiro Yamada858b9372020-06-01 14:57:28 +0900189 if (mod->is_vmlinux)
190 have_vmlinux = 1;
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return mod;
193}
194
195/* A hash of all exported symbols,
196 * struct symbol is also used for lists of unresolved symbols */
197
198#define SYMBOL_HASH_SIZE 1024
199
200struct symbol {
201 struct symbol *next;
202 struct module *module;
203 unsigned int crc;
204 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900205 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300207 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700208 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500209 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
212static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
213
214/* This is based on the hash agorithm from gdbm, via tdb */
215static inline unsigned int tdb_hash(const char *name)
216{
217 unsigned value; /* Used to compute the hash value. */
218 unsigned i; /* Used to cycle through random values. */
219
220 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100221 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
223
224 return (1103515243 * value + 12345);
225}
226
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100227/**
228 * Allocate a new symbols for use in the hash of exported symbols or
229 * the list of unresolved symbols per module
230 **/
231static struct symbol *alloc_symbol(const char *name, unsigned int weak,
232 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
235
236 memset(s, 0, sizeof(*s));
237 strcpy(s->name, name);
238 s->weak = weak;
239 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300240 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return s;
242}
243
244/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700245static struct symbol *new_symbol(const char *name, struct module *module,
246 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900251 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
252
253 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100256static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct symbol *s;
259
260 /* For our purposes, .foo matches foo. PPC64 needs this. */
261 if (name[0] == '.')
262 name++;
263
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100264 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (strcmp(s->name, name) == 0)
266 return s;
267 }
268 return NULL;
269}
270
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100271static bool contains_namespace(struct namespace_list *list,
272 const char *namespace)
273{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900274 for (; list; list = list->next)
275 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100276 return true;
277
278 return false;
279}
280
281static void add_namespace(struct namespace_list **list, const char *namespace)
282{
283 struct namespace_list *ns_entry;
284
285 if (!contains_namespace(*list, namespace)) {
286 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
287 strlen(namespace) + 1));
288 strcpy(ns_entry->namespace, namespace);
289 ns_entry->next = *list;
290 *list = ns_entry;
291 }
292}
293
294static bool module_imports_namespace(struct module *module,
295 const char *namespace)
296{
297 return contains_namespace(module->imported_namespaces, namespace);
298}
299
Mathias Krause7a3ee752014-08-27 20:28:53 +0930300static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700301 const char *str;
302 enum export export;
303} export_list[] = {
304 { .str = "EXPORT_SYMBOL", .export = export_plain },
305 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700306 { .str = "(unknown)", .export = export_unknown },
307};
308
309
310static const char *export_str(enum export ex)
311{
312 return export_list[ex].str;
313}
314
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100315static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700316{
317 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100318
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200319 if (!s)
320 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700321 for (i = 0; export_list[i].export != export_unknown; i++) {
322 if (strcmp(export_list[i].str, s) == 0)
323 return export_list[i].export;
324 }
325 return export_unknown;
326}
327
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900328static void *sym_get_data_by_offset(const struct elf_info *info,
329 unsigned int secindex, unsigned long offset)
Masahiro Yamada6124c042017-09-06 16:19:05 -0700330{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800331 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900332
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900333 if (info->hdr->e_type != ET_REL)
334 offset -= sechdr->sh_addr;
335
336 return (void *)info->hdr + sechdr->sh_offset + offset;
337}
338
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900339static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
340{
341 return sym_get_data_by_offset(info, get_secindex(info, sym),
342 sym->st_value);
343}
344
Masahiro Yamada565587d2020-05-25 14:47:05 +0900345static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
346{
347 return sym_get_data_by_offset(info, info->secindex_strings,
348 sechdr->sh_name);
349}
350
351static const char *sec_name(const struct elf_info *info, int secindex)
352{
353 return sech_name(info, &info->sechdrs[secindex]);
354}
355
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200356#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
357
358static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
359{
360 const char *secname = sec_name(elf, sec);
361
362 if (strstarts(secname, "___ksymtab+"))
363 return export_plain;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200364 else if (strstarts(secname, "___ksymtab_gpl+"))
365 return export_gpl;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200366 else
367 return export_unknown;
368}
369
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200370static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700371{
372 if (sec == elf->export_sec)
373 return export_plain;
374 else if (sec == elf->export_gpl_sec)
375 return export_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700376 else
377 return export_unknown;
378}
379
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900380static const char *namespace_from_kstrtabns(const struct elf_info *info,
381 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100382{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900383 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100384 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100385}
386
Matthias Maennicha2b11182019-10-18 10:31:40 +0100387static void sym_update_namespace(const char *symname, const char *namespace)
388{
389 struct symbol *s = find_symbol(symname);
390
391 /*
392 * That symbol should have been created earlier and thus this is
393 * actually an assertion.
394 */
395 if (!s) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +0900396 error("Could not update namespace(%s) for symbol %s\n",
397 namespace, symname);
Matthias Maennicha2b11182019-10-18 10:31:40 +0100398 return;
399 }
400
401 free(s->namespace);
402 s->namespace =
403 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
404}
405
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100406/**
407 * Add an exported symbol - it may have already been added without a
408 * CRC, in this case just update the CRC
409 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100410static struct symbol *sym_add_exported(const char *name, struct module *mod,
411 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct symbol *s = find_symbol(name);
414
415 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700416 s = new_symbol(name, mod, export);
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900417 } else if (!external_module || s->module->is_vmlinux ||
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900418 s->module == mod) {
419 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
420 mod->name, name, s->module->name,
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900421 s->module->is_vmlinux ? "" : ".ko");
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900422 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900424
425 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700426 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100427 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Masahiro Yamada17436942019-11-15 02:42:24 +0900430static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100431{
432 struct symbol *s = find_symbol(name);
433
Masahiro Yamada17436942019-11-15 02:42:24 +0900434 /*
435 * Ignore stand-alone __crc_*, which might be auto-generated symbols
436 * such as __*_veneer in ARM ELF.
437 */
438 if (!s)
439 return;
440
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100441 s->crc = crc;
442 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900445static void *grab_file(const char *filename, size_t *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930448 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int fd;
450
451 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930452 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930454 if (fstat(fd, &st))
455 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 *size = st.st_size;
458 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930460failed:
461 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (map == MAP_FAILED)
463 return NULL;
464 return map;
465}
466
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900467static void release_file(void *file, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 munmap(file, size);
470}
471
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100472static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100475 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 Elf_Shdr *sechdrs;
477 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200478 const char *secstrings;
479 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 hdr = grab_file(filename, &info->size);
482 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930483 if (ignore_missing_files) {
484 fprintf(stderr, "%s: %s (ignored)\n", filename,
485 strerror(errno));
486 return 0;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200489 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100492 if (info->size < sizeof(*hdr)) {
493 /* file too small, assume this is an empty .o file */
494 return 0;
495 }
496 /* Is this a valid ELF file? */
497 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
498 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
499 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
500 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
501 /* Not an ELF file - silently ignore it */
502 return 0;
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200505 hdr->e_type = TO_NATIVE(hdr->e_type);
506 hdr->e_machine = TO_NATIVE(hdr->e_machine);
507 hdr->e_version = TO_NATIVE(hdr->e_version);
508 hdr->e_entry = TO_NATIVE(hdr->e_entry);
509 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
510 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
511 hdr->e_flags = TO_NATIVE(hdr->e_flags);
512 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
513 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
514 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
515 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
516 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
517 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 sechdrs = (void *)hdr + hdr->e_shoff;
519 info->sechdrs = sechdrs;
520
Petr Stetiara83710e2007-08-27 12:15:07 +0200521 /* Check if file offset is correct */
522 if (hdr->e_shoff > info->size) {
Masahiro Yamada3b09efc2020-06-01 14:57:31 +0900523 fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n",
524 (unsigned long)hdr->e_shoff, filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200525 return 0;
526 }
527
Anders Kaseorg68457562011-05-19 16:55:27 -0600528 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200529 /*
530 * There are more than 64k sections,
531 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200532 */
533 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
534 }
535 else {
536 info->num_sections = hdr->e_shnum;
537 }
538 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600539 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200540 }
541 else {
542 info->secindex_strings = hdr->e_shstrndx;
543 }
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200546 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200547 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
548 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
549 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
550 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
551 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
552 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
553 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
554 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
555 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
556 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200559 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
560 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700561 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900562 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Tejun Heo56fc82c2009-02-06 00:48:02 +0900564 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100565 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
566 "sizeof(*hrd)=%zu\n", filename,
567 (unsigned long)sechdrs[i].sh_offset,
568 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100569 return 0;
570 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700571 secname = secstrings + sechdrs[i].sh_name;
572 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900573 if (nobits)
574 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
576 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700577 } else if (strcmp(secname, "__ksymtab") == 0)
578 info->export_sec = i;
579 else if (strcmp(secname, "__ksymtab_gpl") == 0)
580 info->export_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700581
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200582 if (sechdrs[i].sh_type == SHT_SYMTAB) {
583 unsigned int sh_link_idx;
584 symtab_idx = i;
585 info->symtab_start = (void *)hdr +
586 sechdrs[i].sh_offset;
587 info->symtab_stop = (void *)hdr +
588 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600589 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200590 info->strtab = (void *)hdr +
591 sechdrs[sh_link_idx].sh_offset;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200594 /* 32bit section no. table? ("more than 64k sections") */
595 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
596 symtab_shndx_idx = i;
597 info->symtab_shndx_start = (void *)hdr +
598 sechdrs[i].sh_offset;
599 info->symtab_shndx_stop = (void *)hdr +
600 sechdrs[i].sh_offset + sechdrs[i].sh_size;
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100603 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100604 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* Fix endianness in symbols */
607 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
608 sym->st_shndx = TO_NATIVE(sym->st_shndx);
609 sym->st_name = TO_NATIVE(sym->st_name);
610 sym->st_value = TO_NATIVE(sym->st_value);
611 sym->st_size = TO_NATIVE(sym->st_size);
612 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200613
614 if (symtab_shndx_idx != ~0U) {
615 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600616 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200617 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600618 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200619 symtab_idx);
620 /* Fix endianness */
621 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
622 p++)
623 *p = TO_NATIVE(*p);
624 }
625
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100626 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100629static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 release_file(info->hdr, info->size);
632}
633
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200634static int ignore_undef_symbol(struct elf_info *info, const char *symname)
635{
636 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900637 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200638 return 1;
639 /* ignore global offset table */
640 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
641 return 1;
642 if (info->hdr->e_machine == EM_PPC)
643 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900644 if (strstarts(symname, "_restgpr_") ||
645 strstarts(symname, "_savegpr_") ||
646 strstarts(symname, "_rest32gpr_") ||
647 strstarts(symname, "_save32gpr_") ||
648 strstarts(symname, "_restvr_") ||
649 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200650 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000651 if (info->hdr->e_machine == EM_PPC64)
652 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900653 if (strstarts(symname, "_restgpr0_") ||
654 strstarts(symname, "_savegpr0_") ||
655 strstarts(symname, "_restvr_") ||
656 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100657 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000658 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200659 /* Do not ignore this symbol */
660 return 0;
661}
662
Masahiro Yamada17436942019-11-15 02:42:24 +0900663static void handle_modversion(const struct module *mod,
664 const struct elf_info *info,
665 const Elf_Sym *sym, const char *symname)
666{
667 unsigned int crc;
668
669 if (sym->st_shndx == SHN_UNDEF) {
670 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900671 symname, mod->name, mod->is_vmlinux ? "" : ".ko");
Masahiro Yamada17436942019-11-15 02:42:24 +0900672 return;
673 }
674
675 if (sym->st_shndx == SHN_ABS) {
676 crc = sym->st_value;
677 } else {
678 unsigned int *crcp;
679
680 /* symbol points to the CRC in the ELF object */
681 crcp = sym_get_data(info, sym);
682 crc = TO_NATIVE(*crcp);
683 }
684 sym_set_crc(symname, crc);
685}
686
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900687static void handle_symbol(struct module *mod, struct elf_info *info,
688 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200690 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900691 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200692
Masahiro Yamada33795762020-06-01 14:57:24 +0900693 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200694 export = export_from_secname(info, get_secindex(info, sym));
695 else
696 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 switch (sym->st_shndx) {
699 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900700 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100701 /* Should warn here, but modpost runs before the linker */
702 } else
703 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 case SHN_UNDEF:
706 /* undefined symbol */
707 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
708 ELF_ST_BIND(sym->st_info) != STB_WEAK)
709 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200710 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (info->hdr->e_machine == EM_SPARC ||
713 info->hdr->e_machine == EM_SPARCV9) {
714 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700715 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100717 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700718 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100719 munged[0] = '_';
720 munged[1] = toupper(munged[1]);
721 symname = munged;
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100724
Rusty Russellb92021b2013-03-15 15:04:17 +1030725 mod->unres = alloc_symbol(symname,
726 ELF_ST_BIND(sym->st_info) == STB_WEAK,
727 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729 default:
730 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900731 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100732 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100733 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900735 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900737 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 mod->has_cleanup = 1;
739 break;
740 }
741}
742
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100743/**
744 * Parse tag=value strings from .modinfo section
745 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static char *next_string(char *string, unsigned long *secsize)
747{
748 /* Skip non-zero chars */
749 while (string[0]) {
750 string++;
751 if ((*secsize)-- <= 1)
752 return NULL;
753 }
754
755 /* Skip any zero padding. */
756 while (!string[0]) {
757 string++;
758 if ((*secsize)-- <= 1)
759 return NULL;
760 }
761 return string;
762}
763
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900764static char *get_next_modinfo(struct elf_info *info, const char *tag,
765 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 char *p;
768 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900769 char *modinfo = info->modinfo;
770 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900772 if (prev) {
773 size -= prev - modinfo;
774 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200775 }
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 for (p = modinfo; p; p = next_string(p, &size)) {
778 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
779 return p + taglen + 1;
780 }
781 return NULL;
782}
783
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900784static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200785
786{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900787 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200788}
789
Sam Ravnborg93684d32006-02-19 11:53:35 +0100790/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100791 * Test if string s ends in string sub
792 * return 0 if match
793 **/
794static int strrcmp(const char *s, const char *sub)
795{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100796 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100797
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100798 if (!s || !sub)
799 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100800
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100801 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100802 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100803
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100804 if ((slen == 0) || (sublen == 0))
805 return 1;
806
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100807 if (sublen > slen)
808 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100809
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100810 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100811}
812
Sam Ravnborgff13f922008-01-23 19:54:27 +0100813static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
814{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100815 if (sym)
816 return elf->strtab + sym->st_name;
817 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100818 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100819}
820
Sam Ravnborg10668222008-01-13 22:21:31 +0100821/* The pattern is an array of simple patterns.
822 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100823 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100824 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930825 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100826 */
Trevor Keith5c725132009-09-22 16:43:38 -0700827static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100828{
829 const char *p;
830 while (*pat) {
831 p = *pat++;
832 const char *endp = p + strlen(p) - 1;
833
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930834 /* "*foo*" */
835 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300836 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
837 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930838
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930839 free(bare);
840 if (here != NULL)
841 return 1;
842 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100843 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930844 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100845 if (strrcmp(sym, p + 1) == 0)
846 return 1;
847 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100848 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100849 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100850 if (strncmp(sym, p, strlen(p) - 1) == 0)
851 return 1;
852 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100853 /* no wildcards */
854 else {
855 if (strcmp(p, sym) == 0)
856 return 1;
857 }
858 }
859 /* no match */
860 return 0;
861}
862
Sam Ravnborg10668222008-01-13 22:21:31 +0100863/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930864static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200865{
866 ".comment*",
867 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930868 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800869 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000870 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200871 ".mdebug*", /* alpha, score, mips etc. */
872 ".pdr", /* alpha, score, mips etc. */
873 ".stab*",
874 ".note*",
875 ".got*",
876 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400877 ".xt.prop", /* xtensa */
878 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030879 ".arcextmap*", /* arc */
880 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030881 ".cmem*", /* EZchip */
882 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100883 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600884 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200885 NULL
886};
Sam Ravnborg10668222008-01-13 22:21:31 +0100887
Sam Ravnborge241a632008-01-28 20:13:13 +0100888/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400889 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100890 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400891 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100892 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400893static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900894 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100895{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400896 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100897
Anders Kaseorgb614a692009-04-23 16:49:33 -0400898 if (sechdr->sh_type == SHT_PROGBITS &&
899 !(sechdr->sh_flags & SHF_ALLOC) &&
900 !match(sec, section_white_list)) {
901 warn("%s (%s): unexpected non-allocatable section.\n"
902 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
903 "Note that for example <linux/init.h> contains\n"
904 "section definitions for use in .S files.\n\n",
905 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100906 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100907}
908
909
910
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100911#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930912 ".init.setup", ".init.rodata", ".meminit.rodata", \
913 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100914#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930915 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100916
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100917#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930918 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100919#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930920 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100921
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200922#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930923 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
924 ".pci_fixup_enable", ".pci_fixup_resume", \
925 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200926
Paul Gortmakere24f6622013-06-19 19:30:48 -0400927#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
928#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100929
930#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
931#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100932
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930933#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930934#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Thomas Gleixner65538962020-03-09 22:47:17 +0100935 ".kprobes.text", ".cpuidle.text", ".noinstr.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930936#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400937 ".fixup", ".entry.text", ".exception.text", ".text.*", \
938 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100939
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000940#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000941#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100942
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000943#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000944#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100945
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930946#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
947 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
948
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100949/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930950static const char *const init_data_sections[] =
951 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100952
953/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930954static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100955
956/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930957static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100958 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100959
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930960/* all text sections */
961static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
962
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100963/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930964static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100965
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100966
967/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100968#define DEFAULT_SYMBOL_WHITE_LIST \
969 "*driver", \
970 "*_template", /* scsi uses *_template a lot */ \
971 "*_timer", /* arm uses ops structures named _timer a lot */ \
972 "*_sht", /* scsi also used *_sht to some extent */ \
973 "*_ops", \
974 "*_probe", \
975 "*_probe_one", \
976 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100977
Mathias Krause7a3ee752014-08-27 20:28:53 +0930978static const char *const head_sections[] = { ".head.text*", NULL };
979static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100980 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930981static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100982
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100983enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100984 TEXT_TO_ANY_INIT,
985 DATA_TO_ANY_INIT,
986 TEXT_TO_ANY_EXIT,
987 DATA_TO_ANY_EXIT,
988 XXXINIT_TO_SOME_INIT,
989 XXXEXIT_TO_SOME_EXIT,
990 ANY_INIT_TO_ANY_EXIT,
991 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100992 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930993 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100994};
995
Quentin Casasnovase5d8f592015-04-13 20:55:15 +0930996/**
997 * Describe how to match sections on different criterias:
998 *
999 * @fromsec: Array of sections to be matched.
1000 *
1001 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1002 * this array is forbidden (black-list). Can be empty.
1003 *
1004 * @good_tosec: Relocations applied to a section in @fromsec must be
1005 * targetting sections in this array (white-list). Can be empty.
1006 *
1007 * @mismatch: Type of mismatch.
1008 *
1009 * @symbol_white_list: Do not match a relocation to a symbol in this list
1010 * even if it is targetting a section in @bad_to_sec.
1011 *
1012 * @handler: Specific handler to call when a match is found. If NULL,
1013 * default_mismatch_handler() will be called.
1014 *
1015 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001016struct sectioncheck {
1017 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301018 const char *bad_tosec[20];
1019 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001020 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001021 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301022 void (*handler)(const char *modname, struct elf_info *elf,
1023 const struct sectioncheck* const mismatch,
1024 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1025
Sam Ravnborg10668222008-01-13 22:21:31 +01001026};
1027
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301028static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1029 const struct sectioncheck* const mismatch,
1030 Elf_Rela *r, Elf_Sym *sym,
1031 const char *fromsec);
1032
Mathias Krause7a3ee752014-08-27 20:28:53 +09301033static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001034/* Do not reference init/exit code/data from
1035 * normal code and data
1036 */
1037{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001038 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301039 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001040 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001041 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001042},
1043{
1044 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301045 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001046 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001047 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001048},
1049{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001050 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301051 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001052 .mismatch = DATA_TO_ANY_INIT,
1053 .symbol_white_list = {
1054 "*_template", "*_timer", "*_sht", "*_ops",
1055 "*_probe", "*_probe_one", "*_console", NULL
1056 },
1057},
1058{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001059 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301060 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001061 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001062 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001063},
1064{
1065 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301066 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001067 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001068 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001069},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001070/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001071{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001072 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301073 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001074 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001075 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001076},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001077/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001078{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001079 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301080 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001081 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001082 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001083},
1084/* Do not use exit code/data from init code */
1085{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001086 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301087 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001088 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001089 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001090},
1091/* Do not use init code/data from exit code */
1092{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001093 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301094 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001095 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001096 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001097},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001098{
1099 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301100 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001101 .mismatch = ANY_INIT_TO_ANY_EXIT,
1102 .symbol_white_list = { NULL },
1103},
Sam Ravnborg10668222008-01-13 22:21:31 +01001104/* Do not export init/exit functions or data */
1105{
1106 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301107 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001108 .mismatch = EXPORT_TO_INIT_EXIT,
1109 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301110},
1111{
1112 .fromsec = { "__ex_table", NULL },
1113 /* If you're adding any new black-listed sections in here, consider
1114 * adding a special 'printer' for them in scripts/check_extable.
1115 */
1116 .bad_tosec = { ".altinstr_replacement", NULL },
1117 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1118 .mismatch = EXTABLE_TO_NON_TEXT,
1119 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001120}
1121};
1122
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001123static const struct sectioncheck *section_mismatch(
1124 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001125{
1126 int i;
1127 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1128 const struct sectioncheck *check = &sectioncheck[0];
1129
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301130 /*
1131 * The target section could be the SHT_NUL section when we're
1132 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001133 * doesn't make much sense and causes build failures on parisc
1134 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301135 */
1136 if (*tosec == '\0')
1137 return NULL;
1138
Sam Ravnborg10668222008-01-13 22:21:31 +01001139 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301140 if (match(fromsec, check->fromsec)) {
1141 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1142 return check;
1143 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1144 return check;
1145 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001146 check++;
1147 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001148 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001149}
1150
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001151/**
1152 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001153 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001154 * Pattern 1:
1155 * If a module parameter is declared __initdata and permissions=0
1156 * then this is legal despite the warning generated.
1157 * We cannot see value of permissions here, so just ignore
1158 * this pattern.
1159 * The pattern is identified by:
1160 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001161 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001162 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001163 *
Rusty Russell6a841522010-08-11 23:04:16 -06001164 * Pattern 1a:
1165 * module_param_call() ops can refer to __init set function if permissions=0
1166 * The pattern is identified by:
1167 * tosec = .init.text
1168 * fromsec = .data*
1169 * atsym = __param_ops_*
1170 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001171 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001172 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001173 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001174 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001175 * tosec = init or exit section
1176 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001177 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1178 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001179 *
1180 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001181 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001182 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001183 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001184 * Some symbols belong to init section but still it is ok to reference
1185 * these from non-init sections as these symbols don't have any memory
1186 * allocated for them and symbol address and value are same. So even
1187 * if init section is freed, its ok to reference those symbols.
1188 * For ex. symbols marking the init section boundaries.
1189 * This pattern is identified by
1190 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001191 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301192 * Pattern 5:
1193 * GCC may optimize static inlines when fed constant arg(s) resulting
1194 * in functions like cpumask_empty() -- generating an associated symbol
1195 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1196 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1197 * meaningless section warning. May need to add isra symbols too...
1198 * This pattern is identified by
1199 * tosec = init section
1200 * fromsec = text section
1201 * refsymname = *.constprop.*
1202 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001203 * Pattern 6:
1204 * Hide section mismatch warnings for ELF local symbols. The goal
1205 * is to eliminate false positive modpost warnings caused by
1206 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1207 * Autogenerated symbol names bypass modpost's "Pattern 2"
1208 * whitelisting, which relies on pattern-matching against symbol
1209 * names to work. (One situation where gcc can autogenerate ELF
1210 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001211 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001212static int secref_whitelist(const struct sectioncheck *mismatch,
1213 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001214 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001215{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001216 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001217 if (match(tosec, init_data_sections) &&
1218 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001219 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001220 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001221
Rusty Russell6a841522010-08-11 23:04:16 -06001222 /* Check for pattern 1a */
1223 if (strcmp(tosec, ".init.text") == 0 &&
1224 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001225 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001226 return 0;
1227
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001228 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001229 if (match(tosec, init_exit_sections) &&
1230 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001231 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001232 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001233
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001234 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001235 if (match(fromsec, head_sections) &&
1236 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001237 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001238
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001239 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001240 if (match(tosym, linker_symbols))
1241 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001242
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301243 /* Check for pattern 5 */
1244 if (match(fromsec, text_sections) &&
1245 match(tosec, init_sections) &&
1246 match(fromsym, optim_symbols))
1247 return 0;
1248
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001249 /* Check for pattern 6 */
1250 if (strstarts(fromsym, ".L"))
1251 return 0;
1252
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001253 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001254}
1255
Sami Tolvanen5818c682018-10-23 15:15:35 -07001256static inline int is_arm_mapping_symbol(const char *str)
1257{
1258 return str[0] == '$' && strchr("axtd", str[1])
1259 && (str[2] == '\0' || str[2] == '.');
1260}
1261
1262/*
1263 * If there's no name there, ignore it; likewise, ignore it if it's
1264 * one of the magic symbols emitted used by current ARM tools.
1265 *
1266 * Otherwise if find_symbols_between() returns those symbols, they'll
1267 * fail the whitelist tests and cause lots of false alarms ... fixable
1268 * only by merging __exit and __init sections into __text, bloating
1269 * the kernel (which is especially evil on embedded platforms).
1270 */
1271static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1272{
1273 const char *name = elf->strtab + sym->st_name;
1274
1275 if (!name || !strlen(name))
1276 return 0;
1277 return !is_arm_mapping_symbol(name);
1278}
1279
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001280/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001281 * Find symbol based on relocation record info.
1282 * In some cases the symbol supplied is a valid symbol so
1283 * return refsym. If st_name != 0 we assume this is a valid symbol.
1284 * In other cases the symbol needs to be looked up in the symbol table
1285 * based on section and address.
1286 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001287static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001288 Elf_Sym *relsym)
1289{
1290 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001291 Elf_Sym *near = NULL;
1292 Elf64_Sword distance = 20;
1293 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001294 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001295
1296 if (relsym->st_name != 0)
1297 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001298
1299 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001300 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001301 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001302 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001303 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1304 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001305 if (!is_valid_name(elf, sym))
1306 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001307 if (sym->st_value == addr)
1308 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001309 /* Find a symbol nearby - addr are maybe negative */
1310 d = sym->st_value - addr;
1311 if (d < 0)
1312 d = addr - sym->st_value;
1313 if (d < distance) {
1314 distance = d;
1315 near = sym;
1316 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001317 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001318 /* We need a close match */
1319 if (distance < 20)
1320 return near;
1321 else
1322 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001323}
1324
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001325/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001326 * Find symbols before or equal addr and after addr - in the section sec.
1327 * If we find two symbols with equal offset prefer one with a valid name.
1328 * The ELF format may have a better way to detect what type of symbol
1329 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001330 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001331static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1332 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001333{
1334 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001335 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001336 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001337
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001338 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1339 const char *symsec;
1340
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001341 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001342 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001343 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001344 if (strcmp(symsec, sec) != 0)
1345 continue;
David Brownellda68d612007-02-20 13:58:16 -08001346 if (!is_valid_name(elf, sym))
1347 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001348 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001349 if ((addr - sym->st_value) < distance) {
1350 distance = addr - sym->st_value;
1351 near = sym;
1352 } else if ((addr - sym->st_value) == distance) {
1353 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001354 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001355 }
1356 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001357 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001358}
1359
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001360/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001361 * Convert a section name to the function/data attribute
1362 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001363 * .memexitconst => __memconst
1364 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001365 *
1366 * The memory of returned value has been allocated on a heap. The user of this
1367 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001368*/
1369static char *sec2annotation(const char *s)
1370{
1371 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001372 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001373 char *r = p;
1374
1375 *p++ = '_';
1376 *p++ = '_';
1377 if (*s == '.')
1378 s++;
1379 while (*s && *s != '.')
1380 *p++ = *s++;
1381 *p = '\0';
1382 if (*s == '.')
1383 s++;
1384 if (strstr(s, "rodata") != NULL)
1385 strcat(p, "const ");
1386 else if (strstr(s, "data") != NULL)
1387 strcat(p, "data ");
1388 else
1389 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001390 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001391 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001392 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001393 }
1394}
1395
1396static int is_function(Elf_Sym *sym)
1397{
1398 if (sym)
1399 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1400 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001401 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001402}
1403
Randy Dunlap00759c02011-03-15 14:13:47 -07001404static void print_section_list(const char * const list[20])
1405{
1406 const char *const *s = list;
1407
1408 while (*s) {
1409 fprintf(stderr, "%s", *s);
1410 s++;
1411 if (*s)
1412 fprintf(stderr, ", ");
1413 }
1414 fprintf(stderr, "\n");
1415}
1416
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301417static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1418{
1419 switch (is_func) {
1420 case 0: *name = "variable"; *name_p = ""; break;
1421 case 1: *name = "function"; *name_p = "()"; break;
1422 default: *name = "(unknown reference)"; *name_p = ""; break;
1423 }
1424}
1425
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001426/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001427 * Print a warning about a section mismatch.
1428 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001429 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001430 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001431static void report_sec_mismatch(const char *modname,
1432 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001433 const char *fromsec,
1434 unsigned long long fromaddr,
1435 const char *fromsym,
1436 int from_is_func,
1437 const char *tosec, const char *tosym,
1438 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001439{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001440 const char *from, *from_p;
1441 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001442 char *prl_from;
1443 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001444
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001445 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001446
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301447 get_pretty_name(from_is_func, &from, &from_p);
1448 get_pretty_name(to_is_func, &to, &to_p);
1449
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001450 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1451 "to the %s %s:%s%s\n",
1452 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1453 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001454
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001455 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001456 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001457 prl_from = sec2annotation(fromsec);
1458 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001459 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001460 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001461 "the %s %s%s%s.\n"
1462 "This is often because %s lacks a %s\n"
1463 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001464 prl_from, fromsym,
1465 to, prl_to, tosym, to_p,
1466 fromsym, prl_to, tosym);
1467 free(prl_from);
1468 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001469 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001470 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001471 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001472 fprintf(stderr,
1473 "The variable %s references\n"
1474 "the %s %s%s%s\n"
1475 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001476 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001477 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001478 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001479 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001480 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001481 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001482 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001483 case TEXT_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 function %s() references a %s in an exit section.\n"
1487 "Often the %s %s%s has valid usage outside the exit section\n"
1488 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001489 fromsym, to, to, tosym, to_p, prl_to, tosym);
1490 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001491 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001492 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001493 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001494 fprintf(stderr,
1495 "The variable %s references\n"
1496 "the %s %s%s%s\n"
1497 "If the reference is valid then annotate the\n"
1498 "variable with __exit* (see linux/init.h) or "
1499 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001500 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001501 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001502 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001503 break;
1504 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001505 case XXXINIT_TO_SOME_INIT:
1506 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001507 prl_from = sec2annotation(fromsec);
1508 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001509 fprintf(stderr,
1510 "The %s %s%s%s references\n"
1511 "a %s %s%s%s.\n"
1512 "If %s is only used by %s then\n"
1513 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001514 from, prl_from, fromsym, from_p,
1515 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001516 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001517 free(prl_from);
1518 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001519 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001520 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001521 prl_from = sec2annotation(fromsec);
1522 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001523 fprintf(stderr,
1524 "The %s %s%s%s references\n"
1525 "a %s %s%s%s.\n"
1526 "This is often seen when error handling "
1527 "in the init function\n"
1528 "uses functionality in the exit path.\n"
1529 "The fix is often to remove the %sannotation of\n"
1530 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001531 from, prl_from, fromsym, from_p,
1532 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001533 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001534 free(prl_from);
1535 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001536 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001537 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001538 prl_from = sec2annotation(fromsec);
1539 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001540 fprintf(stderr,
1541 "The %s %s%s%s references\n"
1542 "a %s %s%s%s.\n"
1543 "This is often seen when error handling "
1544 "in the exit function\n"
1545 "uses functionality in the init path.\n"
1546 "The fix is often to remove the %sannotation of\n"
1547 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001548 from, prl_from, fromsym, from_p,
1549 to, prl_to, tosym, to_p,
1550 prl_to, tosym, to_p);
1551 free(prl_from);
1552 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001553 break;
1554 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001555 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001556 fprintf(stderr,
1557 "The symbol %s is exported and annotated %s\n"
1558 "Fix this by removing the %sannotation of %s "
1559 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001560 tosym, prl_to, prl_to, tosym);
1561 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001562 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301563 case EXTABLE_TO_NON_TEXT:
1564 fatal("There's a special handler for this mismatch type, "
1565 "we should never get here.");
1566 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001567 }
1568 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001569}
1570
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301571static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1572 const struct sectioncheck* const mismatch,
1573 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1574{
1575 const char *tosec;
1576 Elf_Sym *to;
1577 Elf_Sym *from;
1578 const char *tosym;
1579 const char *fromsym;
1580
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301581 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1582 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301583
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001584 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301585 return;
1586
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301587 tosec = sec_name(elf, get_secindex(elf, sym));
1588 to = find_elf_symbol(elf, r->r_addend, sym);
1589 tosym = sym_name(elf, to);
1590
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301591 /* check whitelist - we may ignore it */
1592 if (secref_whitelist(mismatch,
1593 fromsec, fromsym, tosec, tosym)) {
1594 report_sec_mismatch(modname, mismatch,
1595 fromsec, r->r_offset, fromsym,
1596 is_function(from), tosec, tosym,
1597 is_function(to));
1598 }
1599}
1600
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301601static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1602{
1603 if (section_index > elf->num_sections)
1604 fatal("section_index is outside elf->num_sections!\n");
1605
1606 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1607}
1608
1609/*
1610 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1611 * to know the sizeof(struct exception_table_entry) for the target architecture.
1612 */
1613static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301614static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301615{
1616 /*
1617 * If we're currently checking the second relocation within __ex_table,
1618 * that relocation offset tells us the offsetof(struct
1619 * exception_table_entry, fixup) which is equal to sizeof(struct
1620 * exception_table_entry) divided by two. We use that to our advantage
1621 * since there's no portable way to get that size as every architecture
1622 * seems to go with different sized types. Not pretty but better than
1623 * hard-coding the size for every architecture..
1624 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301625 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301626 extable_entry_size = r->r_offset * 2;
1627}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301628
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301629static inline bool is_extable_fault_address(Elf_Rela *r)
1630{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301631 /*
1632 * extable_entry_size is only discovered after we've handled the
1633 * _second_ relocation in __ex_table, so only abort when we're not
1634 * handling the first reloc and extable_entry_size is zero.
1635 */
1636 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301637 fatal("extable_entry size hasn't been discovered!\n");
1638
1639 return ((r->r_offset == 0) ||
1640 (r->r_offset % extable_entry_size == 0));
1641}
1642
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301643#define is_second_extable_reloc(Start, Cur, Sec) \
1644 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1645
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301646static void report_extable_warnings(const char* modname, struct elf_info* elf,
1647 const struct sectioncheck* const mismatch,
1648 Elf_Rela* r, Elf_Sym* sym,
1649 const char* fromsec, const char* tosec)
1650{
1651 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1652 const char* fromsym_name = sym_name(elf, fromsym);
1653 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1654 const char* tosym_name = sym_name(elf, tosym);
1655 const char* from_pretty_name;
1656 const char* from_pretty_name_p;
1657 const char* to_pretty_name;
1658 const char* to_pretty_name_p;
1659
1660 get_pretty_name(is_function(fromsym),
1661 &from_pretty_name, &from_pretty_name_p);
1662 get_pretty_name(is_function(tosym),
1663 &to_pretty_name, &to_pretty_name_p);
1664
1665 warn("%s(%s+0x%lx): Section mismatch in reference"
1666 " from the %s %s%s to the %s %s:%s%s\n",
1667 modname, fromsec, (long)r->r_offset, from_pretty_name,
1668 fromsym_name, from_pretty_name_p,
1669 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1670
1671 if (!match(tosec, mismatch->bad_tosec) &&
1672 is_executable_section(elf, get_secindex(elf, sym)))
1673 fprintf(stderr,
1674 "The relocation at %s+0x%lx references\n"
1675 "section \"%s\" which is not in the list of\n"
1676 "authorized sections. If you're adding a new section\n"
1677 "and/or if this reference is valid, add \"%s\" to the\n"
1678 "list of authorized sections to jump to on fault.\n"
1679 "This can be achieved by adding \"%s\" to \n"
1680 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1681 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1682}
1683
1684static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1685 const struct sectioncheck* const mismatch,
1686 Elf_Rela* r, Elf_Sym* sym,
1687 const char *fromsec)
1688{
1689 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1690
1691 sec_mismatch_count++;
1692
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001693 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301694
1695 if (match(tosec, mismatch->bad_tosec))
1696 fatal("The relocation at %s+0x%lx references\n"
1697 "section \"%s\" which is black-listed.\n"
1698 "Something is seriously wrong and should be fixed.\n"
1699 "You might get more information about where this is\n"
1700 "coming from by using scripts/check_extable.sh %s\n",
1701 fromsec, (long)r->r_offset, tosec, modname);
1702 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1703 if (is_extable_fault_address(r))
1704 fatal("The relocation at %s+0x%lx references\n"
1705 "section \"%s\" which is not executable, IOW\n"
1706 "it is not possible for the kernel to fault\n"
1707 "at that address. Something is seriously wrong\n"
1708 "and should be fixed.\n",
1709 fromsec, (long)r->r_offset, tosec);
1710 else
1711 fatal("The relocation at %s+0x%lx references\n"
1712 "section \"%s\" which is not executable, IOW\n"
1713 "the kernel will fault if it ever tries to\n"
1714 "jump to it. Something is seriously wrong\n"
1715 "and should be fixed.\n",
1716 fromsec, (long)r->r_offset, tosec);
1717 }
1718}
1719
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001720static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001721 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001722{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001723 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301724 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001725
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001726 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301727 if (mismatch->handler)
1728 mismatch->handler(modname, elf, mismatch,
1729 r, sym, fromsec);
1730 else
1731 default_mismatch_handler(modname, elf, mismatch,
1732 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001733 }
1734}
1735
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001736static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001737 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001738{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001739 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001740}
1741
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001742static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001743{
1744 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001745 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001746
1747 switch (r_typ) {
1748 case R_386_32:
1749 r->r_addend = TO_NATIVE(*location);
1750 break;
1751 case R_386_PC32:
1752 r->r_addend = TO_NATIVE(*location) + 4;
1753 /* For CONFIG_RELOCATABLE=y */
1754 if (elf->hdr->e_type == ET_EXEC)
1755 r->r_addend += r->r_offset;
1756 break;
1757 }
1758 return 0;
1759}
1760
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001761#ifndef R_ARM_CALL
1762#define R_ARM_CALL 28
1763#endif
1764#ifndef R_ARM_JUMP24
1765#define R_ARM_JUMP24 29
1766#endif
1767
David A. Longc9698e52014-02-14 22:41:18 +01001768#ifndef R_ARM_THM_CALL
1769#define R_ARM_THM_CALL 10
1770#endif
1771#ifndef R_ARM_THM_JUMP24
1772#define R_ARM_THM_JUMP24 30
1773#endif
1774#ifndef R_ARM_THM_JUMP19
1775#define R_ARM_THM_JUMP19 51
1776#endif
1777
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001778static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001779{
1780 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1781
1782 switch (r_typ) {
1783 case R_ARM_ABS32:
1784 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001785 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001786 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001787 break;
1788 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001789 case R_ARM_CALL:
1790 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001791 case R_ARM_THM_CALL:
1792 case R_ARM_THM_JUMP24:
1793 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001794 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001795 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001796 sechdr->sh_offset +
1797 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001798 break;
1799 default:
1800 return 1;
1801 }
1802 return 0;
1803}
1804
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001805static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001806{
1807 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001808 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001809 unsigned int inst;
1810
1811 if (r_typ == R_MIPS_HI16)
1812 return 1; /* skip this */
1813 inst = TO_NATIVE(*location);
1814 switch (r_typ) {
1815 case R_MIPS_LO16:
1816 r->r_addend = inst & 0xffff;
1817 break;
1818 case R_MIPS_26:
1819 r->r_addend = (inst & 0x03ffffff) << 2;
1820 break;
1821 case R_MIPS_32:
1822 r->r_addend = inst;
1823 break;
1824 }
1825 return 0;
1826}
1827
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001828static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001829 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001830{
1831 Elf_Sym *sym;
1832 Elf_Rela *rela;
1833 Elf_Rela r;
1834 unsigned int r_sym;
1835 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001836
Sam Ravnborgff13f922008-01-23 19:54:27 +01001837 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001838 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1839
Sam Ravnborgff13f922008-01-23 19:54:27 +01001840 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001841 fromsec += strlen(".rela");
1842 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001843 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001844 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001845
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001846 for (rela = start; rela < stop; rela++) {
1847 r.r_offset = TO_NATIVE(rela->r_offset);
1848#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001849 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001850 unsigned int r_typ;
1851 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1852 r_sym = TO_NATIVE(r_sym);
1853 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1854 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1855 } else {
1856 r.r_info = TO_NATIVE(rela->r_info);
1857 r_sym = ELF_R_SYM(r.r_info);
1858 }
1859#else
1860 r.r_info = TO_NATIVE(rela->r_info);
1861 r_sym = ELF_R_SYM(r.r_info);
1862#endif
1863 r.r_addend = TO_NATIVE(rela->r_addend);
1864 sym = elf->symtab_start + r_sym;
1865 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001866 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001867 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301868 if (is_second_extable_reloc(start, rela, fromsec))
1869 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001870 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001871 }
1872}
1873
1874static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001875 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001876{
1877 Elf_Sym *sym;
1878 Elf_Rel *rel;
1879 Elf_Rela r;
1880 unsigned int r_sym;
1881 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001882
Sam Ravnborgff13f922008-01-23 19:54:27 +01001883 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001884 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1885
Sam Ravnborgff13f922008-01-23 19:54:27 +01001886 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001887 fromsec += strlen(".rel");
1888 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001889 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001890 return;
1891
1892 for (rel = start; rel < stop; rel++) {
1893 r.r_offset = TO_NATIVE(rel->r_offset);
1894#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001895 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001896 unsigned int r_typ;
1897 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1898 r_sym = TO_NATIVE(r_sym);
1899 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1900 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1901 } else {
1902 r.r_info = TO_NATIVE(rel->r_info);
1903 r_sym = ELF_R_SYM(r.r_info);
1904 }
1905#else
1906 r.r_info = TO_NATIVE(rel->r_info);
1907 r_sym = ELF_R_SYM(r.r_info);
1908#endif
1909 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001910 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001911 case EM_386:
1912 if (addend_386_rel(elf, sechdr, &r))
1913 continue;
1914 break;
1915 case EM_ARM:
1916 if (addend_arm_rel(elf, sechdr, &r))
1917 continue;
1918 break;
1919 case EM_MIPS:
1920 if (addend_mips_rel(elf, sechdr, &r))
1921 continue;
1922 break;
1923 }
1924 sym = elf->symtab_start + r_sym;
1925 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001926 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001927 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301928 if (is_second_extable_reloc(start, rel, fromsec))
1929 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001930 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001931 }
1932}
1933
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001934/**
1935 * A module includes a number of sections that are discarded
1936 * either when loaded or when used as built-in.
1937 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001938 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001939 * Likewise for modules used built-in the sections marked __exit
1940 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001941 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001942 * The check_sec_ref() function traverses all relocation records
1943 * to find all references to a section that reference a section that will
1944 * be discarded and warns about it.
1945 **/
1946static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001947 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001948{
1949 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001950 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001951
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001952 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001953 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001954 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001955 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001956 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001957 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001958 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001959 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001960 }
1961}
1962
Andi Kleen7d02b492014-02-08 09:01:12 +01001963static char *remove_dot(char *s)
1964{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301965 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001966
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301967 if (n && s[n]) {
1968 size_t m = strspn(s + n + 1, "0123456789");
1969 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001970 s[n] = 0;
1971 }
1972 return s;
1973}
1974
Masahiro Yamada8b185742018-05-09 18:50:40 +09001975static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 const char *symname;
1978 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001979 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001980 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 struct module *mod;
1982 struct elf_info info = { };
1983 Elf_Sym *sym;
1984
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001985 if (!parse_elf(&info, modname))
1986 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09001988 {
1989 char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Masahiro Yamadaa82f7942020-06-01 14:57:29 +09001991 /* strip trailing .o */
1992 tmp = NOFAIL(strdup(modname));
1993 tmp[strlen(tmp) - 2] = '\0';
1994 mod = new_module(tmp);
1995 free(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997
Masahiro Yamada5a438af2020-06-01 14:57:26 +09001998 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09001999 license = get_modinfo(&info, "license");
2000 if (!license)
Masahiro Yamada1d6cd3922020-12-01 19:34:16 +09002001 error("missing MODULE_LICENSE() in %s\n", modname);
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002002 while (license) {
2003 if (license_is_gpl_compatible(license))
2004 mod->gpl_compatible = 1;
2005 else {
2006 mod->gpl_compatible = 0;
2007 break;
2008 }
2009 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002010 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002011
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002012 namespace = get_modinfo(&info, "import_ns");
2013 while (namespace) {
2014 add_namespace(&mod->imported_namespaces, namespace);
2015 namespace = get_next_modinfo(&info, "import_ns",
2016 namespace);
2017 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002018 }
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002021 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002023 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 handle_moddevtable(mod, &info, sym, symname);
2025 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002026
Matthias Maennich69923202019-10-18 10:31:42 +01002027 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2028 symname = remove_dot(info.strtab + sym->st_name);
2029
Masahiro Yamada17436942019-11-15 02:42:24 +09002030 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002031 if (strstarts(symname, "__kstrtabns_"))
2032 sym_update_namespace(symname + strlen("__kstrtabns_"),
2033 namespace_from_kstrtabns(&info,
2034 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002035
2036 if (strstarts(symname, "__crc_"))
2037 handle_modversion(mod, &info, sym,
2038 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002039 }
2040
Denis Efremov15bfc232019-08-01 09:06:57 +03002041 // check for static EXPORT_SYMBOL_* functions && global vars
2042 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2043 unsigned char bind = ELF_ST_BIND(sym->st_info);
2044
2045 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2046 struct symbol *s =
2047 find_symbol(remove_dot(info.strtab +
2048 sym->st_name));
2049
2050 if (s)
2051 s->is_static = 0;
2052 }
2053 }
2054
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002055 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002057 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002058 version = get_modinfo(&info, "version");
2059 if (version || all_versions)
2060 get_src_version(modname, mod->srcversion,
2061 sizeof(mod->srcversion) - 1);
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 parse_elf_finish(&info);
2065
Rusty Russell8c8ef422009-03-31 13:05:34 -06002066 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 * never passed as an argument to an exported function, so
2068 * the automatic versioning doesn't pick it up, but it's really
2069 * important anyhow */
2070 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002071 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Rusty Russell712f9b42013-04-04 17:37:38 +10302074static void read_symbols_from_files(const char *filename)
2075{
2076 FILE *in = stdin;
2077 char fname[PATH_MAX];
2078
2079 if (strcmp(filename, "-") != 0) {
2080 in = fopen(filename, "r");
2081 if (!in)
2082 fatal("Can't open filenames file %s: %m", filename);
2083 }
2084
2085 while (fgets(fname, PATH_MAX, in) != NULL) {
2086 if (strends(fname, "\n"))
2087 fname[strlen(fname)-1] = '\0';
2088 read_symbols(fname);
2089 }
2090
2091 if (in != stdin)
2092 fclose(in);
2093}
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095#define SZ 500
2096
2097/* We first write the generated file into memory using the
2098 * following helper, then compare to the file on disk and
2099 * only update the later if anything changed */
2100
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002101void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2102 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 char tmp[SZ];
2105 int len;
2106 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 va_start(ap, fmt);
2109 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002110 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 va_end(ap);
2112}
2113
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002114void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002117 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002118 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120 strncpy(buf->p + buf->pos, s, len);
2121 buf->pos += len;
2122}
2123
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002124static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2125{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002126 switch (exp) {
2127 case export_gpl:
Masahiro Yamadad6d692f2020-12-01 19:34:17 +09002128 error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002129 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002130 break;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002131 case export_plain:
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002132 case export_unknown:
2133 /* ignore */
2134 break;
2135 }
2136}
2137
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002138static void check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002139{
2140 struct symbol *s, *exp;
2141
2142 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002143 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002144 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002145 if (!exp || exp->module == mod) {
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002146 if (have_vmlinux && !s->weak)
Jessica Yu93c95e52020-03-06 17:02:05 +01002147 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2148 "\"%s\" [%s.ko] undefined!\n",
2149 s->name, mod->name);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002150 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002151 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002152 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002153 if (basename)
2154 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002155 else
2156 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002157
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002158 if (exp->namespace &&
2159 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002160 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2161 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2162 basename, exp->name, exp->namespace);
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002163 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002164 }
2165
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002166 if (!mod->gpl_compatible)
2167 check_for_gpl_usage(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002168 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002169}
2170
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002171static void check_modname_len(struct module *mod)
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002172{
2173 const char *mod_name;
2174
2175 mod_name = strrchr(mod->name, '/');
2176 if (mod_name == NULL)
2177 mod_name = mod->name;
2178 else
2179 mod_name++;
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002180 if (strlen(mod_name) >= MODULE_NAME_LEN)
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002181 error("module name is too long [%s.ko]\n", mod->name);
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002182}
2183
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002184/**
2185 * Header for the generated file
2186 **/
2187static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002190 /*
2191 * Include build-salt.h after module.h in order to
2192 * inherit the definitions.
2193 */
Leon Romanovsky51161bf2020-04-19 18:55:06 +03002194 buf_printf(b, "#define INCLUDE_VERMAGIC\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002195 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 buf_printf(b, "#include <linux/vermagic.h>\n");
2197 buf_printf(b, "#include <linux/compiler.h>\n");
2198 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002199 buf_printf(b, "BUILD_SALT;\n");
2200 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002202 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302204 buf_printf(b, "__visible struct module __this_module\n");
Joe Perches33def842020-10-21 19:36:07 -07002205 buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002206 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002208 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (mod->has_cleanup)
2210 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002211 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002213 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 buf_printf(b, "};\n");
2215}
2216
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002217static void add_intree_flag(struct buffer *b, int is_intree)
2218{
2219 if (is_intree)
2220 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2221}
2222
Andi Kleencaf75012018-01-25 15:50:28 -08002223/* Cannot check for assembler */
2224static void add_retpoline(struct buffer *b)
2225{
WANG Chaoe4f35892018-12-11 00:37:25 +08002226 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002227 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2228 buf_printf(b, "#endif\n");
2229}
2230
Trevor Keith5c725132009-09-22 16:43:38 -07002231static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002232{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002233 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002234 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2235}
2236
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002237/**
2238 * Record CRCs for unresolved symbols
2239 **/
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002240static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
2242 struct symbol *s, *exp;
2243
2244 for (s = mod->unres; s; s = s->next) {
2245 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002246 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 s->module = exp->module;
2249 s->crc_valid = exp->crc_valid;
2250 s->crc = exp->crc;
2251 }
2252
2253 if (!modversions)
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002254 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 buf_printf(b, "\n");
2257 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Joe Perches33def842020-10-21 19:36:07 -07002258 buf_printf(b, "__used __section(\"__versions\") = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002261 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002264 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 s->name, mod->name);
2266 continue;
2267 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302268 if (strlen(s->name) >= MODULE_NAME_LEN) {
Masahiro Yamadabc72d722020-12-01 19:34:14 +09002269 error("too long symbol \"%s\" [%s.ko]\n",
2270 s->name, mod->name);
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302271 break;
2272 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002273 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302274 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276
2277 buf_printf(b, "};\n");
2278}
2279
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002280static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 int first = 1;
2284
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002285 /* Clear ->seen flag of modules that own symbols needed by this. */
2286 for (s = mod->unres; s; s = s->next)
2287 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002288 s->module->seen = s->module->is_vmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002291 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002293 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (!s->module)
2295 continue;
2296
2297 if (s->module->seen)
2298 continue;
2299
2300 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002301 p = strrchr(s->module->name, '/');
2302 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002303 p++;
2304 else
2305 p = s->module->name;
2306 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 first = 0;
2308 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002309 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002312static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 if (mod->srcversion[0]) {
2315 buf_printf(b, "\n");
2316 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2317 mod->srcversion);
2318 }
2319}
2320
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002321static void write_buf(struct buffer *b, const char *fname)
2322{
2323 FILE *file;
2324
2325 file = fopen(fname, "w");
2326 if (!file) {
2327 perror(fname);
2328 exit(1);
2329 }
2330 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2331 perror(fname);
2332 exit(1);
2333 }
2334 if (fclose(file) != 0) {
2335 perror(fname);
2336 exit(1);
2337 }
2338}
2339
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002340static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 char *tmp;
2343 FILE *file;
2344 struct stat st;
2345
2346 file = fopen(fname, "r");
2347 if (!file)
2348 goto write;
2349
2350 if (fstat(fileno(file), &st) < 0)
2351 goto close_write;
2352
2353 if (st.st_size != b->pos)
2354 goto close_write;
2355
2356 tmp = NOFAIL(malloc(b->pos));
2357 if (fread(tmp, 1, b->pos, file) != b->pos)
2358 goto free_write;
2359
2360 if (memcmp(tmp, b->p, b->pos) != 0)
2361 goto free_write;
2362
2363 free(tmp);
2364 fclose(file);
2365 return;
2366
2367 free_write:
2368 free(tmp);
2369 close_write:
2370 fclose(file);
2371 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002372 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Ram Paibd5cbce2006-06-08 22:12:53 -07002375/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002376 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002377 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002378static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002380 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002382 buf = read_text_file(fname);
2383 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 /* No symbol versions, silently ignore */
2385 return;
2386
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002387 pos = buf;
2388
2389 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002390 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 unsigned int crc;
2392 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002393 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 if (!(symname = strchr(line, '\t')))
2396 goto fail;
2397 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002398 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 goto fail;
2400 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002401 if (!(export = strchr(modname, '\t')))
2402 goto fail;
2403 *export++ = '\0';
2404 if (!(namespace = strchr(export, '\t')))
2405 goto fail;
2406 *namespace++ = '\0';
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 crc = strtoul(line, &d, 16);
2409 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2410 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002411 mod = find_module(modname);
2412 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002413 mod = new_module(modname);
Masahiro Yamada52c34162020-06-01 14:57:05 +09002414 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002416 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002417 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002418 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002419 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002421 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return;
2423fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002424 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 fatal("parse error in symbol dump file\n");
2426}
2427
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002428/* For normal builds always dump all symbols.
2429 * For external modules only dump symbols
2430 * that are not read from kernel Module.symvers.
2431 **/
2432static int dump_sym(struct symbol *sym)
2433{
2434 if (!external_module)
2435 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002436 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002437 return 0;
2438 return 1;
2439}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002440
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002441static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
2443 struct buffer buf = { };
2444 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002445 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 int n;
2447
2448 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2449 symbol = symbolhash[n];
2450 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002451 if (dump_sym(symbol)) {
2452 namespace = symbol->namespace;
2453 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2454 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002455 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002456 export_str(symbol->export),
2457 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 symbol = symbol->next;
2460 }
2461 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002462 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002463 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002466static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002467{
2468 struct module *mod;
2469 struct namespace_list *ns;
2470 struct buffer ns_deps_buf = {};
2471
2472 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002473
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002474 if (mod->from_dump || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002475 continue;
2476
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002477 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002478
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002479 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2480 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002481
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002482 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002483 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002484
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002485 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002486 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002487}
2488
Masahiro Yamada79247992020-06-01 14:57:07 +09002489struct dump_list {
2490 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002491 const char *file;
2492};
2493
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002494int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
2496 struct module *mod;
2497 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002498 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302499 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 int opt;
Denis Efremov15bfc232019-08-01 09:06:57 +03002501 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002502 struct dump_list *dump_read_start = NULL;
2503 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002505 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002506 switch (opt) {
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002507 case 'e':
2508 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002509 break;
2510 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002511 *dump_read_iter =
2512 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2513 (*dump_read_iter)->file = optarg;
2514 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002515 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002516 case 'm':
2517 modversions = 1;
2518 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302519 case 'n':
2520 ignore_missing_files = 1;
2521 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002522 case 'o':
2523 dump_write = optarg;
2524 break;
2525 case 'a':
2526 all_versions = 1;
2527 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302528 case 'T':
2529 files_source = optarg;
2530 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002531 case 'w':
2532 warn_unresolved = 1;
2533 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302534 case 'E':
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002535 sec_mismatch_warn_only = false;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302536 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002537 case 'N':
2538 allow_missing_ns_imports = 1;
2539 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002540 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002541 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002542 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002543 default:
2544 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
2546 }
2547
Masahiro Yamada79247992020-06-01 14:57:07 +09002548 while (dump_read_start) {
2549 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002550
Masahiro Yamada79247992020-06-01 14:57:07 +09002551 read_dump(dump_read_start->file);
2552 tmp = dump_read_start->next;
2553 free(dump_read_start);
2554 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002557 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Rusty Russell712f9b42013-04-04 17:37:38 +10302560 if (files_source)
2561 read_symbols_from_files(files_source);
2562
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002563 /*
2564 * When there's no vmlinux, don't print warnings about
2565 * unresolved symbols (since there'll be too many ;)
2566 */
2567 if (!have_vmlinux)
2568 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2569
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002570 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302571 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002572
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002573 if (mod->is_vmlinux || mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002574 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 buf.pos = 0;
2577
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002578 check_modname_len(mod);
2579 check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002582 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002583 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002584 add_staging_flag(&buf, mod->name);
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002585 add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002586 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 add_moddevtable(&buf, mod);
2588 add_srcversion(&buf, mod);
2589
2590 sprintf(fname, "%s.mod.c", mod->name);
2591 write_if_changed(&buf, fname);
2592 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002593
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002594 if (missing_namespace_deps)
2595 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if (dump_write)
2598 write_dump(dump_write);
Masahiro Yamadac7299d92020-12-01 19:34:18 +09002599 if (sec_mismatch_count && !sec_mismatch_warn_only)
2600 error("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002601 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002602 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002603 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002604
Masahiro Yamada47346e92019-09-24 21:07:40 +09002605 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002606 if (s->is_static)
Quentin Perretb9ed8472020-12-01 16:52:22 +00002607 error("\"%s\" [%s] is a static %s\n",
2608 s->name, s->module->name,
2609 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002610 }
2611 }
2612
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002613 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Masahiro Yamada0fd3fba2020-12-01 19:34:15 +09002615 return error_occurred ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}