blob: b839c48689dff7c8e0cec94aeb38f8377c23a06a [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
115/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static struct module *modules;
117
Masahiro Yamada8b185742018-05-09 18:50:40 +0900118static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 struct module *mod;
121
122 for (mod = modules; mod; mod = mod->next)
123 if (strcmp(mod->name, modname) == 0)
124 break;
125 return mod;
126}
127
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030128static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030131 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 mod = NOFAIL(malloc(sizeof(*mod)));
134 memset(mod, 0, sizeof(*mod));
135 p = NOFAIL(strdup(modname));
136
137 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030138 if (strends(p, ".o")) {
139 p[strlen(p) - 2] = '\0';
140 mod->is_dot_o = 1;
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* add to list */
144 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200145 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 mod->next = modules;
147 modules = mod;
148
149 return mod;
150}
151
152/* A hash of all exported symbols,
153 * struct symbol is also used for lists of unresolved symbols */
154
155#define SYMBOL_HASH_SIZE 1024
156
157struct symbol {
158 struct symbol *next;
159 struct module *module;
160 unsigned int crc;
161 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900162 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 unsigned int weak:1;
Denis Efremov15bfc232019-08-01 09:06:57 +0300164 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700165 enum export export; /* Type of export */
Gustavo A. R. Silva859c8172020-05-07 13:56:01 -0500166 char name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
169static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
170
171/* This is based on the hash agorithm from gdbm, via tdb */
172static inline unsigned int tdb_hash(const char *name)
173{
174 unsigned value; /* Used to compute the hash value. */
175 unsigned i; /* Used to cycle through random values. */
176
177 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100178 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
180
181 return (1103515243 * value + 12345);
182}
183
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100184/**
185 * Allocate a new symbols for use in the hash of exported symbols or
186 * the list of unresolved symbols per module
187 **/
188static struct symbol *alloc_symbol(const char *name, unsigned int weak,
189 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
192
193 memset(s, 0, sizeof(*s));
194 strcpy(s->name, name);
195 s->weak = weak;
196 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300197 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return s;
199}
200
201/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700202static struct symbol *new_symbol(const char *name, struct module *module,
203 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900208 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
209
210 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100213static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct symbol *s;
216
217 /* For our purposes, .foo matches foo. PPC64 needs this. */
218 if (name[0] == '.')
219 name++;
220
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100221 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (strcmp(s->name, name) == 0)
223 return s;
224 }
225 return NULL;
226}
227
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100228static bool contains_namespace(struct namespace_list *list,
229 const char *namespace)
230{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900231 for (; list; list = list->next)
232 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100233 return true;
234
235 return false;
236}
237
238static void add_namespace(struct namespace_list **list, const char *namespace)
239{
240 struct namespace_list *ns_entry;
241
242 if (!contains_namespace(*list, namespace)) {
243 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
244 strlen(namespace) + 1));
245 strcpy(ns_entry->namespace, namespace);
246 ns_entry->next = *list;
247 *list = ns_entry;
248 }
249}
250
251static bool module_imports_namespace(struct module *module,
252 const char *namespace)
253{
254 return contains_namespace(module->imported_namespaces, namespace);
255}
256
Mathias Krause7a3ee752014-08-27 20:28:53 +0930257static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700258 const char *str;
259 enum export export;
260} export_list[] = {
261 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200262 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700263 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200264 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700265 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
266 { .str = "(unknown)", .export = export_unknown },
267};
268
269
270static const char *export_str(enum export ex)
271{
272 return export_list[ex].str;
273}
274
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100275static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700276{
277 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100278
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200279 if (!s)
280 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700281 for (i = 0; export_list[i].export != export_unknown; i++) {
282 if (strcmp(export_list[i].str, s) == 0)
283 return export_list[i].export;
284 }
285 return export_unknown;
286}
287
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900288static void *sym_get_data_by_offset(const struct elf_info *info,
289 unsigned int secindex, unsigned long offset)
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900290{
Xiao Yang4b8a5cf2020-03-18 18:34:16 +0800291 Elf_Shdr *sechdr = &info->sechdrs[secindex];
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900292
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900293 if (info->hdr->e_type != ET_REL)
294 offset -= sechdr->sh_addr;
295
296 return (void *)info->hdr + sechdr->sh_offset + offset;
297}
298
Masahiro Yamadad2e4d052020-05-25 14:47:04 +0900299static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
300{
301 return sym_get_data_by_offset(info, get_secindex(info, sym),
302 sym->st_value);
303}
304
Masahiro Yamada565587d2020-05-25 14:47:05 +0900305static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr)
306{
307 return sym_get_data_by_offset(info, info->secindex_strings,
308 sechdr->sh_name);
309}
310
311static const char *sec_name(const struct elf_info *info, int secindex)
312{
313 return sech_name(info, &info->sechdrs[secindex]);
314}
315
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200316#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
317
318static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
319{
320 const char *secname = sec_name(elf, sec);
321
322 if (strstarts(secname, "___ksymtab+"))
323 return export_plain;
324 else if (strstarts(secname, "___ksymtab_unused+"))
325 return export_unused;
326 else if (strstarts(secname, "___ksymtab_gpl+"))
327 return export_gpl;
328 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
329 return export_unused_gpl;
330 else if (strstarts(secname, "___ksymtab_gpl_future+"))
331 return export_gpl_future;
332 else
333 return export_unknown;
334}
335
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200336static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700337{
338 if (sec == elf->export_sec)
339 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200340 else if (sec == elf->export_unused_sec)
341 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700342 else if (sec == elf->export_gpl_sec)
343 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200344 else if (sec == elf->export_unused_gpl_sec)
345 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700346 else if (sec == elf->export_gpl_future_sec)
347 return export_gpl_future;
348 else
349 return export_unknown;
350}
351
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900352static const char *namespace_from_kstrtabns(const struct elf_info *info,
353 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100354{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900355 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100356 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100357}
358
Matthias Maennicha2b11182019-10-18 10:31:40 +0100359static void sym_update_namespace(const char *symname, const char *namespace)
360{
361 struct symbol *s = find_symbol(symname);
362
363 /*
364 * That symbol should have been created earlier and thus this is
365 * actually an assertion.
366 */
367 if (!s) {
368 merror("Could not update namespace(%s) for symbol %s\n",
369 namespace, symname);
370 return;
371 }
372
373 free(s->namespace);
374 s->namespace =
375 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
376}
377
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100378/**
379 * Add an exported symbol - it may have already been added without a
380 * CRC, in this case just update the CRC
381 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100382static struct symbol *sym_add_exported(const char *name, struct module *mod,
383 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct symbol *s = find_symbol(name);
386
387 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700388 s = new_symbol(name, mod, export);
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900389 } else if (!external_module || is_vmlinux(s->module->name) ||
390 s->module == mod) {
391 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
392 mod->name, name, s->module->name,
393 is_vmlinux(s->module->name) ? "" : ".ko");
394 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900396
397 s->module = mod;
Ram Paibd5cbce2006-06-08 22:12:53 -0700398 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100399 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Masahiro Yamada17436942019-11-15 02:42:24 +0900402static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100403{
404 struct symbol *s = find_symbol(name);
405
Masahiro Yamada17436942019-11-15 02:42:24 +0900406 /*
407 * Ignore stand-alone __crc_*, which might be auto-generated symbols
408 * such as __*_veneer in ARM ELF.
409 */
410 if (!s)
411 return;
412
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100413 s->crc = crc;
414 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100417void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930420 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int fd;
422
423 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930424 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930426 if (fstat(fd, &st))
427 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 *size = st.st_size;
430 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930432failed:
433 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (map == MAP_FAILED)
435 return NULL;
436 return map;
437}
438
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100439/**
440 * Return a copy of the next line in a mmap'ed file.
441 * spaces in the beginning of the line is trimmed away.
442 * Return a pointer to a static buffer.
443 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100444char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 static char line[4096];
447 int skip = 1;
448 size_t len = 0;
449 signed char *p = (signed char *)file + *pos;
450 char *s = line;
451
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100452 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (skip && isspace(*p)) {
454 p++;
455 continue;
456 }
457 skip = 0;
458 if (*p != '\n' && (*pos < size)) {
459 len++;
460 *s++ = *p++;
461 if (len > 4095)
462 break; /* Too long, stop */
463 } else {
464 /* End of string */
465 *s = '\0';
466 return line;
467 }
468 }
469 /* End of buffer */
470 return NULL;
471}
472
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100473void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 munmap(file, size);
476}
477
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100478static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100481 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 Elf_Shdr *sechdrs;
483 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200484 const char *secstrings;
485 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 hdr = grab_file(filename, &info->size);
488 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930489 if (ignore_missing_files) {
490 fprintf(stderr, "%s: %s (ignored)\n", filename,
491 strerror(errno));
492 return 0;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200495 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100498 if (info->size < sizeof(*hdr)) {
499 /* file too small, assume this is an empty .o file */
500 return 0;
501 }
502 /* Is this a valid ELF file? */
503 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
504 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
505 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
506 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
507 /* Not an ELF file - silently ignore it */
508 return 0;
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200511 hdr->e_type = TO_NATIVE(hdr->e_type);
512 hdr->e_machine = TO_NATIVE(hdr->e_machine);
513 hdr->e_version = TO_NATIVE(hdr->e_version);
514 hdr->e_entry = TO_NATIVE(hdr->e_entry);
515 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
516 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
517 hdr->e_flags = TO_NATIVE(hdr->e_flags);
518 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
519 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
520 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
521 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
522 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
523 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 sechdrs = (void *)hdr + hdr->e_shoff;
525 info->sechdrs = sechdrs;
526
Petr Stetiara83710e2007-08-27 12:15:07 +0200527 /* Check if file offset is correct */
528 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100529 fatal("section header offset=%lu in file '%s' is bigger than "
530 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
531 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200532 return 0;
533 }
534
Anders Kaseorg68457562011-05-19 16:55:27 -0600535 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200536 /*
537 * There are more than 64k sections,
538 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200539 */
540 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
541 }
542 else {
543 info->num_sections = hdr->e_shnum;
544 }
545 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600546 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200547 }
548 else {
549 info->secindex_strings = hdr->e_shstrndx;
550 }
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200553 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200554 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
555 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
556 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
557 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
558 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
559 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
560 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
561 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
562 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
563 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200566 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
567 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700568 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900569 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Tejun Heo56fc82c2009-02-06 00:48:02 +0900571 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100572 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
573 "sizeof(*hrd)=%zu\n", filename,
574 (unsigned long)sechdrs[i].sh_offset,
575 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100576 return 0;
577 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700578 secname = secstrings + sechdrs[i].sh_name;
579 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900580 if (nobits)
581 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
583 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700584 } else if (strcmp(secname, "__ksymtab") == 0)
585 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200586 else if (strcmp(secname, "__ksymtab_unused") == 0)
587 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700588 else if (strcmp(secname, "__ksymtab_gpl") == 0)
589 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200590 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
591 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700592 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
593 info->export_gpl_future_sec = i;
594
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200595 if (sechdrs[i].sh_type == SHT_SYMTAB) {
596 unsigned int sh_link_idx;
597 symtab_idx = i;
598 info->symtab_start = (void *)hdr +
599 sechdrs[i].sh_offset;
600 info->symtab_stop = (void *)hdr +
601 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600602 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200603 info->strtab = (void *)hdr +
604 sechdrs[sh_link_idx].sh_offset;
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200607 /* 32bit section no. table? ("more than 64k sections") */
608 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
609 symtab_shndx_idx = i;
610 info->symtab_shndx_start = (void *)hdr +
611 sechdrs[i].sh_offset;
612 info->symtab_shndx_stop = (void *)hdr +
613 sechdrs[i].sh_offset + sechdrs[i].sh_size;
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100616 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100617 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Fix endianness in symbols */
620 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
621 sym->st_shndx = TO_NATIVE(sym->st_shndx);
622 sym->st_name = TO_NATIVE(sym->st_name);
623 sym->st_value = TO_NATIVE(sym->st_value);
624 sym->st_size = TO_NATIVE(sym->st_size);
625 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200626
627 if (symtab_shndx_idx != ~0U) {
628 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600629 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200630 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600631 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200632 symtab_idx);
633 /* Fix endianness */
634 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
635 p++)
636 *p = TO_NATIVE(*p);
637 }
638
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100639 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100642static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 release_file(info->hdr, info->size);
645}
646
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200647static int ignore_undef_symbol(struct elf_info *info, const char *symname)
648{
649 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900650 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200651 return 1;
652 /* ignore global offset table */
653 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
654 return 1;
655 if (info->hdr->e_machine == EM_PPC)
656 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900657 if (strstarts(symname, "_restgpr_") ||
658 strstarts(symname, "_savegpr_") ||
659 strstarts(symname, "_rest32gpr_") ||
660 strstarts(symname, "_save32gpr_") ||
661 strstarts(symname, "_restvr_") ||
662 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200663 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000664 if (info->hdr->e_machine == EM_PPC64)
665 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900666 if (strstarts(symname, "_restgpr0_") ||
667 strstarts(symname, "_savegpr0_") ||
668 strstarts(symname, "_restvr_") ||
669 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100670 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000671 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200672 /* Do not ignore this symbol */
673 return 0;
674}
675
Masahiro Yamada17436942019-11-15 02:42:24 +0900676static void handle_modversion(const struct module *mod,
677 const struct elf_info *info,
678 const Elf_Sym *sym, const char *symname)
679{
680 unsigned int crc;
681
682 if (sym->st_shndx == SHN_UNDEF) {
683 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
684 symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
685 return;
686 }
687
688 if (sym->st_shndx == SHN_ABS) {
689 crc = sym->st_value;
690 } else {
691 unsigned int *crcp;
692
693 /* symbol points to the CRC in the ELF object */
694 crcp = sym_get_data(info, sym);
695 crc = TO_NATIVE(*crcp);
696 }
697 sym_set_crc(symname, crc);
698}
699
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900700static void handle_symbol(struct module *mod, struct elf_info *info,
701 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200703 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900704 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200705
Frank Rowand258f7422012-04-09 17:59:03 -0700706 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900707 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200708 export = export_from_secname(info, get_secindex(info, sym));
709 else
710 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 switch (sym->st_shndx) {
713 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900714 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100715 /* Should warn here, but modpost runs before the linker */
716 } else
717 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 case SHN_UNDEF:
720 /* undefined symbol */
721 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
722 ELF_ST_BIND(sym->st_info) != STB_WEAK)
723 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200724 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (info->hdr->e_machine == EM_SPARC ||
727 info->hdr->e_machine == EM_SPARCV9) {
728 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700729 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100731 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700732 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100733 munged[0] = '_';
734 munged[1] = toupper(munged[1]);
735 symname = munged;
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100738
Rusty Russellb92021b2013-03-15 15:04:17 +1030739 mod->unres = alloc_symbol(symname,
740 ELF_ST_BIND(sym->st_info) == STB_WEAK,
741 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743 default:
744 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900745 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100746 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100747 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900749 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900751 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 mod->has_cleanup = 1;
753 break;
754 }
755}
756
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100757/**
758 * Parse tag=value strings from .modinfo section
759 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static char *next_string(char *string, unsigned long *secsize)
761{
762 /* Skip non-zero chars */
763 while (string[0]) {
764 string++;
765 if ((*secsize)-- <= 1)
766 return NULL;
767 }
768
769 /* Skip any zero padding. */
770 while (!string[0]) {
771 string++;
772 if ((*secsize)-- <= 1)
773 return NULL;
774 }
775 return string;
776}
777
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900778static char *get_next_modinfo(struct elf_info *info, const char *tag,
779 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 char *p;
782 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900783 char *modinfo = info->modinfo;
784 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900786 if (prev) {
787 size -= prev - modinfo;
788 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200789 }
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 for (p = modinfo; p; p = next_string(p, &size)) {
792 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
793 return p + taglen + 1;
794 }
795 return NULL;
796}
797
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900798static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200799
800{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900801 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200802}
803
Sam Ravnborg93684d32006-02-19 11:53:35 +0100804/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100805 * Test if string s ends in string sub
806 * return 0 if match
807 **/
808static int strrcmp(const char *s, const char *sub)
809{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100810 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100811
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100812 if (!s || !sub)
813 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100814
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100815 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100816 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100817
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100818 if ((slen == 0) || (sublen == 0))
819 return 1;
820
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100821 if (sublen > slen)
822 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100823
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100824 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100825}
826
Sam Ravnborgff13f922008-01-23 19:54:27 +0100827static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
828{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100829 if (sym)
830 return elf->strtab + sym->st_name;
831 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100832 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100833}
834
Sam Ravnborg10668222008-01-13 22:21:31 +0100835/* The pattern is an array of simple patterns.
836 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100837 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100838 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930839 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100840 */
Trevor Keith5c725132009-09-22 16:43:38 -0700841static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100842{
843 const char *p;
844 while (*pat) {
845 p = *pat++;
846 const char *endp = p + strlen(p) - 1;
847
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930848 /* "*foo*" */
849 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300850 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
851 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930852
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930853 free(bare);
854 if (here != NULL)
855 return 1;
856 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100857 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930858 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100859 if (strrcmp(sym, p + 1) == 0)
860 return 1;
861 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100862 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100863 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100864 if (strncmp(sym, p, strlen(p) - 1) == 0)
865 return 1;
866 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100867 /* no wildcards */
868 else {
869 if (strcmp(p, sym) == 0)
870 return 1;
871 }
872 }
873 /* no match */
874 return 0;
875}
876
Sam Ravnborg10668222008-01-13 22:21:31 +0100877/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930878static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200879{
880 ".comment*",
881 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930882 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800883 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000884 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200885 ".mdebug*", /* alpha, score, mips etc. */
886 ".pdr", /* alpha, score, mips etc. */
887 ".stab*",
888 ".note*",
889 ".got*",
890 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400891 ".xt.prop", /* xtensa */
892 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030893 ".arcextmap*", /* arc */
894 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030895 ".cmem*", /* EZchip */
896 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100897 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600898 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200899 NULL
900};
Sam Ravnborg10668222008-01-13 22:21:31 +0100901
Sam Ravnborge241a632008-01-28 20:13:13 +0100902/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400903 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100904 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400905 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100906 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400907static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900908 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100909{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400910 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100911
Anders Kaseorgb614a692009-04-23 16:49:33 -0400912 if (sechdr->sh_type == SHT_PROGBITS &&
913 !(sechdr->sh_flags & SHF_ALLOC) &&
914 !match(sec, section_white_list)) {
915 warn("%s (%s): unexpected non-allocatable section.\n"
916 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
917 "Note that for example <linux/init.h> contains\n"
918 "section definitions for use in .S files.\n\n",
919 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100920 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100921}
922
923
924
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100925#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930926 ".init.setup", ".init.rodata", ".meminit.rodata", \
927 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100928#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930929 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100930
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100931#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930932 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100933#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930934 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100935
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200936#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930937 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
938 ".pci_fixup_enable", ".pci_fixup_resume", \
939 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200940
Paul Gortmakere24f6622013-06-19 19:30:48 -0400941#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
942#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100943
944#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
945#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100946
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930947#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930948#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700949 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930950#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400951 ".fixup", ".entry.text", ".exception.text", ".text.*", \
952 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100953
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000954#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000955#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100956
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000957#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000958#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100959
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930960#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
961 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
962
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100963/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930964static const char *const init_data_sections[] =
965 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100966
967/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930968static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100969
970/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930971static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100972 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100973
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930974/* all text sections */
975static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
976
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100977/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930978static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100979
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100980
981/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100982#define DEFAULT_SYMBOL_WHITE_LIST \
983 "*driver", \
984 "*_template", /* scsi uses *_template a lot */ \
985 "*_timer", /* arm uses ops structures named _timer a lot */ \
986 "*_sht", /* scsi also used *_sht to some extent */ \
987 "*_ops", \
988 "*_probe", \
989 "*_probe_one", \
990 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100991
Mathias Krause7a3ee752014-08-27 20:28:53 +0930992static const char *const head_sections[] = { ".head.text*", NULL };
993static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100994 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930995static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100996
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100997enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100998 TEXT_TO_ANY_INIT,
999 DATA_TO_ANY_INIT,
1000 TEXT_TO_ANY_EXIT,
1001 DATA_TO_ANY_EXIT,
1002 XXXINIT_TO_SOME_INIT,
1003 XXXEXIT_TO_SOME_EXIT,
1004 ANY_INIT_TO_ANY_EXIT,
1005 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001006 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301007 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001008};
1009
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301010/**
1011 * Describe how to match sections on different criterias:
1012 *
1013 * @fromsec: Array of sections to be matched.
1014 *
1015 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1016 * this array is forbidden (black-list). Can be empty.
1017 *
1018 * @good_tosec: Relocations applied to a section in @fromsec must be
1019 * targetting sections in this array (white-list). Can be empty.
1020 *
1021 * @mismatch: Type of mismatch.
1022 *
1023 * @symbol_white_list: Do not match a relocation to a symbol in this list
1024 * even if it is targetting a section in @bad_to_sec.
1025 *
1026 * @handler: Specific handler to call when a match is found. If NULL,
1027 * default_mismatch_handler() will be called.
1028 *
1029 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001030struct sectioncheck {
1031 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301032 const char *bad_tosec[20];
1033 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001034 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001035 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301036 void (*handler)(const char *modname, struct elf_info *elf,
1037 const struct sectioncheck* const mismatch,
1038 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1039
Sam Ravnborg10668222008-01-13 22:21:31 +01001040};
1041
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301042static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1043 const struct sectioncheck* const mismatch,
1044 Elf_Rela *r, Elf_Sym *sym,
1045 const char *fromsec);
1046
Mathias Krause7a3ee752014-08-27 20:28:53 +09301047static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001048/* Do not reference init/exit code/data from
1049 * normal code and data
1050 */
1051{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001052 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301053 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001054 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001055 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001056},
1057{
1058 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301059 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001060 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001061 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001062},
1063{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001064 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301065 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001066 .mismatch = DATA_TO_ANY_INIT,
1067 .symbol_white_list = {
1068 "*_template", "*_timer", "*_sht", "*_ops",
1069 "*_probe", "*_probe_one", "*_console", NULL
1070 },
1071},
1072{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001073 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301074 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001075 .mismatch = TEXT_TO_ANY_EXIT,
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{
1079 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301080 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001081 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001082 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001083},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001084/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001085{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001086 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301087 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001088 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001089 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001090},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001091/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001092{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001093 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301094 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001095 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001096 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001097},
1098/* Do not use exit code/data from init code */
1099{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001100 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301101 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001102 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001103 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001104},
1105/* Do not use init code/data from exit code */
1106{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001107 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301108 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001109 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001110 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001111},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001112{
1113 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301114 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001115 .mismatch = ANY_INIT_TO_ANY_EXIT,
1116 .symbol_white_list = { NULL },
1117},
Sam Ravnborg10668222008-01-13 22:21:31 +01001118/* Do not export init/exit functions or data */
1119{
1120 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301121 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001122 .mismatch = EXPORT_TO_INIT_EXIT,
1123 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301124},
1125{
1126 .fromsec = { "__ex_table", NULL },
1127 /* If you're adding any new black-listed sections in here, consider
1128 * adding a special 'printer' for them in scripts/check_extable.
1129 */
1130 .bad_tosec = { ".altinstr_replacement", NULL },
1131 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1132 .mismatch = EXTABLE_TO_NON_TEXT,
1133 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001134}
1135};
1136
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001137static const struct sectioncheck *section_mismatch(
1138 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001139{
1140 int i;
1141 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1142 const struct sectioncheck *check = &sectioncheck[0];
1143
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301144 /*
1145 * The target section could be the SHT_NUL section when we're
1146 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001147 * doesn't make much sense and causes build failures on parisc
1148 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301149 */
1150 if (*tosec == '\0')
1151 return NULL;
1152
Sam Ravnborg10668222008-01-13 22:21:31 +01001153 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301154 if (match(fromsec, check->fromsec)) {
1155 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1156 return check;
1157 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1158 return check;
1159 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001160 check++;
1161 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001162 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001163}
1164
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001165/**
1166 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001167 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001168 * Pattern 1:
1169 * If a module parameter is declared __initdata and permissions=0
1170 * then this is legal despite the warning generated.
1171 * We cannot see value of permissions here, so just ignore
1172 * this pattern.
1173 * The pattern is identified by:
1174 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001175 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001176 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001177 *
Rusty Russell6a841522010-08-11 23:04:16 -06001178 * Pattern 1a:
1179 * module_param_call() ops can refer to __init set function if permissions=0
1180 * The pattern is identified by:
1181 * tosec = .init.text
1182 * fromsec = .data*
1183 * atsym = __param_ops_*
1184 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001185 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001186 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001187 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001188 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001189 * tosec = init or exit section
1190 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001191 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1192 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001193 *
1194 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001195 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001196 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001197 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001198 * Some symbols belong to init section but still it is ok to reference
1199 * these from non-init sections as these symbols don't have any memory
1200 * allocated for them and symbol address and value are same. So even
1201 * if init section is freed, its ok to reference those symbols.
1202 * For ex. symbols marking the init section boundaries.
1203 * This pattern is identified by
1204 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001205 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301206 * Pattern 5:
1207 * GCC may optimize static inlines when fed constant arg(s) resulting
1208 * in functions like cpumask_empty() -- generating an associated symbol
1209 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1210 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1211 * meaningless section warning. May need to add isra symbols too...
1212 * This pattern is identified by
1213 * tosec = init section
1214 * fromsec = text section
1215 * refsymname = *.constprop.*
1216 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001217 * Pattern 6:
1218 * Hide section mismatch warnings for ELF local symbols. The goal
1219 * is to eliminate false positive modpost warnings caused by
1220 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1221 * Autogenerated symbol names bypass modpost's "Pattern 2"
1222 * whitelisting, which relies on pattern-matching against symbol
1223 * names to work. (One situation where gcc can autogenerate ELF
1224 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001225 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001226static int secref_whitelist(const struct sectioncheck *mismatch,
1227 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001228 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001229{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001230 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001231 if (match(tosec, init_data_sections) &&
1232 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001233 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001234 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001235
Rusty Russell6a841522010-08-11 23:04:16 -06001236 /* Check for pattern 1a */
1237 if (strcmp(tosec, ".init.text") == 0 &&
1238 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001239 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001240 return 0;
1241
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001242 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001243 if (match(tosec, init_exit_sections) &&
1244 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001245 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001246 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001247
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001248 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001249 if (match(fromsec, head_sections) &&
1250 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001251 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001252
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001253 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001254 if (match(tosym, linker_symbols))
1255 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001256
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301257 /* Check for pattern 5 */
1258 if (match(fromsec, text_sections) &&
1259 match(tosec, init_sections) &&
1260 match(fromsym, optim_symbols))
1261 return 0;
1262
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001263 /* Check for pattern 6 */
1264 if (strstarts(fromsym, ".L"))
1265 return 0;
1266
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001267 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001268}
1269
Sami Tolvanen5818c682018-10-23 15:15:35 -07001270static inline int is_arm_mapping_symbol(const char *str)
1271{
1272 return str[0] == '$' && strchr("axtd", str[1])
1273 && (str[2] == '\0' || str[2] == '.');
1274}
1275
1276/*
1277 * If there's no name there, ignore it; likewise, ignore it if it's
1278 * one of the magic symbols emitted used by current ARM tools.
1279 *
1280 * Otherwise if find_symbols_between() returns those symbols, they'll
1281 * fail the whitelist tests and cause lots of false alarms ... fixable
1282 * only by merging __exit and __init sections into __text, bloating
1283 * the kernel (which is especially evil on embedded platforms).
1284 */
1285static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1286{
1287 const char *name = elf->strtab + sym->st_name;
1288
1289 if (!name || !strlen(name))
1290 return 0;
1291 return !is_arm_mapping_symbol(name);
1292}
1293
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001294/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001295 * Find symbol based on relocation record info.
1296 * In some cases the symbol supplied is a valid symbol so
1297 * return refsym. If st_name != 0 we assume this is a valid symbol.
1298 * In other cases the symbol needs to be looked up in the symbol table
1299 * based on section and address.
1300 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001301static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001302 Elf_Sym *relsym)
1303{
1304 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001305 Elf_Sym *near = NULL;
1306 Elf64_Sword distance = 20;
1307 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001308 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001309
1310 if (relsym->st_name != 0)
1311 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001312
1313 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001314 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001315 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001316 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001317 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1318 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001319 if (!is_valid_name(elf, sym))
1320 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001321 if (sym->st_value == addr)
1322 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001323 /* Find a symbol nearby - addr are maybe negative */
1324 d = sym->st_value - addr;
1325 if (d < 0)
1326 d = addr - sym->st_value;
1327 if (d < distance) {
1328 distance = d;
1329 near = sym;
1330 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001331 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001332 /* We need a close match */
1333 if (distance < 20)
1334 return near;
1335 else
1336 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001337}
1338
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001339/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001340 * Find symbols before or equal addr and after addr - in the section sec.
1341 * If we find two symbols with equal offset prefer one with a valid name.
1342 * The ELF format may have a better way to detect what type of symbol
1343 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001344 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001345static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1346 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001347{
1348 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001349 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001350 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001351
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001352 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1353 const char *symsec;
1354
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001355 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001356 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001357 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001358 if (strcmp(symsec, sec) != 0)
1359 continue;
David Brownellda68d612007-02-20 13:58:16 -08001360 if (!is_valid_name(elf, sym))
1361 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001362 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001363 if ((addr - sym->st_value) < distance) {
1364 distance = addr - sym->st_value;
1365 near = sym;
1366 } else if ((addr - sym->st_value) == distance) {
1367 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001368 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001369 }
1370 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001371 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001372}
1373
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001374/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001375 * Convert a section name to the function/data attribute
1376 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001377 * .memexitconst => __memconst
1378 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001379 *
1380 * The memory of returned value has been allocated on a heap. The user of this
1381 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001382*/
1383static char *sec2annotation(const char *s)
1384{
1385 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001386 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001387 char *r = p;
1388
1389 *p++ = '_';
1390 *p++ = '_';
1391 if (*s == '.')
1392 s++;
1393 while (*s && *s != '.')
1394 *p++ = *s++;
1395 *p = '\0';
1396 if (*s == '.')
1397 s++;
1398 if (strstr(s, "rodata") != NULL)
1399 strcat(p, "const ");
1400 else if (strstr(s, "data") != NULL)
1401 strcat(p, "data ");
1402 else
1403 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001404 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001405 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001406 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001407 }
1408}
1409
1410static int is_function(Elf_Sym *sym)
1411{
1412 if (sym)
1413 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1414 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001415 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001416}
1417
Randy Dunlap00759c02011-03-15 14:13:47 -07001418static void print_section_list(const char * const list[20])
1419{
1420 const char *const *s = list;
1421
1422 while (*s) {
1423 fprintf(stderr, "%s", *s);
1424 s++;
1425 if (*s)
1426 fprintf(stderr, ", ");
1427 }
1428 fprintf(stderr, "\n");
1429}
1430
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301431static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1432{
1433 switch (is_func) {
1434 case 0: *name = "variable"; *name_p = ""; break;
1435 case 1: *name = "function"; *name_p = "()"; break;
1436 default: *name = "(unknown reference)"; *name_p = ""; break;
1437 }
1438}
1439
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001440/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001441 * Print a warning about a section mismatch.
1442 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001443 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001444 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001445static void report_sec_mismatch(const char *modname,
1446 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001447 const char *fromsec,
1448 unsigned long long fromaddr,
1449 const char *fromsym,
1450 int from_is_func,
1451 const char *tosec, const char *tosym,
1452 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001453{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001454 const char *from, *from_p;
1455 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001456 char *prl_from;
1457 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001458
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001459 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001460
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301461 get_pretty_name(from_is_func, &from, &from_p);
1462 get_pretty_name(to_is_func, &to, &to_p);
1463
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001464 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1465 "to the %s %s:%s%s\n",
1466 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1467 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001468
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001469 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001470 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001471 prl_from = sec2annotation(fromsec);
1472 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001473 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001474 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001475 "the %s %s%s%s.\n"
1476 "This is often because %s lacks a %s\n"
1477 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001478 prl_from, fromsym,
1479 to, prl_to, tosym, to_p,
1480 fromsym, prl_to, tosym);
1481 free(prl_from);
1482 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001483 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001484 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001485 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001486 fprintf(stderr,
1487 "The variable %s references\n"
1488 "the %s %s%s%s\n"
1489 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001490 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001491 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001492 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001493 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001494 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001495 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001496 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001497 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001498 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001499 fprintf(stderr,
1500 "The function %s() references a %s in an exit section.\n"
1501 "Often the %s %s%s has valid usage outside the exit section\n"
1502 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001503 fromsym, to, to, tosym, to_p, prl_to, tosym);
1504 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001505 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001506 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001507 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001508 fprintf(stderr,
1509 "The variable %s references\n"
1510 "the %s %s%s%s\n"
1511 "If the reference is valid then annotate the\n"
1512 "variable with __exit* (see linux/init.h) or "
1513 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001514 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001515 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001516 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001517 break;
1518 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001519 case XXXINIT_TO_SOME_INIT:
1520 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001521 prl_from = sec2annotation(fromsec);
1522 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001523 fprintf(stderr,
1524 "The %s %s%s%s references\n"
1525 "a %s %s%s%s.\n"
1526 "If %s is only used by %s then\n"
1527 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001528 from, prl_from, fromsym, from_p,
1529 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001530 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001531 free(prl_from);
1532 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001533 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001534 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001535 prl_from = sec2annotation(fromsec);
1536 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001537 fprintf(stderr,
1538 "The %s %s%s%s references\n"
1539 "a %s %s%s%s.\n"
1540 "This is often seen when error handling "
1541 "in the init function\n"
1542 "uses functionality in the exit path.\n"
1543 "The fix is often to remove the %sannotation of\n"
1544 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001545 from, prl_from, fromsym, from_p,
1546 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001547 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001548 free(prl_from);
1549 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001550 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001551 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001552 prl_from = sec2annotation(fromsec);
1553 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001554 fprintf(stderr,
1555 "The %s %s%s%s references\n"
1556 "a %s %s%s%s.\n"
1557 "This is often seen when error handling "
1558 "in the exit function\n"
1559 "uses functionality in the init path.\n"
1560 "The fix is often to remove the %sannotation of\n"
1561 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001562 from, prl_from, fromsym, from_p,
1563 to, prl_to, tosym, to_p,
1564 prl_to, tosym, to_p);
1565 free(prl_from);
1566 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001567 break;
1568 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001569 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001570 fprintf(stderr,
1571 "The symbol %s is exported and annotated %s\n"
1572 "Fix this by removing the %sannotation of %s "
1573 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001574 tosym, prl_to, prl_to, tosym);
1575 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001576 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301577 case EXTABLE_TO_NON_TEXT:
1578 fatal("There's a special handler for this mismatch type, "
1579 "we should never get here.");
1580 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001581 }
1582 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001583}
1584
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301585static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1586 const struct sectioncheck* const mismatch,
1587 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1588{
1589 const char *tosec;
1590 Elf_Sym *to;
1591 Elf_Sym *from;
1592 const char *tosym;
1593 const char *fromsym;
1594
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301595 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1596 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301597
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001598 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301599 return;
1600
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301601 tosec = sec_name(elf, get_secindex(elf, sym));
1602 to = find_elf_symbol(elf, r->r_addend, sym);
1603 tosym = sym_name(elf, to);
1604
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301605 /* check whitelist - we may ignore it */
1606 if (secref_whitelist(mismatch,
1607 fromsec, fromsym, tosec, tosym)) {
1608 report_sec_mismatch(modname, mismatch,
1609 fromsec, r->r_offset, fromsym,
1610 is_function(from), tosec, tosym,
1611 is_function(to));
1612 }
1613}
1614
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301615static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1616{
1617 if (section_index > elf->num_sections)
1618 fatal("section_index is outside elf->num_sections!\n");
1619
1620 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1621}
1622
1623/*
1624 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1625 * to know the sizeof(struct exception_table_entry) for the target architecture.
1626 */
1627static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301628static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301629{
1630 /*
1631 * If we're currently checking the second relocation within __ex_table,
1632 * that relocation offset tells us the offsetof(struct
1633 * exception_table_entry, fixup) which is equal to sizeof(struct
1634 * exception_table_entry) divided by two. We use that to our advantage
1635 * since there's no portable way to get that size as every architecture
1636 * seems to go with different sized types. Not pretty but better than
1637 * hard-coding the size for every architecture..
1638 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301639 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301640 extable_entry_size = r->r_offset * 2;
1641}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301642
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301643static inline bool is_extable_fault_address(Elf_Rela *r)
1644{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301645 /*
1646 * extable_entry_size is only discovered after we've handled the
1647 * _second_ relocation in __ex_table, so only abort when we're not
1648 * handling the first reloc and extable_entry_size is zero.
1649 */
1650 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301651 fatal("extable_entry size hasn't been discovered!\n");
1652
1653 return ((r->r_offset == 0) ||
1654 (r->r_offset % extable_entry_size == 0));
1655}
1656
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301657#define is_second_extable_reloc(Start, Cur, Sec) \
1658 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1659
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301660static void report_extable_warnings(const char* modname, struct elf_info* elf,
1661 const struct sectioncheck* const mismatch,
1662 Elf_Rela* r, Elf_Sym* sym,
1663 const char* fromsec, const char* tosec)
1664{
1665 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1666 const char* fromsym_name = sym_name(elf, fromsym);
1667 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1668 const char* tosym_name = sym_name(elf, tosym);
1669 const char* from_pretty_name;
1670 const char* from_pretty_name_p;
1671 const char* to_pretty_name;
1672 const char* to_pretty_name_p;
1673
1674 get_pretty_name(is_function(fromsym),
1675 &from_pretty_name, &from_pretty_name_p);
1676 get_pretty_name(is_function(tosym),
1677 &to_pretty_name, &to_pretty_name_p);
1678
1679 warn("%s(%s+0x%lx): Section mismatch in reference"
1680 " from the %s %s%s to the %s %s:%s%s\n",
1681 modname, fromsec, (long)r->r_offset, from_pretty_name,
1682 fromsym_name, from_pretty_name_p,
1683 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1684
1685 if (!match(tosec, mismatch->bad_tosec) &&
1686 is_executable_section(elf, get_secindex(elf, sym)))
1687 fprintf(stderr,
1688 "The relocation at %s+0x%lx references\n"
1689 "section \"%s\" which is not in the list of\n"
1690 "authorized sections. If you're adding a new section\n"
1691 "and/or if this reference is valid, add \"%s\" to the\n"
1692 "list of authorized sections to jump to on fault.\n"
1693 "This can be achieved by adding \"%s\" to \n"
1694 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1695 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1696}
1697
1698static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1699 const struct sectioncheck* const mismatch,
1700 Elf_Rela* r, Elf_Sym* sym,
1701 const char *fromsec)
1702{
1703 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1704
1705 sec_mismatch_count++;
1706
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001707 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301708
1709 if (match(tosec, mismatch->bad_tosec))
1710 fatal("The relocation at %s+0x%lx references\n"
1711 "section \"%s\" which is black-listed.\n"
1712 "Something is seriously wrong and should be fixed.\n"
1713 "You might get more information about where this is\n"
1714 "coming from by using scripts/check_extable.sh %s\n",
1715 fromsec, (long)r->r_offset, tosec, modname);
1716 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1717 if (is_extable_fault_address(r))
1718 fatal("The relocation at %s+0x%lx references\n"
1719 "section \"%s\" which is not executable, IOW\n"
1720 "it is not possible for the kernel to fault\n"
1721 "at that address. Something is seriously wrong\n"
1722 "and should be fixed.\n",
1723 fromsec, (long)r->r_offset, tosec);
1724 else
1725 fatal("The relocation at %s+0x%lx references\n"
1726 "section \"%s\" which is not executable, IOW\n"
1727 "the kernel will fault if it ever tries to\n"
1728 "jump to it. Something is seriously wrong\n"
1729 "and should be fixed.\n",
1730 fromsec, (long)r->r_offset, tosec);
1731 }
1732}
1733
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001734static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001735 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001736{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001737 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301738 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001739
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001740 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301741 if (mismatch->handler)
1742 mismatch->handler(modname, elf, mismatch,
1743 r, sym, fromsec);
1744 else
1745 default_mismatch_handler(modname, elf, mismatch,
1746 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001747 }
1748}
1749
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001750static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001751 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001752{
Masahiro Yamadad2e4d052020-05-25 14:47:04 +09001753 return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001754}
1755
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001756static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001757{
1758 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001759 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001760
1761 switch (r_typ) {
1762 case R_386_32:
1763 r->r_addend = TO_NATIVE(*location);
1764 break;
1765 case R_386_PC32:
1766 r->r_addend = TO_NATIVE(*location) + 4;
1767 /* For CONFIG_RELOCATABLE=y */
1768 if (elf->hdr->e_type == ET_EXEC)
1769 r->r_addend += r->r_offset;
1770 break;
1771 }
1772 return 0;
1773}
1774
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001775#ifndef R_ARM_CALL
1776#define R_ARM_CALL 28
1777#endif
1778#ifndef R_ARM_JUMP24
1779#define R_ARM_JUMP24 29
1780#endif
1781
David A. Longc9698e52014-02-14 22:41:18 +01001782#ifndef R_ARM_THM_CALL
1783#define R_ARM_THM_CALL 10
1784#endif
1785#ifndef R_ARM_THM_JUMP24
1786#define R_ARM_THM_JUMP24 30
1787#endif
1788#ifndef R_ARM_THM_JUMP19
1789#define R_ARM_THM_JUMP19 51
1790#endif
1791
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001792static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001793{
1794 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1795
1796 switch (r_typ) {
1797 case R_ARM_ABS32:
1798 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001799 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001800 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001801 break;
1802 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001803 case R_ARM_CALL:
1804 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001805 case R_ARM_THM_CALL:
1806 case R_ARM_THM_JUMP24:
1807 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001808 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001809 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001810 sechdr->sh_offset +
1811 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001812 break;
1813 default:
1814 return 1;
1815 }
1816 return 0;
1817}
1818
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001819static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001820{
1821 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001822 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001823 unsigned int inst;
1824
1825 if (r_typ == R_MIPS_HI16)
1826 return 1; /* skip this */
1827 inst = TO_NATIVE(*location);
1828 switch (r_typ) {
1829 case R_MIPS_LO16:
1830 r->r_addend = inst & 0xffff;
1831 break;
1832 case R_MIPS_26:
1833 r->r_addend = (inst & 0x03ffffff) << 2;
1834 break;
1835 case R_MIPS_32:
1836 r->r_addend = inst;
1837 break;
1838 }
1839 return 0;
1840}
1841
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001842static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001843 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001844{
1845 Elf_Sym *sym;
1846 Elf_Rela *rela;
1847 Elf_Rela r;
1848 unsigned int r_sym;
1849 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001850
Sam Ravnborgff13f922008-01-23 19:54:27 +01001851 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001852 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1853
Sam Ravnborgff13f922008-01-23 19:54:27 +01001854 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001855 fromsec += strlen(".rela");
1856 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001857 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001859
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001860 for (rela = start; rela < stop; rela++) {
1861 r.r_offset = TO_NATIVE(rela->r_offset);
1862#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001863 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001864 unsigned int r_typ;
1865 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1866 r_sym = TO_NATIVE(r_sym);
1867 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1868 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1869 } else {
1870 r.r_info = TO_NATIVE(rela->r_info);
1871 r_sym = ELF_R_SYM(r.r_info);
1872 }
1873#else
1874 r.r_info = TO_NATIVE(rela->r_info);
1875 r_sym = ELF_R_SYM(r.r_info);
1876#endif
1877 r.r_addend = TO_NATIVE(rela->r_addend);
1878 sym = elf->symtab_start + r_sym;
1879 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001880 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001881 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301882 if (is_second_extable_reloc(start, rela, fromsec))
1883 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001884 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001885 }
1886}
1887
1888static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001889 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001890{
1891 Elf_Sym *sym;
1892 Elf_Rel *rel;
1893 Elf_Rela r;
1894 unsigned int r_sym;
1895 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001896
Sam Ravnborgff13f922008-01-23 19:54:27 +01001897 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001898 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1899
Sam Ravnborgff13f922008-01-23 19:54:27 +01001900 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001901 fromsec += strlen(".rel");
1902 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001903 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001904 return;
1905
1906 for (rel = start; rel < stop; rel++) {
1907 r.r_offset = TO_NATIVE(rel->r_offset);
1908#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001909 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001910 unsigned int r_typ;
1911 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1912 r_sym = TO_NATIVE(r_sym);
1913 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1914 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1915 } else {
1916 r.r_info = TO_NATIVE(rel->r_info);
1917 r_sym = ELF_R_SYM(r.r_info);
1918 }
1919#else
1920 r.r_info = TO_NATIVE(rel->r_info);
1921 r_sym = ELF_R_SYM(r.r_info);
1922#endif
1923 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001924 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001925 case EM_386:
1926 if (addend_386_rel(elf, sechdr, &r))
1927 continue;
1928 break;
1929 case EM_ARM:
1930 if (addend_arm_rel(elf, sechdr, &r))
1931 continue;
1932 break;
1933 case EM_MIPS:
1934 if (addend_mips_rel(elf, sechdr, &r))
1935 continue;
1936 break;
1937 }
1938 sym = elf->symtab_start + r_sym;
1939 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001940 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001941 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301942 if (is_second_extable_reloc(start, rel, fromsec))
1943 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001944 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001945 }
1946}
1947
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001948/**
1949 * A module includes a number of sections that are discarded
1950 * either when loaded or when used as built-in.
1951 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001952 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001953 * Likewise for modules used built-in the sections marked __exit
1954 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001955 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001956 * The check_sec_ref() function traverses all relocation records
1957 * to find all references to a section that reference a section that will
1958 * be discarded and warns about it.
1959 **/
1960static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001961 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001962{
1963 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001964 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001965
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001966 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001967 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001968 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001969 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001970 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001971 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001972 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001973 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001974 }
1975}
1976
Andi Kleen7d02b492014-02-08 09:01:12 +01001977static char *remove_dot(char *s)
1978{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301979 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001980
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301981 if (n && s[n]) {
1982 size_t m = strspn(s + n + 1, "0123456789");
1983 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001984 s[n] = 0;
1985 }
1986 return s;
1987}
1988
Masahiro Yamada8b185742018-05-09 18:50:40 +09001989static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 const char *symname;
1992 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001993 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001994 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct module *mod;
1996 struct elf_info info = { };
1997 Elf_Sym *sym;
1998
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01001999 if (!parse_elf(&info, modname))
2000 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 mod = new_module(modname);
2003
2004 /* When there's no vmlinux, don't print warnings about
2005 * unresolved symbols (since there'll be too many ;) */
2006 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 mod->skip = 1;
2009 }
2010
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002011 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002012 if (!license && !is_vmlinux(modname))
Jessica Yu93c95e52020-03-06 17:02:05 +01002013 warn("missing MODULE_LICENSE() in %s\n"
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002014 "see include/linux/module.h for "
2015 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002016 while (license) {
2017 if (license_is_gpl_compatible(license))
2018 mod->gpl_compatible = 1;
2019 else {
2020 mod->gpl_compatible = 0;
2021 break;
2022 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002023 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002024 }
2025
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002026 namespace = get_modinfo(&info, "import_ns");
2027 while (namespace) {
2028 add_namespace(&mod->imported_namespaces, namespace);
2029 namespace = get_next_modinfo(&info, "import_ns", namespace);
2030 }
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002033 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002035 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 handle_moddevtable(mod, &info, sym, symname);
2037 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002038
Matthias Maennich69923202019-10-18 10:31:42 +01002039 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2040 symname = remove_dot(info.strtab + sym->st_name);
2041
Masahiro Yamada17436942019-11-15 02:42:24 +09002042 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002043 if (strstarts(symname, "__kstrtabns_"))
2044 sym_update_namespace(symname + strlen("__kstrtabns_"),
2045 namespace_from_kstrtabns(&info,
2046 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002047
2048 if (strstarts(symname, "__crc_"))
2049 handle_modversion(mod, &info, sym,
2050 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002051 }
2052
Denis Efremov15bfc232019-08-01 09:06:57 +03002053 // check for static EXPORT_SYMBOL_* functions && global vars
2054 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2055 unsigned char bind = ELF_ST_BIND(sym->st_info);
2056
2057 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2058 struct symbol *s =
2059 find_symbol(remove_dot(info.strtab +
2060 sym->st_name));
2061
2062 if (s)
2063 s->is_static = 0;
2064 }
2065 }
2066
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002067 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002068 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002070 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (version)
2072 maybe_frob_rcs_version(modname, version, info.modinfo,
2073 version - (char *)info.hdr);
2074 if (version || (all_versions && !is_vmlinux(modname)))
2075 get_src_version(modname, mod->srcversion,
2076 sizeof(mod->srcversion)-1);
2077
2078 parse_elf_finish(&info);
2079
Rusty Russell8c8ef422009-03-31 13:05:34 -06002080 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 * never passed as an argument to an exported function, so
2082 * the automatic versioning doesn't pick it up, but it's really
2083 * important anyhow */
2084 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002085 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Rusty Russell712f9b42013-04-04 17:37:38 +10302088static void read_symbols_from_files(const char *filename)
2089{
2090 FILE *in = stdin;
2091 char fname[PATH_MAX];
2092
2093 if (strcmp(filename, "-") != 0) {
2094 in = fopen(filename, "r");
2095 if (!in)
2096 fatal("Can't open filenames file %s: %m", filename);
2097 }
2098
2099 while (fgets(fname, PATH_MAX, in) != NULL) {
2100 if (strends(fname, "\n"))
2101 fname[strlen(fname)-1] = '\0';
2102 read_symbols(fname);
2103 }
2104
2105 if (in != stdin)
2106 fclose(in);
2107}
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109#define SZ 500
2110
2111/* We first write the generated file into memory using the
2112 * following helper, then compare to the file on disk and
2113 * only update the later if anything changed */
2114
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002115void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2116 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 char tmp[SZ];
2119 int len;
2120 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 va_start(ap, fmt);
2123 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f022006-03-16 23:04:08 -08002124 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 va_end(ap);
2126}
2127
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002128void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f022006-03-16 23:04:08 -08002131 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002132 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 strncpy(buf->p + buf->pos, s, len);
2135 buf->pos += len;
2136}
2137
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002138static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2139{
2140 const char *e = is_vmlinux(m) ?"":".ko";
2141
2142 switch (exp) {
2143 case export_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002144 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002145 "uses GPL-only symbol '%s'\n", m, e, s);
2146 break;
2147 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002148 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002149 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2150 break;
2151 case export_gpl_future:
Jessica Yu93c95e52020-03-06 17:02:05 +01002152 warn("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002153 "uses future GPL-only symbol '%s'\n", m, e, s);
2154 break;
2155 case export_plain:
2156 case export_unused:
2157 case export_unknown:
2158 /* ignore */
2159 break;
2160 }
2161}
2162
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002163static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002164{
2165 const char *e = is_vmlinux(m) ?"":".ko";
2166
2167 switch (exp) {
2168 case export_unused:
2169 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002170 warn("module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002171 "uses symbol '%s' marked UNUSED\n", m, e, s);
2172 break;
2173 default:
2174 /* ignore */
2175 break;
2176 }
2177}
2178
Masahiro Yamada3b415282018-11-23 16:57:23 +09002179static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002180{
2181 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002182 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002183
2184 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002185 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002186 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002187 if (!exp || exp->module == mod) {
2188 if (have_vmlinux && !s->weak) {
Jessica Yu93c95e52020-03-06 17:02:05 +01002189 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2190 "\"%s\" [%s.ko] undefined!\n",
2191 s->name, mod->name);
2192 if (!warn_unresolved)
Masahiro Yamada3b415282018-11-23 16:57:23 +09002193 err = 1;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002194 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002195 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002196 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002197 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002198 if (basename)
2199 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002200 else
2201 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002202
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002203 if (exp->namespace &&
2204 !module_imports_namespace(mod, exp->namespace)) {
Jessica Yu54b77842020-03-06 17:02:06 +01002205 modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR,
2206 "module %s uses symbol %s from namespace %s, but does not import it.\n",
2207 basename, exp->name, exp->namespace);
2208 if (!allow_missing_ns_imports)
2209 err = 1;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002210 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002211 }
2212
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002213 if (!mod->gpl_compatible)
2214 check_for_gpl_usage(exp->export, basename, exp->name);
2215 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002216 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002217
2218 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002219}
2220
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002221static int check_modname_len(struct module *mod)
2222{
2223 const char *mod_name;
2224
2225 mod_name = strrchr(mod->name, '/');
2226 if (mod_name == NULL)
2227 mod_name = mod->name;
2228 else
2229 mod_name++;
2230 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2231 merror("module name is too long [%s.ko]\n", mod->name);
2232 return 1;
2233 }
2234
2235 return 0;
2236}
2237
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002238/**
2239 * Header for the generated file
2240 **/
2241static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002244 /*
2245 * Include build-salt.h after module.h in order to
2246 * inherit the definitions.
2247 */
2248 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 buf_printf(b, "#include <linux/vermagic.h>\n");
2250 buf_printf(b, "#include <linux/compiler.h>\n");
2251 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002252 buf_printf(b, "BUILD_SALT;\n");
2253 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002255 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302257 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002258 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002259 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002261 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (mod->has_cleanup)
2263 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002264 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002266 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 buf_printf(b, "};\n");
2268}
2269
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002270static void add_intree_flag(struct buffer *b, int is_intree)
2271{
2272 if (is_intree)
2273 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2274}
2275
Andi Kleencaf75012018-01-25 15:50:28 -08002276/* Cannot check for assembler */
2277static void add_retpoline(struct buffer *b)
2278{
WANG Chaoe4f35892018-12-11 00:37:25 +08002279 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002280 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2281 buf_printf(b, "#endif\n");
2282}
2283
Trevor Keith5c725132009-09-22 16:43:38 -07002284static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002285{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002286 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002287 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2288}
2289
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002290/**
2291 * Record CRCs for unresolved symbols
2292 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002293static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
2295 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002296 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 for (s = mod->unres; s; s = s->next) {
2299 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002300 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 s->module = exp->module;
2303 s->crc_valid = exp->crc_valid;
2304 s->crc = exp->crc;
2305 }
2306
2307 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002308 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 buf_printf(b, "\n");
2311 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002312 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002315 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002318 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 s->name, mod->name);
2320 continue;
2321 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302322 if (strlen(s->name) >= MODULE_NAME_LEN) {
2323 merror("too long symbol \"%s\" [%s.ko]\n",
2324 s->name, mod->name);
2325 err = 1;
2326 break;
2327 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002328 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302329 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331
2332 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002333
2334 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002337static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 int first = 1;
2341
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002342 /* Clear ->seen flag of modules that own symbols needed by this. */
2343 for (s = mod->unres; s; s = s->next)
2344 if (s->module)
2345 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002348 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002350 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (!s->module)
2352 continue;
2353
2354 if (s->module->seen)
2355 continue;
2356
2357 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002358 p = strrchr(s->module->name, '/');
2359 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002360 p++;
2361 else
2362 p = s->module->name;
2363 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 first = 0;
2365 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002366 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002369static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
2371 if (mod->srcversion[0]) {
2372 buf_printf(b, "\n");
2373 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2374 mod->srcversion);
2375 }
2376}
2377
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002378static void write_buf(struct buffer *b, const char *fname)
2379{
2380 FILE *file;
2381
2382 file = fopen(fname, "w");
2383 if (!file) {
2384 perror(fname);
2385 exit(1);
2386 }
2387 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2388 perror(fname);
2389 exit(1);
2390 }
2391 if (fclose(file) != 0) {
2392 perror(fname);
2393 exit(1);
2394 }
2395}
2396
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002397static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
2399 char *tmp;
2400 FILE *file;
2401 struct stat st;
2402
2403 file = fopen(fname, "r");
2404 if (!file)
2405 goto write;
2406
2407 if (fstat(fileno(file), &st) < 0)
2408 goto close_write;
2409
2410 if (st.st_size != b->pos)
2411 goto close_write;
2412
2413 tmp = NOFAIL(malloc(b->pos));
2414 if (fread(tmp, 1, b->pos, file) != b->pos)
2415 goto free_write;
2416
2417 if (memcmp(tmp, b->p, b->pos) != 0)
2418 goto free_write;
2419
2420 free(tmp);
2421 fclose(file);
2422 return;
2423
2424 free_write:
2425 free(tmp);
2426 close_write:
2427 fclose(file);
2428 write:
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002429 write_buf(b, fname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Ram Paibd5cbce2006-06-08 22:12:53 -07002432/* parse Module.symvers file. line format:
Jessica Yu51900442020-03-11 18:01:20 +01002433 * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
Ram Paibd5cbce2006-06-08 22:12:53 -07002434 **/
Masahiro Yamada52c34162020-06-01 14:57:05 +09002435static void read_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 unsigned long size, pos = 0;
2438 void *file = grab_file(fname, &size);
2439 char *line;
2440
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002441 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /* No symbol versions, silently ignore */
2443 return;
2444
2445 while ((line = get_next_line(&pos, file, size))) {
Jessica Yu51900442020-03-11 18:01:20 +01002446 char *symname, *namespace, *modname, *d, *export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 unsigned int crc;
2448 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002449 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 if (!(symname = strchr(line, '\t')))
2452 goto fail;
2453 *symname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002454 if (!(modname = strchr(symname, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 goto fail;
2456 *modname++ = '\0';
Jessica Yu51900442020-03-11 18:01:20 +01002457 if (!(export = strchr(modname, '\t')))
2458 goto fail;
2459 *export++ = '\0';
2460 if (!(namespace = strchr(export, '\t')))
2461 goto fail;
2462 *namespace++ = '\0';
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 crc = strtoul(line, &d, 16);
2465 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2466 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002467 mod = find_module(modname);
2468 if (!mod) {
2469 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002471 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 mod->skip = 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002473 mod->from_dump = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002475 s = sym_add_exported(symname, mod, export_no(export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002476 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002477 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002478 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302480 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return;
2482fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302483 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 fatal("parse error in symbol dump file\n");
2485}
2486
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002487/* For normal builds always dump all symbols.
2488 * For external modules only dump symbols
2489 * that are not read from kernel Module.symvers.
2490 **/
2491static int dump_sym(struct symbol *sym)
2492{
2493 if (!external_module)
2494 return 1;
Masahiro Yamada52c34162020-06-01 14:57:05 +09002495 if (sym->module->from_dump)
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002496 return 0;
2497 return 1;
2498}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002499
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002500static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 struct buffer buf = { };
2503 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002504 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 int n;
2506
2507 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2508 symbol = symbolhash[n];
2509 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002510 if (dump_sym(symbol)) {
2511 namespace = symbol->namespace;
2512 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2513 symbol->crc, symbol->name,
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002514 symbol->module->name,
Jessica Yu51900442020-03-11 18:01:20 +01002515 export_str(symbol->export),
2516 namespace ? namespace : "");
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 symbol = symbol->next;
2519 }
2520 }
Masahiro Yamada436b2ac2020-06-01 14:57:12 +09002521 write_buf(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002522 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002525static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002526{
2527 struct module *mod;
2528 struct namespace_list *ns;
2529 struct buffer ns_deps_buf = {};
2530
2531 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002532
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002533 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002534 continue;
2535
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002536 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002537
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002538 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2539 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002540
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002541 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002542 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002543
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002544 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002545 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002546}
2547
Masahiro Yamada79247992020-06-01 14:57:07 +09002548struct dump_list {
2549 struct dump_list *next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002550 const char *file;
2551};
2552
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002553int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 struct module *mod;
2556 struct buffer buf = { };
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002557 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302558 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002560 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002561 int n;
Masahiro Yamada79247992020-06-01 14:57:07 +09002562 struct dump_list *dump_read_start = NULL;
2563 struct dump_list **dump_read_iter = &dump_read_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002565 while ((opt = getopt(argc, argv, "ei:mnsT:o:awENd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002566 switch (opt) {
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002567 case 'e':
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002568 external_module = 1;
Masahiro Yamadae3fb4df2020-06-01 14:57:08 +09002569 break;
2570 case 'i':
Masahiro Yamada79247992020-06-01 14:57:07 +09002571 *dump_read_iter =
2572 NOFAIL(calloc(1, sizeof(**dump_read_iter)));
2573 (*dump_read_iter)->file = optarg;
2574 dump_read_iter = &(*dump_read_iter)->next;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002575 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002576 case 'm':
2577 modversions = 1;
2578 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302579 case 'n':
2580 ignore_missing_files = 1;
2581 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002582 case 'o':
2583 dump_write = optarg;
2584 break;
2585 case 'a':
2586 all_versions = 1;
2587 break;
2588 case 's':
2589 vmlinux_section_warnings = 0;
2590 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302591 case 'T':
2592 files_source = optarg;
2593 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002594 case 'w':
2595 warn_unresolved = 1;
2596 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302597 case 'E':
2598 sec_mismatch_fatal = 1;
2599 break;
Jessica Yu54b77842020-03-06 17:02:06 +01002600 case 'N':
2601 allow_missing_ns_imports = 1;
2602 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002603 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002604 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002605 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002606 default:
2607 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
2609 }
2610
Masahiro Yamada79247992020-06-01 14:57:07 +09002611 while (dump_read_start) {
2612 struct dump_list *tmp;
Masahiro Yamada2beee862020-06-01 14:57:04 +09002613
Masahiro Yamada79247992020-06-01 14:57:07 +09002614 read_dump(dump_read_start->file);
2615 tmp = dump_read_start->next;
2616 free(dump_read_start);
2617 dump_read_start = tmp;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002620 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Rusty Russell712f9b42013-04-04 17:37:38 +10302623 if (files_source)
2624 read_symbols_from_files(files_source);
2625
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002626 err = 0;
2627
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002628 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302629 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002630
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002631 if (mod->skip)
2632 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 buf.pos = 0;
2635
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002636 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002637 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002640 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002641 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002642 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002643 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002644 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 add_moddevtable(&buf, mod);
2646 add_srcversion(&buf, mod);
2647
2648 sprintf(fname, "%s.mod.c", mod->name);
2649 write_if_changed(&buf, fname);
2650 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002651
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002652 if (missing_namespace_deps)
2653 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (dump_write)
2656 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002657 if (sec_mismatch_count && sec_mismatch_fatal)
Jessica Yu93c95e52020-03-06 17:02:05 +01002658 fatal("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002659 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002660 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002661 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002662
Masahiro Yamada47346e92019-09-24 21:07:40 +09002663 for (s = symbolhash[n]; s; s = s->next) {
2664 /*
2665 * Do not check "vmlinux". This avoids the same warnings
2666 * shown twice, and false-positives for ARCH=um.
2667 */
2668 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2669 continue;
2670
Denis Efremov15bfc232019-08-01 09:06:57 +03002671 if (s->is_static)
2672 warn("\"%s\" [%s] is a static %s\n",
2673 s->name, s->module->name,
2674 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002675 }
2676 }
2677
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002678 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002680 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}