blob: b317328ae21b0055677990c8733bc1488eaee482 [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;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103037static int sec_mismatch_fatal = 0;
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
Sam Ravnborgc96fca22006-07-01 11:44:23 +020043enum export {
44 export_plain, export_unused, export_gpl,
45 export_unused_gpl, export_gpl_future, export_unknown
46};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080048/* In kernel, this size is defined in linux/module.h;
49 * here we use Elf_Addr instead of long for covering cross-compile
50 */
51
52#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
53
Jessica Yu93c95e52020-03-06 17:02:05 +010054void __attribute__((format(printf, 2, 3)))
55modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
Jessica Yu93c95e52020-03-06 17:02:05 +010059 switch (loglevel) {
60 case LOG_WARN:
61 fprintf(stderr, "WARNING: ");
62 break;
63 case LOG_ERROR:
64 fprintf(stderr, "ERROR: ");
65 break;
66 case LOG_FATAL:
67 fprintf(stderr, "FATAL: ");
68 break;
69 default: /* invalid loglevel, ignore */
70 break;
71 }
72
73 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 va_start(arglist, fmt);
76 vfprintf(stderr, fmt, arglist);
77 va_end(arglist);
78
Jessica Yu93c95e52020-03-06 17:02:05 +010079 if (loglevel == LOG_FATAL)
80 exit(1);
Matthew Wilcox2a116652006-10-07 05:35:32 -060081}
82
Rusty Russelld4ef1c32013-04-04 17:37:32 +103083static inline bool strends(const char *str, const char *postfix)
84{
85 if (strlen(str) < strlen(postfix))
86 return false;
87
88 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
89}
90
Sam Ravnborg040fcc82006-01-28 22:15:55 +010091static int is_vmlinux(const char *modname)
92{
93 const char *myname;
94
Sam Ravnborgdf578e72008-01-11 19:17:15 +010095 myname = strrchr(modname, '/');
96 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010097 myname++;
98 else
99 myname = modname;
100
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200101 return (strcmp(myname, "vmlinux") == 0) ||
102 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void *do_nofail(void *ptr, const char *expr)
106{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100107 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100108 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return ptr;
111}
112
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900113char *read_text_file(const char *filename)
114{
115 struct stat st;
116 size_t nbytes;
117 int fd;
118 char *buf;
119
120 fd = open(filename, O_RDONLY);
121 if (fd < 0) {
122 perror(filename);
123 exit(1);
124 }
125
126 if (fstat(fd, &st) < 0) {
127 perror(filename);
128 exit(1);
129 }
130
131 buf = NOFAIL(malloc(st.st_size + 1));
132
133 nbytes = st.st_size;
134
135 while (nbytes) {
136 ssize_t bytes_read;
137
138 bytes_read = read(fd, buf, nbytes);
139 if (bytes_read < 0) {
140 perror(filename);
141 exit(1);
142 }
143
144 nbytes -= bytes_read;
145 }
146 buf[st.st_size] = '\0';
147
148 close(fd);
149
150 return buf;
151}
152
153char *get_line(char **stringp)
154{
155 /* do not return the unwanted extra line at EOF */
156 if (*stringp && **stringp == '\0')
157 return NULL;
158
159 return strsep(stringp, "\n");
160}
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;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030178 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 mod = NOFAIL(malloc(sizeof(*mod)));
181 memset(mod, 0, sizeof(*mod));
182 p = NOFAIL(strdup(modname));
183
184 /* strip trailing .o */
Masahiro Yamada33795762020-06-01 14:57:24 +0900185 if (strends(p, ".o"))
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030186 p[strlen(p) - 2] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /* add to list */
189 mod->name = p;
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900190 mod->is_vmlinux = is_vmlinux(modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200191 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 mod->next = modules;
193 modules = mod;
194
Masahiro Yamada858b9372020-06-01 14:57:28 +0900195 if (mod->is_vmlinux)
196 have_vmlinux = 1;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return mod;
199}
200
201/* A hash of all exported symbols,
202 * struct symbol is also used for lists of unresolved symbols */
203
204#define SYMBOL_HASH_SIZE 1024
205
206struct symbol {
207 struct symbol *next;
208 struct module *module;
209 unsigned int crc;
210 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900211 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300213 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700214 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500215 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
219
220/* This is based on the hash agorithm from gdbm, via tdb */
221static inline unsigned int tdb_hash(const char *name)
222{
223 unsigned value; /* Used to compute the hash value. */
224 unsigned i; /* Used to cycle through random values. */
225
226 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100227 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
229
230 return (1103515243 * value + 12345);
231}
232
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100233/**
234 * Allocate a new symbols for use in the hash of exported symbols or
235 * the list of unresolved symbols per module
236 **/
237static struct symbol *alloc_symbol(const char *name, unsigned int weak,
238 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
241
242 memset(s, 0, sizeof(*s));
243 strcpy(s->name, name);
244 s->weak = weak;
245 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300246 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return s;
248}
249
250/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700251static struct symbol *new_symbol(const char *name, struct module *module,
252 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900257 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
258
259 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100262static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct symbol *s;
265
266 /* For our purposes, .foo matches foo. PPC64 needs this. */
267 if (name[0] == '.')
268 name++;
269
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100270 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (strcmp(s->name, name) == 0)
272 return s;
273 }
274 return NULL;
275}
276
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100277static bool contains_namespace(struct namespace_list *list,
278 const char *namespace)
279{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900280 for (; list; list = list->next)
281 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100282 return true;
283
284 return false;
285}
286
287static void add_namespace(struct namespace_list **list, const char *namespace)
288{
289 struct namespace_list *ns_entry;
290
291 if (!contains_namespace(*list, namespace)) {
292 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
293 strlen(namespace) + 1));
294 strcpy(ns_entry->namespace, namespace);
295 ns_entry->next = *list;
296 *list = ns_entry;
297 }
298}
299
300static bool module_imports_namespace(struct module *module,
301 const char *namespace)
302{
303 return contains_namespace(module->imported_namespaces, namespace);
304}
305
Mathias Krause7a3ee752014-08-27 20:28:53 +0930306static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700307 const char *str;
308 enum export export;
309} export_list[] = {
310 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200311 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700312 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200313 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700314 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
315 { .str = "(unknown)", .export = export_unknown },
316};
317
318
319static const char *export_str(enum export ex)
320{
321 return export_list[ex].str;
322}
323
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100324static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700325{
326 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100327
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200328 if (!s)
329 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700330 for (i = 0; export_list[i].export != export_unknown; i++) {
331 if (strcmp(export_list[i].str, s) == 0)
332 return export_list[i].export;
333 }
334 return export_unknown;
335}
336
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900337static void *sym_get_data_by_offset(const struct elf_info *info,
338 unsigned int secindex, unsigned long offset)
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900339{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800340 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900341
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900342 if (info->hdr->e_type != ET_REL)
343 offset -= sechdr->sh_addr;
344
345 return (void *)info->hdr + sechdr->sh_offset + offset;
346}
347
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900348static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
349{
350 return sym_get_data_by_offset(info, get_secindex(info, sym),
351 sym->st_value);
352}
353
Masahiro Yamada565587d2020-05-25 14:47:05 +0900354static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
355{
356 return sym_get_data_by_offset(info, info->secindex_strings,
357 sechdr->sh_name);
358}
359
360static const char *sec_name(const struct elf_info *info, int secindex)
361{
362 return sech_name(info, &info->sechdrs[secindex]);
363}
364
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200365#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
366
367static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
368{
369 const char *secname = sec_name(elf, sec);
370
371 if (strstarts(secname, "___ksymtab+"))
372 return export_plain;
373 else if (strstarts(secname, "___ksymtab_unused+"))
374 return export_unused;
375 else if (strstarts(secname, "___ksymtab_gpl+"))
376 return export_gpl;
377 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
378 return export_unused_gpl;
379 else if (strstarts(secname, "___ksymtab_gpl_future+"))
380 return export_gpl_future;
381 else
382 return export_unknown;
383}
384
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200385static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700386{
387 if (sec == elf->export_sec)
388 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200389 else if (sec == elf->export_unused_sec)
390 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700391 else if (sec == elf->export_gpl_sec)
392 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200393 else if (sec == elf->export_unused_gpl_sec)
394 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700395 else if (sec == elf->export_gpl_future_sec)
396 return export_gpl_future;
397 else
398 return export_unknown;
399}
400
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900401static const char *namespace_from_kstrtabns(const struct elf_info *info,
402 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100403{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900404 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100405 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100406}
407
Matthias Maennicha2b11182019-10-18 10:31:40 +0100408static void sym_update_namespace(const char *symname, const char *namespace)
409{
410 struct symbol *s = find_symbol(symname);
411
412 /*
413 * That symbol should have been created earlier and thus this is
414 * actually an assertion.
415 */
416 if (!s) {
417 merror("Could not update namespace(%s) for symbol %s\n",
418 namespace, symname);
419 return;
420 }
421
422 free(s->namespace);
423 s->namespace =
424 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
425}
426
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100427/**
428 * Add an exported symbol - it may have already been added without a
429 * CRC, in this case just update the CRC
430 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100431static struct symbol *sym_add_exported(const char *name, struct module *mod,
432 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct symbol *s = find_symbol(name);
435
436 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700437 s = new_symbol(name, mod, export);
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900438 } else if (!external_module || s->module->is_vmlinux ||
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900439 s->module == mod) {
440 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
441 mod->name, name, s->module->name,
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900442 s->module->is_vmlinux ? "" : ".ko");
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900443 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900445
446 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700447 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100448 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Masahiro Yamada17436942019-11-15 02:42:24 +0900451static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100452{
453 struct symbol *s = find_symbol(name);
454
Masahiro Yamada17436942019-11-15 02:42:24 +0900455 /*
456 * Ignore stand-alone __crc_*, which might be auto-generated symbols
457 * such as __*_veneer in ARM ELF.
458 */
459 if (!s)
460 return;
461
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100462 s->crc = crc;
463 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Masahiro Yamada75893572020-06-01 14:57:21 +0900466static void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930469 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int fd;
471
472 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930473 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930475 if (fstat(fd, &st))
476 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 *size = st.st_size;
479 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930481failed:
482 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (map == MAP_FAILED)
484 return NULL;
485 return map;
486}
487
Masahiro Yamada75893572020-06-01 14:57:21 +0900488static void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 munmap(file, size);
491}
492
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100493static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100496 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 Elf_Shdr *sechdrs;
498 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200499 const char *secstrings;
500 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 hdr = grab_file(filename, &info->size);
503 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930504 if (ignore_missing_files) {
505 fprintf(stderr, "%s: %s (ignored)\n", filename,
506 strerror(errno));
507 return 0;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200510 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100513 if (info->size < sizeof(*hdr)) {
514 /* file too small, assume this is an empty .o file */
515 return 0;
516 }
517 /* Is this a valid ELF file? */
518 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
519 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
520 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
521 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
522 /* Not an ELF file - silently ignore it */
523 return 0;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200526 hdr->e_type = TO_NATIVE(hdr->e_type);
527 hdr->e_machine = TO_NATIVE(hdr->e_machine);
528 hdr->e_version = TO_NATIVE(hdr->e_version);
529 hdr->e_entry = TO_NATIVE(hdr->e_entry);
530 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
531 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
532 hdr->e_flags = TO_NATIVE(hdr->e_flags);
533 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
534 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
535 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
536 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
537 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
538 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 sechdrs = (void *)hdr + hdr->e_shoff;
540 info->sechdrs = sechdrs;
541
Petr Stetiara83710e2007-08-27 12:15:07 +0200542 /* Check if file offset is correct */
543 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100544 fatal("section header offset=%lu in file '%s' is bigger than "
545 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
546 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200547 return 0;
548 }
549
Anders Kaseorg68457562011-05-19 16:55:27 -0600550 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200551 /*
552 * There are more than 64k sections,
553 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200554 */
555 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
556 }
557 else {
558 info->num_sections = hdr->e_shnum;
559 }
560 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600561 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200562 }
563 else {
564 info->secindex_strings = hdr->e_shstrndx;
565 }
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200568 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200569 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
570 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
571 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
572 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
573 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
574 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
575 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
576 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
577 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
578 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200581 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
582 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700583 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900584 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Tejun Heo56fc82c2009-02-06 00:48:02 +0900586 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100587 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
588 "sizeof(*hrd)=%zu\n", filename,
589 (unsigned long)sechdrs[i].sh_offset,
590 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100591 return 0;
592 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700593 secname = secstrings + sechdrs[i].sh_name;
594 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900595 if (nobits)
596 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
598 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700599 } else if (strcmp(secname, "__ksymtab") == 0)
600 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200601 else if (strcmp(secname, "__ksymtab_unused") == 0)
602 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700603 else if (strcmp(secname, "__ksymtab_gpl") == 0)
604 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200605 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
606 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700607 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
608 info->export_gpl_future_sec = i;
609
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200610 if (sechdrs[i].sh_type == SHT_SYMTAB) {
611 unsigned int sh_link_idx;
612 symtab_idx = i;
613 info->symtab_start = (void *)hdr +
614 sechdrs[i].sh_offset;
615 info->symtab_stop = (void *)hdr +
616 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600617 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200618 info->strtab = (void *)hdr +
619 sechdrs[sh_link_idx].sh_offset;
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200622 /* 32bit section no. table? ("more than 64k sections") */
623 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
624 symtab_shndx_idx = i;
625 info->symtab_shndx_start = (void *)hdr +
626 sechdrs[i].sh_offset;
627 info->symtab_shndx_stop = (void *)hdr +
628 sechdrs[i].sh_offset + sechdrs[i].sh_size;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100631 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100632 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Fix endianness in symbols */
635 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
636 sym->st_shndx = TO_NATIVE(sym->st_shndx);
637 sym->st_name = TO_NATIVE(sym->st_name);
638 sym->st_value = TO_NATIVE(sym->st_value);
639 sym->st_size = TO_NATIVE(sym->st_size);
640 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200641
642 if (symtab_shndx_idx != ~0U) {
643 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600644 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200645 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600646 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200647 symtab_idx);
648 /* Fix endianness */
649 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
650 p++)
651 *p = TO_NATIVE(*p);
652 }
653
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100654 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100657static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 release_file(info->hdr, info->size);
660}
661
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200662static int ignore_undef_symbol(struct elf_info *info, const char *symname)
663{
664 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900665 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200666 return 1;
667 /* ignore global offset table */
668 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
669 return 1;
670 if (info->hdr->e_machine == EM_PPC)
671 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900672 if (strstarts(symname, "_restgpr_") ||
673 strstarts(symname, "_savegpr_") ||
674 strstarts(symname, "_rest32gpr_") ||
675 strstarts(symname, "_save32gpr_") ||
676 strstarts(symname, "_restvr_") ||
677 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200678 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000679 if (info->hdr->e_machine == EM_PPC64)
680 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900681 if (strstarts(symname, "_restgpr0_") ||
682 strstarts(symname, "_savegpr0_") ||
683 strstarts(symname, "_restvr_") ||
684 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100685 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000686 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200687 /* Do not ignore this symbol */
688 return 0;
689}
690
Masahiro Yamada17436942019-11-15 02:42:24 +0900691static void handle_modversion(const struct module *mod,
692 const struct elf_info *info,
693 const Elf_Sym *sym, const char *symname)
694{
695 unsigned int crc;
696
697 if (sym->st_shndx == SHN_UNDEF) {
698 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
Masahiro Yamada5a438af2020-06-01 14:57:26 +0900699 symname, mod->name, mod->is_vmlinux ? "" : ".ko");
Masahiro Yamada17436942019-11-15 02:42:24 +0900700 return;
701 }
702
703 if (sym->st_shndx == SHN_ABS) {
704 crc = sym->st_value;
705 } else {
706 unsigned int *crcp;
707
708 /* symbol points to the CRC in the ELF object */
709 crcp = sym_get_data(info, sym);
710 crc = TO_NATIVE(*crcp);
711 }
712 sym_set_crc(symname, crc);
713}
714
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900715static void handle_symbol(struct module *mod, struct elf_info *info,
716 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200718 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900719 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200720
Masahiro Yamada33795762020-06-01 14:57:24 +0900721 if (strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200722 export = export_from_secname(info, get_secindex(info, sym));
723 else
724 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 switch (sym->st_shndx) {
727 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900728 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100729 /* Should warn here, but modpost runs before the linker */
730 } else
731 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 case SHN_UNDEF:
734 /* undefined symbol */
735 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
736 ELF_ST_BIND(sym->st_info) != STB_WEAK)
737 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200738 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (info->hdr->e_machine == EM_SPARC ||
741 info->hdr->e_machine == EM_SPARCV9) {
742 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700743 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100745 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700746 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100747 munged[0] = '_';
748 munged[1] = toupper(munged[1]);
749 symname = munged;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100752
Rusty Russellb92021b2013-03-15 15:04:17 +1030753 mod->unres = alloc_symbol(symname,
754 ELF_ST_BIND(sym->st_info) == STB_WEAK,
755 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
757 default:
758 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900759 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100760 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100761 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900763 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900765 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 mod->has_cleanup = 1;
767 break;
768 }
769}
770
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100771/**
772 * Parse tag=value strings from .modinfo section
773 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static char *next_string(char *string, unsigned long *secsize)
775{
776 /* Skip non-zero chars */
777 while (string[0]) {
778 string++;
779 if ((*secsize)-- <= 1)
780 return NULL;
781 }
782
783 /* Skip any zero padding. */
784 while (!string[0]) {
785 string++;
786 if ((*secsize)-- <= 1)
787 return NULL;
788 }
789 return string;
790}
791
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900792static char *get_next_modinfo(struct elf_info *info, const char *tag,
793 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 char *p;
796 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900797 char *modinfo = info->modinfo;
798 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900800 if (prev) {
801 size -= prev - modinfo;
802 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200803 }
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 for (p = modinfo; p; p = next_string(p, &size)) {
806 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
807 return p + taglen + 1;
808 }
809 return NULL;
810}
811
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900812static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200813
814{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900815 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200816}
817
Sam Ravnborg93684d32006-02-19 11:53:35 +0100818/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100819 * Test if string s ends in string sub
820 * return 0 if match
821 **/
822static int strrcmp(const char *s, const char *sub)
823{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100824 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100825
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100826 if (!s || !sub)
827 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100828
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100829 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100830 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100831
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100832 if ((slen == 0) || (sublen == 0))
833 return 1;
834
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100835 if (sublen > slen)
836 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100837
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100838 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100839}
840
Sam Ravnborgff13f922008-01-23 19:54:27 +0100841static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
842{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100843 if (sym)
844 return elf->strtab + sym->st_name;
845 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100846 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100847}
848
Sam Ravnborg10668222008-01-13 22:21:31 +0100849/* The pattern is an array of simple patterns.
850 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100851 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100852 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930853 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100854 */
Trevor Keith5c725132009-09-22 16:43:38 -0700855static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100856{
857 const char *p;
858 while (*pat) {
859 p = *pat++;
860 const char *endp = p + strlen(p) - 1;
861
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930862 /* "*foo*" */
863 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300864 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
865 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930866
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930867 free(bare);
868 if (here != NULL)
869 return 1;
870 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100871 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930872 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100873 if (strrcmp(sym, p + 1) == 0)
874 return 1;
875 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100876 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100877 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100878 if (strncmp(sym, p, strlen(p) - 1) == 0)
879 return 1;
880 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100881 /* no wildcards */
882 else {
883 if (strcmp(p, sym) == 0)
884 return 1;
885 }
886 }
887 /* no match */
888 return 0;
889}
890
Sam Ravnborg10668222008-01-13 22:21:31 +0100891/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930892static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200893{
894 ".comment*",
895 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930896 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800897 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000898 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200899 ".mdebug*", /* alpha, score, mips etc. */
900 ".pdr", /* alpha, score, mips etc. */
901 ".stab*",
902 ".note*",
903 ".got*",
904 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400905 ".xt.prop", /* xtensa */
906 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030907 ".arcextmap*", /* arc */
908 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030909 ".cmem*", /* EZchip */
910 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100911 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600912 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200913 NULL
914};
Sam Ravnborg10668222008-01-13 22:21:31 +0100915
Sam Ravnborge241a632008-01-28 20:13:13 +0100916/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400917 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100918 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400919 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100920 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400921static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900922 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100923{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400924 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100925
Anders Kaseorgb614a692009-04-23 16:49:33 -0400926 if (sechdr->sh_type == SHT_PROGBITS &&
927 !(sechdr->sh_flags & SHF_ALLOC) &&
928 !match(sec, section_white_list)) {
929 warn("%s (%s): unexpected non-allocatable section.\n"
930 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
931 "Note that for example <linux/init.h> contains\n"
932 "section definitions for use in .S files.\n\n",
933 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100934 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100935}
936
937
938
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100939#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930940 ".init.setup", ".init.rodata", ".meminit.rodata", \
941 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100942#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930943 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100944
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100945#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930946 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100947#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930948 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100949
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200950#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930951 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
952 ".pci_fixup_enable", ".pci_fixup_resume", \
953 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200954
Paul Gortmakere24f6622013-06-19 19:30:48 -0400955#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
956#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100957
958#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
959#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100960
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930961#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930962#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700963 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930964#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400965 ".fixup", ".entry.text", ".exception.text", ".text.*", \
966 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100967
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000968#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000969#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100970
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000971#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000972#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100973
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930974#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
975 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
976
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100977/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930978static const char *const init_data_sections[] =
979 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100980
981/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930982static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100983
984/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930985static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100986 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100987
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930988/* all text sections */
989static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
990
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100991/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930992static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100993
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100994
995/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100996#define DEFAULT_SYMBOL_WHITE_LIST \
997 "*driver", \
998 "*_template", /* scsi uses *_template a lot */ \
999 "*_timer", /* arm uses ops structures named _timer a lot */ \
1000 "*_sht", /* scsi also used *_sht to some extent */ \
1001 "*_ops", \
1002 "*_probe", \
1003 "*_probe_one", \
1004 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001005
Mathias Krause7a3ee752014-08-27 20:28:53 +09301006static const char *const head_sections[] = { ".head.text*", NULL };
1007static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001008 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301009static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001010
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001011enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001012 TEXT_TO_ANY_INIT,
1013 DATA_TO_ANY_INIT,
1014 TEXT_TO_ANY_EXIT,
1015 DATA_TO_ANY_EXIT,
1016 XXXINIT_TO_SOME_INIT,
1017 XXXEXIT_TO_SOME_EXIT,
1018 ANY_INIT_TO_ANY_EXIT,
1019 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001020 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301021 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001022};
1023
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301024/**
1025 * Describe how to match sections on different criterias:
1026 *
1027 * @fromsec: Array of sections to be matched.
1028 *
1029 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1030 * this array is forbidden (black-list). Can be empty.
1031 *
1032 * @good_tosec: Relocations applied to a section in @fromsec must be
1033 * targetting sections in this array (white-list). Can be empty.
1034 *
1035 * @mismatch: Type of mismatch.
1036 *
1037 * @symbol_white_list: Do not match a relocation to a symbol in this list
1038 * even if it is targetting a section in @bad_to_sec.
1039 *
1040 * @handler: Specific handler to call when a match is found. If NULL,
1041 * default_mismatch_handler() will be called.
1042 *
1043 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001044struct sectioncheck {
1045 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301046 const char *bad_tosec[20];
1047 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001048 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001049 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301050 void (*handler)(const char *modname, struct elf_info *elf,
1051 const struct sectioncheck* const mismatch,
1052 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1053
Sam Ravnborg10668222008-01-13 22:21:31 +01001054};
1055
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301056static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1057 const struct sectioncheck* const mismatch,
1058 Elf_Rela *r, Elf_Sym *sym,
1059 const char *fromsec);
1060
Mathias Krause7a3ee752014-08-27 20:28:53 +09301061static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001062/* Do not reference init/exit code/data from
1063 * normal code and data
1064 */
1065{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001066 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301067 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001068 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001069 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001070},
1071{
1072 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301073 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001074 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001075 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001076},
1077{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001078 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301079 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001080 .mismatch = DATA_TO_ANY_INIT,
1081 .symbol_white_list = {
1082 "*_template", "*_timer", "*_sht", "*_ops",
1083 "*_probe", "*_probe_one", "*_console", NULL
1084 },
1085},
1086{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001087 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301088 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001089 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001090 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001091},
1092{
1093 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301094 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001095 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001096 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001097},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001098/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001099{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001100 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301101 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001102 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001103 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001104},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001105/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001106{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001107 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301108 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001109 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001110 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001111},
1112/* Do not use exit code/data from init code */
1113{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001114 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301115 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001116 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001117 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001118},
1119/* Do not use init code/data from exit code */
1120{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001121 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301122 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001123 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001124 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001125},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001126{
1127 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301128 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001129 .mismatch = ANY_INIT_TO_ANY_EXIT,
1130 .symbol_white_list = { NULL },
1131},
Sam Ravnborg10668222008-01-13 22:21:31 +01001132/* Do not export init/exit functions or data */
1133{
1134 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301135 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001136 .mismatch = EXPORT_TO_INIT_EXIT,
1137 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301138},
1139{
1140 .fromsec = { "__ex_table", NULL },
1141 /* If you're adding any new black-listed sections in here, consider
1142 * adding a special 'printer' for them in scripts/check_extable.
1143 */
1144 .bad_tosec = { ".altinstr_replacement", NULL },
1145 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1146 .mismatch = EXTABLE_TO_NON_TEXT,
1147 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001148}
1149};
1150
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001151static const struct sectioncheck *section_mismatch(
1152 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001153{
1154 int i;
1155 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1156 const struct sectioncheck *check = &sectioncheck[0];
1157
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301158 /*
1159 * The target section could be the SHT_NUL section when we're
1160 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001161 * doesn't make much sense and causes build failures on parisc
1162 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301163 */
1164 if (*tosec == '\0')
1165 return NULL;
1166
Sam Ravnborg10668222008-01-13 22:21:31 +01001167 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301168 if (match(fromsec, check->fromsec)) {
1169 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1170 return check;
1171 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1172 return check;
1173 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001174 check++;
1175 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001176 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001177}
1178
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001179/**
1180 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001181 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001182 * Pattern 1:
1183 * If a module parameter is declared __initdata and permissions=0
1184 * then this is legal despite the warning generated.
1185 * We cannot see value of permissions here, so just ignore
1186 * this pattern.
1187 * The pattern is identified by:
1188 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001189 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001190 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001191 *
Rusty Russell6a841522010-08-11 23:04:16 -06001192 * Pattern 1a:
1193 * module_param_call() ops can refer to __init set function if permissions=0
1194 * The pattern is identified by:
1195 * tosec = .init.text
1196 * fromsec = .data*
1197 * atsym = __param_ops_*
1198 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001199 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001200 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001201 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001202 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001203 * tosec = init or exit section
1204 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001205 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1206 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001207 *
1208 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001209 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001210 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001211 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001212 * Some symbols belong to init section but still it is ok to reference
1213 * these from non-init sections as these symbols don't have any memory
1214 * allocated for them and symbol address and value are same. So even
1215 * if init section is freed, its ok to reference those symbols.
1216 * For ex. symbols marking the init section boundaries.
1217 * This pattern is identified by
1218 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001219 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301220 * Pattern 5:
1221 * GCC may optimize static inlines when fed constant arg(s) resulting
1222 * in functions like cpumask_empty() -- generating an associated symbol
1223 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1224 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1225 * meaningless section warning. May need to add isra symbols too...
1226 * This pattern is identified by
1227 * tosec = init section
1228 * fromsec = text section
1229 * refsymname = *.constprop.*
1230 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001231 * Pattern 6:
1232 * Hide section mismatch warnings for ELF local symbols. The goal
1233 * is to eliminate false positive modpost warnings caused by
1234 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1235 * Autogenerated symbol names bypass modpost's "Pattern 2"
1236 * whitelisting, which relies on pattern-matching against symbol
1237 * names to work. (One situation where gcc can autogenerate ELF
1238 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001239 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001240static int secref_whitelist(const struct sectioncheck *mismatch,
1241 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001242 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001243{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001244 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001245 if (match(tosec, init_data_sections) &&
1246 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001247 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001248 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001249
Rusty Russell6a841522010-08-11 23:04:16 -06001250 /* Check for pattern 1a */
1251 if (strcmp(tosec, ".init.text") == 0 &&
1252 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001253 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001254 return 0;
1255
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001256 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001257 if (match(tosec, init_exit_sections) &&
1258 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001259 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001260 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001261
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001262 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001263 if (match(fromsec, head_sections) &&
1264 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001265 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001266
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001267 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001268 if (match(tosym, linker_symbols))
1269 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001270
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301271 /* Check for pattern 5 */
1272 if (match(fromsec, text_sections) &&
1273 match(tosec, init_sections) &&
1274 match(fromsym, optim_symbols))
1275 return 0;
1276
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001277 /* Check for pattern 6 */
1278 if (strstarts(fromsym, ".L"))
1279 return 0;
1280
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001281 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001282}
1283
Sami Tolvanen5818c682018-10-23 15:15:35 -07001284static inline int is_arm_mapping_symbol(const char *str)
1285{
1286 return str[0] == '$' && strchr("axtd", str[1])
1287 && (str[2] == '\0' || str[2] == '.');
1288}
1289
1290/*
1291 * If there's no name there, ignore it; likewise, ignore it if it's
1292 * one of the magic symbols emitted used by current ARM tools.
1293 *
1294 * Otherwise if find_symbols_between() returns those symbols, they'll
1295 * fail the whitelist tests and cause lots of false alarms ... fixable
1296 * only by merging __exit and __init sections into __text, bloating
1297 * the kernel (which is especially evil on embedded platforms).
1298 */
1299static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1300{
1301 const char *name = elf->strtab + sym->st_name;
1302
1303 if (!name || !strlen(name))
1304 return 0;
1305 return !is_arm_mapping_symbol(name);
1306}
1307
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001308/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001309 * Find symbol based on relocation record info.
1310 * In some cases the symbol supplied is a valid symbol so
1311 * return refsym. If st_name != 0 we assume this is a valid symbol.
1312 * In other cases the symbol needs to be looked up in the symbol table
1313 * based on section and address.
1314 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001315static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001316 Elf_Sym *relsym)
1317{
1318 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001319 Elf_Sym *near = NULL;
1320 Elf64_Sword distance = 20;
1321 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001322 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001323
1324 if (relsym->st_name != 0)
1325 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001326
1327 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001328 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001329 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001330 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001331 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1332 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001333 if (!is_valid_name(elf, sym))
1334 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001335 if (sym->st_value == addr)
1336 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001337 /* Find a symbol nearby - addr are maybe negative */
1338 d = sym->st_value - addr;
1339 if (d < 0)
1340 d = addr - sym->st_value;
1341 if (d < distance) {
1342 distance = d;
1343 near = sym;
1344 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001345 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001346 /* We need a close match */
1347 if (distance < 20)
1348 return near;
1349 else
1350 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001351}
1352
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001353/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001354 * Find symbols before or equal addr and after addr - in the section sec.
1355 * If we find two symbols with equal offset prefer one with a valid name.
1356 * The ELF format may have a better way to detect what type of symbol
1357 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001358 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001359static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1360 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001361{
1362 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001363 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001364 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001365
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001366 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1367 const char *symsec;
1368
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001369 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001370 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001371 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001372 if (strcmp(symsec, sec) != 0)
1373 continue;
David Brownellda68d612007-02-20 13:58:16 -08001374 if (!is_valid_name(elf, sym))
1375 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001376 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001377 if ((addr - sym->st_value) < distance) {
1378 distance = addr - sym->st_value;
1379 near = sym;
1380 } else if ((addr - sym->st_value) == distance) {
1381 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001382 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001383 }
1384 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001385 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001386}
1387
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001388/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001389 * Convert a section name to the function/data attribute
1390 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001391 * .memexitconst => __memconst
1392 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001393 *
1394 * The memory of returned value has been allocated on a heap. The user of this
1395 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001396*/
1397static char *sec2annotation(const char *s)
1398{
1399 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001400 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001401 char *r = p;
1402
1403 *p++ = '_';
1404 *p++ = '_';
1405 if (*s == '.')
1406 s++;
1407 while (*s && *s != '.')
1408 *p++ = *s++;
1409 *p = '\0';
1410 if (*s == '.')
1411 s++;
1412 if (strstr(s, "rodata") != NULL)
1413 strcat(p, "const ");
1414 else if (strstr(s, "data") != NULL)
1415 strcat(p, "data ");
1416 else
1417 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001418 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001419 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001420 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001421 }
1422}
1423
1424static int is_function(Elf_Sym *sym)
1425{
1426 if (sym)
1427 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1428 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001429 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001430}
1431
Randy Dunlap00759c02011-03-15 14:13:47 -07001432static void print_section_list(const char * const list[20])
1433{
1434 const char *const *s = list;
1435
1436 while (*s) {
1437 fprintf(stderr, "%s", *s);
1438 s++;
1439 if (*s)
1440 fprintf(stderr, ", ");
1441 }
1442 fprintf(stderr, "\n");
1443}
1444
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301445static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1446{
1447 switch (is_func) {
1448 case 0: *name = "variable"; *name_p = ""; break;
1449 case 1: *name = "function"; *name_p = "()"; break;
1450 default: *name = "(unknown reference)"; *name_p = ""; break;
1451 }
1452}
1453
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001454/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001455 * Print a warning about a section mismatch.
1456 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001457 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001458 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001459static void report_sec_mismatch(const char *modname,
1460 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001461 const char *fromsec,
1462 unsigned long long fromaddr,
1463 const char *fromsym,
1464 int from_is_func,
1465 const char *tosec, const char *tosym,
1466 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001467{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001468 const char *from, *from_p;
1469 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001470 char *prl_from;
1471 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001472
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001473 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001474
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301475 get_pretty_name(from_is_func, &from, &from_p);
1476 get_pretty_name(to_is_func, &to, &to_p);
1477
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001478 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1479 "to the %s %s:%s%s\n",
1480 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1481 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001482
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001483 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001484 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001485 prl_from = sec2annotation(fromsec);
1486 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001487 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001488 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001489 "the %s %s%s%s.\n"
1490 "This is often because %s lacks a %s\n"
1491 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001492 prl_from, fromsym,
1493 to, prl_to, tosym, to_p,
1494 fromsym, prl_to, tosym);
1495 free(prl_from);
1496 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001497 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001498 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001499 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001500 fprintf(stderr,
1501 "The variable %s references\n"
1502 "the %s %s%s%s\n"
1503 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001504 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001505 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001506 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001507 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001508 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001509 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001510 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001511 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001512 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001513 fprintf(stderr,
1514 "The function %s() references a %s in an exit section.\n"
1515 "Often the %s %s%s has valid usage outside the exit section\n"
1516 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001517 fromsym, to, to, tosym, to_p, prl_to, tosym);
1518 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001519 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001520 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001521 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001522 fprintf(stderr,
1523 "The variable %s references\n"
1524 "the %s %s%s%s\n"
1525 "If the reference is valid then annotate the\n"
1526 "variable with __exit* (see linux/init.h) or "
1527 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001528 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001529 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001530 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001531 break;
1532 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001533 case XXXINIT_TO_SOME_INIT:
1534 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001535 prl_from = sec2annotation(fromsec);
1536 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001537 fprintf(stderr,
1538 "The %s %s%s%s references\n"
1539 "a %s %s%s%s.\n"
1540 "If %s is only used by %s then\n"
1541 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001542 from, prl_from, fromsym, from_p,
1543 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001544 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001545 free(prl_from);
1546 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001547 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001548 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001549 prl_from = sec2annotation(fromsec);
1550 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001551 fprintf(stderr,
1552 "The %s %s%s%s references\n"
1553 "a %s %s%s%s.\n"
1554 "This is often seen when error handling "
1555 "in the init function\n"
1556 "uses functionality in the exit path.\n"
1557 "The fix is often to remove the %sannotation of\n"
1558 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001559 from, prl_from, fromsym, from_p,
1560 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001561 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001562 free(prl_from);
1563 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001564 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001565 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001566 prl_from = sec2annotation(fromsec);
1567 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001568 fprintf(stderr,
1569 "The %s %s%s%s references\n"
1570 "a %s %s%s%s.\n"
1571 "This is often seen when error handling "
1572 "in the exit function\n"
1573 "uses functionality in the init path.\n"
1574 "The fix is often to remove the %sannotation of\n"
1575 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001576 from, prl_from, fromsym, from_p,
1577 to, prl_to, tosym, to_p,
1578 prl_to, tosym, to_p);
1579 free(prl_from);
1580 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001581 break;
1582 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001583 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001584 fprintf(stderr,
1585 "The symbol %s is exported and annotated %s\n"
1586 "Fix this by removing the %sannotation of %s "
1587 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001588 tosym, prl_to, prl_to, tosym);
1589 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001590 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301591 case EXTABLE_TO_NON_TEXT:
1592 fatal("There's a special handler for this mismatch type, "
1593 "we should never get here.");
1594 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001595 }
1596 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001597}
1598
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301599static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1600 const struct sectioncheck* const mismatch,
1601 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1602{
1603 const char *tosec;
1604 Elf_Sym *to;
1605 Elf_Sym *from;
1606 const char *tosym;
1607 const char *fromsym;
1608
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301609 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1610 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301611
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001612 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301613 return;
1614
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301615 tosec = sec_name(elf, get_secindex(elf, sym));
1616 to = find_elf_symbol(elf, r->r_addend, sym);
1617 tosym = sym_name(elf, to);
1618
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301619 /* check whitelist - we may ignore it */
1620 if (secref_whitelist(mismatch,
1621 fromsec, fromsym, tosec, tosym)) {
1622 report_sec_mismatch(modname, mismatch,
1623 fromsec, r->r_offset, fromsym,
1624 is_function(from), tosec, tosym,
1625 is_function(to));
1626 }
1627}
1628
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301629static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1630{
1631 if (section_index > elf->num_sections)
1632 fatal("section_index is outside elf->num_sections!\n");
1633
1634 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1635}
1636
1637/*
1638 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1639 * to know the sizeof(struct exception_table_entry) for the target architecture.
1640 */
1641static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301642static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301643{
1644 /*
1645 * If we're currently checking the second relocation within __ex_table,
1646 * that relocation offset tells us the offsetof(struct
1647 * exception_table_entry, fixup) which is equal to sizeof(struct
1648 * exception_table_entry) divided by two. We use that to our advantage
1649 * since there's no portable way to get that size as every architecture
1650 * seems to go with different sized types. Not pretty but better than
1651 * hard-coding the size for every architecture..
1652 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301653 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301654 extable_entry_size = r->r_offset * 2;
1655}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301656
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301657static inline bool is_extable_fault_address(Elf_Rela *r)
1658{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301659 /*
1660 * extable_entry_size is only discovered after we've handled the
1661 * _second_ relocation in __ex_table, so only abort when we're not
1662 * handling the first reloc and extable_entry_size is zero.
1663 */
1664 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301665 fatal("extable_entry size hasn't been discovered!\n");
1666
1667 return ((r->r_offset == 0) ||
1668 (r->r_offset % extable_entry_size == 0));
1669}
1670
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301671#define is_second_extable_reloc(Start, Cur, Sec) \
1672 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1673
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301674static void report_extable_warnings(const char* modname, struct elf_info* elf,
1675 const struct sectioncheck* const mismatch,
1676 Elf_Rela* r, Elf_Sym* sym,
1677 const char* fromsec, const char* tosec)
1678{
1679 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1680 const char* fromsym_name = sym_name(elf, fromsym);
1681 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1682 const char* tosym_name = sym_name(elf, tosym);
1683 const char* from_pretty_name;
1684 const char* from_pretty_name_p;
1685 const char* to_pretty_name;
1686 const char* to_pretty_name_p;
1687
1688 get_pretty_name(is_function(fromsym),
1689 &from_pretty_name, &from_pretty_name_p);
1690 get_pretty_name(is_function(tosym),
1691 &to_pretty_name, &to_pretty_name_p);
1692
1693 warn("%s(%s+0x%lx): Section mismatch in reference"
1694 " from the %s %s%s to the %s %s:%s%s\n",
1695 modname, fromsec, (long)r->r_offset, from_pretty_name,
1696 fromsym_name, from_pretty_name_p,
1697 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1698
1699 if (!match(tosec, mismatch->bad_tosec) &&
1700 is_executable_section(elf, get_secindex(elf, sym)))
1701 fprintf(stderr,
1702 "The relocation at %s+0x%lx references\n"
1703 "section \"%s\" which is not in the list of\n"
1704 "authorized sections. If you're adding a new section\n"
1705 "and/or if this reference is valid, add \"%s\" to the\n"
1706 "list of authorized sections to jump to on fault.\n"
1707 "This can be achieved by adding \"%s\" to \n"
1708 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1709 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1710}
1711
1712static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1713 const struct sectioncheck* const mismatch,
1714 Elf_Rela* r, Elf_Sym* sym,
1715 const char *fromsec)
1716{
1717 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1718
1719 sec_mismatch_count++;
1720
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001721 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301722
1723 if (match(tosec, mismatch->bad_tosec))
1724 fatal("The relocation at %s+0x%lx references\n"
1725 "section \"%s\" which is black-listed.\n"
1726 "Something is seriously wrong and should be fixed.\n"
1727 "You might get more information about where this is\n"
1728 "coming from by using scripts/check_extable.sh %s\n",
1729 fromsec, (long)r->r_offset, tosec, modname);
1730 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1731 if (is_extable_fault_address(r))
1732 fatal("The relocation at %s+0x%lx references\n"
1733 "section \"%s\" which is not executable, IOW\n"
1734 "it is not possible for the kernel to fault\n"
1735 "at that address. Something is seriously wrong\n"
1736 "and should be fixed.\n",
1737 fromsec, (long)r->r_offset, tosec);
1738 else
1739 fatal("The relocation at %s+0x%lx references\n"
1740 "section \"%s\" which is not executable, IOW\n"
1741 "the kernel will fault if it ever tries to\n"
1742 "jump to it. Something is seriously wrong\n"
1743 "and should be fixed.\n",
1744 fromsec, (long)r->r_offset, tosec);
1745 }
1746}
1747
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001748static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001749 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001750{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001751 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301752 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001753
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001754 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301755 if (mismatch->handler)
1756 mismatch->handler(modname, elf, mismatch,
1757 r, sym, fromsec);
1758 else
1759 default_mismatch_handler(modname, elf, mismatch,
1760 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001761 }
1762}
1763
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001764static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001765 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001766{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001767 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001768}
1769
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001770static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001771{
1772 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001773 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001774
1775 switch (r_typ) {
1776 case R_386_32:
1777 r->r_addend = TO_NATIVE(*location);
1778 break;
1779 case R_386_PC32:
1780 r->r_addend = TO_NATIVE(*location) + 4;
1781 /* For CONFIG_RELOCATABLE=y */
1782 if (elf->hdr->e_type == ET_EXEC)
1783 r->r_addend += r->r_offset;
1784 break;
1785 }
1786 return 0;
1787}
1788
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001789#ifndef R_ARM_CALL
1790#define R_ARM_CALL 28
1791#endif
1792#ifndef R_ARM_JUMP24
1793#define R_ARM_JUMP24 29
1794#endif
1795
David A. Longc9698e52014-02-14 22:41:18 +01001796#ifndef R_ARM_THM_CALL
1797#define R_ARM_THM_CALL 10
1798#endif
1799#ifndef R_ARM_THM_JUMP24
1800#define R_ARM_THM_JUMP24 30
1801#endif
1802#ifndef R_ARM_THM_JUMP19
1803#define R_ARM_THM_JUMP19 51
1804#endif
1805
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001806static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001807{
1808 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1809
1810 switch (r_typ) {
1811 case R_ARM_ABS32:
1812 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001813 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001814 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001815 break;
1816 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001817 case R_ARM_CALL:
1818 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001819 case R_ARM_THM_CALL:
1820 case R_ARM_THM_JUMP24:
1821 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001822 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001823 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001824 sechdr->sh_offset +
1825 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001826 break;
1827 default:
1828 return 1;
1829 }
1830 return 0;
1831}
1832
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001833static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001834{
1835 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001836 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001837 unsigned int inst;
1838
1839 if (r_typ == R_MIPS_HI16)
1840 return 1; /* skip this */
1841 inst = TO_NATIVE(*location);
1842 switch (r_typ) {
1843 case R_MIPS_LO16:
1844 r->r_addend = inst & 0xffff;
1845 break;
1846 case R_MIPS_26:
1847 r->r_addend = (inst & 0x03ffffff) << 2;
1848 break;
1849 case R_MIPS_32:
1850 r->r_addend = inst;
1851 break;
1852 }
1853 return 0;
1854}
1855
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001856static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001857 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858{
1859 Elf_Sym *sym;
1860 Elf_Rela *rela;
1861 Elf_Rela r;
1862 unsigned int r_sym;
1863 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001864
Sam Ravnborgff13f922008-01-23 19:54:27 +01001865 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001866 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1867
Sam Ravnborgff13f922008-01-23 19:54:27 +01001868 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001869 fromsec += strlen(".rela");
1870 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001871 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001872 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001873
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001874 for (rela = start; rela < stop; rela++) {
1875 r.r_offset = TO_NATIVE(rela->r_offset);
1876#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001877 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001878 unsigned int r_typ;
1879 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1880 r_sym = TO_NATIVE(r_sym);
1881 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1882 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1883 } else {
1884 r.r_info = TO_NATIVE(rela->r_info);
1885 r_sym = ELF_R_SYM(r.r_info);
1886 }
1887#else
1888 r.r_info = TO_NATIVE(rela->r_info);
1889 r_sym = ELF_R_SYM(r.r_info);
1890#endif
1891 r.r_addend = TO_NATIVE(rela->r_addend);
1892 sym = elf->symtab_start + r_sym;
1893 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001894 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001895 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301896 if (is_second_extable_reloc(start, rela, fromsec))
1897 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001898 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001899 }
1900}
1901
1902static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001903 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001904{
1905 Elf_Sym *sym;
1906 Elf_Rel *rel;
1907 Elf_Rela r;
1908 unsigned int r_sym;
1909 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001910
Sam Ravnborgff13f922008-01-23 19:54:27 +01001911 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001912 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1913
Sam Ravnborgff13f922008-01-23 19:54:27 +01001914 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001915 fromsec += strlen(".rel");
1916 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001917 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001918 return;
1919
1920 for (rel = start; rel < stop; rel++) {
1921 r.r_offset = TO_NATIVE(rel->r_offset);
1922#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001923 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001924 unsigned int r_typ;
1925 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1926 r_sym = TO_NATIVE(r_sym);
1927 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1928 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1929 } else {
1930 r.r_info = TO_NATIVE(rel->r_info);
1931 r_sym = ELF_R_SYM(r.r_info);
1932 }
1933#else
1934 r.r_info = TO_NATIVE(rel->r_info);
1935 r_sym = ELF_R_SYM(r.r_info);
1936#endif
1937 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001938 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001939 case EM_386:
1940 if (addend_386_rel(elf, sechdr, &r))
1941 continue;
1942 break;
1943 case EM_ARM:
1944 if (addend_arm_rel(elf, sechdr, &r))
1945 continue;
1946 break;
1947 case EM_MIPS:
1948 if (addend_mips_rel(elf, sechdr, &r))
1949 continue;
1950 break;
1951 }
1952 sym = elf->symtab_start + r_sym;
1953 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001954 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001955 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301956 if (is_second_extable_reloc(start, rel, fromsec))
1957 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001958 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001959 }
1960}
1961
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001962/**
1963 * A module includes a number of sections that are discarded
1964 * either when loaded or when used as built-in.
1965 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001966 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001967 * Likewise for modules used built-in the sections marked __exit
1968 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001969 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001970 * The check_sec_ref() function traverses all relocation records
1971 * to find all references to a section that reference a section that will
1972 * be discarded and warns about it.
1973 **/
1974static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001975 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001976{
1977 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001978 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001979
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001980 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001981 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001982 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001983 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001984 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001985 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001986 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001987 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001988 }
1989}
1990
Andi Kleen7d02b492014-02-08 09:01:12 +01001991static char *remove_dot(char *s)
1992{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301993 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001994
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301995 if (n && s[n]) {
1996 size_t m = strspn(s + n + 1, "0123456789");
1997 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001998 s[n] = 0;
1999 }
2000 return s;
2001}
2002
Masahiro Yamada8b185742018-05-09 18:50:40 +09002003static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 const char *symname;
2006 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002007 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002008 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct module *mod;
2010 struct elf_info info = { };
2011 Elf_Sym *sym;
2012
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002013 if (!parse_elf(&info, modname))
2014 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 mod = new_module(modname);
2017
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002018 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002019 license = get_modinfo(&info, "license");
2020 if (!license)
2021 warn("missing MODULE_LICENSE() in %s\n", modname);
2022 while (license) {
2023 if (license_is_gpl_compatible(license))
2024 mod->gpl_compatible = 1;
2025 else {
2026 mod->gpl_compatible = 0;
2027 break;
2028 }
2029 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002030 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002031
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002032 namespace = get_modinfo(&info, "import_ns");
2033 while (namespace) {
2034 add_namespace(&mod->imported_namespaces, namespace);
2035 namespace = get_next_modinfo(&info, "import_ns",
2036 namespace);
2037 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002038 }
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002041 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002043 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 handle_moddevtable(mod, &info, sym, symname);
2045 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002046
Matthias Maennich69923202019-10-18 10:31:42 +01002047 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2048 symname = remove_dot(info.strtab + sym->st_name);
2049
Masahiro Yamada17436942019-11-15 02:42:24 +09002050 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002051 if (strstarts(symname, "__kstrtabns_"))
2052 sym_update_namespace(symname + strlen("__kstrtabns_"),
2053 namespace_from_kstrtabns(&info,
2054 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002055
2056 if (strstarts(symname, "__crc_"))
2057 handle_modversion(mod, &info, sym,
2058 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002059 }
2060
Denis Efremov15bfc232019-08-01 09:06:57 +03002061 // check for static EXPORT_SYMBOL_* functions && global vars
2062 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2063 unsigned char bind = ELF_ST_BIND(sym->st_info);
2064
2065 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2066 struct symbol *s =
2067 find_symbol(remove_dot(info.strtab +
2068 sym->st_name));
2069
2070 if (s)
2071 s->is_static = 0;
2072 }
2073 }
2074
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002075 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002077 if (!mod->is_vmlinux) {
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002078 version = get_modinfo(&info, "version");
2079 if (version || all_versions)
2080 get_src_version(modname, mod->srcversion,
2081 sizeof(mod->srcversion) - 1);
2082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 parse_elf_finish(&info);
2085
Rusty Russell8c8ef422009-03-31 13:05:34 -06002086 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 * never passed as an argument to an exported function, so
2088 * the automatic versioning doesn't pick it up, but it's really
2089 * important anyhow */
2090 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002091 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Rusty Russell712f9b42013-04-04 17:37:38 +10302094static void read_symbols_from_files(const char *filename)
2095{
2096 FILE *in = stdin;
2097 char fname[PATH_MAX];
2098
2099 if (strcmp(filename, "-") != 0) {
2100 in = fopen(filename, "r");
2101 if (!in)
2102 fatal("Can't open filenames file %s: %m", filename);
2103 }
2104
2105 while (fgets(fname, PATH_MAX, in) != NULL) {
2106 if (strends(fname, "\n"))
2107 fname[strlen(fname)-1] = '\0';
2108 read_symbols(fname);
2109 }
2110
2111 if (in != stdin)
2112 fclose(in);
2113}
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115#define SZ 500
2116
2117/* We first write the generated file into memory using the
2118 * following helper, then compare to the file on disk and
2119 * only update the later if anything changed */
2120
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002121void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2122 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 char tmp[SZ];
2125 int len;
2126 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 va_start(ap, fmt);
2129 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002130 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 va_end(ap);
2132}
2133
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002134void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002137 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002138 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140 strncpy(buf->p + buf->pos, s, len);
2141 buf->pos += len;
2142}
2143
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002144static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2145{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002146 switch (exp) {
2147 case export_gpl:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002148 fatal("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
2149 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002150 break;
2151 case export_unused_gpl:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002152 fatal("GPL-incompatible module %s.ko uses GPL-only symbol marked UNUSED '%s'\n",
2153 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002154 break;
2155 case export_gpl_future:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002156 warn("GPL-incompatible module %s.ko uses future GPL-only symbol '%s'\n",
2157 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002158 break;
2159 case export_plain:
2160 case export_unused:
2161 case export_unknown:
2162 /* ignore */
2163 break;
2164 }
2165}
2166
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002167static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002168{
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002169 switch (exp) {
2170 case export_unused:
2171 case export_unused_gpl:
Masahiro Yamada1be5fa62020-06-01 14:57:25 +09002172 warn("module %s.ko uses symbol '%s' marked UNUSED\n",
2173 m, s);
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002174 break;
2175 default:
2176 /* ignore */
2177 break;
2178 }
2179}
2180
Masahiro Yamada3b415282018-11-23 16:57:23 +09002181static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002182{
2183 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002184 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002185
2186 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002187 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002188 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002189 if (!exp || exp->module == mod) {
2190 if (have_vmlinux && !s->weak) {
Jessica Yu93c95e52020-03-06 17:02:05 +01002191 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2192 "\"%s\" [%s.ko] undefined!\n",
2193 s->name, mod->name);
2194 if (!warn_unresolved)
Masahiro Yamada3b415282018-11-23 16:57:23 +09002195 err = 1;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002196 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002197 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002198 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002199 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002200 if (basename)
2201 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002202 else
2203 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002204
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002205 if (exp->namespace &&
2206 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002207 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2208 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2209 basename, exp->name, exp->namespace);
2210 if (!allow_missing_ns_imports)
2211 err = 1;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002212 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002213 }
2214
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002215 if (!mod->gpl_compatible)
2216 check_for_gpl_usage(exp->export, basename, exp->name);
2217 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002218 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002219
2220 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002221}
2222
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002223static int check_modname_len(struct module *mod)
2224{
2225 const char *mod_name;
2226
2227 mod_name = strrchr(mod->name, '/');
2228 if (mod_name == NULL)
2229 mod_name = mod->name;
2230 else
2231 mod_name++;
2232 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2233 merror("module name is too long [%s.ko]\n", mod->name);
2234 return 1;
2235 }
2236
2237 return 0;
2238}
2239
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002240/**
2241 * Header for the generated file
2242 **/
2243static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
2245 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002246 /*
2247 * Include build-salt.h after module.h in order to
2248 * inherit the definitions.
2249 */
2250 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 buf_printf(b, "#include <linux/vermagic.h>\n");
2252 buf_printf(b, "#include <linux/compiler.h>\n");
2253 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002254 buf_printf(b, "BUILD_SALT;\n");
2255 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002257 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302259 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002260 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002261 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002263 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (mod->has_cleanup)
2265 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002266 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002268 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 buf_printf(b, "};\n");
2270}
2271
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002272static void add_intree_flag(struct buffer *b, int is_intree)
2273{
2274 if (is_intree)
2275 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2276}
2277
Andi Kleencaf75012018-01-25 15:50:28 -08002278/* Cannot check for assembler */
2279static void add_retpoline(struct buffer *b)
2280{
WANG Chaoe4f35892018-12-11 00:37:25 +08002281 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002282 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2283 buf_printf(b, "#endif\n");
2284}
2285
Trevor Keith5c725132009-09-22 16:43:38 -07002286static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002287{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002288 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002289 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2290}
2291
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002292/**
2293 * Record CRCs for unresolved symbols
2294 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002295static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002298 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 for (s = mod->unres; s; s = s->next) {
2301 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002302 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 s->module = exp->module;
2305 s->crc_valid = exp->crc_valid;
2306 s->crc = exp->crc;
2307 }
2308
2309 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 buf_printf(b, "\n");
2313 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002314 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002317 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002320 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 s->name, mod->name);
2322 continue;
2323 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302324 if (strlen(s->name) >= MODULE_NAME_LEN) {
2325 merror("too long symbol \"%s\" [%s.ko]\n",
2326 s->name, mod->name);
2327 err = 1;
2328 break;
2329 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002330 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302331 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
2333
2334 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002335
2336 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002339static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 int first = 1;
2343
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002344 /* Clear ->seen flag of modules that own symbols needed by this. */
2345 for (s = mod->unres; s; s = s->next)
2346 if (s->module)
Masahiro Yamada5a438af2020-06-01 14:57:26 +09002347 s->module->seen = s->module->is_vmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002350 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002352 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (!s->module)
2354 continue;
2355
2356 if (s->module->seen)
2357 continue;
2358
2359 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002360 p = strrchr(s->module->name, '/');
2361 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002362 p++;
2363 else
2364 p = s->module->name;
2365 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 first = 0;
2367 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002368 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002371static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 if (mod->srcversion[0]) {
2374 buf_printf(b, "\n");
2375 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2376 mod->srcversion);
2377 }
2378}
2379
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002380static void write_buf(struct buffer *b, const char *fname)
2381{
2382 FILE *file;
2383
2384 file = fopen(fname, "w");
2385 if (!file) {
2386 perror(fname);
2387 exit(1);
2388 }
2389 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2390 perror(fname);
2391 exit(1);
2392 }
2393 if (fclose(file) != 0) {
2394 perror(fname);
2395 exit(1);
2396 }
2397}
2398
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002399static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
2401 char *tmp;
2402 FILE *file;
2403 struct stat st;
2404
2405 file = fopen(fname, "r");
2406 if (!file)
2407 goto write;
2408
2409 if (fstat(fileno(file), &st) < 0)
2410 goto close_write;
2411
2412 if (st.st_size != b->pos)
2413 goto close_write;
2414
2415 tmp = NOFAIL(malloc(b->pos));
2416 if (fread(tmp, 1, b->pos, file) != b->pos)
2417 goto free_write;
2418
2419 if (memcmp(tmp, b->p, b->pos) != 0)
2420 goto free_write;
2421
2422 free(tmp);
2423 fclose(file);
2424 return;
2425
2426 free_write:
2427 free(tmp);
2428 close_write:
2429 fclose(file);
2430 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002431 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
Ram Paibd5cbce2006-06-08 22:12:53 -07002434/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002435 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002436 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002437static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002439 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002441 buf = read_text_file(fname);
2442 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 /* No symbol versions, silently ignore */
2444 return;
2445
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002446 pos = buf;
2447
2448 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002449 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 unsigned int crc;
2451 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002452 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 if (!(symname = strchr(line, '\t')))
2455 goto fail;
2456 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002457 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto fail;
2459 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002460 if (!(export = strchr(modname, '\t')))
2461 goto fail;
2462 *export++ = '\0';
2463 if (!(namespace = strchr(export, '\t')))
2464 goto fail;
2465 *namespace++ = '\0';
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 crc = strtoul(line, &d, 16);
2468 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2469 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002470 mod = find_module(modname);
2471 if (!mod) {
Jan Beulich0fa3a882009-03-12 12:28:30 +00002472 mod = new_module(modname);
Masahiro Yamada52c34162020-06-01 14:57:05 +09002473 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002475 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002476 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002477 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002478 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002480 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return;
2482fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002483 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 fatal("parse error in symbol dump file\n");
2485}
2486
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002487/* For normal builds always dump all symbols.
2488 * For external modules only dump symbols
2489 * that are not read from kernel Module.symvers.
2490 **/
2491static int dump_sym(struct symbol *sym)
2492{
2493 if (!external_module)
2494 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002495 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002496 return 0;
2497 return 1;
2498}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002499
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002500static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 struct buffer buf = { };
2503 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002504 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 int n;
2506
2507 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2508 symbol = symbolhash[n];
2509 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002510 if (dump_sym(symbol)) {
2511 namespace = symbol->namespace;
2512 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2513 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002514 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002515 export_str(symbol->export),
2516 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 symbol = symbol->next;
2519 }
2520 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002521 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002522 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002525static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002526{
2527 struct module *mod;
2528 struct namespace_list *ns;
2529 struct buffer ns_deps_buf = {};
2530
2531 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002532
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002533 if (mod->from_dump || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002534 continue;
2535
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002536 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002537
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002538 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2539 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002540
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002541 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002542 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002543
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002544 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002545 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002546}
2547
Masahiro Yamada79247992020-06-01 14:57:07 +09002548struct dump_list {
2549 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002550 const char *file;
2551};
2552
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002553int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 struct module *mod;
2556 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002557 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302558 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002560 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002561 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002562 struct dump_list *dump_read_start = NULL;
2563 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Masahiro Yamada467b82d2020-06-01 14:57:22 +09002565 while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002566 switch (opt) {
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002567 case 'e':
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002568 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002569 break;
2570 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002571 *dump_read_iter =
2572 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2573 (*dump_read_iter)->file = optarg;
2574 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002575 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002576 case 'm':
2577 modversions = 1;
2578 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302579 case 'n':
2580 ignore_missing_files = 1;
2581 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002582 case 'o':
2583 dump_write = optarg;
2584 break;
2585 case 'a':
2586 all_versions = 1;
2587 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302588 case 'T':
2589 files_source = optarg;
2590 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002591 case 'w':
2592 warn_unresolved = 1;
2593 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302594 case 'E':
2595 sec_mismatch_fatal = 1;
2596 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002597 case 'N':
2598 allow_missing_ns_imports = 1;
2599 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002600 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002601 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002602 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002603 default:
2604 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
2606 }
2607
Masahiro Yamada79247992020-06-01 14:57:07 +09002608 while (dump_read_start) {
2609 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002610
Masahiro Yamada79247992020-06-01 14:57:07 +09002611 read_dump(dump_read_start->file);
2612 tmp = dump_read_start->next;
2613 free(dump_read_start);
2614 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002617 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Rusty Russell712f9b42013-04-04 17:37:38 +10302620 if (files_source)
2621 read_symbols_from_files(files_source);
2622
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002623 /*
2624 * When there's no vmlinux, don't print warnings about
2625 * unresolved symbols (since there'll be too many ;)
2626 */
2627 if (!have_vmlinux)
2628 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2629
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002630 err = 0;
2631
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002632 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302633 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002634
Masahiro Yamada0b19d542020-06-01 14:57:27 +09002635 if (mod->is_vmlinux || mod->from_dump)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002636 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 buf.pos = 0;
2639
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002640 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002641 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002644 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002645 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002646 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002647 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002648 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 add_moddevtable(&buf, mod);
2650 add_srcversion(&buf, mod);
2651
2652 sprintf(fname, "%s.mod.c", mod->name);
2653 write_if_changed(&buf, fname);
2654 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002655
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002656 if (missing_namespace_deps)
2657 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (dump_write)
2660 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002661 if (sec_mismatch_count && sec_mismatch_fatal)
Jessica Yu93c95e52020-03-06 17:02:05 +01002662 fatal("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002663 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002664 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002665 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002666
Masahiro Yamada47346e92019-09-24 21:07:40 +09002667 for (s = symbolhash[n]; s; s = s->next) {
Denis Efremov15bfc232019-08-01 09:06:57 +03002668 if (s->is_static)
2669 warn("\"%s\" [%s] is a static %s\n",
2670 s->name, s->module->name,
2671 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002672 }
2673 }
2674
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002675 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}