blob: 93019349f0229890570d25da28d553b9fc91b0d3 [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;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020033/* Warn about section mismatch in vmlinux if set to 1 */
34static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070035/* Only warn about unresolved symbols */
36static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070037/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010038static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103039static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093040/* ignore missing files */
41static int ignore_missing_files;
Jessica Yu54b77842020-03-06 17:02:06 +010042/* If set to 1, only warn (instead of error) about missing ns imports */
43static int allow_missing_ns_imports;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010044
Sam Ravnborgc96fca22006-07-01 11:44:23 +020045enum export {
46 export_plain, export_unused, export_gpl,
47 export_unused_gpl, export_gpl_future, export_unknown
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080050/* In kernel, this size is defined in linux/module.h;
51 * here we use Elf_Addr instead of long for covering cross-compile
52 */
53
54#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
55
Jessica Yu93c95e52020-03-06 17:02:05 +010056void __attribute__((format(printf, 2, 3)))
57modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 va_list arglist;
60
Jessica Yu93c95e52020-03-06 17:02:05 +010061 switch (loglevel) {
62 case LOG_WARN:
63 fprintf(stderr, "WARNING: ");
64 break;
65 case LOG_ERROR:
66 fprintf(stderr, "ERROR: ");
67 break;
68 case LOG_FATAL:
69 fprintf(stderr, "FATAL: ");
70 break;
71 default: /* invalid loglevel, ignore */
72 break;
73 }
74
75 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 va_start(arglist, fmt);
78 vfprintf(stderr, fmt, arglist);
79 va_end(arglist);
80
Jessica Yu93c95e52020-03-06 17:02:05 +010081 if (loglevel == LOG_FATAL)
82 exit(1);
Matthew Wilcox2a116652006-10-07 05:35:32 -060083}
84
Rusty Russelld4ef1c32013-04-04 17:37:32 +103085static inline bool strends(const char *str, const char *postfix)
86{
87 if (strlen(str) < strlen(postfix))
88 return false;
89
90 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
91}
92
Sam Ravnborg040fcc82006-01-28 22:15:55 +010093static int is_vmlinux(const char *modname)
94{
95 const char *myname;
96
Sam Ravnborgdf578e72008-01-11 19:17:15 +010097 myname = strrchr(modname, '/');
98 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010099 myname++;
100 else
101 myname = modname;
102
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200103 return (strcmp(myname, "vmlinux") == 0) ||
104 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107void *do_nofail(void *ptr, const char *expr)
108{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100109 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100110 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return ptr;
113}
114
Masahiro Yamadaac5100f2020-06-01 14:57:17 +0900115char *read_text_file(const char *filename)
116{
117 struct stat st;
118 size_t nbytes;
119 int fd;
120 char *buf;
121
122 fd = open(filename, O_RDONLY);
123 if (fd < 0) {
124 perror(filename);
125 exit(1);
126 }
127
128 if (fstat(fd, &st) < 0) {
129 perror(filename);
130 exit(1);
131 }
132
133 buf = NOFAIL(malloc(st.st_size + 1));
134
135 nbytes = st.st_size;
136
137 while (nbytes) {
138 ssize_t bytes_read;
139
140 bytes_read = read(fd, buf, nbytes);
141 if (bytes_read < 0) {
142 perror(filename);
143 exit(1);
144 }
145
146 nbytes -= bytes_read;
147 }
148 buf[st.st_size] = '\0';
149
150 close(fd);
151
152 return buf;
153}
154
155char *get_line(char **stringp)
156{
157 /* do not return the unwanted extra line at EOF */
158 if (*stringp && **stringp == '\0')
159 return NULL;
160
161 return strsep(stringp, "\n");
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static struct module *modules;
166
Masahiro Yamada8b185742018-05-09 18:50:40 +0900167static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct module *mod;
170
171 for (mod = modules; mod; mod = mod->next)
172 if (strcmp(mod->name, modname) == 0)
173 break;
174 return mod;
175}
176
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030177static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030180 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 mod = NOFAIL(malloc(sizeof(*mod)));
183 memset(mod, 0, sizeof(*mod));
184 p = NOFAIL(strdup(modname));
185
186 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030187 if (strends(p, ".o")) {
188 p[strlen(p) - 2] = '\0';
189 mod->is_dot_o = 1;
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /* add to list */
193 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200194 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 mod->next = modules;
196 modules = mod;
197
198 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 Yamada7ef9ab32019-11-15 02:42:26 +0900438 } else if (!external_module || is_vmlinux(s->module->name) ||
439 s->module == mod) {
440 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
441 mod->name, name, s->module->name,
442 is_vmlinux(s->module->name) ? "" : ".ko");
443 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",
699 symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
700 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
Frank Rowand258f7422012-04-09 17:59:03 -0700721 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900722 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200723 export = export_from_secname(info, get_secindex(info, sym));
724 else
725 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 switch (sym->st_shndx) {
728 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900729 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100730 /* Should warn here, but modpost runs before the linker */
731 } else
732 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 case SHN_UNDEF:
735 /* undefined symbol */
736 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
737 ELF_ST_BIND(sym->st_info) != STB_WEAK)
738 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200739 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (info->hdr->e_machine == EM_SPARC ||
742 info->hdr->e_machine == EM_SPARCV9) {
743 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700744 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100746 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700747 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100748 munged[0] = '_';
749 munged[1] = toupper(munged[1]);
750 symname = munged;
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100753
Rusty Russellb92021b2013-03-15 15:04:17 +1030754 mod->unres = alloc_symbol(symname,
755 ELF_ST_BIND(sym->st_info) == STB_WEAK,
756 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 break;
758 default:
759 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900760 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100761 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100762 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900764 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900766 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 mod->has_cleanup = 1;
768 break;
769 }
770}
771
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100772/**
773 * Parse tag=value strings from .modinfo section
774 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static char *next_string(char *string, unsigned long *secsize)
776{
777 /* Skip non-zero chars */
778 while (string[0]) {
779 string++;
780 if ((*secsize)-- <= 1)
781 return NULL;
782 }
783
784 /* Skip any zero padding. */
785 while (!string[0]) {
786 string++;
787 if ((*secsize)-- <= 1)
788 return NULL;
789 }
790 return string;
791}
792
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900793static char *get_next_modinfo(struct elf_info *info, const char *tag,
794 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 char *p;
797 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900798 char *modinfo = info->modinfo;
799 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900801 if (prev) {
802 size -= prev - modinfo;
803 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 for (p = modinfo; p; p = next_string(p, &size)) {
807 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
808 return p + taglen + 1;
809 }
810 return NULL;
811}
812
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900813static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200814
815{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900816 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200817}
818
Sam Ravnborg93684d32006-02-19 11:53:35 +0100819/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100820 * Test if string s ends in string sub
821 * return 0 if match
822 **/
823static int strrcmp(const char *s, const char *sub)
824{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100825 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100826
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100827 if (!s || !sub)
828 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100829
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100830 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100831 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100832
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100833 if ((slen == 0) || (sublen == 0))
834 return 1;
835
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100836 if (sublen > slen)
837 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100838
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100839 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100840}
841
Sam Ravnborgff13f922008-01-23 19:54:27 +0100842static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
843{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100844 if (sym)
845 return elf->strtab + sym->st_name;
846 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100847 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100848}
849
Sam Ravnborg10668222008-01-13 22:21:31 +0100850/* The pattern is an array of simple patterns.
851 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100852 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100853 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930854 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100855 */
Trevor Keith5c725132009-09-22 16:43:38 -0700856static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100857{
858 const char *p;
859 while (*pat) {
860 p = *pat++;
861 const char *endp = p + strlen(p) - 1;
862
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930863 /* "*foo*" */
864 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300865 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
866 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930867
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930868 free(bare);
869 if (here != NULL)
870 return 1;
871 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100872 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930873 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100874 if (strrcmp(sym, p + 1) == 0)
875 return 1;
876 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100877 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100878 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100879 if (strncmp(sym, p, strlen(p) - 1) == 0)
880 return 1;
881 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100882 /* no wildcards */
883 else {
884 if (strcmp(p, sym) == 0)
885 return 1;
886 }
887 }
888 /* no match */
889 return 0;
890}
891
Sam Ravnborg10668222008-01-13 22:21:31 +0100892/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930893static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200894{
895 ".comment*",
896 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930897 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800898 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000899 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200900 ".mdebug*", /* alpha, score, mips etc. */
901 ".pdr", /* alpha, score, mips etc. */
902 ".stab*",
903 ".note*",
904 ".got*",
905 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400906 ".xt.prop", /* xtensa */
907 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030908 ".arcextmap*", /* arc */
909 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030910 ".cmem*", /* EZchip */
911 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100912 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600913 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200914 NULL
915};
Sam Ravnborg10668222008-01-13 22:21:31 +0100916
Sam Ravnborge241a632008-01-28 20:13:13 +0100917/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400918 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100919 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400920 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100921 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400922static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900923 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100924{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400925 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100926
Anders Kaseorgb614a692009-04-23 16:49:33 -0400927 if (sechdr->sh_type == SHT_PROGBITS &&
928 !(sechdr->sh_flags & SHF_ALLOC) &&
929 !match(sec, section_white_list)) {
930 warn("%s (%s): unexpected non-allocatable section.\n"
931 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
932 "Note that for example <linux/init.h> contains\n"
933 "section definitions for use in .S files.\n\n",
934 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100935 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100936}
937
938
939
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100940#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930941 ".init.setup", ".init.rodata", ".meminit.rodata", \
942 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100943#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930944 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100945
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100946#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930947 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100948#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930949 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100950
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200951#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930952 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
953 ".pci_fixup_enable", ".pci_fixup_resume", \
954 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200955
Paul Gortmakere24f6622013-06-19 19:30:48 -0400956#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
957#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100958
959#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
960#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100961
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930962#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930963#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700964 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930965#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400966 ".fixup", ".entry.text", ".exception.text", ".text.*", \
967 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100968
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000969#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000970#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100971
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000972#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000973#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100974
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930975#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
976 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
977
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100978/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930979static const char *const init_data_sections[] =
980 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100981
982/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930983static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100984
985/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930986static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100987 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100988
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930989/* all text sections */
990static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
991
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100992/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930993static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100994
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100995
996/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100997#define DEFAULT_SYMBOL_WHITE_LIST \
998 "*driver", \
999 "*_template", /* scsi uses *_template a lot */ \
1000 "*_timer", /* arm uses ops structures named _timer a lot */ \
1001 "*_sht", /* scsi also used *_sht to some extent */ \
1002 "*_ops", \
1003 "*_probe", \
1004 "*_probe_one", \
1005 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001006
Mathias Krause7a3ee752014-08-27 20:28:53 +09301007static const char *const head_sections[] = { ".head.text*", NULL };
1008static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001009 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301010static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001011
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001012enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001013 TEXT_TO_ANY_INIT,
1014 DATA_TO_ANY_INIT,
1015 TEXT_TO_ANY_EXIT,
1016 DATA_TO_ANY_EXIT,
1017 XXXINIT_TO_SOME_INIT,
1018 XXXEXIT_TO_SOME_EXIT,
1019 ANY_INIT_TO_ANY_EXIT,
1020 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001021 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301022 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001023};
1024
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301025/**
1026 * Describe how to match sections on different criterias:
1027 *
1028 * @fromsec: Array of sections to be matched.
1029 *
1030 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1031 * this array is forbidden (black-list). Can be empty.
1032 *
1033 * @good_tosec: Relocations applied to a section in @fromsec must be
1034 * targetting sections in this array (white-list). Can be empty.
1035 *
1036 * @mismatch: Type of mismatch.
1037 *
1038 * @symbol_white_list: Do not match a relocation to a symbol in this list
1039 * even if it is targetting a section in @bad_to_sec.
1040 *
1041 * @handler: Specific handler to call when a match is found. If NULL,
1042 * default_mismatch_handler() will be called.
1043 *
1044 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001045struct sectioncheck {
1046 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301047 const char *bad_tosec[20];
1048 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001049 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001050 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301051 void (*handler)(const char *modname, struct elf_info *elf,
1052 const struct sectioncheck* const mismatch,
1053 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1054
Sam Ravnborg10668222008-01-13 22:21:31 +01001055};
1056
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301057static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1058 const struct sectioncheck* const mismatch,
1059 Elf_Rela *r, Elf_Sym *sym,
1060 const char *fromsec);
1061
Mathias Krause7a3ee752014-08-27 20:28:53 +09301062static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001063/* Do not reference init/exit code/data from
1064 * normal code and data
1065 */
1066{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001067 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301068 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001069 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001070 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001071},
1072{
1073 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301074 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001075 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001076 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001077},
1078{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001079 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301080 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001081 .mismatch = DATA_TO_ANY_INIT,
1082 .symbol_white_list = {
1083 "*_template", "*_timer", "*_sht", "*_ops",
1084 "*_probe", "*_probe_one", "*_console", NULL
1085 },
1086},
1087{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001088 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301089 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001090 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001091 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001092},
1093{
1094 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301095 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001096 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001097 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001098},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001099/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001100{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001101 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301102 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001103 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001104 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001105},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001106/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001107{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001108 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301109 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001110 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001111 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001112},
1113/* Do not use exit code/data from init code */
1114{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001115 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301116 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001117 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001118 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001119},
1120/* Do not use init code/data from exit code */
1121{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001122 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301123 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001124 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001125 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001126},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001127{
1128 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301129 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001130 .mismatch = ANY_INIT_TO_ANY_EXIT,
1131 .symbol_white_list = { NULL },
1132},
Sam Ravnborg10668222008-01-13 22:21:31 +01001133/* Do not export init/exit functions or data */
1134{
1135 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301136 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001137 .mismatch = EXPORT_TO_INIT_EXIT,
1138 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301139},
1140{
1141 .fromsec = { "__ex_table", NULL },
1142 /* If you're adding any new black-listed sections in here, consider
1143 * adding a special 'printer' for them in scripts/check_extable.
1144 */
1145 .bad_tosec = { ".altinstr_replacement", NULL },
1146 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1147 .mismatch = EXTABLE_TO_NON_TEXT,
1148 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001149}
1150};
1151
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001152static const struct sectioncheck *section_mismatch(
1153 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001154{
1155 int i;
1156 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1157 const struct sectioncheck *check = &sectioncheck[0];
1158
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301159 /*
1160 * The target section could be the SHT_NUL section when we're
1161 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001162 * doesn't make much sense and causes build failures on parisc
1163 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301164 */
1165 if (*tosec == '\0')
1166 return NULL;
1167
Sam Ravnborg10668222008-01-13 22:21:31 +01001168 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301169 if (match(fromsec, check->fromsec)) {
1170 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1171 return check;
1172 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1173 return check;
1174 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001175 check++;
1176 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001177 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001178}
1179
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001180/**
1181 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001182 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001183 * Pattern 1:
1184 * If a module parameter is declared __initdata and permissions=0
1185 * then this is legal despite the warning generated.
1186 * We cannot see value of permissions here, so just ignore
1187 * this pattern.
1188 * The pattern is identified by:
1189 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001190 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001191 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001192 *
Rusty Russell6a841522010-08-11 23:04:16 -06001193 * Pattern 1a:
1194 * module_param_call() ops can refer to __init set function if permissions=0
1195 * The pattern is identified by:
1196 * tosec = .init.text
1197 * fromsec = .data*
1198 * atsym = __param_ops_*
1199 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001200 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001201 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001202 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001203 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001204 * tosec = init or exit section
1205 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001206 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1207 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001208 *
1209 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001210 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001211 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001212 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001213 * Some symbols belong to init section but still it is ok to reference
1214 * these from non-init sections as these symbols don't have any memory
1215 * allocated for them and symbol address and value are same. So even
1216 * if init section is freed, its ok to reference those symbols.
1217 * For ex. symbols marking the init section boundaries.
1218 * This pattern is identified by
1219 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001220 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301221 * Pattern 5:
1222 * GCC may optimize static inlines when fed constant arg(s) resulting
1223 * in functions like cpumask_empty() -- generating an associated symbol
1224 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1225 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1226 * meaningless section warning. May need to add isra symbols too...
1227 * This pattern is identified by
1228 * tosec = init section
1229 * fromsec = text section
1230 * refsymname = *.constprop.*
1231 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001232 * Pattern 6:
1233 * Hide section mismatch warnings for ELF local symbols. The goal
1234 * is to eliminate false positive modpost warnings caused by
1235 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1236 * Autogenerated symbol names bypass modpost's "Pattern 2"
1237 * whitelisting, which relies on pattern-matching against symbol
1238 * names to work. (One situation where gcc can autogenerate ELF
1239 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001240 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001241static int secref_whitelist(const struct sectioncheck *mismatch,
1242 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001243 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001244{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001245 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001246 if (match(tosec, init_data_sections) &&
1247 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001248 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001249 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001250
Rusty Russell6a841522010-08-11 23:04:16 -06001251 /* Check for pattern 1a */
1252 if (strcmp(tosec, ".init.text") == 0 &&
1253 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001254 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001255 return 0;
1256
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001257 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001258 if (match(tosec, init_exit_sections) &&
1259 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001260 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001261 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001262
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001263 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001264 if (match(fromsec, head_sections) &&
1265 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001266 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001267
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001268 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001269 if (match(tosym, linker_symbols))
1270 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001271
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301272 /* Check for pattern 5 */
1273 if (match(fromsec, text_sections) &&
1274 match(tosec, init_sections) &&
1275 match(fromsym, optim_symbols))
1276 return 0;
1277
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001278 /* Check for pattern 6 */
1279 if (strstarts(fromsym, ".L"))
1280 return 0;
1281
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001282 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001283}
1284
Sami Tolvanen5818c682018-10-23 15:15:35 -07001285static inline int is_arm_mapping_symbol(const char *str)
1286{
1287 return str[0] == '$' && strchr("axtd", str[1])
1288 && (str[2] == '\0' || str[2] == '.');
1289}
1290
1291/*
1292 * If there's no name there, ignore it; likewise, ignore it if it's
1293 * one of the magic symbols emitted used by current ARM tools.
1294 *
1295 * Otherwise if find_symbols_between() returns those symbols, they'll
1296 * fail the whitelist tests and cause lots of false alarms ... fixable
1297 * only by merging __exit and __init sections into __text, bloating
1298 * the kernel (which is especially evil on embedded platforms).
1299 */
1300static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1301{
1302 const char *name = elf->strtab + sym->st_name;
1303
1304 if (!name || !strlen(name))
1305 return 0;
1306 return !is_arm_mapping_symbol(name);
1307}
1308
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001309/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001310 * Find symbol based on relocation record info.
1311 * In some cases the symbol supplied is a valid symbol so
1312 * return refsym. If st_name != 0 we assume this is a valid symbol.
1313 * In other cases the symbol needs to be looked up in the symbol table
1314 * based on section and address.
1315 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001316static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001317 Elf_Sym *relsym)
1318{
1319 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001320 Elf_Sym *near = NULL;
1321 Elf64_Sword distance = 20;
1322 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001323 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001324
1325 if (relsym->st_name != 0)
1326 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001327
1328 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001329 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001330 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001331 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001332 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1333 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001334 if (!is_valid_name(elf, sym))
1335 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001336 if (sym->st_value == addr)
1337 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001338 /* Find a symbol nearby - addr are maybe negative */
1339 d = sym->st_value - addr;
1340 if (d < 0)
1341 d = addr - sym->st_value;
1342 if (d < distance) {
1343 distance = d;
1344 near = sym;
1345 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001346 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001347 /* We need a close match */
1348 if (distance < 20)
1349 return near;
1350 else
1351 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001352}
1353
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001354/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001355 * Find symbols before or equal addr and after addr - in the section sec.
1356 * If we find two symbols with equal offset prefer one with a valid name.
1357 * The ELF format may have a better way to detect what type of symbol
1358 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001359 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001360static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1361 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001362{
1363 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001364 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001365 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001366
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001367 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1368 const char *symsec;
1369
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001370 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001371 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001372 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001373 if (strcmp(symsec, sec) != 0)
1374 continue;
David Brownellda68d612007-02-20 13:58:16 -08001375 if (!is_valid_name(elf, sym))
1376 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001377 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001378 if ((addr - sym->st_value) < distance) {
1379 distance = addr - sym->st_value;
1380 near = sym;
1381 } else if ((addr - sym->st_value) == distance) {
1382 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001383 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001384 }
1385 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001386 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001387}
1388
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001389/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001390 * Convert a section name to the function/data attribute
1391 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001392 * .memexitconst => __memconst
1393 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001394 *
1395 * The memory of returned value has been allocated on a heap. The user of this
1396 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001397*/
1398static char *sec2annotation(const char *s)
1399{
1400 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001401 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001402 char *r = p;
1403
1404 *p++ = '_';
1405 *p++ = '_';
1406 if (*s == '.')
1407 s++;
1408 while (*s && *s != '.')
1409 *p++ = *s++;
1410 *p = '\0';
1411 if (*s == '.')
1412 s++;
1413 if (strstr(s, "rodata") != NULL)
1414 strcat(p, "const ");
1415 else if (strstr(s, "data") != NULL)
1416 strcat(p, "data ");
1417 else
1418 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001419 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001420 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001421 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001422 }
1423}
1424
1425static int is_function(Elf_Sym *sym)
1426{
1427 if (sym)
1428 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1429 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001430 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001431}
1432
Randy Dunlap00759c02011-03-15 14:13:47 -07001433static void print_section_list(const char * const list[20])
1434{
1435 const char *const *s = list;
1436
1437 while (*s) {
1438 fprintf(stderr, "%s", *s);
1439 s++;
1440 if (*s)
1441 fprintf(stderr, ", ");
1442 }
1443 fprintf(stderr, "\n");
1444}
1445
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301446static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1447{
1448 switch (is_func) {
1449 case 0: *name = "variable"; *name_p = ""; break;
1450 case 1: *name = "function"; *name_p = "()"; break;
1451 default: *name = "(unknown reference)"; *name_p = ""; break;
1452 }
1453}
1454
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001455/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001456 * Print a warning about a section mismatch.
1457 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001458 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001459 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001460static void report_sec_mismatch(const char *modname,
1461 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001462 const char *fromsec,
1463 unsigned long long fromaddr,
1464 const char *fromsym,
1465 int from_is_func,
1466 const char *tosec, const char *tosym,
1467 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001468{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001469 const char *from, *from_p;
1470 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001471 char *prl_from;
1472 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001473
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001474 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001475
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301476 get_pretty_name(from_is_func, &from, &from_p);
1477 get_pretty_name(to_is_func, &to, &to_p);
1478
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001479 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1480 "to the %s %s:%s%s\n",
1481 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1482 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001483
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001484 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001485 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001486 prl_from = sec2annotation(fromsec);
1487 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001488 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001489 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001490 "the %s %s%s%s.\n"
1491 "This is often because %s lacks a %s\n"
1492 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001493 prl_from, fromsym,
1494 to, prl_to, tosym, to_p,
1495 fromsym, prl_to, tosym);
1496 free(prl_from);
1497 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001498 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001499 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001500 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001501 fprintf(stderr,
1502 "The variable %s references\n"
1503 "the %s %s%s%s\n"
1504 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001505 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001506 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001507 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001508 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001509 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001510 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001511 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001512 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001513 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001514 fprintf(stderr,
1515 "The function %s() references a %s in an exit section.\n"
1516 "Often the %s %s%s has valid usage outside the exit section\n"
1517 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001518 fromsym, to, to, tosym, to_p, prl_to, tosym);
1519 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001520 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001521 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001522 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001523 fprintf(stderr,
1524 "The variable %s references\n"
1525 "the %s %s%s%s\n"
1526 "If the reference is valid then annotate the\n"
1527 "variable with __exit* (see linux/init.h) or "
1528 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001529 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001530 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001531 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001532 break;
1533 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001534 case XXXINIT_TO_SOME_INIT:
1535 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001536 prl_from = sec2annotation(fromsec);
1537 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001538 fprintf(stderr,
1539 "The %s %s%s%s references\n"
1540 "a %s %s%s%s.\n"
1541 "If %s is only used by %s then\n"
1542 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001543 from, prl_from, fromsym, from_p,
1544 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001545 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001546 free(prl_from);
1547 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001548 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001549 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001550 prl_from = sec2annotation(fromsec);
1551 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001552 fprintf(stderr,
1553 "The %s %s%s%s references\n"
1554 "a %s %s%s%s.\n"
1555 "This is often seen when error handling "
1556 "in the init function\n"
1557 "uses functionality in the exit path.\n"
1558 "The fix is often to remove the %sannotation of\n"
1559 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001560 from, prl_from, fromsym, from_p,
1561 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001562 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001563 free(prl_from);
1564 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001565 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001566 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001567 prl_from = sec2annotation(fromsec);
1568 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001569 fprintf(stderr,
1570 "The %s %s%s%s references\n"
1571 "a %s %s%s%s.\n"
1572 "This is often seen when error handling "
1573 "in the exit function\n"
1574 "uses functionality in the init path.\n"
1575 "The fix is often to remove the %sannotation of\n"
1576 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001577 from, prl_from, fromsym, from_p,
1578 to, prl_to, tosym, to_p,
1579 prl_to, tosym, to_p);
1580 free(prl_from);
1581 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001582 break;
1583 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001584 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001585 fprintf(stderr,
1586 "The symbol %s is exported and annotated %s\n"
1587 "Fix this by removing the %sannotation of %s "
1588 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001589 tosym, prl_to, prl_to, tosym);
1590 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001591 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301592 case EXTABLE_TO_NON_TEXT:
1593 fatal("There's a special handler for this mismatch type, "
1594 "we should never get here.");
1595 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001596 }
1597 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001598}
1599
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301600static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1601 const struct sectioncheck* const mismatch,
1602 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1603{
1604 const char *tosec;
1605 Elf_Sym *to;
1606 Elf_Sym *from;
1607 const char *tosym;
1608 const char *fromsym;
1609
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301610 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1611 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301612
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001613 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301614 return;
1615
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301616 tosec = sec_name(elf, get_secindex(elf, sym));
1617 to = find_elf_symbol(elf, r->r_addend, sym);
1618 tosym = sym_name(elf, to);
1619
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301620 /* check whitelist - we may ignore it */
1621 if (secref_whitelist(mismatch,
1622 fromsec, fromsym, tosec, tosym)) {
1623 report_sec_mismatch(modname, mismatch,
1624 fromsec, r->r_offset, fromsym,
1625 is_function(from), tosec, tosym,
1626 is_function(to));
1627 }
1628}
1629
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301630static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1631{
1632 if (section_index > elf->num_sections)
1633 fatal("section_index is outside elf->num_sections!\n");
1634
1635 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1636}
1637
1638/*
1639 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1640 * to know the sizeof(struct exception_table_entry) for the target architecture.
1641 */
1642static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301643static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301644{
1645 /*
1646 * If we're currently checking the second relocation within __ex_table,
1647 * that relocation offset tells us the offsetof(struct
1648 * exception_table_entry, fixup) which is equal to sizeof(struct
1649 * exception_table_entry) divided by two. We use that to our advantage
1650 * since there's no portable way to get that size as every architecture
1651 * seems to go with different sized types. Not pretty but better than
1652 * hard-coding the size for every architecture..
1653 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301654 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301655 extable_entry_size = r->r_offset * 2;
1656}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301657
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301658static inline bool is_extable_fault_address(Elf_Rela *r)
1659{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301660 /*
1661 * extable_entry_size is only discovered after we've handled the
1662 * _second_ relocation in __ex_table, so only abort when we're not
1663 * handling the first reloc and extable_entry_size is zero.
1664 */
1665 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301666 fatal("extable_entry size hasn't been discovered!\n");
1667
1668 return ((r->r_offset == 0) ||
1669 (r->r_offset % extable_entry_size == 0));
1670}
1671
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301672#define is_second_extable_reloc(Start, Cur, Sec) \
1673 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1674
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301675static void report_extable_warnings(const char* modname, struct elf_info* elf,
1676 const struct sectioncheck* const mismatch,
1677 Elf_Rela* r, Elf_Sym* sym,
1678 const char* fromsec, const char* tosec)
1679{
1680 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1681 const char* fromsym_name = sym_name(elf, fromsym);
1682 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1683 const char* tosym_name = sym_name(elf, tosym);
1684 const char* from_pretty_name;
1685 const char* from_pretty_name_p;
1686 const char* to_pretty_name;
1687 const char* to_pretty_name_p;
1688
1689 get_pretty_name(is_function(fromsym),
1690 &from_pretty_name, &from_pretty_name_p);
1691 get_pretty_name(is_function(tosym),
1692 &to_pretty_name, &to_pretty_name_p);
1693
1694 warn("%s(%s+0x%lx): Section mismatch in reference"
1695 " from the %s %s%s to the %s %s:%s%s\n",
1696 modname, fromsec, (long)r->r_offset, from_pretty_name,
1697 fromsym_name, from_pretty_name_p,
1698 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1699
1700 if (!match(tosec, mismatch->bad_tosec) &&
1701 is_executable_section(elf, get_secindex(elf, sym)))
1702 fprintf(stderr,
1703 "The relocation at %s+0x%lx references\n"
1704 "section \"%s\" which is not in the list of\n"
1705 "authorized sections. If you're adding a new section\n"
1706 "and/or if this reference is valid, add \"%s\" to the\n"
1707 "list of authorized sections to jump to on fault.\n"
1708 "This can be achieved by adding \"%s\" to \n"
1709 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1710 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1711}
1712
1713static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1714 const struct sectioncheck* const mismatch,
1715 Elf_Rela* r, Elf_Sym* sym,
1716 const char *fromsec)
1717{
1718 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1719
1720 sec_mismatch_count++;
1721
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001722 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301723
1724 if (match(tosec, mismatch->bad_tosec))
1725 fatal("The relocation at %s+0x%lx references\n"
1726 "section \"%s\" which is black-listed.\n"
1727 "Something is seriously wrong and should be fixed.\n"
1728 "You might get more information about where this is\n"
1729 "coming from by using scripts/check_extable.sh %s\n",
1730 fromsec, (long)r->r_offset, tosec, modname);
1731 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1732 if (is_extable_fault_address(r))
1733 fatal("The relocation at %s+0x%lx references\n"
1734 "section \"%s\" which is not executable, IOW\n"
1735 "it is not possible for the kernel to fault\n"
1736 "at that address. Something is seriously wrong\n"
1737 "and should be fixed.\n",
1738 fromsec, (long)r->r_offset, tosec);
1739 else
1740 fatal("The relocation at %s+0x%lx references\n"
1741 "section \"%s\" which is not executable, IOW\n"
1742 "the kernel will fault if it ever tries to\n"
1743 "jump to it. Something is seriously wrong\n"
1744 "and should be fixed.\n",
1745 fromsec, (long)r->r_offset, tosec);
1746 }
1747}
1748
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001749static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001750 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001751{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001752 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301753 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001754
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001755 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301756 if (mismatch->handler)
1757 mismatch->handler(modname, elf, mismatch,
1758 r, sym, fromsec);
1759 else
1760 default_mismatch_handler(modname, elf, mismatch,
1761 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001762 }
1763}
1764
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001765static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001766 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001767{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001768 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001769}
1770
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001771static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001772{
1773 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001774 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001775
1776 switch (r_typ) {
1777 case R_386_32:
1778 r->r_addend = TO_NATIVE(*location);
1779 break;
1780 case R_386_PC32:
1781 r->r_addend = TO_NATIVE(*location) + 4;
1782 /* For CONFIG_RELOCATABLE=y */
1783 if (elf->hdr->e_type == ET_EXEC)
1784 r->r_addend += r->r_offset;
1785 break;
1786 }
1787 return 0;
1788}
1789
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001790#ifndef R_ARM_CALL
1791#define R_ARM_CALL 28
1792#endif
1793#ifndef R_ARM_JUMP24
1794#define R_ARM_JUMP24 29
1795#endif
1796
David A. Longc9698e52014-02-14 22:41:18 +01001797#ifndef R_ARM_THM_CALL
1798#define R_ARM_THM_CALL 10
1799#endif
1800#ifndef R_ARM_THM_JUMP24
1801#define R_ARM_THM_JUMP24 30
1802#endif
1803#ifndef R_ARM_THM_JUMP19
1804#define R_ARM_THM_JUMP19 51
1805#endif
1806
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001807static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001808{
1809 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1810
1811 switch (r_typ) {
1812 case R_ARM_ABS32:
1813 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001814 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001815 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001816 break;
1817 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001818 case R_ARM_CALL:
1819 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001820 case R_ARM_THM_CALL:
1821 case R_ARM_THM_JUMP24:
1822 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001823 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001824 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001825 sechdr->sh_offset +
1826 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001827 break;
1828 default:
1829 return 1;
1830 }
1831 return 0;
1832}
1833
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001834static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001835{
1836 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001837 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001838 unsigned int inst;
1839
1840 if (r_typ == R_MIPS_HI16)
1841 return 1; /* skip this */
1842 inst = TO_NATIVE(*location);
1843 switch (r_typ) {
1844 case R_MIPS_LO16:
1845 r->r_addend = inst & 0xffff;
1846 break;
1847 case R_MIPS_26:
1848 r->r_addend = (inst & 0x03ffffff) << 2;
1849 break;
1850 case R_MIPS_32:
1851 r->r_addend = inst;
1852 break;
1853 }
1854 return 0;
1855}
1856
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001857static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001858 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001859{
1860 Elf_Sym *sym;
1861 Elf_Rela *rela;
1862 Elf_Rela r;
1863 unsigned int r_sym;
1864 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001865
Sam Ravnborgff13f922008-01-23 19:54:27 +01001866 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001867 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1868
Sam Ravnborgff13f922008-01-23 19:54:27 +01001869 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001870 fromsec += strlen(".rela");
1871 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001872 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001873 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001874
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001875 for (rela = start; rela < stop; rela++) {
1876 r.r_offset = TO_NATIVE(rela->r_offset);
1877#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001878 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001879 unsigned int r_typ;
1880 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1881 r_sym = TO_NATIVE(r_sym);
1882 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1883 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1884 } else {
1885 r.r_info = TO_NATIVE(rela->r_info);
1886 r_sym = ELF_R_SYM(r.r_info);
1887 }
1888#else
1889 r.r_info = TO_NATIVE(rela->r_info);
1890 r_sym = ELF_R_SYM(r.r_info);
1891#endif
1892 r.r_addend = TO_NATIVE(rela->r_addend);
1893 sym = elf->symtab_start + r_sym;
1894 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001895 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001896 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301897 if (is_second_extable_reloc(start, rela, fromsec))
1898 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001899 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001900 }
1901}
1902
1903static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001904 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001905{
1906 Elf_Sym *sym;
1907 Elf_Rel *rel;
1908 Elf_Rela r;
1909 unsigned int r_sym;
1910 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001911
Sam Ravnborgff13f922008-01-23 19:54:27 +01001912 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001913 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1914
Sam Ravnborgff13f922008-01-23 19:54:27 +01001915 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001916 fromsec += strlen(".rel");
1917 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001918 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001919 return;
1920
1921 for (rel = start; rel < stop; rel++) {
1922 r.r_offset = TO_NATIVE(rel->r_offset);
1923#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001924 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001925 unsigned int r_typ;
1926 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1927 r_sym = TO_NATIVE(r_sym);
1928 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1929 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1930 } else {
1931 r.r_info = TO_NATIVE(rel->r_info);
1932 r_sym = ELF_R_SYM(r.r_info);
1933 }
1934#else
1935 r.r_info = TO_NATIVE(rel->r_info);
1936 r_sym = ELF_R_SYM(r.r_info);
1937#endif
1938 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001939 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001940 case EM_386:
1941 if (addend_386_rel(elf, sechdr, &r))
1942 continue;
1943 break;
1944 case EM_ARM:
1945 if (addend_arm_rel(elf, sechdr, &r))
1946 continue;
1947 break;
1948 case EM_MIPS:
1949 if (addend_mips_rel(elf, sechdr, &r))
1950 continue;
1951 break;
1952 }
1953 sym = elf->symtab_start + r_sym;
1954 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001955 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001956 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301957 if (is_second_extable_reloc(start, rel, fromsec))
1958 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001959 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001960 }
1961}
1962
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001963/**
1964 * A module includes a number of sections that are discarded
1965 * either when loaded or when used as built-in.
1966 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001967 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001968 * Likewise for modules used built-in the sections marked __exit
1969 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001970 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001971 * The check_sec_ref() function traverses all relocation records
1972 * to find all references to a section that reference a section that will
1973 * be discarded and warns about it.
1974 **/
1975static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001976 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001977{
1978 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001979 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001980
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001981 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001982 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001983 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001984 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001985 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001986 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001987 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001988 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001989 }
1990}
1991
Andi Kleen7d02b492014-02-08 09:01:12 +01001992static char *remove_dot(char *s)
1993{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301994 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001995
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301996 if (n && s[n]) {
1997 size_t m = strspn(s + n + 1, "0123456789");
1998 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001999 s[n] = 0;
2000 }
2001 return s;
2002}
2003
Masahiro Yamada8b185742018-05-09 18:50:40 +09002004static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 const char *symname;
2007 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002008 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002009 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 struct module *mod;
2011 struct elf_info info = { };
2012 Elf_Sym *sym;
2013
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002014 if (!parse_elf(&info, modname))
2015 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 mod = new_module(modname);
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 mod->skip = 1;
2022 }
2023
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002024 if (!is_vmlinux(modname)) {
2025 license = get_modinfo(&info, "license");
2026 if (!license)
2027 warn("missing MODULE_LICENSE() in %s\n", modname);
2028 while (license) {
2029 if (license_is_gpl_compatible(license))
2030 mod->gpl_compatible = 1;
2031 else {
2032 mod->gpl_compatible = 0;
2033 break;
2034 }
2035 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002036 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002037
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002038 namespace = get_modinfo(&info, "import_ns");
2039 while (namespace) {
2040 add_namespace(&mod->imported_namespaces, namespace);
2041 namespace = get_next_modinfo(&info, "import_ns",
2042 namespace);
2043 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002044 }
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002047 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002049 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 handle_moddevtable(mod, &info, sym, symname);
2051 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002052
Matthias Maennich69923202019-10-18 10:31:42 +01002053 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2054 symname = remove_dot(info.strtab + sym->st_name);
2055
Masahiro Yamada17436942019-11-15 02:42:24 +09002056 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002057 if (strstarts(symname, "__kstrtabns_"))
2058 sym_update_namespace(symname + strlen("__kstrtabns_"),
2059 namespace_from_kstrtabns(&info,
2060 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002061
2062 if (strstarts(symname, "__crc_"))
2063 handle_modversion(mod, &info, sym,
2064 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002065 }
2066
Denis Efremov15bfc232019-08-01 09:06:57 +03002067 // check for static EXPORT_SYMBOL_* functions && global vars
2068 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2069 unsigned char bind = ELF_ST_BIND(sym->st_info);
2070
2071 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2072 struct symbol *s =
2073 find_symbol(remove_dot(info.strtab +
2074 sym->st_name));
2075
2076 if (s)
2077 s->is_static = 0;
2078 }
2079 }
2080
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002081 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002082 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Masahiro Yamada4ddea2f2020-06-01 14:57:16 +09002084 if (!is_vmlinux(modname)) {
2085 version = get_modinfo(&info, "version");
2086 if (version || all_versions)
2087 get_src_version(modname, mod->srcversion,
2088 sizeof(mod->srcversion) - 1);
2089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 parse_elf_finish(&info);
2092
Rusty Russell8c8ef422009-03-31 13:05:34 -06002093 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 * never passed as an argument to an exported function, so
2095 * the automatic versioning doesn't pick it up, but it's really
2096 * important anyhow */
2097 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002098 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Rusty Russell712f9b42013-04-04 17:37:38 +10302101static void read_symbols_from_files(const char *filename)
2102{
2103 FILE *in = stdin;
2104 char fname[PATH_MAX];
2105
2106 if (strcmp(filename, "-") != 0) {
2107 in = fopen(filename, "r");
2108 if (!in)
2109 fatal("Can't open filenames file %s: %m", filename);
2110 }
2111
2112 while (fgets(fname, PATH_MAX, in) != NULL) {
2113 if (strends(fname, "\n"))
2114 fname[strlen(fname)-1] = '\0';
2115 read_symbols(fname);
2116 }
2117
2118 if (in != stdin)
2119 fclose(in);
2120}
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122#define SZ 500
2123
2124/* We first write the generated file into memory using the
2125 * following helper, then compare to the file on disk and
2126 * only update the later if anything changed */
2127
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002128void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2129 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 char tmp[SZ];
2132 int len;
2133 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 va_start(ap, fmt);
2136 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002137 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 va_end(ap);
2139}
2140
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002141void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002144 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002145 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147 strncpy(buf->p + buf->pos, s, len);
2148 buf->pos += len;
2149}
2150
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002151static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2152{
2153 const char *e = is_vmlinux(m) ?"":".ko";
2154
2155 switch (exp) {
2156 case export_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002157 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002158 "uses GPL-only symbol '%s'\n", m, e, s);
2159 break;
2160 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002161 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002162 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2163 break;
2164 case export_gpl_future:
Jessica Yu93c95e52020-03-06 17:02:05 +01002165 warn("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002166 "uses future GPL-only symbol '%s'\n", m, e, s);
2167 break;
2168 case export_plain:
2169 case export_unused:
2170 case export_unknown:
2171 /* ignore */
2172 break;
2173 }
2174}
2175
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002176static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002177{
2178 const char *e = is_vmlinux(m) ?"":".ko";
2179
2180 switch (exp) {
2181 case export_unused:
2182 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002183 warn("module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002184 "uses symbol '%s' marked UNUSED\n", m, e, s);
2185 break;
2186 default:
2187 /* ignore */
2188 break;
2189 }
2190}
2191
Masahiro Yamada3b415282018-11-23 16:57:23 +09002192static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002193{
2194 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002195 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002196
2197 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002198 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002199 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002200 if (!exp || exp->module == mod) {
2201 if (have_vmlinux && !s->weak) {
Jessica Yu93c95e52020-03-06 17:02:05 +01002202 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2203 "\"%s\" [%s.ko] undefined!\n",
2204 s->name, mod->name);
2205 if (!warn_unresolved)
Masahiro Yamada3b415282018-11-23 16:57:23 +09002206 err = 1;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002207 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002208 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002209 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002210 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002211 if (basename)
2212 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002213 else
2214 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002215
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002216 if (exp->namespace &&
2217 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002218 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2219 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2220 basename, exp->name, exp->namespace);
2221 if (!allow_missing_ns_imports)
2222 err = 1;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002223 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002224 }
2225
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002226 if (!mod->gpl_compatible)
2227 check_for_gpl_usage(exp->export, basename, exp->name);
2228 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002229 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002230
2231 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002232}
2233
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002234static int check_modname_len(struct module *mod)
2235{
2236 const char *mod_name;
2237
2238 mod_name = strrchr(mod->name, '/');
2239 if (mod_name == NULL)
2240 mod_name = mod->name;
2241 else
2242 mod_name++;
2243 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2244 merror("module name is too long [%s.ko]\n", mod->name);
2245 return 1;
2246 }
2247
2248 return 0;
2249}
2250
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002251/**
2252 * Header for the generated file
2253 **/
2254static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
2256 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002257 /*
2258 * Include build-salt.h after module.h in order to
2259 * inherit the definitions.
2260 */
2261 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 buf_printf(b, "#include <linux/vermagic.h>\n");
2263 buf_printf(b, "#include <linux/compiler.h>\n");
2264 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002265 buf_printf(b, "BUILD_SALT;\n");
2266 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002268 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302270 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002271 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002272 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002274 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (mod->has_cleanup)
2276 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002277 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002279 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 buf_printf(b, "};\n");
2281}
2282
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002283static void add_intree_flag(struct buffer *b, int is_intree)
2284{
2285 if (is_intree)
2286 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2287}
2288
Andi Kleencaf75012018-01-25 15:50:28 -08002289/* Cannot check for assembler */
2290static void add_retpoline(struct buffer *b)
2291{
WANG Chaoe4f35892018-12-11 00:37:25 +08002292 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002293 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2294 buf_printf(b, "#endif\n");
2295}
2296
Trevor Keith5c725132009-09-22 16:43:38 -07002297static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002298{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002299 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002300 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2301}
2302
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002303/**
2304 * Record CRCs for unresolved symbols
2305 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002306static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002309 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 for (s = mod->unres; s; s = s->next) {
2312 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002313 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 s->module = exp->module;
2316 s->crc_valid = exp->crc_valid;
2317 s->crc = exp->crc;
2318 }
2319
2320 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 buf_printf(b, "\n");
2324 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002325 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002328 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002331 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 s->name, mod->name);
2333 continue;
2334 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302335 if (strlen(s->name) >= MODULE_NAME_LEN) {
2336 merror("too long symbol \"%s\" [%s.ko]\n",
2337 s->name, mod->name);
2338 err = 1;
2339 break;
2340 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002341 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302342 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344
2345 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002346
2347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002350static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 int first = 1;
2354
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002355 /* Clear ->seen flag of modules that own symbols needed by this. */
2356 for (s = mod->unres; s; s = s->next)
2357 if (s->module)
2358 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002361 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002363 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (!s->module)
2365 continue;
2366
2367 if (s->module->seen)
2368 continue;
2369
2370 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002371 p = strrchr(s->module->name, '/');
2372 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002373 p++;
2374 else
2375 p = s->module->name;
2376 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 first = 0;
2378 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002379 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002382static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 if (mod->srcversion[0]) {
2385 buf_printf(b, "\n");
2386 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2387 mod->srcversion);
2388 }
2389}
2390
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002391static void write_buf(struct buffer *b, const char *fname)
2392{
2393 FILE *file;
2394
2395 file = fopen(fname, "w");
2396 if (!file) {
2397 perror(fname);
2398 exit(1);
2399 }
2400 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2401 perror(fname);
2402 exit(1);
2403 }
2404 if (fclose(file) != 0) {
2405 perror(fname);
2406 exit(1);
2407 }
2408}
2409
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002410static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
2412 char *tmp;
2413 FILE *file;
2414 struct stat st;
2415
2416 file = fopen(fname, "r");
2417 if (!file)
2418 goto write;
2419
2420 if (fstat(fileno(file), &st) < 0)
2421 goto close_write;
2422
2423 if (st.st_size != b->pos)
2424 goto close_write;
2425
2426 tmp = NOFAIL(malloc(b->pos));
2427 if (fread(tmp, 1, b->pos, file) != b->pos)
2428 goto free_write;
2429
2430 if (memcmp(tmp, b->p, b->pos) != 0)
2431 goto free_write;
2432
2433 free(tmp);
2434 fclose(file);
2435 return;
2436
2437 free_write:
2438 free(tmp);
2439 close_write:
2440 fclose(file);
2441 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002442 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
Ram Paibd5cbce2006-06-08 22:12:53 -07002445/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002446 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002447 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002448static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002450 char *buf, *pos, *line;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002452 buf = read_text_file(fname);
2453 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 /* No symbol versions, silently ignore */
2455 return;
2456
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002457 pos = buf;
2458
2459 while ((line = get_line(&pos))) {
Jessica Yu51900442020-03-11 18:01:20 +01002460 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 unsigned int crc;
2462 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002463 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 if (!(symname = strchr(line, '\t')))
2466 goto fail;
2467 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002468 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 goto fail;
2470 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002471 if (!(export = strchr(modname, '\t')))
2472 goto fail;
2473 *export++ = '\0';
2474 if (!(namespace = strchr(export, '\t')))
2475 goto fail;
2476 *namespace++ = '\0';
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 crc = strtoul(line, &d, 16);
2479 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2480 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002481 mod = find_module(modname);
2482 if (!mod) {
2483 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002485 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 mod->skip = 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002487 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002489 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002490 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002491 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002492 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002494 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return;
2496fail:
Masahiro Yamada70f30cf2020-06-01 14:57:20 +09002497 free(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 fatal("parse error in symbol dump file\n");
2499}
2500
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002501/* For normal builds always dump all symbols.
2502 * For external modules only dump symbols
2503 * that are not read from kernel Module.symvers.
2504 **/
2505static int dump_sym(struct symbol *sym)
2506{
2507 if (!external_module)
2508 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002509 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002510 return 0;
2511 return 1;
2512}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002513
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002514static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
2516 struct buffer buf = { };
2517 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002518 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 int n;
2520
2521 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2522 symbol = symbolhash[n];
2523 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002524 if (dump_sym(symbol)) {
2525 namespace = symbol->namespace;
2526 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2527 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002528 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002529 export_str(symbol->export),
2530 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 symbol = symbol->next;
2533 }
2534 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002535 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002536 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002539static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002540{
2541 struct module *mod;
2542 struct namespace_list *ns;
2543 struct buffer ns_deps_buf = {};
2544
2545 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002546
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002547 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002548 continue;
2549
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002550 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002551
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002552 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2553 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002554
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002555 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002556 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002557
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002558 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002559 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002560}
2561
Masahiro Yamada79247992020-06-01 14:57:07 +09002562struct dump_list {
2563 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002564 const char *file;
2565};
2566
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002567int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 struct module *mod;
2570 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002571 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302572 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002574 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002575 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002576 struct dump_list *dump_read_start = NULL;
2577 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002579 while ((opt = getopt(argc, argv, "ei:mnsT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002580 switch (opt) {
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002581 case 'e':
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002582 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002583 break;
2584 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002585 *dump_read_iter =
2586 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2587 (*dump_read_iter)->file = optarg;
2588 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002589 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002590 case 'm':
2591 modversions = 1;
2592 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302593 case 'n':
2594 ignore_missing_files = 1;
2595 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002596 case 'o':
2597 dump_write = optarg;
2598 break;
2599 case 'a':
2600 all_versions = 1;
2601 break;
2602 case 's':
2603 vmlinux_section_warnings = 0;
2604 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302605 case 'T':
2606 files_source = optarg;
2607 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002608 case 'w':
2609 warn_unresolved = 1;
2610 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302611 case 'E':
2612 sec_mismatch_fatal = 1;
2613 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002614 case 'N':
2615 allow_missing_ns_imports = 1;
2616 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002617 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002618 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002619 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002620 default:
2621 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623 }
2624
Masahiro Yamada79247992020-06-01 14:57:07 +09002625 while (dump_read_start) {
2626 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002627
Masahiro Yamada79247992020-06-01 14:57:07 +09002628 read_dump(dump_read_start->file);
2629 tmp = dump_read_start->next;
2630 free(dump_read_start);
2631 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002634 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Rusty Russell712f9b42013-04-04 17:37:38 +10302637 if (files_source)
2638 read_symbols_from_files(files_source);
2639
Masahiro Yamada7e8a3232020-06-01 14:57:13 +09002640 /*
2641 * When there's no vmlinux, don't print warnings about
2642 * unresolved symbols (since there'll be too many ;)
2643 */
2644 if (!have_vmlinux)
2645 warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n");
2646
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002647 err = 0;
2648
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002649 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302650 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002651
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002652 if (mod->skip)
2653 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 buf.pos = 0;
2656
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002657 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002658 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002661 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002662 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002663 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002664 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002665 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 add_moddevtable(&buf, mod);
2667 add_srcversion(&buf, mod);
2668
2669 sprintf(fname, "%s.mod.c", mod->name);
2670 write_if_changed(&buf, fname);
2671 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002672
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002673 if (missing_namespace_deps)
2674 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (dump_write)
2677 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002678 if (sec_mismatch_count && sec_mismatch_fatal)
Jessica Yu93c95e52020-03-06 17:02:05 +01002679 fatal("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002680 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002681 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002682 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002683
Masahiro Yamada47346e92019-09-24 21:07:40 +09002684 for (s = symbolhash[n]; s; s = s->next) {
2685 /*
2686 * Do not check "vmlinux". This avoids the same warnings
2687 * shown twice, and false-positives for ARCH=um.
2688 */
2689 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2690 continue;
2691
Denis Efremov15bfc232019-08-01 09:06:57 +03002692 if (s->is_static)
2693 warn("\"%s\" [%s] is a static %s\n",
2694 s->name, s->module->name,
2695 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002696 }
2697 }
2698
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002699 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002701 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}