blob: 2cdbcdf197a3f55dcc2c7a7252035e4c28a819b2 [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
15#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070017#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103018#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103019#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093020#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020022#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093025static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093027static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010030/* If we are modposting external module set to 1 */
31static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020032/* Warn about section mismatch in vmlinux if set to 1 */
33static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070034/* Only warn about unresolved symbols */
35static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070036/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010037static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103038static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093039/* ignore missing files */
40static int ignore_missing_files;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010041
Sam Ravnborgc96fca22006-07-01 11:44:23 +020042enum export {
43 export_plain, export_unused, export_gpl,
44 export_unused_gpl, export_gpl_future, export_unknown
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080047/* In kernel, this size is defined in linux/module.h;
48 * here we use Elf_Addr instead of long for covering cross-compile
49 */
50
51#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
52
Andi Kleen6d9a89e2007-11-22 03:43:08 +010053#define PRINTF __attribute__ ((format (printf, 1, 2)))
54
55PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
59 fprintf(stderr, "FATAL: ");
60
61 va_start(arglist, fmt);
62 vfprintf(stderr, fmt, arglist);
63 va_end(arglist);
64
65 exit(1);
66}
67
Andi Kleen6d9a89e2007-11-22 03:43:08 +010068PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 va_list arglist;
71
72 fprintf(stderr, "WARNING: ");
73
74 va_start(arglist, fmt);
75 vfprintf(stderr, fmt, arglist);
76 va_end(arglist);
77}
78
Andi Kleen6d9a89e2007-11-22 03:43:08 +010079PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060080{
81 va_list arglist;
82
83 fprintf(stderr, "ERROR: ");
84
85 va_start(arglist, fmt);
86 vfprintf(stderr, fmt, arglist);
87 va_end(arglist);
88}
89
Rusty Russelld4ef1c32013-04-04 17:37:32 +103090static inline bool strends(const char *str, const char *postfix)
91{
92 if (strlen(str) < strlen(postfix))
93 return false;
94
95 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
96}
97
Sam Ravnborg040fcc82006-01-28 22:15:55 +010098static int is_vmlinux(const char *modname)
99{
100 const char *myname;
101
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100102 myname = strrchr(modname, '/');
103 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100104 myname++;
105 else
106 myname = modname;
107
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200108 return (strcmp(myname, "vmlinux") == 0) ||
109 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void *do_nofail(void *ptr, const char *expr)
113{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100114 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ptr;
118}
119
120/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct module *modules;
122
Masahiro Yamada8b185742018-05-09 18:50:40 +0900123static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct module *mod;
126
127 for (mod = modules; mod; mod = mod->next)
128 if (strcmp(mod->name, modname) == 0)
129 break;
130 return mod;
131}
132
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030133static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030136 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 mod = NOFAIL(malloc(sizeof(*mod)));
139 memset(mod, 0, sizeof(*mod));
140 p = NOFAIL(strdup(modname));
141
142 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030143 if (strends(p, ".o")) {
144 p[strlen(p) - 2] = '\0';
145 mod->is_dot_o = 1;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* add to list */
149 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200150 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 mod->next = modules;
152 modules = mod;
153
154 return mod;
155}
156
157/* A hash of all exported symbols,
158 * struct symbol is also used for lists of unresolved symbols */
159
160#define SYMBOL_HASH_SIZE 1024
161
162struct symbol {
163 struct symbol *next;
164 struct module *module;
165 unsigned int crc;
166 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900167 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100169 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
170 unsigned int kernel:1; /* 1 if symbol is from kernel
171 * (only for external modules) **/
Rusty Russellb6568b12013-11-07 12:09:13 +1030172 unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */
Denis Efremov15bfc232019-08-01 09:06:57 +0300173 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700174 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 char name[0];
176};
177
178static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
179
180/* This is based on the hash agorithm from gdbm, via tdb */
181static inline unsigned int tdb_hash(const char *name)
182{
183 unsigned value; /* Used to compute the hash value. */
184 unsigned i; /* Used to cycle through random values. */
185
186 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100187 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
189
190 return (1103515243 * value + 12345);
191}
192
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100193/**
194 * Allocate a new symbols for use in the hash of exported symbols or
195 * the list of unresolved symbols per module
196 **/
197static struct symbol *alloc_symbol(const char *name, unsigned int weak,
198 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
201
202 memset(s, 0, sizeof(*s));
203 strcpy(s->name, name);
204 s->weak = weak;
205 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300206 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return s;
208}
209
210/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700211static struct symbol *new_symbol(const char *name, struct module *module,
212 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned int hash;
215 struct symbol *new;
216
217 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
218 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
219 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700220 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100221 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100224static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct symbol *s;
227
228 /* For our purposes, .foo matches foo. PPC64 needs this. */
229 if (name[0] == '.')
230 name++;
231
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100232 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (strcmp(s->name, name) == 0)
234 return s;
235 }
236 return NULL;
237}
238
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100239static bool contains_namespace(struct namespace_list *list,
240 const char *namespace)
241{
242 struct namespace_list *ns_entry;
243
244 for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
245 if (strcmp(ns_entry->namespace, namespace) == 0)
246 return true;
247
248 return false;
249}
250
251static void add_namespace(struct namespace_list **list, const char *namespace)
252{
253 struct namespace_list *ns_entry;
254
255 if (!contains_namespace(*list, namespace)) {
256 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
257 strlen(namespace) + 1));
258 strcpy(ns_entry->namespace, namespace);
259 ns_entry->next = *list;
260 *list = ns_entry;
261 }
262}
263
264static bool module_imports_namespace(struct module *module,
265 const char *namespace)
266{
267 return contains_namespace(module->imported_namespaces, namespace);
268}
269
Mathias Krause7a3ee752014-08-27 20:28:53 +0930270static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700271 const char *str;
272 enum export export;
273} export_list[] = {
274 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200275 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700276 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200277 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700278 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
279 { .str = "(unknown)", .export = export_unknown },
280};
281
282
283static const char *export_str(enum export ex)
284{
285 return export_list[ex].str;
286}
287
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100288static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700289{
290 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100291
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200292 if (!s)
293 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700294 for (i = 0; export_list[i].export != export_unknown; i++) {
295 if (strcmp(export_list[i].str, s) == 0)
296 return export_list[i].export;
297 }
298 return export_unknown;
299}
300
Masahiro Yamada6124c042017-09-06 16:19:05 -0700301static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
302{
303 return (void *)elf->hdr +
304 elf->sechdrs[elf->secindex_strings].sh_offset +
305 sechdr->sh_name;
306}
307
308static const char *sec_name(struct elf_info *elf, int secindex)
309{
310 return sech_name(elf, &elf->sechdrs[secindex]);
311}
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200312
313#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
314
315static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
316{
317 const char *secname = sec_name(elf, sec);
318
319 if (strstarts(secname, "___ksymtab+"))
320 return export_plain;
321 else if (strstarts(secname, "___ksymtab_unused+"))
322 return export_unused;
323 else if (strstarts(secname, "___ksymtab_gpl+"))
324 return export_gpl;
325 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
326 return export_unused_gpl;
327 else if (strstarts(secname, "___ksymtab_gpl_future+"))
328 return export_gpl_future;
329 else
330 return export_unknown;
331}
332
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200333static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700334{
335 if (sec == elf->export_sec)
336 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200337 else if (sec == elf->export_unused_sec)
338 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700339 else if (sec == elf->export_gpl_sec)
340 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200341 else if (sec == elf->export_unused_gpl_sec)
342 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700343 else if (sec == elf->export_gpl_future_sec)
344 return export_gpl_future;
345 else
346 return export_unknown;
347}
348
Matthias Maennich69923202019-10-18 10:31:42 +0100349static const char *namespace_from_kstrtabns(struct elf_info *info,
350 Elf_Sym *kstrtabns)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100351{
Matthias Maennich69923202019-10-18 10:31:42 +0100352 char *value = info->ksymtab_strings + kstrtabns->st_value;
353 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100354}
355
Matthias Maennicha2b11182019-10-18 10:31:40 +0100356static void sym_update_namespace(const char *symname, const char *namespace)
357{
358 struct symbol *s = find_symbol(symname);
359
360 /*
361 * That symbol should have been created earlier and thus this is
362 * actually an assertion.
363 */
364 if (!s) {
365 merror("Could not update namespace(%s) for symbol %s\n",
366 namespace, symname);
367 return;
368 }
369
370 free(s->namespace);
371 s->namespace =
372 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
373}
374
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100375/**
376 * Add an exported symbol - it may have already been added without a
377 * CRC, in this case just update the CRC
378 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100379static struct symbol *sym_add_exported(const char *name, struct module *mod,
380 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct symbol *s = find_symbol(name);
383
384 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700385 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100386 } else {
387 if (!s->preloaded) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100388 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
389 mod->name, name, s->module->name,
390 is_vmlinux(s->module->name) ? "" : ".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700391 } else {
Paul Bollebaec30e2014-04-16 18:05:35 +0200392 /* In case Module.symvers was out of date */
Trent Piepho4b219602007-10-11 16:40:10 -0700393 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100396 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100397 s->vmlinux = is_vmlinux(mod->name);
398 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700399 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100400 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100403static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700404 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100405{
406 struct symbol *s = find_symbol(name);
407
Rusty Russellb6568b12013-11-07 12:09:13 +1030408 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700409 s = new_symbol(name, mod, export);
Rusty Russellb6568b12013-11-07 12:09:13 +1030410 /* Don't complain when we find it later. */
411 s->preloaded = 1;
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;
Matthias Maennich69923202019-10-18 10:31:42 +0100594 else if (strcmp(secname, "__ksymtab_strings") == 0)
595 info->ksymtab_strings = (void *)hdr +
596 sechdrs[i].sh_offset -
597 sechdrs[i].sh_addr;
Ram Paibd5cbce2006-06-08 22:12:53 -0700598
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200599 if (sechdrs[i].sh_type == SHT_SYMTAB) {
600 unsigned int sh_link_idx;
601 symtab_idx = i;
602 info->symtab_start = (void *)hdr +
603 sechdrs[i].sh_offset;
604 info->symtab_stop = (void *)hdr +
605 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600606 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200607 info->strtab = (void *)hdr +
608 sechdrs[sh_link_idx].sh_offset;
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200611 /* 32bit section no. table? ("more than 64k sections") */
612 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
613 symtab_shndx_idx = i;
614 info->symtab_shndx_start = (void *)hdr +
615 sechdrs[i].sh_offset;
616 info->symtab_shndx_stop = (void *)hdr +
617 sechdrs[i].sh_offset + sechdrs[i].sh_size;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100620 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100621 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* Fix endianness in symbols */
624 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
625 sym->st_shndx = TO_NATIVE(sym->st_shndx);
626 sym->st_name = TO_NATIVE(sym->st_name);
627 sym->st_value = TO_NATIVE(sym->st_value);
628 sym->st_size = TO_NATIVE(sym->st_size);
629 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200630
631 if (symtab_shndx_idx != ~0U) {
632 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600633 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200634 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600635 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200636 symtab_idx);
637 /* Fix endianness */
638 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
639 p++)
640 *p = TO_NATIVE(*p);
641 }
642
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100643 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100646static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 release_file(info->hdr, info->size);
649}
650
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200651static int ignore_undef_symbol(struct elf_info *info, const char *symname)
652{
653 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900654 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200655 return 1;
656 /* ignore global offset table */
657 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
658 return 1;
659 if (info->hdr->e_machine == EM_PPC)
660 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900661 if (strstarts(symname, "_restgpr_") ||
662 strstarts(symname, "_savegpr_") ||
663 strstarts(symname, "_rest32gpr_") ||
664 strstarts(symname, "_save32gpr_") ||
665 strstarts(symname, "_restvr_") ||
666 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200667 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000668 if (info->hdr->e_machine == EM_PPC64)
669 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900670 if (strstarts(symname, "_restgpr0_") ||
671 strstarts(symname, "_savegpr0_") ||
672 strstarts(symname, "_restvr_") ||
673 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100674 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000675 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200676 /* Do not ignore this symbol */
677 return 0;
678}
679
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100680static void handle_modversions(struct module *mod, struct elf_info *info,
681 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200684 enum export export;
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100685 bool is_crc = false;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900686 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200687
Frank Rowand258f7422012-04-09 17:59:03 -0700688 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900689 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200690 export = export_from_secname(info, get_secindex(info, sym));
691 else
692 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Andi Kleenb5064652013-11-12 15:08:38 -0800694 /* CRC'd symbol */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900695 if (strstarts(symname, "__crc_")) {
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100696 is_crc = true;
Andi Kleenb5064652013-11-12 15:08:38 -0800697 crc = (unsigned int) sym->st_value;
Ard Biesheuvel56067812017-02-03 09:54:05 +0000698 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
699 unsigned int *crcp;
700
701 /* symbol points to the CRC in the ELF object */
702 crcp = (void *)info->hdr + sym->st_value +
703 info->sechdrs[sym->st_shndx].sh_offset -
704 (info->hdr->e_type != ET_REL ?
705 info->sechdrs[sym->st_shndx].sh_addr : 0);
Fredrik Noring54a71512019-03-27 19:12:50 +0100706 crc = TO_NATIVE(*crcp);
Ard Biesheuvel56067812017-02-03 09:54:05 +0000707 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900708 sym_update_crc(symname + strlen("__crc_"), mod, crc,
Andi Kleenb5064652013-11-12 15:08:38 -0800709 export);
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 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;
Ben Colline8d529012005-08-19 13:44:57 -0700726/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
727#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
728/* add compatibility with older glibc */
729#ifndef STT_SPARC_REGISTER
730#define STT_SPARC_REGISTER STT_REGISTER
731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (info->hdr->e_machine == EM_SPARC ||
733 info->hdr->e_machine == EM_SPARCV9) {
734 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700735 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100737 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700738 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100739 munged[0] = '_';
740 munged[1] = toupper(munged[1]);
741 symname = munged;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100745
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100746 if (is_crc) {
747 const char *e = is_vmlinux(mod->name) ?"":".ko";
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900748 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
749 symname + strlen("__crc_"), mod->name, e);
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100750 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030751 mod->unres = alloc_symbol(symname,
752 ELF_ST_BIND(sym->st_info) == STB_WEAK,
753 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755 default:
756 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900757 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100758 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100759 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900761 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900763 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 mod->has_cleanup = 1;
765 break;
766 }
767}
768
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100769/**
770 * Parse tag=value strings from .modinfo section
771 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static char *next_string(char *string, unsigned long *secsize)
773{
774 /* Skip non-zero chars */
775 while (string[0]) {
776 string++;
777 if ((*secsize)-- <= 1)
778 return NULL;
779 }
780
781 /* Skip any zero padding. */
782 while (!string[0]) {
783 string++;
784 if ((*secsize)-- <= 1)
785 return NULL;
786 }
787 return string;
788}
789
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900790static char *get_next_modinfo(struct elf_info *info, const char *tag,
791 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 char *p;
794 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900795 char *modinfo = info->modinfo;
796 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900798 if (prev) {
799 size -= prev - modinfo;
800 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200801 }
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 for (p = modinfo; p; p = next_string(p, &size)) {
804 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
805 return p + taglen + 1;
806 }
807 return NULL;
808}
809
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900810static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200811
812{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900813 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200814}
815
Sam Ravnborg93684d32006-02-19 11:53:35 +0100816/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100817 * Test if string s ends in string sub
818 * return 0 if match
819 **/
820static int strrcmp(const char *s, const char *sub)
821{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100822 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100823
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100824 if (!s || !sub)
825 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100826
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100827 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100828 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100829
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100830 if ((slen == 0) || (sublen == 0))
831 return 1;
832
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100833 if (sublen > slen)
834 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100835
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100836 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100837}
838
Sam Ravnborgff13f922008-01-23 19:54:27 +0100839static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
840{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100841 if (sym)
842 return elf->strtab + sym->st_name;
843 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100844 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100845}
846
Sam Ravnborg10668222008-01-13 22:21:31 +0100847/* The pattern is an array of simple patterns.
848 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100849 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100850 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930851 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100852 */
Trevor Keith5c725132009-09-22 16:43:38 -0700853static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100854{
855 const char *p;
856 while (*pat) {
857 p = *pat++;
858 const char *endp = p + strlen(p) - 1;
859
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930860 /* "*foo*" */
861 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300862 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
863 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930864
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930865 free(bare);
866 if (here != NULL)
867 return 1;
868 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100869 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930870 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100871 if (strrcmp(sym, p + 1) == 0)
872 return 1;
873 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100874 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100875 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100876 if (strncmp(sym, p, strlen(p) - 1) == 0)
877 return 1;
878 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100879 /* no wildcards */
880 else {
881 if (strcmp(p, sym) == 0)
882 return 1;
883 }
884 }
885 /* no match */
886 return 0;
887}
888
Sam Ravnborg10668222008-01-13 22:21:31 +0100889/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930890static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200891{
892 ".comment*",
893 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930894 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800895 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000896 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200897 ".mdebug*", /* alpha, score, mips etc. */
898 ".pdr", /* alpha, score, mips etc. */
899 ".stab*",
900 ".note*",
901 ".got*",
902 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400903 ".xt.prop", /* xtensa */
904 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030905 ".arcextmap*", /* arc */
906 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030907 ".cmem*", /* EZchip */
908 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100909 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600910 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200911 NULL
912};
Sam Ravnborg10668222008-01-13 22:21:31 +0100913
Sam Ravnborge241a632008-01-28 20:13:13 +0100914/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400915 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100916 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400917 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100918 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400919static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900920 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100921{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400922 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100923
Anders Kaseorgb614a692009-04-23 16:49:33 -0400924 if (sechdr->sh_type == SHT_PROGBITS &&
925 !(sechdr->sh_flags & SHF_ALLOC) &&
926 !match(sec, section_white_list)) {
927 warn("%s (%s): unexpected non-allocatable section.\n"
928 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
929 "Note that for example <linux/init.h> contains\n"
930 "section definitions for use in .S files.\n\n",
931 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100932 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100933}
934
935
936
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100937#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930938 ".init.setup", ".init.rodata", ".meminit.rodata", \
939 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100940#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930941 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100942
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100943#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930944 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100945#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930946 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100947
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200948#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930949 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
950 ".pci_fixup_enable", ".pci_fixup_resume", \
951 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200952
Paul Gortmakere24f6622013-06-19 19:30:48 -0400953#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
954#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100955
956#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
957#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100958
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930959#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930960#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700961 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930962#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400963 ".fixup", ".entry.text", ".exception.text", ".text.*", \
964 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100965
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000966#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000967#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100968
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000969#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000970#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100971
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930972#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
973 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
974
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100975/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930976static const char *const init_data_sections[] =
977 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100978
979/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930980static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100981
982/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930983static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100984 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100985
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930986/* all text sections */
987static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
988
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100989/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930990static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100991
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100992
993/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100994#define DEFAULT_SYMBOL_WHITE_LIST \
995 "*driver", \
996 "*_template", /* scsi uses *_template a lot */ \
997 "*_timer", /* arm uses ops structures named _timer a lot */ \
998 "*_sht", /* scsi also used *_sht to some extent */ \
999 "*_ops", \
1000 "*_probe", \
1001 "*_probe_one", \
1002 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001003
Mathias Krause7a3ee752014-08-27 20:28:53 +09301004static const char *const head_sections[] = { ".head.text*", NULL };
1005static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001006 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301007static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001008
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001009enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001010 TEXT_TO_ANY_INIT,
1011 DATA_TO_ANY_INIT,
1012 TEXT_TO_ANY_EXIT,
1013 DATA_TO_ANY_EXIT,
1014 XXXINIT_TO_SOME_INIT,
1015 XXXEXIT_TO_SOME_EXIT,
1016 ANY_INIT_TO_ANY_EXIT,
1017 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001018 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301019 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001020};
1021
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301022/**
1023 * Describe how to match sections on different criterias:
1024 *
1025 * @fromsec: Array of sections to be matched.
1026 *
1027 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1028 * this array is forbidden (black-list). Can be empty.
1029 *
1030 * @good_tosec: Relocations applied to a section in @fromsec must be
1031 * targetting sections in this array (white-list). Can be empty.
1032 *
1033 * @mismatch: Type of mismatch.
1034 *
1035 * @symbol_white_list: Do not match a relocation to a symbol in this list
1036 * even if it is targetting a section in @bad_to_sec.
1037 *
1038 * @handler: Specific handler to call when a match is found. If NULL,
1039 * default_mismatch_handler() will be called.
1040 *
1041 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001042struct sectioncheck {
1043 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301044 const char *bad_tosec[20];
1045 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001046 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001047 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301048 void (*handler)(const char *modname, struct elf_info *elf,
1049 const struct sectioncheck* const mismatch,
1050 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1051
Sam Ravnborg10668222008-01-13 22:21:31 +01001052};
1053
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301054static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1055 const struct sectioncheck* const mismatch,
1056 Elf_Rela *r, Elf_Sym *sym,
1057 const char *fromsec);
1058
Mathias Krause7a3ee752014-08-27 20:28:53 +09301059static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001060/* Do not reference init/exit code/data from
1061 * normal code and data
1062 */
1063{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001064 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301065 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001066 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001067 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001068},
1069{
1070 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301071 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001072 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001073 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001074},
1075{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001076 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301077 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001078 .mismatch = DATA_TO_ANY_INIT,
1079 .symbol_white_list = {
1080 "*_template", "*_timer", "*_sht", "*_ops",
1081 "*_probe", "*_probe_one", "*_console", NULL
1082 },
1083},
1084{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001085 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301086 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001087 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001088 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001089},
1090{
1091 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301092 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001093 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001094 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001095},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001096/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001097{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001098 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301099 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001100 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001101 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001102},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001103/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001104{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001105 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301106 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001107 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001108 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001109},
1110/* Do not use exit code/data from init code */
1111{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001112 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301113 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001114 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001115 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001116},
1117/* Do not use init code/data from exit code */
1118{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001119 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301120 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001121 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001122 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001123},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001124{
1125 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301126 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001127 .mismatch = ANY_INIT_TO_ANY_EXIT,
1128 .symbol_white_list = { NULL },
1129},
Sam Ravnborg10668222008-01-13 22:21:31 +01001130/* Do not export init/exit functions or data */
1131{
1132 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301133 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001134 .mismatch = EXPORT_TO_INIT_EXIT,
1135 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301136},
1137{
1138 .fromsec = { "__ex_table", NULL },
1139 /* If you're adding any new black-listed sections in here, consider
1140 * adding a special 'printer' for them in scripts/check_extable.
1141 */
1142 .bad_tosec = { ".altinstr_replacement", NULL },
1143 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1144 .mismatch = EXTABLE_TO_NON_TEXT,
1145 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001146}
1147};
1148
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001149static const struct sectioncheck *section_mismatch(
1150 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001151{
1152 int i;
1153 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1154 const struct sectioncheck *check = &sectioncheck[0];
1155
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301156 /*
1157 * The target section could be the SHT_NUL section when we're
1158 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001159 * doesn't make much sense and causes build failures on parisc
1160 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301161 */
1162 if (*tosec == '\0')
1163 return NULL;
1164
Sam Ravnborg10668222008-01-13 22:21:31 +01001165 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301166 if (match(fromsec, check->fromsec)) {
1167 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1168 return check;
1169 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1170 return check;
1171 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001172 check++;
1173 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001174 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001175}
1176
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001177/**
1178 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001179 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001180 * Pattern 1:
1181 * If a module parameter is declared __initdata and permissions=0
1182 * then this is legal despite the warning generated.
1183 * We cannot see value of permissions here, so just ignore
1184 * this pattern.
1185 * The pattern is identified by:
1186 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001187 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001188 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001189 *
Rusty Russell6a841522010-08-11 23:04:16 -06001190 * Pattern 1a:
1191 * module_param_call() ops can refer to __init set function if permissions=0
1192 * The pattern is identified by:
1193 * tosec = .init.text
1194 * fromsec = .data*
1195 * atsym = __param_ops_*
1196 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001197 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001198 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001199 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001200 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001201 * tosec = init or exit section
1202 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001203 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1204 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001205 *
1206 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001207 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001208 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001209 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001210 * Some symbols belong to init section but still it is ok to reference
1211 * these from non-init sections as these symbols don't have any memory
1212 * allocated for them and symbol address and value are same. So even
1213 * if init section is freed, its ok to reference those symbols.
1214 * For ex. symbols marking the init section boundaries.
1215 * This pattern is identified by
1216 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001217 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301218 * Pattern 5:
1219 * GCC may optimize static inlines when fed constant arg(s) resulting
1220 * in functions like cpumask_empty() -- generating an associated symbol
1221 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1222 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1223 * meaningless section warning. May need to add isra symbols too...
1224 * This pattern is identified by
1225 * tosec = init section
1226 * fromsec = text section
1227 * refsymname = *.constprop.*
1228 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001229 * Pattern 6:
1230 * Hide section mismatch warnings for ELF local symbols. The goal
1231 * is to eliminate false positive modpost warnings caused by
1232 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1233 * Autogenerated symbol names bypass modpost's "Pattern 2"
1234 * whitelisting, which relies on pattern-matching against symbol
1235 * names to work. (One situation where gcc can autogenerate ELF
1236 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001237 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001238static int secref_whitelist(const struct sectioncheck *mismatch,
1239 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001240 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001241{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001242 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001243 if (match(tosec, init_data_sections) &&
1244 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001245 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001246 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001247
Rusty Russell6a841522010-08-11 23:04:16 -06001248 /* Check for pattern 1a */
1249 if (strcmp(tosec, ".init.text") == 0 &&
1250 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001251 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001252 return 0;
1253
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001254 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001255 if (match(tosec, init_exit_sections) &&
1256 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001257 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001258 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001259
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001260 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001261 if (match(fromsec, head_sections) &&
1262 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001263 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001264
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001265 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001266 if (match(tosym, linker_symbols))
1267 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001268
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301269 /* Check for pattern 5 */
1270 if (match(fromsec, text_sections) &&
1271 match(tosec, init_sections) &&
1272 match(fromsym, optim_symbols))
1273 return 0;
1274
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001275 /* Check for pattern 6 */
1276 if (strstarts(fromsym, ".L"))
1277 return 0;
1278
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001279 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001280}
1281
Sami Tolvanen5818c682018-10-23 15:15:35 -07001282static inline int is_arm_mapping_symbol(const char *str)
1283{
1284 return str[0] == '$' && strchr("axtd", str[1])
1285 && (str[2] == '\0' || str[2] == '.');
1286}
1287
1288/*
1289 * If there's no name there, ignore it; likewise, ignore it if it's
1290 * one of the magic symbols emitted used by current ARM tools.
1291 *
1292 * Otherwise if find_symbols_between() returns those symbols, they'll
1293 * fail the whitelist tests and cause lots of false alarms ... fixable
1294 * only by merging __exit and __init sections into __text, bloating
1295 * the kernel (which is especially evil on embedded platforms).
1296 */
1297static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1298{
1299 const char *name = elf->strtab + sym->st_name;
1300
1301 if (!name || !strlen(name))
1302 return 0;
1303 return !is_arm_mapping_symbol(name);
1304}
1305
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001306/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001307 * Find symbol based on relocation record info.
1308 * In some cases the symbol supplied is a valid symbol so
1309 * return refsym. If st_name != 0 we assume this is a valid symbol.
1310 * In other cases the symbol needs to be looked up in the symbol table
1311 * based on section and address.
1312 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001313static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001314 Elf_Sym *relsym)
1315{
1316 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001317 Elf_Sym *near = NULL;
1318 Elf64_Sword distance = 20;
1319 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001320 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001321
1322 if (relsym->st_name != 0)
1323 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001324
1325 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001326 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001327 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001328 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001329 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1330 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001331 if (!is_valid_name(elf, sym))
1332 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001333 if (sym->st_value == addr)
1334 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001335 /* Find a symbol nearby - addr are maybe negative */
1336 d = sym->st_value - addr;
1337 if (d < 0)
1338 d = addr - sym->st_value;
1339 if (d < distance) {
1340 distance = d;
1341 near = sym;
1342 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001343 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001344 /* We need a close match */
1345 if (distance < 20)
1346 return near;
1347 else
1348 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001349}
1350
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001351/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001352 * Find symbols before or equal addr and after addr - in the section sec.
1353 * If we find two symbols with equal offset prefer one with a valid name.
1354 * The ELF format may have a better way to detect what type of symbol
1355 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001356 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001357static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1358 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001359{
1360 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001361 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001362 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001363
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001364 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1365 const char *symsec;
1366
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001367 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001368 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001369 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001370 if (strcmp(symsec, sec) != 0)
1371 continue;
David Brownellda68d612007-02-20 13:58:16 -08001372 if (!is_valid_name(elf, sym))
1373 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001374 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001375 if ((addr - sym->st_value) < distance) {
1376 distance = addr - sym->st_value;
1377 near = sym;
1378 } else if ((addr - sym->st_value) == distance) {
1379 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001380 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001381 }
1382 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001383 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001384}
1385
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001386/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001387 * Convert a section name to the function/data attribute
1388 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001389 * .memexitconst => __memconst
1390 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001391 *
1392 * The memory of returned value has been allocated on a heap. The user of this
1393 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001394*/
1395static char *sec2annotation(const char *s)
1396{
1397 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001398 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001399 char *r = p;
1400
1401 *p++ = '_';
1402 *p++ = '_';
1403 if (*s == '.')
1404 s++;
1405 while (*s && *s != '.')
1406 *p++ = *s++;
1407 *p = '\0';
1408 if (*s == '.')
1409 s++;
1410 if (strstr(s, "rodata") != NULL)
1411 strcat(p, "const ");
1412 else if (strstr(s, "data") != NULL)
1413 strcat(p, "data ");
1414 else
1415 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001416 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001417 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001418 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001419 }
1420}
1421
1422static int is_function(Elf_Sym *sym)
1423{
1424 if (sym)
1425 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1426 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001427 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001428}
1429
Randy Dunlap00759c02011-03-15 14:13:47 -07001430static void print_section_list(const char * const list[20])
1431{
1432 const char *const *s = list;
1433
1434 while (*s) {
1435 fprintf(stderr, "%s", *s);
1436 s++;
1437 if (*s)
1438 fprintf(stderr, ", ");
1439 }
1440 fprintf(stderr, "\n");
1441}
1442
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301443static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1444{
1445 switch (is_func) {
1446 case 0: *name = "variable"; *name_p = ""; break;
1447 case 1: *name = "function"; *name_p = "()"; break;
1448 default: *name = "(unknown reference)"; *name_p = ""; break;
1449 }
1450}
1451
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001452/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001453 * Print a warning about a section mismatch.
1454 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001455 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001456 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001457static void report_sec_mismatch(const char *modname,
1458 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001459 const char *fromsec,
1460 unsigned long long fromaddr,
1461 const char *fromsym,
1462 int from_is_func,
1463 const char *tosec, const char *tosym,
1464 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001465{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001466 const char *from, *from_p;
1467 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001468 char *prl_from;
1469 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001470
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001471 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001472
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301473 get_pretty_name(from_is_func, &from, &from_p);
1474 get_pretty_name(to_is_func, &to, &to_p);
1475
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001476 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1477 "to the %s %s:%s%s\n",
1478 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1479 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001480
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001481 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001482 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001483 prl_from = sec2annotation(fromsec);
1484 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001485 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001486 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001487 "the %s %s%s%s.\n"
1488 "This is often because %s lacks a %s\n"
1489 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001490 prl_from, fromsym,
1491 to, prl_to, tosym, to_p,
1492 fromsym, prl_to, tosym);
1493 free(prl_from);
1494 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001495 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001496 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001497 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001498 fprintf(stderr,
1499 "The variable %s references\n"
1500 "the %s %s%s%s\n"
1501 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001502 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001503 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001504 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001505 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001506 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001507 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001508 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001509 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001510 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001511 fprintf(stderr,
1512 "The function %s() references a %s in an exit section.\n"
1513 "Often the %s %s%s has valid usage outside the exit section\n"
1514 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001515 fromsym, to, to, tosym, to_p, prl_to, tosym);
1516 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001517 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001518 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001519 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001520 fprintf(stderr,
1521 "The variable %s references\n"
1522 "the %s %s%s%s\n"
1523 "If the reference is valid then annotate the\n"
1524 "variable with __exit* (see linux/init.h) or "
1525 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001526 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001527 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001528 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001529 break;
1530 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001531 case XXXINIT_TO_SOME_INIT:
1532 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001533 prl_from = sec2annotation(fromsec);
1534 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001535 fprintf(stderr,
1536 "The %s %s%s%s references\n"
1537 "a %s %s%s%s.\n"
1538 "If %s is only used by %s then\n"
1539 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001540 from, prl_from, fromsym, from_p,
1541 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001542 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001543 free(prl_from);
1544 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001545 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001546 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001547 prl_from = sec2annotation(fromsec);
1548 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001549 fprintf(stderr,
1550 "The %s %s%s%s references\n"
1551 "a %s %s%s%s.\n"
1552 "This is often seen when error handling "
1553 "in the init function\n"
1554 "uses functionality in the exit path.\n"
1555 "The fix is often to remove the %sannotation of\n"
1556 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001557 from, prl_from, fromsym, from_p,
1558 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001559 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001560 free(prl_from);
1561 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001562 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001563 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001564 prl_from = sec2annotation(fromsec);
1565 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001566 fprintf(stderr,
1567 "The %s %s%s%s references\n"
1568 "a %s %s%s%s.\n"
1569 "This is often seen when error handling "
1570 "in the exit function\n"
1571 "uses functionality in the init path.\n"
1572 "The fix is often to remove the %sannotation of\n"
1573 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001574 from, prl_from, fromsym, from_p,
1575 to, prl_to, tosym, to_p,
1576 prl_to, tosym, to_p);
1577 free(prl_from);
1578 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001579 break;
1580 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001581 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001582 fprintf(stderr,
1583 "The symbol %s is exported and annotated %s\n"
1584 "Fix this by removing the %sannotation of %s "
1585 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001586 tosym, prl_to, prl_to, tosym);
1587 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001588 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301589 case EXTABLE_TO_NON_TEXT:
1590 fatal("There's a special handler for this mismatch type, "
1591 "we should never get here.");
1592 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001593 }
1594 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001595}
1596
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301597static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1598 const struct sectioncheck* const mismatch,
1599 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1600{
1601 const char *tosec;
1602 Elf_Sym *to;
1603 Elf_Sym *from;
1604 const char *tosym;
1605 const char *fromsym;
1606
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301607 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1608 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301609
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001610 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301611 return;
1612
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301613 tosec = sec_name(elf, get_secindex(elf, sym));
1614 to = find_elf_symbol(elf, r->r_addend, sym);
1615 tosym = sym_name(elf, to);
1616
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301617 /* check whitelist - we may ignore it */
1618 if (secref_whitelist(mismatch,
1619 fromsec, fromsym, tosec, tosym)) {
1620 report_sec_mismatch(modname, mismatch,
1621 fromsec, r->r_offset, fromsym,
1622 is_function(from), tosec, tosym,
1623 is_function(to));
1624 }
1625}
1626
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301627static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1628{
1629 if (section_index > elf->num_sections)
1630 fatal("section_index is outside elf->num_sections!\n");
1631
1632 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1633}
1634
1635/*
1636 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1637 * to know the sizeof(struct exception_table_entry) for the target architecture.
1638 */
1639static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301640static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301641{
1642 /*
1643 * If we're currently checking the second relocation within __ex_table,
1644 * that relocation offset tells us the offsetof(struct
1645 * exception_table_entry, fixup) which is equal to sizeof(struct
1646 * exception_table_entry) divided by two. We use that to our advantage
1647 * since there's no portable way to get that size as every architecture
1648 * seems to go with different sized types. Not pretty but better than
1649 * hard-coding the size for every architecture..
1650 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301651 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301652 extable_entry_size = r->r_offset * 2;
1653}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301654
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301655static inline bool is_extable_fault_address(Elf_Rela *r)
1656{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301657 /*
1658 * extable_entry_size is only discovered after we've handled the
1659 * _second_ relocation in __ex_table, so only abort when we're not
1660 * handling the first reloc and extable_entry_size is zero.
1661 */
1662 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301663 fatal("extable_entry size hasn't been discovered!\n");
1664
1665 return ((r->r_offset == 0) ||
1666 (r->r_offset % extable_entry_size == 0));
1667}
1668
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301669#define is_second_extable_reloc(Start, Cur, Sec) \
1670 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1671
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301672static void report_extable_warnings(const char* modname, struct elf_info* elf,
1673 const struct sectioncheck* const mismatch,
1674 Elf_Rela* r, Elf_Sym* sym,
1675 const char* fromsec, const char* tosec)
1676{
1677 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1678 const char* fromsym_name = sym_name(elf, fromsym);
1679 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1680 const char* tosym_name = sym_name(elf, tosym);
1681 const char* from_pretty_name;
1682 const char* from_pretty_name_p;
1683 const char* to_pretty_name;
1684 const char* to_pretty_name_p;
1685
1686 get_pretty_name(is_function(fromsym),
1687 &from_pretty_name, &from_pretty_name_p);
1688 get_pretty_name(is_function(tosym),
1689 &to_pretty_name, &to_pretty_name_p);
1690
1691 warn("%s(%s+0x%lx): Section mismatch in reference"
1692 " from the %s %s%s to the %s %s:%s%s\n",
1693 modname, fromsec, (long)r->r_offset, from_pretty_name,
1694 fromsym_name, from_pretty_name_p,
1695 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1696
1697 if (!match(tosec, mismatch->bad_tosec) &&
1698 is_executable_section(elf, get_secindex(elf, sym)))
1699 fprintf(stderr,
1700 "The relocation at %s+0x%lx references\n"
1701 "section \"%s\" which is not in the list of\n"
1702 "authorized sections. If you're adding a new section\n"
1703 "and/or if this reference is valid, add \"%s\" to the\n"
1704 "list of authorized sections to jump to on fault.\n"
1705 "This can be achieved by adding \"%s\" to \n"
1706 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1707 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1708}
1709
1710static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1711 const struct sectioncheck* const mismatch,
1712 Elf_Rela* r, Elf_Sym* sym,
1713 const char *fromsec)
1714{
1715 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1716
1717 sec_mismatch_count++;
1718
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001719 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301720
1721 if (match(tosec, mismatch->bad_tosec))
1722 fatal("The relocation at %s+0x%lx references\n"
1723 "section \"%s\" which is black-listed.\n"
1724 "Something is seriously wrong and should be fixed.\n"
1725 "You might get more information about where this is\n"
1726 "coming from by using scripts/check_extable.sh %s\n",
1727 fromsec, (long)r->r_offset, tosec, modname);
1728 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1729 if (is_extable_fault_address(r))
1730 fatal("The relocation at %s+0x%lx references\n"
1731 "section \"%s\" which is not executable, IOW\n"
1732 "it is not possible for the kernel to fault\n"
1733 "at that address. Something is seriously wrong\n"
1734 "and should be fixed.\n",
1735 fromsec, (long)r->r_offset, tosec);
1736 else
1737 fatal("The relocation at %s+0x%lx references\n"
1738 "section \"%s\" which is not executable, IOW\n"
1739 "the kernel will fault if it ever tries to\n"
1740 "jump to it. Something is seriously wrong\n"
1741 "and should be fixed.\n",
1742 fromsec, (long)r->r_offset, tosec);
1743 }
1744}
1745
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001746static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001747 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001748{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001749 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301750 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001751
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001752 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301753 if (mismatch->handler)
1754 mismatch->handler(modname, elf, mismatch,
1755 r, sym, fromsec);
1756 else
1757 default_mismatch_handler(modname, elf, mismatch,
1758 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001759 }
1760}
1761
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001762static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001763 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001764{
1765 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001766 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001767
1768 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001769 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001770}
1771
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001772static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001773{
1774 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001775 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001776
1777 switch (r_typ) {
1778 case R_386_32:
1779 r->r_addend = TO_NATIVE(*location);
1780 break;
1781 case R_386_PC32:
1782 r->r_addend = TO_NATIVE(*location) + 4;
1783 /* For CONFIG_RELOCATABLE=y */
1784 if (elf->hdr->e_type == ET_EXEC)
1785 r->r_addend += r->r_offset;
1786 break;
1787 }
1788 return 0;
1789}
1790
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001791#ifndef R_ARM_CALL
1792#define R_ARM_CALL 28
1793#endif
1794#ifndef R_ARM_JUMP24
1795#define R_ARM_JUMP24 29
1796#endif
1797
David A. Longc9698e52014-02-14 22:41:18 +01001798#ifndef R_ARM_THM_CALL
1799#define R_ARM_THM_CALL 10
1800#endif
1801#ifndef R_ARM_THM_JUMP24
1802#define R_ARM_THM_JUMP24 30
1803#endif
1804#ifndef R_ARM_THM_JUMP19
1805#define R_ARM_THM_JUMP19 51
1806#endif
1807
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001808static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001809{
1810 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1811
1812 switch (r_typ) {
1813 case R_ARM_ABS32:
1814 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001815 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001816 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001817 break;
1818 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001819 case R_ARM_CALL:
1820 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001821 case R_ARM_THM_CALL:
1822 case R_ARM_THM_JUMP24:
1823 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001824 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001825 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001826 sechdr->sh_offset +
1827 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001828 break;
1829 default:
1830 return 1;
1831 }
1832 return 0;
1833}
1834
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001835static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001836{
1837 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001838 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001839 unsigned int inst;
1840
1841 if (r_typ == R_MIPS_HI16)
1842 return 1; /* skip this */
1843 inst = TO_NATIVE(*location);
1844 switch (r_typ) {
1845 case R_MIPS_LO16:
1846 r->r_addend = inst & 0xffff;
1847 break;
1848 case R_MIPS_26:
1849 r->r_addend = (inst & 0x03ffffff) << 2;
1850 break;
1851 case R_MIPS_32:
1852 r->r_addend = inst;
1853 break;
1854 }
1855 return 0;
1856}
1857
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001859 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001860{
1861 Elf_Sym *sym;
1862 Elf_Rela *rela;
1863 Elf_Rela r;
1864 unsigned int r_sym;
1865 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001866
Sam Ravnborgff13f922008-01-23 19:54:27 +01001867 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001868 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1869
Sam Ravnborgff13f922008-01-23 19:54:27 +01001870 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001871 fromsec += strlen(".rela");
1872 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001873 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001874 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001875
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001876 for (rela = start; rela < stop; rela++) {
1877 r.r_offset = TO_NATIVE(rela->r_offset);
1878#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001879 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001880 unsigned int r_typ;
1881 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1882 r_sym = TO_NATIVE(r_sym);
1883 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1884 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1885 } else {
1886 r.r_info = TO_NATIVE(rela->r_info);
1887 r_sym = ELF_R_SYM(r.r_info);
1888 }
1889#else
1890 r.r_info = TO_NATIVE(rela->r_info);
1891 r_sym = ELF_R_SYM(r.r_info);
1892#endif
1893 r.r_addend = TO_NATIVE(rela->r_addend);
1894 sym = elf->symtab_start + r_sym;
1895 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001896 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001897 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301898 if (is_second_extable_reloc(start, rela, fromsec))
1899 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001900 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001901 }
1902}
1903
1904static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001905 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001906{
1907 Elf_Sym *sym;
1908 Elf_Rel *rel;
1909 Elf_Rela r;
1910 unsigned int r_sym;
1911 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001912
Sam Ravnborgff13f922008-01-23 19:54:27 +01001913 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001914 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1915
Sam Ravnborgff13f922008-01-23 19:54:27 +01001916 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001917 fromsec += strlen(".rel");
1918 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001919 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001920 return;
1921
1922 for (rel = start; rel < stop; rel++) {
1923 r.r_offset = TO_NATIVE(rel->r_offset);
1924#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001925 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001926 unsigned int r_typ;
1927 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1928 r_sym = TO_NATIVE(r_sym);
1929 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1930 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1931 } else {
1932 r.r_info = TO_NATIVE(rel->r_info);
1933 r_sym = ELF_R_SYM(r.r_info);
1934 }
1935#else
1936 r.r_info = TO_NATIVE(rel->r_info);
1937 r_sym = ELF_R_SYM(r.r_info);
1938#endif
1939 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001940 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001941 case EM_386:
1942 if (addend_386_rel(elf, sechdr, &r))
1943 continue;
1944 break;
1945 case EM_ARM:
1946 if (addend_arm_rel(elf, sechdr, &r))
1947 continue;
1948 break;
1949 case EM_MIPS:
1950 if (addend_mips_rel(elf, sechdr, &r))
1951 continue;
1952 break;
1953 }
1954 sym = elf->symtab_start + r_sym;
1955 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001956 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001957 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301958 if (is_second_extable_reloc(start, rel, fromsec))
1959 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001960 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001961 }
1962}
1963
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001964/**
1965 * A module includes a number of sections that are discarded
1966 * either when loaded or when used as built-in.
1967 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001968 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001969 * Likewise for modules used built-in the sections marked __exit
1970 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001971 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001972 * The check_sec_ref() function traverses all relocation records
1973 * to find all references to a section that reference a section that will
1974 * be discarded and warns about it.
1975 **/
1976static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001977 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001978{
1979 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001980 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001981
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001982 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001983 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001984 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001985 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001986 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001987 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001988 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001989 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001990 }
1991}
1992
Andi Kleen7d02b492014-02-08 09:01:12 +01001993static char *remove_dot(char *s)
1994{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301995 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001996
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301997 if (n && s[n]) {
1998 size_t m = strspn(s + n + 1, "0123456789");
1999 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01002000 s[n] = 0;
2001 }
2002 return s;
2003}
2004
Masahiro Yamada8b185742018-05-09 18:50:40 +09002005static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 const char *symname;
2008 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002009 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002010 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 struct module *mod;
2012 struct elf_info info = { };
2013 Elf_Sym *sym;
2014
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002015 if (!parse_elf(&info, modname))
2016 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 mod = new_module(modname);
2019
2020 /* When there's no vmlinux, don't print warnings about
2021 * unresolved symbols (since there'll be too many ;) */
2022 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 mod->skip = 1;
2025 }
2026
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002027 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002028 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002029 warn("modpost: missing MODULE_LICENSE() in %s\n"
2030 "see include/linux/module.h for "
2031 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002032 while (license) {
2033 if (license_is_gpl_compatible(license))
2034 mod->gpl_compatible = 1;
2035 else {
2036 mod->gpl_compatible = 0;
2037 break;
2038 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002039 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002040 }
2041
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002042 namespace = get_modinfo(&info, "import_ns");
2043 while (namespace) {
2044 add_namespace(&mod->imported_namespaces, namespace);
2045 namespace = get_next_modinfo(&info, "import_ns", namespace);
2046 }
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002049 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 handle_modversions(mod, &info, sym, symname);
2052 handle_moddevtable(mod, &info, sym, symname);
2053 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002054
Matthias Maennich69923202019-10-18 10:31:42 +01002055 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2056 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2057 symname = remove_dot(info.strtab + sym->st_name);
2058
2059 if (strstarts(symname, "__kstrtabns_"))
2060 sym_update_namespace(symname + strlen("__kstrtabns_"),
2061 namespace_from_kstrtabns(&info,
2062 sym));
2063 }
2064
Denis Efremov15bfc232019-08-01 09:06:57 +03002065 // check for static EXPORT_SYMBOL_* functions && global vars
2066 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2067 unsigned char bind = ELF_ST_BIND(sym->st_info);
2068
2069 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2070 struct symbol *s =
2071 find_symbol(remove_dot(info.strtab +
2072 sym->st_name));
2073
2074 if (s)
2075 s->is_static = 0;
2076 }
2077 }
2078
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002079 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002080 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002082 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (version)
2084 maybe_frob_rcs_version(modname, version, info.modinfo,
2085 version - (char *)info.hdr);
2086 if (version || (all_versions && !is_vmlinux(modname)))
2087 get_src_version(modname, mod->srcversion,
2088 sizeof(mod->srcversion)-1);
2089
2090 parse_elf_finish(&info);
2091
Rusty Russell8c8ef422009-03-31 13:05:34 -06002092 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 * never passed as an argument to an exported function, so
2094 * the automatic versioning doesn't pick it up, but it's really
2095 * important anyhow */
2096 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002097 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
Rusty Russell712f9b42013-04-04 17:37:38 +10302100static void read_symbols_from_files(const char *filename)
2101{
2102 FILE *in = stdin;
2103 char fname[PATH_MAX];
2104
2105 if (strcmp(filename, "-") != 0) {
2106 in = fopen(filename, "r");
2107 if (!in)
2108 fatal("Can't open filenames file %s: %m", filename);
2109 }
2110
2111 while (fgets(fname, PATH_MAX, in) != NULL) {
2112 if (strends(fname, "\n"))
2113 fname[strlen(fname)-1] = '\0';
2114 read_symbols(fname);
2115 }
2116
2117 if (in != stdin)
2118 fclose(in);
2119}
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121#define SZ 500
2122
2123/* We first write the generated file into memory using the
2124 * following helper, then compare to the file on disk and
2125 * only update the later if anything changed */
2126
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002127void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2128 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 char tmp[SZ];
2131 int len;
2132 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 va_start(ap, fmt);
2135 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002136 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 va_end(ap);
2138}
2139
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002140void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002143 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002144 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
2146 strncpy(buf->p + buf->pos, s, len);
2147 buf->pos += len;
2148}
2149
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002150static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2151{
2152 const char *e = is_vmlinux(m) ?"":".ko";
2153
2154 switch (exp) {
2155 case export_gpl:
2156 fatal("modpost: GPL-incompatible module %s%s "
2157 "uses GPL-only symbol '%s'\n", m, e, s);
2158 break;
2159 case export_unused_gpl:
2160 fatal("modpost: GPL-incompatible module %s%s "
2161 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2162 break;
2163 case export_gpl_future:
2164 warn("modpost: GPL-incompatible module %s%s "
2165 "uses future GPL-only symbol '%s'\n", m, e, s);
2166 break;
2167 case export_plain:
2168 case export_unused:
2169 case export_unknown:
2170 /* ignore */
2171 break;
2172 }
2173}
2174
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002175static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002176{
2177 const char *e = is_vmlinux(m) ?"":".ko";
2178
2179 switch (exp) {
2180 case export_unused:
2181 case export_unused_gpl:
2182 warn("modpost: module %s%s "
2183 "uses symbol '%s' marked UNUSED\n", m, e, s);
2184 break;
2185 default:
2186 /* ignore */
2187 break;
2188 }
2189}
2190
Masahiro Yamada3b415282018-11-23 16:57:23 +09002191static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002192{
2193 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002194 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002195
2196 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002197 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002198 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002199 if (!exp || exp->module == mod) {
2200 if (have_vmlinux && !s->weak) {
2201 if (warn_unresolved) {
2202 warn("\"%s\" [%s.ko] undefined!\n",
2203 s->name, mod->name);
2204 } else {
2205 merror("\"%s\" [%s.ko] undefined!\n",
2206 s->name, mod->name);
2207 err = 1;
2208 }
2209 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002210 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002211 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002212 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002213 if (basename)
2214 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002215 else
2216 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002217
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002218 if (exp->namespace &&
2219 !module_imports_namespace(mod, exp->namespace)) {
2220 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2221 basename, exp->name, exp->namespace);
2222 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002223 }
2224
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002225 if (!mod->gpl_compatible)
2226 check_for_gpl_usage(exp->export, basename, exp->name);
2227 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002228 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002229
2230 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002231}
2232
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002233static int check_modname_len(struct module *mod)
2234{
2235 const char *mod_name;
2236
2237 mod_name = strrchr(mod->name, '/');
2238 if (mod_name == NULL)
2239 mod_name = mod->name;
2240 else
2241 mod_name++;
2242 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2243 merror("module name is too long [%s.ko]\n", mod->name);
2244 return 1;
2245 }
2246
2247 return 0;
2248}
2249
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002250/**
2251 * Header for the generated file
2252 **/
2253static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Laura Abbott9afb7192018-07-05 17:49:37 -07002255 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 buf_printf(b, "#include <linux/module.h>\n");
2257 buf_printf(b, "#include <linux/vermagic.h>\n");
2258 buf_printf(b, "#include <linux/compiler.h>\n");
2259 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002260 buf_printf(b, "BUILD_SALT;\n");
2261 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002263 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302265 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002266 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002267 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002269 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (mod->has_cleanup)
2271 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002272 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002274 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 buf_printf(b, "};\n");
2276}
2277
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002278static void add_intree_flag(struct buffer *b, int is_intree)
2279{
2280 if (is_intree)
2281 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2282}
2283
Andi Kleencaf75012018-01-25 15:50:28 -08002284/* Cannot check for assembler */
2285static void add_retpoline(struct buffer *b)
2286{
WANG Chaoe4f35892018-12-11 00:37:25 +08002287 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002288 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2289 buf_printf(b, "#endif\n");
2290}
2291
Trevor Keith5c725132009-09-22 16:43:38 -07002292static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002293{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002294 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002295 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2296}
2297
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002298/**
2299 * Record CRCs for unresolved symbols
2300 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002301static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002304 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 for (s = mod->unres; s; s = s->next) {
2307 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002308 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 s->module = exp->module;
2311 s->crc_valid = exp->crc_valid;
2312 s->crc = exp->crc;
2313 }
2314
2315 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002316 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 buf_printf(b, "\n");
2319 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002320 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002323 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002326 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 s->name, mod->name);
2328 continue;
2329 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302330 if (strlen(s->name) >= MODULE_NAME_LEN) {
2331 merror("too long symbol \"%s\" [%s.ko]\n",
2332 s->name, mod->name);
2333 err = 1;
2334 break;
2335 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002336 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302337 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339
2340 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002341
2342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002345static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 int first = 1;
2349
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002350 /* Clear ->seen flag of modules that own symbols needed by this. */
2351 for (s = mod->unres; s; s = s->next)
2352 if (s->module)
2353 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002356 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002358 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (!s->module)
2360 continue;
2361
2362 if (s->module->seen)
2363 continue;
2364
2365 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002366 p = strrchr(s->module->name, '/');
2367 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002368 p++;
2369 else
2370 p = s->module->name;
2371 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 first = 0;
2373 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002374 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002377static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
2379 if (mod->srcversion[0]) {
2380 buf_printf(b, "\n");
2381 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2382 mod->srcversion);
2383 }
2384}
2385
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002386static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
2388 char *tmp;
2389 FILE *file;
2390 struct stat st;
2391
2392 file = fopen(fname, "r");
2393 if (!file)
2394 goto write;
2395
2396 if (fstat(fileno(file), &st) < 0)
2397 goto close_write;
2398
2399 if (st.st_size != b->pos)
2400 goto close_write;
2401
2402 tmp = NOFAIL(malloc(b->pos));
2403 if (fread(tmp, 1, b->pos, file) != b->pos)
2404 goto free_write;
2405
2406 if (memcmp(tmp, b->p, b->pos) != 0)
2407 goto free_write;
2408
2409 free(tmp);
2410 fclose(file);
2411 return;
2412
2413 free_write:
2414 free(tmp);
2415 close_write:
2416 fclose(file);
2417 write:
2418 file = fopen(fname, "w");
2419 if (!file) {
2420 perror(fname);
2421 exit(1);
2422 }
2423 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2424 perror(fname);
2425 exit(1);
2426 }
2427 fclose(file);
2428}
2429
Ram Paibd5cbce2006-06-08 22:12:53 -07002430/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002431 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002432 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002433static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 unsigned long size, pos = 0;
2436 void *file = grab_file(fname, &size);
2437 char *line;
2438
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002439 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 /* No symbol versions, silently ignore */
2441 return;
2442
2443 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002444 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 unsigned int crc;
2446 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002447 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 if (!(symname = strchr(line, '\t')))
2450 goto fail;
2451 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002452 if (!(namespace = strchr(symname, '\t')))
2453 goto fail;
2454 *namespace++ = '\0';
2455 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 goto fail;
2457 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002458 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002459 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002460 if (export && ((end = strchr(export, '\t')) != NULL))
2461 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 crc = strtoul(line, &d, 16);
2463 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2464 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002465 mod = find_module(modname);
2466 if (!mod) {
2467 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002469 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 mod->skip = 1;
2471 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002472 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002473 s->kernel = kernel;
2474 s->preloaded = 1;
Denis Efremov15bfc232019-08-01 09:06:57 +03002475 s->is_static = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -07002476 sym_update_crc(symname, mod, crc, export_no(export));
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002477 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302479 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return;
2481fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302482 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 fatal("parse error in symbol dump file\n");
2484}
2485
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002486/* For normal builds always dump all symbols.
2487 * For external modules only dump symbols
2488 * that are not read from kernel Module.symvers.
2489 **/
2490static int dump_sym(struct symbol *sym)
2491{
2492 if (!external_module)
2493 return 1;
2494 if (sym->vmlinux || sym->kernel)
2495 return 0;
2496 return 1;
2497}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002498
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002499static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501 struct buffer buf = { };
2502 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002503 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 int n;
2505
2506 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2507 symbol = symbolhash[n];
2508 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002509 if (dump_sym(symbol)) {
2510 namespace = symbol->namespace;
2511 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2512 symbol->crc, symbol->name,
2513 namespace ? namespace : "",
2514 symbol->module->name,
2515 export_str(symbol->export));
2516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 symbol = symbol->next;
2518 }
2519 }
2520 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002521 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002524static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002525{
2526 struct module *mod;
2527 struct namespace_list *ns;
2528 struct buffer ns_deps_buf = {};
2529
2530 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002531
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002532 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002533 continue;
2534
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002535 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002536
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002537 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2538 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002539
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002540 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002541 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002542
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002543 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002544 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002545}
2546
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002547struct ext_sym_list {
2548 struct ext_sym_list *next;
2549 const char *file;
2550};
2551
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002552int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 struct module *mod;
2555 struct buffer buf = { };
Masahiro Yamada39808e42019-10-03 19:29:14 +09002556 char *kernel_read = NULL;
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;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002562 struct ext_sym_list *extsym_iter;
2563 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002565 while ((opt = getopt(argc, argv, "i:e:mnsT:o:awEd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002566 switch (opt) {
2567 case 'i':
2568 kernel_read = optarg;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002569 external_module = 1;
2570 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002571 case 'e':
2572 external_module = 1;
2573 extsym_iter =
2574 NOFAIL(malloc(sizeof(*extsym_iter)));
2575 extsym_iter->next = extsym_start;
2576 extsym_iter->file = optarg;
2577 extsym_start = extsym_iter;
2578 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002579 case 'm':
2580 modversions = 1;
2581 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302582 case 'n':
2583 ignore_missing_files = 1;
2584 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002585 case 'o':
2586 dump_write = optarg;
2587 break;
2588 case 'a':
2589 all_versions = 1;
2590 break;
2591 case 's':
2592 vmlinux_section_warnings = 0;
2593 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302594 case 'T':
2595 files_source = optarg;
2596 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002597 case 'w':
2598 warn_unresolved = 1;
2599 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302600 case 'E':
2601 sec_mismatch_fatal = 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
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002611 if (kernel_read)
2612 read_dump(kernel_read, 1);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002613 while (extsym_start) {
2614 read_dump(extsym_start->file, 0);
2615 extsym_iter = extsym_start->next;
2616 free(extsym_start);
2617 extsym_start = extsym_iter;
2618 }
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)
2658 fatal("modpost: Section mismatches detected.\n"
2659 "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}